phpDocumentor SpoolTemplate
[ Back ] [ class tree: SpoolTemplate ] [ index: SpoolTemplate ] [ all elements ]

Source for file sptpl.php

Documentation is available at sptpl.php

  1. <?php
  2. /**
  3. * SpoolTemplate (sptpl)
  4. *
  5. * @author Andrioli Darvin <darvin@andrioli.com>
  6. * @version $Header: d:\cvs/classistd/sptpl/sptpl.php,v 2.11 2005/03/17 12:46:48 Darvin Exp $
  7. * @package SpoolTemplate
  8. */
  9. /*
  10. *
  11. * Template system to write spool files for printing by lp, print or
  12. * similar command
  13. *
  14. * +-------------------------------------------------------------------------+
  15. * | Sptpl Rel. 2.1.0 |
  16. * +-------------------------------------------------------------------------+
  17. * | Copyright (c) 2003-2005 Andrioli Darvin |
  18. * | Email <darvin (inside) andrioli (dot) com> |
  19. * | Web http://www.andrioli.com/en/sptpl.html |
  20. * | Download http://www.phpclasses.org/browse.html/package/1326.html |
  21. * | |
  22. * +-------------------------------------------------------------------------+
  23. * | This library is free software; you can redistribute it and/or modify |
  24. * | it under the terms of the GNU Lesser General Public License as |
  25. * | published by the Free Software Foundation; either version 2 of the |
  26. * | License, or (at your option) any later version. |
  27. * | |
  28. * | This library is distributed in the hope that it will be useful, but |
  29. * | WITHOUT ANY WARRANTY; without even the implied warranty of |
  30. * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
  31. * | Lesser General Public License for more details. |
  32. * | |
  33. * | You should have received a copy of the GNU Lesser General Public |
  34. * | License along with this library; if not, write to the Free Software |
  35. * | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
  36. * +-------------------------------------------------------------------------+
  37. */
  38. /**
  39. * @const MAXPAGEROWS Max number of row per page
  40. */
  41. define('MAXPAGEROWS',66);
  42. /**
  43. * @const NEWPAGECHAR
  44. */
  45. define('NEWPAGECHAR',chr(12));
  46.  
  47. /**
  48. * @const CURRENTVALUE Use the current values from query row
  49. */
  50. define('CURRENTVALUE',0);
  51. /**
  52. * @const OLDVALUE Use the values from the previous query row
  53. */
  54. define('OLDVALUE',1);
  55. /**
  56. * SpoolTemplate class.
  57. * Module to build report from templates. Main class.
  58. */
  59. class sptpl {
  60. /**
  61. * Dom class of the template file
  62. * @var object DomXML $TplDom
  63. */
  64. var $TplDom;
  65.  
  66. /**
  67. * Table of the report definition
  68. * @var array $Report
  69. */
  70. var $Report;
  71. /**
  72. * Directory where look for the template files
  73. * @var string $TemplateDir
  74. */
  75. var $TemplateDir;
  76.  
  77. /**
  78. * Predefined id, if not specified into block tag.
  79. * @var integer $BlockId
  80. */
  81. var $BlockId;
  82. /**
  83. * Which order use to print all block defined in the current report.
  84. * For each index the value is the block id (repot index)
  85. * @var array $PrintOrder
  86. */
  87. var $PrintOrder;
  88.  
  89. /**
  90. * Class dataStorage istance
  91. * @var object DataStorage $clsDataStorage
  92. */
  93. var $clsDataStorage;
  94. /**
  95. * Class PageManager istance
  96. * @var object CPageMgr $clsPageMgr
  97. */
  98. var $clsPageMgr;
  99. /**
  100. * Class CXml2Array istance
  101. * This istance holds the configuration file
  102. * @var object CXml2Array
  103. */
  104. var $Config;
  105. /**
  106. * Initialize this class.
  107. * @access public
  108. */
  109. function sptpl()
  110. {
  111. if(DEBUG) $GLOBALS['dbg']->f_in('sptpl');
  112. // If I haven't any value for the template directory I aassume the current.
  113. // Otherwise you must specify some value inside the sptpl.inc
  114. // print_r($GLOBALS);
  115. // print_r(key_exists($GLOBALS['TemplateDir']));
  116. $this->TemplateDir=(array_key_exists('TemplateDir',$GLOBALS))?$GLOBALS['TemplateDir']:"./";
  117. $this->Values=array();
  118. $this->Report['blk']=array();
  119. $this->BlockId=0;
  120. $this->PrintOrder=array();
  121.  
  122. // Start the DataStorage module
  123. $this->clsDataStorage=new DataStorage();
  124. // ... and the page manager
  125. $this->clsPageMgr=new CPageMgr($this->clsDataStorage);
  126. if(DEBUG) $GLOBALS['dbg']->f_out();
  127. }
  128.  
  129.  
  130. /************************************************************
  131. * Parsing configuration functions
  132. **************************************************************/
  133.  
  134. /**
  135. * Load the specified template file
  136. * @param string $TplFile
  137. * @access public
  138. */
  139. function LoadTemplate($TplFile)
  140. {
  141. // Exist the request file ?
  142. if(!file_exists($this->TemplateDir.$TplFile))
  143. trigger_error('File '.$this->TemplateDir.$TplFile.' does not exist',E_USER_ERROR);
  144.  
  145. // load the configuration file into CXmlArray module
  146. $this->Config=new CXml2Array();
  147. $this->Config->LoadFromFile($this->TemplateDir.$TplFile);
  148. $FirstTag=TRUE;
  149. // loop on root's child
  150. while(($ret=$this->Config->EachChild())!=FALSE)
  151. {
  152. list($ChildName,$ChildValue)=$ret;
  153. switch(strtolower($ChildName)) {
  154. case 'pagemanager' :
  155. if($FirstTag)
  156. $this->_NewPageMgr($ChildValue);
  157. else
  158. trigger_error('Pagemanager tag should be the first', E_USER_ERROR);
  159. $FirstTag=FALSE;
  160. break;
  161. case 'constant' :
  162. $this->clsDataStorage->AddConstantXml($ChildValue);
  163. $FirstTag=FALSE;
  164. break;
  165. case 'field' :
  166. $this->clsDataStorage->AddFieldXml($ChildValue);
  167. $FirstTag=FALSE;
  168. break;
  169. case 'report' :
  170. $this->_AddReport($ChildValue);
  171. $FirstTag=FALSE;
  172. break;
  173. case 'font' :
  174. $this->clsPageMgr->NewFont($ChildValue);
  175. break;
  176. default:
  177. trigger_error('Unknown tag '.$ChildName,E_USER_ERROR);
  178. break;
  179. }
  180. }
  181. $this->clsPageMgr->EndParseXml();
  182. // $GLOBALS['dbg']->pray($this->clsPageMgr);
  183. }
  184.  
  185. /**
  186. * Istance the new page manager for this report
  187. * @param object CXml2Array CXML2array with the name of the class to use
  188. * @access private
  189. */
  190. function _NewPageMgr($cfg)
  191. {
  192. $ClassName=$cfg->GetText();
  193. if(!class_exists($ClassName))
  194. trigger_error('Error: class '.$ClassName.' not defined', E_USER_ERROR);
  195. // ... and the page manager
  196. $cmd='$this->clsPageMgr=new '.$ClassName.'($this->clsDataStorage);';
  197. eval($cmd);
  198. if(!is_object($this->clsPageMgr))
  199. trigger_error('Error during istance of '.$ClassName,E_USER_ERROR);
  200. }
  201.  
  202. /**
  203. * Parse the configuration node for a new report
  204. * @param object CXml2Array CXml2Array with the constant definition
  205. * @access private
  206. */
  207. function _AddReport($Cfg)
  208. {
  209. if(DEBUG) $GLOBALS['dbg']->f_in('_AddReport');
  210. // loop on root's child
  211.  
  212. while(($ret=$Cfg->EachChild())!=FALSE)
  213. {
  214. list($ChildName,$ChildValue)=$ret;
  215. // echo '<br>child:';
  216. // print_r($child);
  217.  
  218. switch(strtolower($ChildName)) {
  219. case 'margin' :
  220. case 'pagesize' :
  221. case 'beginreport' :
  222. case 'closereport' :
  223. case 'openpage' :
  224. case 'closepage' :
  225. case 'columnset' :
  226. $this->clsPageMgr->ParseXml($ChildValue);
  227. break;
  228. case 'font' :
  229. $this->clsPageMgr->NewFont($ChildValue);
  230. break;
  231. case 'block' :
  232. case 'row' :
  233. case 'newpage' :
  234. $this->BlockId++;
  235. $cBlock=new CBlock($this->clsDataStorage,$this->clsPageMgr,$this->BlockId,$ChildValue);
  236. $idBlock=$cBlock->GetId();
  237. if(array_key_exists($idBlock,$this->Report['blk']))
  238. trigger_error("Block with id=".$idBlock." already exists",E_USER_ERROR);
  239. $this->Report['blk'][$idBlock]=$cBlock;
  240. $this->PrintOrder[]=$idBlock;
  241. break;
  242. default:
  243. trigger_error('Unknown tag '.$ChildName,E_USER_ERROR);
  244. break;
  245. }
  246. }
  247. $this->clsPageMgr->CheckMargins();
  248. if(DEBUG) $GLOBALS['dbg']->f_out();
  249. }
  250.  
  251.  
  252. /************************************************************
  253. * Template rendering functions
  254. **************************************************************/
  255.  
  256. /**
  257. * Run the template and made the file
  258. *
  259. * @param string $OutFile output file name
  260. * @access public
  261. */
  262. function run($OutFile)
  263. {
  264. if(DEBUG) $GLOBALS['dbg']->f_in('run');
  265. $this->clsPageMgr->BeginReport($OutFile);
  266.  
  267. //Loop over each block
  268. $conta=0;
  269. foreach($this->PrintOrder as $block )
  270. {
  271. $this->Report['blk'][$block]->RunBlock();
  272. }
  273. $this->clsPageMgr->CloseReport();
  274. if(DEBUG) $GLOBALS['dbg']->f_out();
  275. }
  276.  
  277.  
  278. /**
  279. * Put into the array $Value each data from the fiels array.
  280. * The parameter $fields is an associative array where the index is the data field name
  281. * and the value is the data field value
  282. *
  283. * @parameter array $fields Data field for the current row.
  284. * @access private
  285. *
  286. */
  287. function _ToValue($fields)
  288. {
  289. foreach($fields as $key =>$value)
  290. $this->Values[$key]=$value;
  291. }
  292.  
  293. /**
  294. * Remove from $Values all data fields added from the current data row.
  295. *
  296. * @parameter array $fields Data field for the current row.
  297. * @access private
  298. */
  299. function _RemoveFromValue($fields)
  300. {
  301. foreach($fields as $key =>$value)
  302. unset($this->Values[$key]);
  303. }
  304.  
  305. /**
  306. * Set/add a value for data field. This function is
  307. * an interface to the same function inside the DataManager class,
  308. * actually it calls that function.
  309. *
  310. * @parameter string $varName data field name
  311. * @parameter string $value value for data field. It may be an array
  312. * @return boolean
  313. * @access public
  314. *
  315. */
  316. function SetVar($varName,$value)
  317. {
  318. return($this->clsDataStorage->SetVar($varName,$value));
  319. }
  320.  
  321. function _DumpInternals()
  322. {
  323. echo "\n<br>Text: " .$GLOBALS['dbg']->print_r_to_var($this->Report);
  324. }
  325.  
  326. /**
  327. * Return True if the current PHP version is 5
  328. * False if is PHP4
  329. * @return boolean
  330. * @access public
  331. */
  332. function __php5()
  333. {
  334. return((version_compare(phpversion(), "5.0.0", "<")==-1)?false:true);
  335. }
  336. }
  337.  
  338. ?>

Documentation generated on Mon, 28 Mar 2005 15:13:09 +0200 by phpDocumentor 1.3.0RC3