- <?php
- /**
- * @copyright sptpl_clsCounter.php is part of Sptpl project {@link http://www.andrioli.com/en/sptpl.html} and it is LGPL
- * @author Andrioli Darvin <darvin (inside) andrioli (dot) com>
- * @version $Header: d:\cvs/classistd/sptpl/sptpl_clsCounter.php,v 2.2 2005/03/02 21:00:54 Darvin Exp $
- */
- /*
- * +-------------------------------------------------------------------------+
- * | Sptpl |
- * +-------------------------------------------------------------------------+
- * | 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 |
- * +-------------------------------------------------------------------------+
- */
-
- class CCounter {
-
- var $name;
-
- var $field;
-
- var $DataMgr;
-
- var $AutoIncrement;
-
- /**
- * Class constructor
- *
- * @param object CDataStorage
- * @param object CXml2Array
- * @access public
- */
- function CCounter(&$RefToDataMgr,$Cfg)
- {
- $this->DataMgr=&$RefToDataMgr;
- if(!is_object($this->DataMgr))
- trigger_error('Internal error: CCounter, first parameter should be an object', E_USER_ERROR);
- $this->AutoIncrement=FALSE;
- $this->ParseXml($Cfg);
- }
- /**
- * Parse the block tag and its childs and set the intenal structure
- *
- * @param object CXml2Array
- * @access public
- */
- function ParseXml($Cfg)
- {
- while(($ret=$Cfg->EachAttribute())!=FALSE)
- {
- list($AttribName,$value)=$ret;
- switch(strtolower($AttribName))
- {
- case 'name' :
- $this->name=$value;
- break;
- case 'field' :
- $fin=$value;
- $this->field=$this->_ExtractVar($fin);
- break;
- case 'autoincrement' :
- if(strtolower($value)=='y')
- $this->AutoIncrement=TRUE;
- break;
-
- }
- }
- if($this->name=='')
- trigger_error('Configuration error: counter without attribute name',E_USER_ERROR);
- // set the counter to start value
- $this->ResetValue();
- }
-
- function GetName()
- {
- return($this->name);
- }
-
- /**
- * Parse the text and decode the function or the data field from the given text
- *
- * @param string Text
- * @access private
- */
- function _ExtractVar($Text)
- {
- $OutText="";
- if(preg_match("/^(\w+)\(([\$\w\][,\'\" ]+)*\)*/",$Text,$var))
- {
- // Function Type: $var[1] -> Function name
- // $var[2] -> parameters
- // Is this function defined inside the DataManager class?
- // If so, I prepend $this to the function's name
- if(method_exists($this->DataMgr,$var[1]))
- {
- $OutText.='$this->DataMgr->';
- }
-
- $OutText.=$var[1]."(";
- if(array_key_exists(2,$var))
- {
- $param=explode(",",$var[2]);
- $i=0;
- foreach($param as $value)
- {
- if($i) { $OutText.=",";}
- $OutText.=$this->_ExtractVar($value);
- $i++;
- }
- }
- $OutText.=")";
- }
- else
- {
- if(preg_match('/^\$([a-zA-Z0-9]+)((\[[0-9a-zA-Z\'\"]+\])*)*/',$Text,$var)) //'"
- {
- // Variable Type: $var[1] -> Variable name
- // $var[2] -> squares bracket used by array
- $OutText.='$this->DataMgr->GetEvalValue("'.$var[1].'",$this->DataMgr->Values["'.$var[1].'"]'.$var[2].')';
- }
- else
- $OutText=$Text;
- }
- return($OutText);
- }
-
-
- function ResetValue()
- {
- $this->DataMgr->SetVar($this->name,0);
- }
-
- function UpdateValue()
- {
- $OldValue=$this->DataMgr->GetVar($this->name);
- if($this->AutoIncrement)
- $NewValue=$OldValue+1;
- else
- eval('$NewValue=$OldValue+'.$this->field.';');
- $this->DataMgr->SetVar($this->name,$NewValue);
- }
-
-
- } // end CCounter class
- ?>