Location: PHPKode > projects > FiForms Framework > FiForms/FiForms-includes/FiReports_XMLDefLoader.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_XMLDefLoader.inc.php

Loads an XML report definition into a FiReport

*******************************************************************************
*/

require_once("XPath.class.php");
if(!isset($FIFORMS_CONFIG))
{
        die('No Configuration found. Did you perhaps call an include file' .
                        ' directly instead of calling it as part of the FiForms' .
                        ' application?'); 
}
require_once($FIFORMS_CONFIG['AUTH_MODULE']);

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

class XMLDefLoader
{
    private $xmlObject;  // XPath Parser
    private $xmlString;  // XML Source String
    public $includePath; // Local path on server to load included XML reports from
    public $errorMsg;    // Error Message
        
    public function getQuerySet(&$parentObject,$node,$prefix = "")
    // Loads a single query with associated properties
    // For nested query, this function calls itself recursively
    {
        if(!$this->xmlObject)
        {
           $this->errorMsg .= "No XML Object Available in getQuerySet";
           return false;
        }

        $nodeArray = $this->xmlObject->match($node);
        foreach($nodeArray as $thisNode)
        {
            if(strpos($thisNode,'callquery'))
            {
                $filename = $this->includePath.'/'.$this->xmlObject->match(
                    "string(".$thisNode."/@src)"
                    );
                $query = $this->xmlObject->match(
                    "string(".$thisNode."/@query)"
                    );
                $XMLString = @file_get_contents($filename);
                if($XMLString)
                {
                
                $ldr = new XMLDefLoader($XMLString);
                if(!$ldr->getQuerySet($parentObject,$query,"document('".$filename."')"))
                {
                  $this->errorMsg .= $ldr->errorMsg;
                }
                }
                else
                {
                  $this->errorMsg .= "Could not load external query from $filename.";
                }
            }
            else
            {
                $query = $this->xmlObject->match("string($thisNode/sql[1])");
                $fields = $this->xmlObject->match("$thisNode/sql/f | $thisNode/f");
                $parentObject[$prefix.$thisNode]->fieldInfo = array();
                foreach($fields as $fieldInfo)
                {
                        $fieldObj = new FiReportField();
                        $fieldObj->name = $this->xmlObject->match("string($fieldInfo)");
                        $fieldObj->alias = $this->xmlObject->match("string($fieldInfo/@a)");
                        $fieldObj->summary = $this->xmlObject->match("string($fieldInfo/@summary)");
                        $fieldObj->format = $this->xmlObject->match("string($fieldInfo/@format)");
                        $fieldObj->formatopts = $this->xmlObject->match("string($fieldInfo/@formatopts)");
                        $fieldObj->layout = $this->xmlObject->match("string($fieldInfo/@layout)");
                        $parentObject[$prefix.$thisNode]->fieldInfo[] = $fieldObj;
                }
                $parentObject[$prefix.$thisNode]->sql = html_entity_decode($query,ENT_QUOTES);
                $parentObject[$prefix.$thisNode]->name =
                    $this->xmlObject->match(
                    "string(".$thisNode."/@resultname)"
                    );
                        $outputYN =       
                    $this->xmlObject->match(
                    "string(".$thisNode."/@output)"
                    );
    
                $parentObject[$prefix.$thisNode]->output = ($outputYN == 'no') ? FALSE : TRUE;
    
                $conid =
                    $this->xmlObject->match(
                    "string(".$thisNode."/@connectid)"
                    );
                if($conid == "")
                {
                        $conid = "1";
                }
                $parentObject[$prefix.$thisNode]->connectid = $conid;
                $this->getQuerySet($parentObject[$prefix.$thisNode]->subQueries,$thisNode."/query | ".$thisNode."/callquery",$prefix);
            } // if
        } // foreach
    } // function getQuerySet
        
    public function XMLDefLoader($XMLString = "")
    {
        $this->xmlObject = new XPath();
        if($XMLString)
        {
          $this->xmlObject->importFromString($XMLString);
          $this->xmlString = $XMLString;
        }
    }
    
    public function getDefinition(&$reportObject,$XMLString = "")
    // Loads the report definition from XML string and applies it to existing
    // FiReportXML object referenced by reportObject
    {
        if($XMLString)
        {
          $this->xmlObject->importFromString($XMLString);
          $this->xmlString = $XMLString;
        }
        if(!$this->xmlObject)
        {
           $this->errorMsg .= "No XML Object Available in getDefinition";
           return false;
        }
        $reportObject->title = 
              $this->xmlObject->match(
              "string(/reportdef/title[1])"
              );
        $reportObject->stylesheet = 
              $this->xmlObject->match(
              "string(/reportdef/stylesheet[1]/@href)"
              );
        $reportObject->user = 
              $this->xmlObject->match(
              "string(/reportdef/security[1]/@user)"
              );
        $reportObject->group = 
              $this->xmlObject->match(
              "string(/reportdef/security[1]/@group)"
              );
        if($reportObject->stylesheet == "")
        {
            $reportObject->stylesheet = "tableView.xsl";
        }
        $connectionNodes = $this->xmlObject->match(
            "/reportdef/connect"
            );
    
        foreach($connectionNodes as $node)
        {
          $cID = 
            $this->xmlObject->match(
            "string(".$node."/@id)"
            );
          if($cID == "")
          {
                $cID = "1";
          }
          //if($cID != "")
          //{
                $reportObject->connections[$cID]->server = 
              $this->xmlObject->match(
              "string(".$node."/@server)"
              );
                $reportObject->connections[$cID]->database = 
                $this->xmlObject->match(
                "string(".$node."/@db)"
                );
                $reportObject->connections[$cID]->user = 
                $this->xmlObject->match(
                "string(".$node."/@user)"
                );
                $reportObject->connections[$cID]->passwd = 
                    $this->xmlObject->match(
                "string(".$node."/@passwd)"
                    );
            if($reportObject->connections[$cID]->user == "")
            {
              $auth = new FiFormsAuth();
              
              if($auth->username == "")
              {
                $this->errorMsg .= "No Username";
                $auth->connectFailure();
                return(false);
              }
              $reportObject->connections[$cID]->user = $auth->username;
              $reportObject->connections[$cID]->passwd = $auth->passwd;
            }
         // } // if cID
    
        }  //foreach connectionNodes

        $reportObject->reportDefXML = substr($this->xmlString,strpos($this->xmlString,"<reportdef"));
        
            $this->getQuerySet(
               $reportObject->queryset,
               "/reportdef/query | /reportdef/callquery"
               );
        return true;
    } // function getDefinition

} // class XMLDefLoader

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

?>
Return current item: FiForms Framework