- <?php
- /**
- * SpoolTemplate (sptpl)
- *
- * @author Andrioli Darvin <darvin@andrioli.com>
- * @version $Header: d:\cvs/classistd/sptpl/sptpl.php,v 2.11 2005/03/17 12:46:48 Darvin Exp $
- * @package SpoolTemplate
- */
- /*
- *
- * Template system to write spool files for printing by lp, print or
- * similar command
- *
- * +-------------------------------------------------------------------------+
- * | Sptpl Rel. 2.1.0 |
- * +-------------------------------------------------------------------------+
- * | Copyright (c) 2003-2005 Andrioli Darvin |
- * | Email <darvin (inside) andrioli (dot) com> |
- * | Web http://www.andrioli.com/en/sptpl.html |
- * | Download http://www.phpclasses.org/browse.html/package/1326.html |
- * | |
- * +-------------------------------------------------------------------------+
- * | This library is free software; you can redistribute it and/or modify |
- * | it under the terms of the GNU Lesser 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 |
- * | Lesser General Public License for more details. |
- * | |
- * | You should have received a copy of the GNU Lesser 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 |
- * +-------------------------------------------------------------------------+
- */
- /**
- * @const MAXPAGEROWS Max number of row per page
- */
- define('MAXPAGEROWS',66);
- /**
- * @const NEWPAGECHAR
- */
- define('NEWPAGECHAR',chr(12));
-
- /**
- * @const CURRENTVALUE Use the current values from query row
- */
- define('CURRENTVALUE',0);
- /**
- * @const OLDVALUE Use the values from the previous query row
- */
- define('OLDVALUE',1);
-
- /**
- * SpoolTemplate class.
- * Module to build report from templates. Main class.
- */
- class sptpl {
- /**
- * Dom class of the template file
- * @var object DomXML $TplDom
- */
- var $TplDom;
-
- /**
- * Table of the report definition
- * @var array $Report
- */
- var $Report;
- /**
- * Directory where look for the template files
- * @var string $TemplateDir
- */
- var $TemplateDir;
-
- /**
- * Predefined id, if not specified into block tag.
- * @var integer $BlockId
- */
- var $BlockId;
- /**
- * Which order use to print all block defined in the current report.
- * For each index the value is the block id (repot index)
- * @var array $PrintOrder
- */
- var $PrintOrder;
-
- /**
- * Class dataStorage istance
- * @var object DataStorage $clsDataStorage
- */
- var $clsDataStorage;
- /**
- * Class PageManager istance
- * @var object CPageMgr $clsPageMgr
- */
- var $clsPageMgr;
- /**
- * Class CXml2Array istance
- * This istance holds the configuration file
- * @var object CXml2Array
- */
- var $Config;
- /**
- * Initialize this class.
- * @access public
- */
- function sptpl()
- {
- if(DEBUG) $GLOBALS['dbg']->f_in('sptpl');
- // If I haven't any value for the template directory I aassume the current.
- // Otherwise you must specify some value inside the sptpl.inc
- // print_r($GLOBALS);
- // print_r(key_exists($GLOBALS['TemplateDir']));
- $this->TemplateDir=(array_key_exists('TemplateDir',$GLOBALS))?$GLOBALS['TemplateDir']:"./";
- $this->Values=array();
- $this->Report['blk']=array();
- $this->BlockId=0;
- $this->PrintOrder=array();
-
- // Start the DataStorage module
- $this->clsDataStorage=new DataStorage();
- // ... and the page manager
- $this->clsPageMgr=new CPageMgr($this->clsDataStorage);
- if(DEBUG) $GLOBALS['dbg']->f_out();
- }
-
-
- /************************************************************
- * Parsing configuration functions
- **************************************************************/
-
- /**
- * Load the specified template file
- * @param string $TplFile
- * @access public
- */
- function LoadTemplate($TplFile)
- {
- // Exist the request file ?
- if(!file_exists($this->TemplateDir.$TplFile))
- trigger_error('File '.$this->TemplateDir.$TplFile.' does not exist',E_USER_ERROR);
-
- // load the configuration file into CXmlArray module
- $this->Config=new CXml2Array();
- $this->Config->LoadFromFile($this->TemplateDir.$TplFile);
- $FirstTag=TRUE;
- // loop on root's child
- while(($ret=$this->Config->EachChild())!=FALSE)
- {
- list($ChildName,$ChildValue)=$ret;
- switch(strtolower($ChildName)) {
- case 'pagemanager' :
- if($FirstTag)
- $this->_NewPageMgr($ChildValue);
- else
- trigger_error('Pagemanager tag should be the first', E_USER_ERROR);
- $FirstTag=FALSE;
- break;
- case 'constant' :
- $this->clsDataStorage->AddConstantXml($ChildValue);
- $FirstTag=FALSE;
- break;
- case 'field' :
- $this->clsDataStorage->AddFieldXml($ChildValue);
- $FirstTag=FALSE;
- break;
- case 'report' :
- $this->_AddReport($ChildValue);
- $FirstTag=FALSE;
- break;
- case 'font' :
- $this->clsPageMgr->NewFont($ChildValue);
- break;
- default:
- trigger_error('Unknown tag '.$ChildName,E_USER_ERROR);
- break;
- }
- }
- $this->clsPageMgr->EndParseXml();
- // $GLOBALS['dbg']->pray($this->clsPageMgr);
- }
-
- /**
- * Istance the new page manager for this report
- * @param object CXml2Array CXML2array with the name of the class to use
- * @access private
- */
- function _NewPageMgr($cfg)
- {
- $ClassName=$cfg->GetText();
- if(!class_exists($ClassName))
- trigger_error('Error: class '.$ClassName.' not defined', E_USER_ERROR);
- // ... and the page manager
- $cmd='$this->clsPageMgr=new '.$ClassName.'($this->clsDataStorage);';
- eval($cmd);
- if(!is_object($this->clsPageMgr))
- trigger_error('Error during istance of '.$ClassName,E_USER_ERROR);
- }
-
- /**
- * Parse the configuration node for a new report
- * @param object CXml2Array CXml2Array with the constant definition
- * @access private
- */
- function _AddReport($Cfg)
- {
- if(DEBUG) $GLOBALS['dbg']->f_in('_AddReport');
- // loop on root's child
-
- while(($ret=$Cfg->EachChild())!=FALSE)
- {
- list($ChildName,$ChildValue)=$ret;
- // echo '<br>child:';
- // print_r($child);
-
- switch(strtolower($ChildName)) {
- case 'margin' :
- case 'pagesize' :
- case 'beginreport' :
- case 'closereport' :
- case 'openpage' :
- case 'closepage' :
- case 'columnset' :
- $this->clsPageMgr->ParseXml($ChildValue);
- break;
- case 'font' :
- $this->clsPageMgr->NewFont($ChildValue);
- break;
- case 'block' :
- case 'row' :
- case 'newpage' :
- $this->BlockId++;
- $cBlock=new CBlock($this->clsDataStorage,$this->clsPageMgr,$this->BlockId,$ChildValue);
- $idBlock=$cBlock->GetId();
- if(array_key_exists($idBlock,$this->Report['blk']))
- trigger_error("Block with id=".$idBlock." already exists",E_USER_ERROR);
- $this->Report['blk'][$idBlock]=$cBlock;
- $this->PrintOrder[]=$idBlock;
- break;
- default:
- trigger_error('Unknown tag '.$ChildName,E_USER_ERROR);
- break;
- }
-
- }
- $this->clsPageMgr->CheckMargins();
- if(DEBUG) $GLOBALS['dbg']->f_out();
- }
-
-
- /************************************************************
- * Template rendering functions
- **************************************************************/
-
- /**
- * Run the template and made the file
- *
- * @param string $OutFile output file name
- * @access public
- */
- function run($OutFile)
- {
- if(DEBUG) $GLOBALS['dbg']->f_in('run');
- $this->clsPageMgr->BeginReport($OutFile);
-
- //Loop over each block
- $conta=0;
- foreach($this->PrintOrder as $block )
- {
- $this->Report['blk'][$block]->RunBlock();
- }
- $this->clsPageMgr->CloseReport();
- if(DEBUG) $GLOBALS['dbg']->f_out();
- }
-
-
- /**
- * Put into the array $Value each data from the fiels array.
- * The parameter $fields is an associative array where the index is the data field name
- * and the value is the data field value
- *
- * @parameter array $fields Data field for the current row.
- * @access private
- *
- */
- function _ToValue($fields)
- {
- foreach($fields as $key =>$value)
- $this->Values[$key]=$value;
- }
-
- /**
- * Remove from $Values all data fields added from the current data row.
- *
- * @parameter array $fields Data field for the current row.
- * @access private
- */
- function _RemoveFromValue($fields)
- {
- foreach($fields as $key =>$value)
- unset($this->Values[$key]);
- }
-
- /**
- * Set/add a value for data field. This function is
- * an interface to the same function inside the DataManager class,
- * actually it calls that function.
- *
- * @parameter string $varName data field name
- * @parameter string $value value for data field. It may be an array
- * @return boolean
- * @access public
- *
- */
- function SetVar($varName,$value)
- {
- return($this->clsDataStorage->SetVar($varName,$value));
- }
-
- function _DumpInternals()
- {
- echo "\n<br>Text: " .$GLOBALS['dbg']->print_r_to_var($this->Report);
- }
-
- /**
- * Return True if the current PHP version is 5
- * False if is PHP4
- * @return boolean
- * @access public
- */
- function __php5()
- {
- return((version_compare(phpversion(), "5.0.0", "<")==-1)?false:true);
- }
- }
-
- ?>