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

Source for file sptpl_clsCounter.php

Documentation is available at sptpl_clsCounter.php

  1. <?php
  2. /**
  3. * @copyright sptpl_clsCounter.php is part of Sptpl project {@link http://www.andrioli.com/en/sptpl.html} and it is LGPL
  4. * @author Andrioli Darvin <darvin (inside) andrioli (dot) com>
  5. * @version $Header: d:\cvs/classistd/sptpl/sptpl_clsCounter.php,v 2.2 2005/03/02 21:00:54 Darvin Exp $
  6. */
  7. /*
  8. * +-------------------------------------------------------------------------+
  9. * | Sptpl |
  10. * +-------------------------------------------------------------------------+
  11. * | Copyright (c) 2003-2005 Andrioli Darvin |
  12. * | Email <darvin (inside) andrioli (dot) com> |
  13. * | Web http://www.andrioli.com/en/sptpl.html |
  14. * | Download http://www.phpclasses.org/browse.html/package/1326.html |
  15. * | |
  16. * +-------------------------------------------------------------------------+
  17. * | This library is free software; you can redistribute it and/or modify |
  18. * | it under the terms of the GNU Lesser General Public License as |
  19. * | published by the Free Software Foundation; either version 2 of the |
  20. * | License, or (at your option) any later version. |
  21. * | |
  22. * | This library is distributed in the hope that it will be useful, but |
  23. * | WITHOUT ANY WARRANTY; without even the implied warranty of |
  24. * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
  25. * | Lesser General Public License for more details. |
  26. * | |
  27. * | You should have received a copy of the GNU Lesser General Public |
  28. * | License along with this library; if not, write to the Free Software |
  29. * | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
  30. * +-------------------------------------------------------------------------+
  31. */
  32.  
  33. class CCounter {
  34.  
  35. var $name;
  36.  
  37. var $field;
  38.  
  39. var $DataMgr;
  40.  
  41. var $AutoIncrement;
  42.  
  43. /**
  44. * Class constructor
  45. *
  46. * @param object CDataStorage
  47. * @param object CXml2Array
  48. * @access public
  49. */
  50. function CCounter(&$RefToDataMgr,$Cfg)
  51. {
  52. $this->DataMgr=&$RefToDataMgr;
  53. if(!is_object($this->DataMgr))
  54. trigger_error('Internal error: CCounter, first parameter should be an object', E_USER_ERROR);
  55. $this->AutoIncrement=FALSE;
  56. $this->ParseXml($Cfg);
  57. }
  58. /**
  59. * Parse the block tag and its childs and set the intenal structure
  60. *
  61. * @param object CXml2Array
  62. * @access public
  63. */
  64. function ParseXml($Cfg)
  65. {
  66. while(($ret=$Cfg->EachAttribute())!=FALSE)
  67. {
  68. list($AttribName,$value)=$ret;
  69. switch(strtolower($AttribName))
  70. {
  71. case 'name' :
  72. $this->name=$value;
  73. break;
  74. case 'field' :
  75. $fin=$value;
  76. $this->field=$this->_ExtractVar($fin);
  77. break;
  78. case 'autoincrement' :
  79. if(strtolower($value)=='y')
  80. $this->AutoIncrement=TRUE;
  81. break;
  82.  
  83. }
  84. }
  85. if($this->name=='')
  86. trigger_error('Configuration error: counter without attribute name',E_USER_ERROR);
  87. // set the counter to start value
  88. $this->ResetValue();
  89. }
  90.  
  91. function GetName()
  92. {
  93. return($this->name);
  94. }
  95.  
  96. /**
  97. * Parse the text and decode the function or the data field from the given text
  98. *
  99. * @param string Text
  100. * @access private
  101. */
  102. function _ExtractVar($Text)
  103. {
  104. $OutText="";
  105. if(preg_match("/^(\w+)\(([\$\w\][,\'\" ]+)*\)*/",$Text,$var))
  106. {
  107. // Function Type: $var[1] -> Function name
  108. // $var[2] -> parameters
  109. // Is this function defined inside the DataManager class?
  110. // If so, I prepend $this to the function's name
  111. if(method_exists($this->DataMgr,$var[1]))
  112. {
  113. $OutText.='$this->DataMgr->';
  114. }
  115.  
  116. $OutText.=$var[1]."(";
  117. if(array_key_exists(2,$var))
  118. {
  119. $param=explode(",",$var[2]);
  120. $i=0;
  121. foreach($param as $value)
  122. {
  123. if($i) { $OutText.=",";}
  124. $OutText.=$this->_ExtractVar($value);
  125. $i++;
  126. }
  127. }
  128. $OutText.=")";
  129. }
  130. else
  131. {
  132. if(preg_match('/^\$([a-zA-Z0-9]+)((\[[0-9a-zA-Z\'\"]+\])*)*/',$Text,$var)) //'"
  133. {
  134. // Variable Type: $var[1] -> Variable name
  135. // $var[2] -> squares bracket used by array
  136. $OutText.='$this->DataMgr->GetEvalValue("'.$var[1].'",$this->DataMgr->Values["'.$var[1].'"]'.$var[2].')';
  137. }
  138. else
  139. $OutText=$Text;
  140. }
  141. return($OutText);
  142. }
  143.  
  144.  
  145. function ResetValue()
  146. {
  147. $this->DataMgr->SetVar($this->name,0);
  148. }
  149.  
  150. function UpdateValue()
  151. {
  152. $OldValue=$this->DataMgr->GetVar($this->name);
  153. if($this->AutoIncrement)
  154. $NewValue=$OldValue+1;
  155. else
  156. eval('$NewValue=$OldValue+'.$this->field.';');
  157. $this->DataMgr->SetVar($this->name,$NewValue);
  158. }
  159.  
  160.  
  161. } // end CCounter class
  162. ?>

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