- <?php
- /**
- * sptpl_clsDataStorage.php
- *
- * Data item manager class.
- * This file is part of the project SpoolTemplate.
- * There is only one instance of this class!!
- *
- * @copyright sptpl_clsDataStorage.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_clsDataStorage.php,v 2.3 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 DataStorage {
-
- /**
- * Table of the constant name
- * @var array $ConstantTbl
- */
- var $ConstantTbl;
- /**
- * Table of the data field definition
- * @var array $DataFieldTbl
- */
- var $DataFieldTbl;
- /**
- * Table of the variables/constant value
- * The variable name is the first index. The variable
- * may be an array
- * @var array $Values
- */
- var $Values;
-
- var $ValidDataFieldAttr=array("sourcefield" => 1,
- "stringformat" => 1,
- "dateformat" => 1,
- "value" => 1);
- /**
- * Array containing the counters defined. The index is
- * the counter's name.
- * @var object CCounter $Counters
- */
- var $Counters;
- /**
- * Class constructor
- */
- function DataStorage() {
- $this->ConstantTbl=array();
- $this->DataFieldTbl=array();
- $this->Values=array();
- $this->Counters=array();
- }
-
- /**
- * Load the specified template file
- * @param object CXml2Array DomNode with the constant definition
- * @access public
- */
- function AddConstantXml($Cfg)
- {
- $ConstName=$ConstValue=FALSE;
- //loop through the attributes list
- while(($ret=$Cfg->EachAttribute())!=FALSE)
- {
- list($AttribName,$value)=$ret;
- if(strtolower($AttribName)=='name') { $ConstName=$value;}
- if(strtolower($AttribName)=='value') { $ConstValue=$value;}
- }
- if($ConstName==FALSE)
- trigger_error("Constant definition without attribute name",E_USER_ERROR);
- if($ConstValue==FALSE)
- trigger_error("Constant ".$ConstName.", definition without attribute value",E_USER_ERROR);
-
- if(array_key_exists($ConstName,$this->ConstantTbl))
- trigger_error("Constant ".$ConstName.", already defined",E_USER_ERROR);
-
- // Only to define the exist
- $this->ConstantTbl[$ConstName]=TRUE;
- $this->Values[$ConstName]=$ConstValue;
- }
-
- /**
- * Add new field to field table
- * @param object XCml2Array Object with the fields definition
- * @access public
- */
- function AddFieldXml($Cfg)
- {
- $FieldName=FALSE;
- //loop through the attributes list
- while(($ret=$Cfg->EachAttribute())!=FALSE)
- {
- list($AttribName,$value)=$ret;
- if(strtolower($AttribName)=='name') { $FieldName=$value;}
- }
- if($FieldName==FALSE)
- trigger_error("Field definition without attribute name",E_USER_ERROR);
-
- //now create the empty structure for the new data field
- $this->_NewEmptyField($FieldName);
-
- // loop through the node's child, to set the option for the
- // data field
- // loop on root's child
- while(($ret=$Cfg->EachChild())!=FALSE)
- {
- list($ChildName,$value)=$ret;
- $this->_SetFieldAttrib($FieldName,strtolower($ChildName),$value->GetText());
- }
- }
-
- /**
- * Initialize the memory structure for a new data field
- * @param string fieldName
- * @access private
- */
- function _NewEmptyField($Name)
- {
- if(array_key_exists($Name,$this->DataFieldTbl))
- trigger_error("Data field ".$Name.", already defined",E_USER_ERROR);
-
- // create the new structure
- foreach($this->ValidDataFieldAttr as $val)
- $this->DataFieldTbl[$Name][$val]="";
- // If no souurce filedis specified assume tha same
- $this->DataFieldTbl[$Name]["sourcefield"]=$Name;
- $this->Values[$Name]="";
- }
-
- /**
- * Set the attribute 'attr' to the 'value' for the named 'data field'
- * @param string Name data field name
- * @param string Attr attribute name
- * @param string Value attribute value
- * @access private
- */
- function _SetFieldAttrib($Name,$Attr,$Value)
- {
-
- if(!array_key_exists($Attr,$this->ValidDataFieldAttr))
- trigger_error('Unknown attribute '.$Attr." for data field ".$Name,E_USER_ERROR);
- $this->DataFieldTbl[$Name][$Attr]=$Value;
- if($Attr=="value")
- $this->Values[$Name]=$Value;
- }
-
- /**
- *
- * @parameter object CXml2array $Cfg
- * @access public
- */
- function AddCounter($Cfg)
- {
- $c=new CCounter($this,$Cfg);
- $name=$c->GetName();
- $this->Counters[$name]=$c;
- return($name);
- }
-
- /**
- *
- * @parameter string $name
- * @access public
- */
- function RemoveCounter($name)
- {
- if(!array_key_exists($name,$this->Counters))
- trigger_error('Internal error, RemoveCounter, the counter '.$name.' does not exists.',E_USER_ERROR);
- $this->Counters[$name]->ClearCounter();
- unset($this->Counters[$name]);
- }
-
- /**
- * Return tha value for tha data field $VarName. Function used during eval'ing the row
- * Use GetVar to retrieve the value of the specified data item
- *
- * This function apply the format parameter to the value
- * @parameter string $VarName data field name
- * @parameter string $Value value for data field
- * @return string
- * @access public
- *
- */
- function GetEvalValue($VarName,$Value)
- {
- if(!array_key_exists($VarName,$this->DataFieldTbl))
- { $text=trim($Value); }
- else
- {
- $text=$this->_format($VarName,$Value);
- }
- return($text);
- }
-
- /**
- * Format the value according to stringformat parameter
- *
- * @parameter string $VarName data field name
- * @parameter string $Value value for data field
- * @return string
- * @access private
- *
- */
- function _format($VarName,$Value)
- {
- if($this->DataFieldTbl[$VarName]['stringformat']!='')
- {
- // var_dump($this->DataFieldTbl[$VarName]['stringformat']);
- $text=sprintf($this->DataFieldTbl[$VarName]['stringformat'],$Value);
- // var_dump($text);
- return($text);
- }
- }
-
- /**
- * Check whether a data field exists.
- * It doesn't support tha array
- * @parameter string $VarName Data field name. VarName must be scalar
- * @param integer Use the value from the current row or from the previous row
- * @return boolean
- * @access public
- */
- function ExistValue($VarName,$ValueType=CURRENTVALUE)
- {
- if($ValueType==CURRENTVALUE)
- return(array_key_exists($VarName,$this->Values));
- else
- return(array_key_exists($VarName,$this->OldValues));
- }
-
- /**
- * Set/add a value for data field
- *
- * @parameter string $varName data field name. VarName must be scalar
- * @parameter string $value value for data field. It may be an array
- * @return boolean
- * @access public
- *
- */
- function SetVar($varName,$value)
- {
- $this->Values[$varName]=$value;
- return(TRUE);
- }
-
- /**
- * Return tha value for tha data field $VarName
- *
- * @parameter string $VarName data field name. VarName must be scalar
- * @parameter integer $ValueType where get the value, from the current or
- * the previous?
- * @return string
- * @access public
- *
- */
- function GetVar($VarName,$ValueType=CURRENTVALUE)
- {
- if($ValueType==CURRENTVALUE) {
- if($this->ExistValue($VarName,$ValueType))
- return($this->Values[$VarName]);
- else
- trigger_error('Data field '.$VarName.' does not exist',E_USER_ERROR);
- }
- else {
- if($this->ExistValue($VarName,$ValueType))
- return($this->OldValues[$VarName]);
- else
- trigger_error('Data field '.$VarName.' does not exist',E_USER_ERROR);
- }
-
- }
-
- /**
- * 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 public
- *
- */
- function ToValue($fields)
- {
- foreach($fields as $key =>$value)
- {
- $this->Values[$key]=$value;
- // add information for some internal function (sum, count,...)
- $count='_count_'.$key;
- $sum='_sum_'.$key;
- if(!is_numeric($value))
- $this->Values[$sum]='n/a';
- else
- $this->Add($sum,$value);
- $this->Add($count);
- }
- }
-
- /**
- * Remove from $Values all data fields added from the current data row.
- *
- * @parameter array $fields Data field for the current row.
- * @access public
- */
- function RemoveFromValue($fields)
- {
- // Before remove, save the old value
- $this->OldValues=$this->Values;
-
- foreach($fields as $key =>$value)
- {
- unset($this->Values[$key]);
- $count='_count_'.$key;
- $sum='_sum_'.$key;
- unset($this->Values[$count]);
- unset($this->Values[$sum]);
- }
- }
-
- /**
- * Add $value to data item $varName. If $value is not specified
- * 1 is assumed.
- *
- * @parameter string $varName Data item name
- * @parameter mixed $value
- * @access public
- */
- function Add($varName,$value=1)
- {
- $previous=(array_key_exists($varName,$this->Values))?$this->Values[$varName]:0;
- if(!is_numeric($previous)||
- !is_numeric($value))
- {
- $this->Values[$varName]='N/A';
- }
- else
- $this->Values[$varName]=$previous+$value;
- }
-
- /**
- * Evaluate the text (=run eval on the text passed)
- *
- * @param string
- */
- function EvalText($text)
- {
- // print '<br>'.$text;
- // $txt=$text;
- eval('$txt='.$text.';');
- return($txt);
- }
-
- /**
- * Set the counters to zero
- * @param array CounterName array holding the name of the counter to set to zero
- * @access public
- */
- function ResetCounter($CounterName)
- {
- if(!count($CounterName)) {return;}
- foreach($CounterName as $name)
- {
- $this->Counters[$name]->ResetValue();
- }
- }
- /**
- * Update counter. Each counter will be added to its field
- * @param array CounterName array holding the name of the counter to update
- * @access public
- */
- function UpdateCounters($CounterName)
- {
- foreach($CounterName as $name)
- {
- $this->Counters[$name]->UpdateValue();
- }
- }
-
- /***************************************************************
- * BUILT-IN FUNCTIONS
- ****************************************************************
- */
- /**
- * Interface to the PHP function abs
- *
- * @parameter mixed
- * @access private
- * @return mixed
- */
- function abs($number)
- {
- return(abs($number));
- }
- /**
- * Interface to the PHP function ceil
- *
- * @parameter mixed
- * @access private
- * @return mixed
- */
- function ceil($number)
- {
- return(ceil($number));
- }
- /**
- * Interface to the PHP function floor
- *
- * @parameter float
- * @access private
- * @return float
- */
- function floor($number)
- {
- return(floor($number));
- }
- /**
- * Interface to the PHP function round
- *
- * @parameter float
- * @access private
- * @return float
- */
- function round($number)
- {
- return(round($number));
- }
- /**
- * Interface to the PHP function sin
- *
- * @parameter float
- * @access private
- * @return float
- */
- function sin($number)
- {
- return(sin($number));
- }
- /**
- * Interface to the PHP function cos
- *
- * @parameter float
- * @access private
- * @return float
- */
- function cos($number)
- {
- return(cos($number));
- }
- /**
- * Interface to the PHP function tan
- *
- * @parameter float
- * @access private
- * @return float
- */
- function tan($number)
- {
- return(tan($number));
- }
-
- /**
- * Interface to the PHP function atan
- *
- * @parameter float
- * @access private
- * @return float
- */
- function atan($number)
- {
- return(atan($number));
- }
- /**
- * Interface to the PHP function asin
- *
- * @parameter float
- * @access private
- * @return float
- */
- function asin($number)
- {
- return(asin($number));
- }
- /**
- * Interface to the PHP function acos
- *
- * @parameter float
- * @access private
- * @return float
- */
- function acos($number)
- {
- return(acos($number));
- }
- /**
- * Interface to the PHP function log
- *
- * @parameter float
- * @access private
- * @return float
- */
- function log($number)
- {
- return(log($number));
- }
- /**
- * Interface to the PHP function log10
- *
- * @parameter float
- * @access private
- * @return float
- */
- function log10($number)
- {
- return(log10($number));
- }
- /**
- * Interface to the PHP function exp
- *
- * @parameter float
- * @access private
- * @return float
- */
- function exp($number)
- {
- return(exp($number));
- }
- /**
- * Interface to the PHP function sqrt
- *
- * @parameter float
- * @access private
- * @return float
- */
- function sqrt($number)
- {
- if($number>0)
- return(sqrt($number));
- else
- return(0);
- }
-
- /**
- * Interface to the PHP function date
- *
- * @parameter string
- * @parameter integer
- * @access private
- * @return string
- */
- function date($format,$timestamp)
- {
- return(date($format,$timestamp));
- }
-
- /**
- * Interface to the PHP function date
- *
- * @parameter string
- * @parameter integer
- * @access private
- * @return string
- */
- function date_format($InputFormat,$OutputFormat,$date)
- {
- $len=strlen($InputFormat);
- if($len==0)
- return("");
- $year=0;
- $month=0;
- $day=0;
- $hour=0;
- $minute=0;
- $second=0;
- $InStrPtr=0;
- for($i=0;$i<$len;$i++)
- {
- switch(substr($InputFormat,$i,1))
- {
- case 'd': // day leading 0
- $day=substr($date,$InStrPtr,2);
- $InStrPtr+=2;
- break;
- case 'h': // hour 00-12
- case 'H': // hour 00-24
- $hour=substr($date,$InStrPtr,2);
- $InStrPtr+=2;
- break;
- case 'i': // minutes
- $minute=substr($date,$InStrPtr,2);
- $InStrPtr+=2;
- break;
- case 'm': // month
- $month=substr($date,$InStrPtr,2);
- $InStrPtr+=2;
- break;
- case 's': // second
- $second=substr($date,$InStrPtr,2);
- $InStrPtr+=2;
- break;
- case 'Y': // year 4 digits
- $year=substr($date,$InStrPtr,4);
- $InStrPtr+=4;
- break;
- default: // don't care of this
- $InStrPtr+=1;
- break;
- }
- }
- return(date($OutputFormat,mktime($hour,$minute,$second,$month,$day,$year)));
- }
-
- /*
- Function to add...
- number_format($number, num_decimal, decimal_point, thousand_sep)
- year, month, day
- point(string, decimal-point) formatta gli importi
- trim, ltrim, rtrim,
- */
- /**
- * Interface to the PHP function strtolower
- *
- * @parameter string
- * @access private
- * @return string
- */
- function strtolower($text)
- {
- return(strtolower($text));
- }
-
- /**
- * Interface to the PHP function strtoupper
- *
- * @parameter string
- * @access private
- * @return string
- */
- function strtoupper($text)
- {
- return(strtoupper($text));
- }
-
- /**
- * Interface to the PHP function number_format
- *
- * @parameter float
- * @parameter integer
- * @parameter string
- * @parameter string
- * @access private
- * @return string
- */
- function number_format($number,$decimal=0,$dec_point='.',$thousands_sep=',')
- {
- return(number_format($number,$decimal,$dec_point,$thousands_sep));
- }
-
-
- /**
- * Test function!
- * This function will be use by test1.php to check the procedure to
- * call the method of this class
- * Note: this method will be called by EvalText during the evaluation
- * of the row, so the function may be considered as private
- *
- * @parameter string $txt
- * @access private
- */
- function foo_internal($txt)
- {
- return('from the inside:'.$txt);
- }
-
- } // end DataStorage class
-
- ?>