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

Source for file sptpl_clsDataStorage.php

Documentation is available at sptpl_clsDataStorage.php

  1. <?php
  2. /**
  3. * sptpl_clsDataStorage.php
  4. *
  5. * Data item manager class.
  6. * This file is part of the project SpoolTemplate.
  7. * There is only one instance of this class!!
  8. *
  9. * @copyright sptpl_clsDataStorage.php is part of Sptpl project {@link http://www.andrioli.com/en/sptpl.html} and it is LGPL
  10. * @author Andrioli Darvin <darvin (inside) andrioli (dot) com>
  11. * @version $Header: d:\cvs/classistd/sptpl/sptpl_clsDataStorage.php,v 2.3 2005/03/02 21:00:54 Darvin Exp $
  12. */
  13. /*
  14. * +-------------------------------------------------------------------------+
  15. * | Sptpl |
  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.  
  40. class DataStorage {
  41.  
  42. /**
  43. * Table of the constant name
  44. * @var array $ConstantTbl
  45. */
  46. var $ConstantTbl;
  47. /**
  48. * Table of the data field definition
  49. * @var array $DataFieldTbl
  50. */
  51. var $DataFieldTbl;
  52. /**
  53. * Table of the variables/constant value
  54. * The variable name is the first index. The variable
  55. * may be an array
  56. * @var array $Values
  57. */
  58. var $Values;
  59.  
  60. var $ValidDataFieldAttr=array("sourcefield" => 1,
  61. "stringformat" => 1,
  62. "dateformat" => 1,
  63. "value" => 1);
  64. /**
  65. * Array containing the counters defined. The index is
  66. * the counter's name.
  67. * @var object CCounter $Counters
  68. */
  69. var $Counters;
  70. /**
  71. * Class constructor
  72. */
  73. function DataStorage() {
  74. $this->ConstantTbl=array();
  75. $this->DataFieldTbl=array();
  76. $this->Values=array();
  77. $this->Counters=array();
  78. }
  79.  
  80. /**
  81. * Load the specified template file
  82. * @param object CXml2Array DomNode with the constant definition
  83. * @access public
  84. */
  85. function AddConstantXml($Cfg)
  86. {
  87. $ConstName=$ConstValue=FALSE;
  88. //loop through the attributes list
  89. while(($ret=$Cfg->EachAttribute())!=FALSE)
  90. {
  91. list($AttribName,$value)=$ret;
  92. if(strtolower($AttribName)=='name') { $ConstName=$value;}
  93. if(strtolower($AttribName)=='value') { $ConstValue=$value;}
  94. }
  95. if($ConstName==FALSE)
  96. trigger_error("Constant definition without attribute name",E_USER_ERROR);
  97. if($ConstValue==FALSE)
  98. trigger_error("Constant ".$ConstName.", definition without attribute value",E_USER_ERROR);
  99.  
  100. if(array_key_exists($ConstName,$this->ConstantTbl))
  101. trigger_error("Constant ".$ConstName.", already defined",E_USER_ERROR);
  102.  
  103. // Only to define the exist
  104. $this->ConstantTbl[$ConstName]=TRUE;
  105. $this->Values[$ConstName]=$ConstValue;
  106. }
  107.  
  108. /**
  109. * Add new field to field table
  110. * @param object XCml2Array Object with the fields definition
  111. * @access public
  112. */
  113. function AddFieldXml($Cfg)
  114. {
  115. $FieldName=FALSE;
  116. //loop through the attributes list
  117. while(($ret=$Cfg->EachAttribute())!=FALSE)
  118. {
  119. list($AttribName,$value)=$ret;
  120. if(strtolower($AttribName)=='name') { $FieldName=$value;}
  121. }
  122. if($FieldName==FALSE)
  123. trigger_error("Field definition without attribute name",E_USER_ERROR);
  124.  
  125. //now create the empty structure for the new data field
  126. $this->_NewEmptyField($FieldName);
  127.  
  128. // loop through the node's child, to set the option for the
  129. // data field
  130. // loop on root's child
  131. while(($ret=$Cfg->EachChild())!=FALSE)
  132. {
  133. list($ChildName,$value)=$ret;
  134. $this->_SetFieldAttrib($FieldName,strtolower($ChildName),$value->GetText());
  135. }
  136. }
  137.  
  138. /**
  139. * Initialize the memory structure for a new data field
  140. * @param string fieldName
  141. * @access private
  142. */
  143. function _NewEmptyField($Name)
  144. {
  145. if(array_key_exists($Name,$this->DataFieldTbl))
  146. trigger_error("Data field ".$Name.", already defined",E_USER_ERROR);
  147.  
  148. // create the new structure
  149. foreach($this->ValidDataFieldAttr as $val)
  150. $this->DataFieldTbl[$Name][$val]="";
  151. // If no souurce filedis specified assume tha same
  152. $this->DataFieldTbl[$Name]["sourcefield"]=$Name;
  153. $this->Values[$Name]="";
  154. }
  155.  
  156. /**
  157. * Set the attribute 'attr' to the 'value' for the named 'data field'
  158. * @param string Name data field name
  159. * @param string Attr attribute name
  160. * @param string Value attribute value
  161. * @access private
  162. */
  163. function _SetFieldAttrib($Name,$Attr,$Value)
  164. {
  165.  
  166. if(!array_key_exists($Attr,$this->ValidDataFieldAttr))
  167. trigger_error('Unknown attribute '.$Attr." for data field ".$Name,E_USER_ERROR);
  168. $this->DataFieldTbl[$Name][$Attr]=$Value;
  169. if($Attr=="value")
  170. $this->Values[$Name]=$Value;
  171. }
  172.  
  173. /**
  174. *
  175. * @parameter object CXml2array $Cfg
  176. * @access public
  177. */
  178. function AddCounter($Cfg)
  179. {
  180. $c=new CCounter($this,$Cfg);
  181. $name=$c->GetName();
  182. $this->Counters[$name]=$c;
  183. return($name);
  184. }
  185.  
  186. /**
  187. *
  188. * @parameter string $name
  189. * @access public
  190. */
  191. function RemoveCounter($name)
  192. {
  193. if(!array_key_exists($name,$this->Counters))
  194. trigger_error('Internal error, RemoveCounter, the counter '.$name.' does not exists.',E_USER_ERROR);
  195. $this->Counters[$name]->ClearCounter();
  196. unset($this->Counters[$name]);
  197. }
  198.  
  199. /**
  200. * Return tha value for tha data field $VarName. Function used during eval'ing the row
  201. * Use GetVar to retrieve the value of the specified data item
  202. *
  203. * This function apply the format parameter to the value
  204. * @parameter string $VarName data field name
  205. * @parameter string $Value value for data field
  206. * @return string
  207. * @access public
  208. *
  209. */
  210. function GetEvalValue($VarName,$Value)
  211. {
  212. if(!array_key_exists($VarName,$this->DataFieldTbl))
  213. { $text=trim($Value); }
  214. else
  215. {
  216. $text=$this->_format($VarName,$Value);
  217. }
  218. return($text);
  219. }
  220.  
  221. /**
  222. * Format the value according to stringformat parameter
  223. *
  224. * @parameter string $VarName data field name
  225. * @parameter string $Value value for data field
  226. * @return string
  227. * @access private
  228. *
  229. */
  230. function _format($VarName,$Value)
  231. {
  232. if($this->DataFieldTbl[$VarName]['stringformat']!='')
  233. {
  234. // var_dump($this->DataFieldTbl[$VarName]['stringformat']);
  235. $text=sprintf($this->DataFieldTbl[$VarName]['stringformat'],$Value);
  236. // var_dump($text);
  237. return($text);
  238. }
  239. }
  240.  
  241. /**
  242. * Check whether a data field exists.
  243. * It doesn't support tha array
  244. * @parameter string $VarName Data field name. VarName must be scalar
  245. * @param integer Use the value from the current row or from the previous row
  246. * @return boolean
  247. * @access public
  248. */
  249. function ExistValue($VarName,$ValueType=CURRENTVALUE)
  250. {
  251. if($ValueType==CURRENTVALUE)
  252. return(array_key_exists($VarName,$this->Values));
  253. else
  254. return(array_key_exists($VarName,$this->OldValues));
  255. }
  256.  
  257. /**
  258. * Set/add a value for data field
  259. *
  260. * @parameter string $varName data field name. VarName must be scalar
  261. * @parameter string $value value for data field. It may be an array
  262. * @return boolean
  263. * @access public
  264. *
  265. */
  266. function SetVar($varName,$value)
  267. {
  268. $this->Values[$varName]=$value;
  269. return(TRUE);
  270. }
  271.  
  272. /**
  273. * Return tha value for tha data field $VarName
  274. *
  275. * @parameter string $VarName data field name. VarName must be scalar
  276. * @parameter integer $ValueType where get the value, from the current or
  277. * the previous?
  278. * @return string
  279. * @access public
  280. *
  281. */
  282. function GetVar($VarName,$ValueType=CURRENTVALUE)
  283. {
  284. if($ValueType==CURRENTVALUE) {
  285. if($this->ExistValue($VarName,$ValueType))
  286. return($this->Values[$VarName]);
  287. else
  288. trigger_error('Data field '.$VarName.' does not exist',E_USER_ERROR);
  289. }
  290. else {
  291. if($this->ExistValue($VarName,$ValueType))
  292. return($this->OldValues[$VarName]);
  293. else
  294. trigger_error('Data field '.$VarName.' does not exist',E_USER_ERROR);
  295. }
  296.  
  297. }
  298.  
  299. /**
  300. * Put into the array $Value each data from the fiels array.
  301. * The parameter $fields is an associative array where the index is the data field name
  302. * and the value is the data field value
  303. *
  304. * @parameter array $fields Data field for the current row.
  305. * @access public
  306. *
  307. */
  308. function ToValue($fields)
  309. {
  310. foreach($fields as $key =>$value)
  311. {
  312. $this->Values[$key]=$value;
  313. // add information for some internal function (sum, count,...)
  314. $count='_count_'.$key;
  315. $sum='_sum_'.$key;
  316. if(!is_numeric($value))
  317. $this->Values[$sum]='n/a';
  318. else
  319. $this->Add($sum,$value);
  320. $this->Add($count);
  321. }
  322. }
  323.  
  324. /**
  325. * Remove from $Values all data fields added from the current data row.
  326. *
  327. * @parameter array $fields Data field for the current row.
  328. * @access public
  329. */
  330. function RemoveFromValue($fields)
  331. {
  332. // Before remove, save the old value
  333. $this->OldValues=$this->Values;
  334.  
  335. foreach($fields as $key =>$value)
  336. {
  337. unset($this->Values[$key]);
  338. $count='_count_'.$key;
  339. $sum='_sum_'.$key;
  340. unset($this->Values[$count]);
  341. unset($this->Values[$sum]);
  342. }
  343. }
  344.  
  345. /**
  346. * Add $value to data item $varName. If $value is not specified
  347. * 1 is assumed.
  348. *
  349. * @parameter string $varName Data item name
  350. * @parameter mixed $value
  351. * @access public
  352. */
  353. function Add($varName,$value=1)
  354. {
  355. $previous=(array_key_exists($varName,$this->Values))?$this->Values[$varName]:0;
  356. if(!is_numeric($previous)||
  357. !is_numeric($value))
  358. {
  359. $this->Values[$varName]='N/A';
  360. }
  361. else
  362. $this->Values[$varName]=$previous+$value;
  363. }
  364.  
  365. /**
  366. * Evaluate the text (=run eval on the text passed)
  367. *
  368. * @param string
  369. */
  370. function EvalText($text)
  371. {
  372. // print '<br>'.$text;
  373. // $txt=$text;
  374. eval('$txt='.$text.';');
  375. return($txt);
  376. }
  377.  
  378. /**
  379. * Set the counters to zero
  380. * @param array CounterName array holding the name of the counter to set to zero
  381. * @access public
  382. */
  383. function ResetCounter($CounterName)
  384. {
  385. if(!count($CounterName)) {return;}
  386. foreach($CounterName as $name)
  387. {
  388. $this->Counters[$name]->ResetValue();
  389. }
  390. }
  391. /**
  392. * Update counter. Each counter will be added to its field
  393. * @param array CounterName array holding the name of the counter to update
  394. * @access public
  395. */
  396. function UpdateCounters($CounterName)
  397. {
  398. foreach($CounterName as $name)
  399. {
  400. $this->Counters[$name]->UpdateValue();
  401. }
  402. }
  403.  
  404. /***************************************************************
  405. * BUILT-IN FUNCTIONS
  406. ****************************************************************
  407. */
  408. /**
  409. * Interface to the PHP function abs
  410. *
  411. * @parameter mixed
  412. * @access private
  413. * @return mixed
  414. */
  415. function abs($number)
  416. {
  417. return(abs($number));
  418. }
  419. /**
  420. * Interface to the PHP function ceil
  421. *
  422. * @parameter mixed
  423. * @access private
  424. * @return mixed
  425. */
  426. function ceil($number)
  427. {
  428. return(ceil($number));
  429. }
  430. /**
  431. * Interface to the PHP function floor
  432. *
  433. * @parameter float
  434. * @access private
  435. * @return float
  436. */
  437. function floor($number)
  438. {
  439. return(floor($number));
  440. }
  441. /**
  442. * Interface to the PHP function round
  443. *
  444. * @parameter float
  445. * @access private
  446. * @return float
  447. */
  448. function round($number)
  449. {
  450. return(round($number));
  451. }
  452. /**
  453. * Interface to the PHP function sin
  454. *
  455. * @parameter float
  456. * @access private
  457. * @return float
  458. */
  459. function sin($number)
  460. {
  461. return(sin($number));
  462. }
  463. /**
  464. * Interface to the PHP function cos
  465. *
  466. * @parameter float
  467. * @access private
  468. * @return float
  469. */
  470. function cos($number)
  471. {
  472. return(cos($number));
  473. }
  474. /**
  475. * Interface to the PHP function tan
  476. *
  477. * @parameter float
  478. * @access private
  479. * @return float
  480. */
  481. function tan($number)
  482. {
  483. return(tan($number));
  484. }
  485.  
  486. /**
  487. * Interface to the PHP function atan
  488. *
  489. * @parameter float
  490. * @access private
  491. * @return float
  492. */
  493. function atan($number)
  494. {
  495. return(atan($number));
  496. }
  497. /**
  498. * Interface to the PHP function asin
  499. *
  500. * @parameter float
  501. * @access private
  502. * @return float
  503. */
  504. function asin($number)
  505. {
  506. return(asin($number));
  507. }
  508. /**
  509. * Interface to the PHP function acos
  510. *
  511. * @parameter float
  512. * @access private
  513. * @return float
  514. */
  515. function acos($number)
  516. {
  517. return(acos($number));
  518. }
  519. /**
  520. * Interface to the PHP function log
  521. *
  522. * @parameter float
  523. * @access private
  524. * @return float
  525. */
  526. function log($number)
  527. {
  528. return(log($number));
  529. }
  530. /**
  531. * Interface to the PHP function log10
  532. *
  533. * @parameter float
  534. * @access private
  535. * @return float
  536. */
  537. function log10($number)
  538. {
  539. return(log10($number));
  540. }
  541. /**
  542. * Interface to the PHP function exp
  543. *
  544. * @parameter float
  545. * @access private
  546. * @return float
  547. */
  548. function exp($number)
  549. {
  550. return(exp($number));
  551. }
  552. /**
  553. * Interface to the PHP function sqrt
  554. *
  555. * @parameter float
  556. * @access private
  557. * @return float
  558. */
  559. function sqrt($number)
  560. {
  561. if($number>0)
  562. return(sqrt($number));
  563. else
  564. return(0);
  565. }
  566.  
  567. /**
  568. * Interface to the PHP function date
  569. *
  570. * @parameter string
  571. * @parameter integer
  572. * @access private
  573. * @return string
  574. */
  575. function date($format,$timestamp)
  576. {
  577. return(date($format,$timestamp));
  578. }
  579.  
  580. /**
  581. * Interface to the PHP function date
  582. *
  583. * @parameter string
  584. * @parameter integer
  585. * @access private
  586. * @return string
  587. */
  588. function date_format($InputFormat,$OutputFormat,$date)
  589. {
  590. $len=strlen($InputFormat);
  591. if($len==0)
  592. return("");
  593. $year=0;
  594. $month=0;
  595. $day=0;
  596. $hour=0;
  597. $minute=0;
  598. $second=0;
  599. $InStrPtr=0;
  600. for($i=0;$i<$len;$i++)
  601. {
  602. switch(substr($InputFormat,$i,1))
  603. {
  604. case 'd': // day leading 0
  605. $day=substr($date,$InStrPtr,2);
  606. $InStrPtr+=2;
  607. break;
  608. case 'h': // hour 00-12
  609. case 'H': // hour 00-24
  610. $hour=substr($date,$InStrPtr,2);
  611. $InStrPtr+=2;
  612. break;
  613. case 'i': // minutes
  614. $minute=substr($date,$InStrPtr,2);
  615. $InStrPtr+=2;
  616. break;
  617. case 'm': // month
  618. $month=substr($date,$InStrPtr,2);
  619. $InStrPtr+=2;
  620. break;
  621. case 's': // second
  622. $second=substr($date,$InStrPtr,2);
  623. $InStrPtr+=2;
  624. break;
  625. case 'Y': // year 4 digits
  626. $year=substr($date,$InStrPtr,4);
  627. $InStrPtr+=4;
  628. break;
  629. default: // don't care of this
  630. $InStrPtr+=1;
  631. break;
  632. }
  633. }
  634. return(date($OutputFormat,mktime($hour,$minute,$second,$month,$day,$year)));
  635. }
  636.  
  637. /*
  638. Function to add...
  639. number_format($number, num_decimal, decimal_point, thousand_sep)
  640. year, month, day
  641. point(string, decimal-point) formatta gli importi
  642. trim, ltrim, rtrim,
  643. */
  644. /**
  645. * Interface to the PHP function strtolower
  646. *
  647. * @parameter string
  648. * @access private
  649. * @return string
  650. */
  651. function strtolower($text)
  652. {
  653. return(strtolower($text));
  654. }
  655.  
  656. /**
  657. * Interface to the PHP function strtoupper
  658. *
  659. * @parameter string
  660. * @access private
  661. * @return string
  662. */
  663. function strtoupper($text)
  664. {
  665. return(strtoupper($text));
  666. }
  667.  
  668. /**
  669. * Interface to the PHP function number_format
  670. *
  671. * @parameter float
  672. * @parameter integer
  673. * @parameter string
  674. * @parameter string
  675. * @access private
  676. * @return string
  677. */
  678. function number_format($number,$decimal=0,$dec_point='.',$thousands_sep=',')
  679. {
  680. return(number_format($number,$decimal,$dec_point,$thousands_sep));
  681. }
  682.  
  683.  
  684. /**
  685. * Test function!
  686. * This function will be use by test1.php to check the procedure to
  687. * call the method of this class
  688. * Note: this method will be called by EvalText during the evaluation
  689. * of the row, so the function may be considered as private
  690. *
  691. * @parameter string $txt
  692. * @access private
  693. */
  694. function foo_internal($txt)
  695. {
  696. return('from the inside:'.$txt);
  697. }
  698.  
  699. } // end DataStorage class
  700.  
  701. ?>

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