Location: PHPKode > projects > FiForms Framework > FiForms/FiForms-includes/FiReports_FiReportXML.inc.php
<?php
/*
*******************************************************************************

    FiReportXML -- Rapid XML Report Generator
    Copyright (C) 2004  Daniel McFeeters

    included with

    FiForms -- A collection of PHP classes designed 
    to facilitate rapid development of web-database software

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


    The original author of this library can be contacted at the following 
    address:

    Daniel McFeeters
    182 Baker Rd.
    Faubush, KY 42544-6526
    email:databases [at] fiforms [dot] org
    http://www.fiforms.org/


Project Started January 20, 2004
*******************************************************************************
FiForms_FiReportXML.inc.php
*******************************************************************************
*/

require_once("FiReports_FiReport.inc.php");
require_once("FiReports_xsltWrapper.inc.php");

/* ?><code><?php */

class FiReportXML extends FiReport
{
    public $stylesheet;      // URI of stylesheet to apply to XML result
    public $serverTransform; // if TRUE, transform XML on server
    private $searchPat;   // Array of regExp patterns to search for in text
    private $replacePat;      // Matching array of regExp replaements to make
    public $encoding;
    public $dumpBinField;     // name of single binary field to dump to output
    public $allowCache;       // boolean cache flag

    public function FiReportXML()
    {
        $this->times['Initialization'] = time();
        $this->FiReport();
        $this->mimetype = "application/xml";
        $this->searchPat = array(
                "/\&amp\;\#(\d+)\;/",
                "/\b\'|\x92/",  // Apostrophe or Closing Single Quote '
                "/\'\b|\x91/",  // Opening Single Quote '
                "/\"\b|\x93/",  // Opening Double Quote "
                "/\"|\x94/",    // Closing Double Quote "
                "/[\x80-\xFF]/"  
                                //Any other non-ASCII chars
            );
        $this->replacePat = array(
                "&#\$1;",
                "&#8217;",
                "&#8217;",  //&#8216;
                "&#8220;",
                "&#8221;",
                ""
                //"&#65533;"
        );
        $this->encoding = "UTF-8"; //"ISO-8859-1";
        $this->outputXMLNS = $GLOBALS['FIFORMS_CONFIG']['REPORT_OUTPUT_XMLNS'];
        $this->allowCache = false;
    }

    private function appendDataSet($queryset)
    // Execute a single query and append the resulting XML to docString.
    // For a tree of XML objects, this function calls itself recursively.
    {   
        $this->times[] = time();
        $sqlQuery = $this->parQuery($queryset->sql);
        if($queryset->connectid && $this->connections[$queryset->connectid]->link)
        {
          $resource = mysql_query(
              $sqlQuery,
              $this->connections[$queryset->connectid]->link
              );
          $error = mysql_error();
        }
        else
        {
          $error = "No Connection ID #".$queryset->connectid." for ".$this->connections[$queryset->connectid]->server." or non-existent link.";
          //print_r($this);
          //die();
        }

                if($queryset->output || $error)
                {
                $this->docString .= "<dataset name=\"".
                                $queryset->name."\" ";
                        if($GLOBALS['FIFORMS_CONFIG']['SHOW_DEFINITION'])
                        {
                            if((array_key_exists('debug',$_GET) || array_key_exists('debug',$_POST)))
                            {
                                        $this->docString .= "query=\"".
                                        htmlentities($sqlQuery)."\" ";
                            }
                        }
                if($error)
                {
                        $this->docString .= "error=\"".htmlentities($error)."\" ";
                }
                    $this->docString .= ">\n";
                    if(is_array($queryset->fieldInfo))
                    {
                        foreach($queryset->fieldInfo as $field)
                        {
                                $this->docString .= "<f"
                                .($field->alias ? " a=\"".$field->alias."\"":"")
                                .($field->summary ? " summary=\"".$field->summary."\"":"")
                                .($field->format ? " format=\"".$field->format."\"":"")
                                .($field->formatopts ? " formatopts=\"".$field->formatopts."\"":"")
                                .($field->layout ? " layout=\"".$field->layout."\"":"")
                                .">".$field->name."</f>";
                        }
                    }
                $rowKey = 0;
                while($rowArray = @mysql_fetch_array($resource,MYSQL_ASSOC))
                {
                    $rowKey++;
                        $this->docString .= "<row id=\"$rowKey\">\n";
                        $fieldNum = 0;
                    foreach($rowArray as $fieldKey => $fieldValue)
                    {
                        if($this->dumpBinField && $fieldKey == $this->dumpBinField)
                        {
                                header('Content-type: '.$this->mimetype);
                                echo $fieldValue;
                                die();
                        }
                            $this->dynParams[$fieldKey] = addslashes($fieldValue);
                        $this->docString .= 
                                    '<field name="'.htmlentities($fieldKey).'" t="'.htmlentities(mysql_field_type($resource,$fieldNum)).'">'.
                                    preg_replace($this->searchPat,
                                        $this->replacePat,
                                        htmlspecialchars($fieldValue,ENT_NOQUOTES,"UTF-8")).
                                                        "</field>\n";
                                        $fieldNum++;
                    }
                            if(isset($queryset->subQueries))
                            {
                                foreach($queryset->subQueries as $thisQuery)
                                {
                                    $this->appendDataSet($thisQuery);
                                }
                            }
                            $this->docString .= "</row>\n";
                }  // while rowArray
                error_reporting(7);
                    $this->docString .= "</dataset>\n";
        }  // if output
    } // function appendDataSet

    public function getDocString()
    {
        if(($this->group && is_array($this->groupsAllowed) && !in_array($this->group,$this->groupsAllowed)) || ($this->user && is_array($this->userAllowed) && !in_array($this->user,$this->usersAllowed)))
                {
                    $this->errorMsg .= 'Permission Denied';
                    return false;
                }
        $this->times['beforeConnect'] = time();
        $this->makeConnections();
        $this->times['afterConnect'] = time();
        $stylesheet = $this->stylesheet;
        $this->docString = <<<EOD
<?xml version="1.0" encoding="$this->encoding"?>
<!DOCTYPE report PUBLIC "-//FIFORMS/DTD FIREPORT 1.1//EN" "http://xml.fiforms.org/dtd/11/FiReport.dtd">

EOD;
        if($stylesheet)
        $this->docString .= <<<EOD
<?xml-stylesheet type="text/xsl" href="$stylesheet"?>

EOD;
        $reportTag = ($this->outputXMLNS) ? "report xmlns=\"http://xml.fiforms.org/FiReports/\"" : "report";
        $this->docString .= <<<EOD
<$reportTag>
EOD;
        if($GLOBALS['FIFORMS_CONFIG']['SHOW_DEFINITION'] === TRUE)
        {
           $this->docString .= $this->reportDefXML."\n";
        }
        $paramArray = &$this->paramArray;
        $this->docString .= "<parameters>\n";
        foreach($paramArray as $name => $param)
        {
            if($name != "_DEF")
            {
            $this->docString .= 
                  "    <param name=\"$name\" value=\"".htmlentities($param)."\" />\n";
            }
        }
        $this->docString .= "</parameters>\n";

        foreach($this->queryset as $thisQuery)
        {
            $this->appendDataSet($thisQuery);
        }    
        if($GLOBALS['FIFORMS_CONFIG']['SHOW_DEFINITION'] && ($_GET['debug'] || $_POST['debug']))
        {
          $this->docString .= "<times>\n";
          foreach($this->times as $thispoint => $thistime)
          {
            $this->docString .= "    <time point=\"$thispoint\" value=\"$thistime\" />\n";
          }
          $this->docString .= "</times>\n";
        }
        $this->docString .= "<messages>".htmlentities($this->errorMsg)."</messages></report>";
        return true;
    } // function returnXML

    public function generateReport()
    {
        $this->getDocString() or die('Error Generating Report: '.$this->errorMsg);
        header('Content-type: '.$this->mimetype);
        if(!$this->allowCache)
        {
            header('Cache-control: no-store');
            header('Expires: 0');
        }
        else
        {
            header('Cache-control: max-age=600');
        }

        if($this->serverTransform)
        {
            $xslt = new xsltWrapper();
            $outputData = $xslt->transformStringByFile($this->docString,$this->stylesheet);
        if($outputData)
        {
                echo $outputData;
        }
        else
        {
                $outputData = $xslt->errorMsg;
            echo "<html><head><title>Error in XSLT Stylesheet Translation</title><link href=\"tableViewStyle.css\" media=\"screen,print\" rel=\"StyleSheet\" type=\"text/css\">
</head><body><p class=\"error\">Error in XSLT Stylesheet Translation</p><pre>$outputData</pre></body></html>";
        }
        }
        else
        {
            echo($this->docString);
        }
    }

} // class FiReportXML 

/* ?></code><?php */

?>
Return current item: FiForms Framework