eZ Publish  [4.0]
eztemplatelogicoperator.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZTemplateLogicOperator class
00004 //
00005 // Created on: <18-Apr-2002 12:15:07 amos>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*!
00032   \class eZTemplateLogicOperator eztemplatelogicoperator.php
00033   \ingroup eZTemplateOperators
00034   \brief Logical operators for creating and manipulating booleans
00035 
00036   This class adds powerful template handling by enabling logical operators
00037   which alter the output of templates from input values.
00038 
00039   How counts are interpreted:
00040   -# If the data is an array the array count is used
00041   -# If the data is an object the object attribute count is used
00042   -# If the data is a numeric the value is used
00043   -# If the data is a boolean false is 0 and true is 1
00044   -# For all other data 0 is used
00045 
00046   Data is considered null (or false) if the data count is 0 (see above) or
00047   the data is really null (is_null). Data is considered true if it is not null.
00048 
00049   The supported operators are:
00050   - lt\n
00051     Returns true if the input count is less than the parameter data count. See how
00052     count is interpreted above.
00053   - le\n
00054     Same as lt but use less than or equal to.
00055   - gt\n
00056     Same as lt but returns true for input greater than data.
00057   - ge\n
00058     Same as gt but use greater than or equal to.
00059   - eq\n
00060     Returns true if all the input parameters match. Matching is casual meaning
00061     that an integer of value 0 will match a boolean of type false.
00062   - ne\n
00063     Returns true if one or more of the input parameters does not match.
00064     Matching is casual meaning that an integer of value 0 will match a boolean
00065     of type false.
00066   - null\n
00067     Returns true if the data is null, false otherwise
00068   - not\n
00069     Returns true if the data is false or false if data is true
00070   - true
00071   - false\n
00072     Creates a true/false boolean
00073   - or\n
00074     Evaluates all parameter values until one is found to be true (see above), then
00075     returns that value. The remaining parameters are not evaluated at all.
00076     If there are no parameter or all elements were false it returns false.
00077   - and\n
00078     Evaluates all parameter values until one is found to be false (see above), then
00079     returns that false. The remaining parameters are not evaluated at all.
00080     If there are no parameter it returns false, if no elements were false it returns the last parameter value.
00081   - choose\n
00082     Uses the input count to pick one of the parameter elements. The input count equals
00083     the parameter index.
00084 
00085 */
00086 
00087 class eZTemplateLogicOperator
00088 {
00089     /*!
00090      Initializes the operator class with the various operator names.
00091     */
00092     function eZTemplateLogicOperator()
00093     {
00094         $this->Operators = array( 'lt', 'gt', 'le', 'ge', 'eq', 'ne',
00095                                   'null', 'not',
00096                                   'or', 'and',
00097                                   'true', 'false', 'choose' );
00098         foreach ( $this->Operators as $operator )
00099         {
00100             $name = $operator . 'Name';
00101             $name[0] = $name[0] & "\xdf";
00102             $this->$name = $operator;
00103         }
00104     }
00105 
00106     /*!
00107      Returns the operators in this class.
00108     */
00109     function operatorList()
00110     {
00111         return $this->Operators;
00112     }
00113 
00114     /*!
00115      \return true to tell the template engine that the parameter list exists per operator type.
00116     */
00117     function namedParameterPerOperator()
00118     {
00119         return true;
00120     }
00121 
00122     function operatorTemplateHints()
00123     {
00124         return array( $this->LtName => array( 'input' => true,
00125                                               'output' => true,
00126                                               'parameters' => 1,
00127                                               'element-transformation' => true,
00128                                               'transform-parameters' => true,
00129                                               'input-as-parameter' => true,
00130                                               'element-transformation-func' => 'logicalComparisonTransformation'),
00131                       $this->GtName => array( 'input' => true,
00132                                               'output' => true,
00133                                               'parameters' => 1,
00134                                               'element-transformation' => true,
00135                                               'transform-parameters' => true,
00136                                               'input-as-parameter' => true,
00137                                               'element-transformation-func' => 'logicalComparisonTransformation'),
00138                       $this->LeName => array( 'input' => true,
00139                                               'output' => true,
00140                                               'parameters' => 1,
00141                                               'element-transformation' => true,
00142                                               'transform-parameters' => true,
00143                                               'input-as-parameter' => true,
00144                                               'element-transformation-func' => 'logicalComparisonTransformation'),
00145                       $this->GeName => array( 'input' => true,
00146                                               'output' => true,
00147                                               'parameters' => 1,
00148                                               'element-transformation' => true,
00149                                               'transform-parameters' => true,
00150                                               'input-as-parameter' => true,
00151                                               'element-transformation-func' => 'logicalComparisonTransformation'),
00152 
00153                       $this->EqName => array( 'input' => true,
00154                                               'output' => true,
00155                                               'parameters' => true,
00156                                               'element-transformation' => true,
00157                                               'transform-parameters' => true,
00158                                               'input-as-parameter' => true,
00159                                               'element-transformation-func' => 'logicalComparisonTransformation'),
00160                       $this->NeName => array( 'input' => true,
00161                                               'output' => true,
00162                                               'parameters' => true,
00163                                               'element-transformation' => true,
00164                                               'transform-parameters' => true,
00165                                               'input-as-parameter' => true,
00166                                               'element-transformation-func' => 'logicalComparisonTransformation'),
00167 
00168                       $this->NullName  => array( 'input' => true,
00169                                                  'output' => true,
00170                                                  'parameters' => false ),
00171 
00172                       $this->OrName => array( 'input' => true,
00173                                               'output' => true,
00174                                               'parameters' => true,
00175                                               'element-transformation' => true,
00176                                               'transform-parameters' => true,
00177                                               'input-as-parameter' => true,
00178                                               'element-transformation-func' => 'logicalComparisonTransformation'),
00179                       $this->AndName => array( 'input' => true,
00180                                                'output' => true,
00181                                                'parameters' => true,
00182                                                'element-transformation' => true,
00183                                                'transform-parameters' => true,
00184                                                'input-as-parameter' => true,
00185                                                'element-transformation-func' => 'logicalComparisonTransformation'),
00186 
00187                       $this->NotName => array( 'input' => true,
00188                                                'output' => true,
00189                                                'parameters' => true,
00190                                                'element-transformation' => true,
00191                                                'transform-parameters' => true,
00192                                                'input-as-parameter' => true,
00193                                                'element-transformation-func' => 'negateTransformation'),
00194 
00195                       $this->ChooseName => array( 'input' => true,
00196                                                   'output' => true,
00197                                                   'parameters' => true,
00198                                                   'element-transformation' => true,
00199                                                   'transform-parameters' => true,
00200                                                   'input-as-parameter' => 'always',
00201                                                   'element-transformation-func' => 'chooseTransformation'),
00202                       $this->TrueName => array( 'input' => false,
00203                                                 'output' => true,
00204                                                 'parameters' => false,
00205                                                 'static' => true,
00206                                                 'element-transformation' => true,
00207                                                 'transform-parameters' => true,
00208                                                 'input-as-parameter' => true,
00209                                                 'element-transformation-func' => 'trueFalseTransformation'),
00210                       $this->FalseName => array( 'input' => false,
00211                                                  'output' => true,
00212                                                  'parameters' => false,
00213                                                  'static' => true,
00214                                                  'element-transformation' => true,
00215                                                  'transform-parameters' => true,
00216                                                  'input-as-parameter' => true,
00217                                                  'element-transformation-func' => 'trueFalseTransformation') );
00218     }
00219 
00220     function operatorCompiledStaticData( $operatorName )
00221     {
00222         switch( $operatorName )
00223         {
00224             case $this->TrueName:
00225             {
00226                 return true;
00227             } break;
00228             case $this->FalseName:
00229             {
00230                 return false;
00231             } break;
00232         }
00233         return false;
00234     }
00235 
00236     /*!
00237      See eZTemplateOperator::namedParameterList
00238     */
00239     function namedParameterList()
00240     {
00241         return array( $this->LtName => array( "threshold" => array( "type" => "mixed",
00242                                                                     "required" => true,
00243                                                                     "default" => false ) ),
00244                       $this->GtName => array( "threshold" => array( "type" => "mixed",
00245                                                                     "required" => true,
00246                                                                     "default" => false ) ),
00247                       $this->LeName => array( "threshold" => array( "type" => "mixed",
00248                                                                     "required" => true,
00249                                                                     "default" => false ) ),
00250                       $this->GeName => array( "threshold" => array( "type" => "mixed",
00251                                                                     "required" => true,
00252                                                                     "default" => false ) ) );
00253     }
00254 
00255 
00256     function logicalComparisonTransformation( $operatorName, &$node, $tpl, &$resourceData,
00257                                                $element, $lastElement, $elementList, $elementTree, &$parameters )
00258     {
00259         $values = array();
00260         $function = $operatorName;
00261         $minParameterCount = $maxParameterCount = 2;
00262 
00263         switch ( $operatorName )
00264         {
00265         case 'lt':
00266             $operator = '<';
00267             break;
00268         case 'le':
00269             $operator = '<=';
00270             break;
00271         case 'gt':
00272             $operator = '>';
00273             break;
00274         case 'ge':
00275             $operator = '>=';
00276             break;
00277         case 'eq':
00278             $operator = '==';
00279             break;
00280         case 'ne':
00281             $operator = '!=';
00282             break;
00283         case 'and':
00284             $operator = 'and';
00285             $maxParameterCount = false;
00286             break;
00287         case 'or':
00288             $operator = 'or';
00289             $maxParameterCount = false;
00290             break;
00291         }
00292         if ( ( count( $parameters ) < 2 ) ||
00293              ( $maxParameterCount && ( count( $parameters ) > $maxParameterCount ) ) )
00294         {
00295             return false;
00296         }
00297         $newElements = array();
00298 
00299         if ( $operatorName == 'or' )
00300         {
00301             $staticResult = false;
00302             $staticValue = null;
00303             $dynamicParameters = array();
00304             $addDynamic = false;
00305             $lastValue = null;
00306             foreach ( $parameters as $parameter )
00307             {
00308                 if ( $addDynamic )
00309                 {
00310                     $dynamicParameters[] = $parameter;
00311                     continue;
00312                 }
00313                 if ( eZTemplateNodeTool::isStaticElement( $parameter ) )
00314                 {
00315                     $parameterValue = eZTemplateNodeTool::elementStaticValue( $parameter );
00316                     if ( $staticValue === null )
00317                     {
00318                         $staticValue = $parameterValue;
00319                     }
00320                     else
00321                     {
00322                         $staticValue = ( $staticValue or $parameterValue );
00323                     }
00324                     $lastValue = $parameterValue;
00325                     if ( $parameterValue )
00326                     {
00327                         $staticResult = true;
00328                         break;
00329                     }
00330                     continue;
00331                 }
00332                 $addDynamic = true;
00333                 $dynamicParameters[] = $parameter;
00334                 $staticValue = null;
00335             }
00336             if ( count( $dynamicParameters ) == 0 )
00337             {
00338                 if ( !$staticResult )
00339                     $lastValue = false;
00340                 $newElements[] = eZTemplateNodeTool::createStaticElement( $lastValue );
00341                 return $newElements;
00342             }
00343 
00344             $code = '';
00345             $counter = 0;
00346             foreach ( $dynamicParameters as $parameter )
00347             {
00348                 if ( $counter++ )
00349                 {
00350                     $code .= "else ";
00351                 }
00352                 $code .= ( "if ( %$counter% )\n" .
00353                            "    %output% = %$counter%;\n" );
00354                 $values[] = $parameter;
00355             }
00356             $code .= ( "else\n" .
00357                        "    %output% = false;\n" );
00358         }
00359         else if ( $operatorName == 'and' )
00360         {
00361             $staticResult = false;
00362             $staticValue = null;
00363             $dynamicParameters = array();
00364             $addDynamic = false;
00365             $lastValue = null;
00366             foreach ( $parameters as $parameter )
00367             {
00368                 if ( $addDynamic )
00369                 {
00370                     $dynamicParameters[] = $parameter;
00371                     continue;
00372                 }
00373                 if ( eZTemplateNodeTool::isStaticElement( $parameter ) )
00374                 {
00375                     $parameterValue = eZTemplateNodeTool::elementStaticValue( $parameter );
00376                     if ( $staticValue === null )
00377                     {
00378                         $staticValue = $parameterValue;
00379                     }
00380                     else
00381                     {
00382                         $staticValue = ( $staticValue and $parameterValue );
00383                     }
00384                     $lastValue = $parameterValue;
00385                     if ( !$parameterValue )
00386                     {
00387                         $lastValue = false;
00388                         $staticResult = true;
00389                         break;
00390                     }
00391                     continue;
00392                 }
00393                 $addDynamic = true;
00394                 $dynamicParameters[] = $parameter;
00395                 $staticValue = null;
00396             }
00397             if ( count( $dynamicParameters ) == 0 )
00398             {
00399                 $newElements[] = eZTemplateNodeTool::createStaticElement( $lastValue );
00400                 return $newElements;
00401             }
00402 
00403             $code = '';
00404             $counter = 0;
00405             foreach ( $dynamicParameters as $parameter )
00406             {
00407                 if ( $counter++ )
00408                 {
00409                     $code .= "else ";
00410                 }
00411                 $code .= ( "if ( !%$counter% )\n" .
00412                            "    %output% = false;\n" );
00413                 $values[] = $parameter;
00414             }
00415             $code .= ( "else\n" .
00416                        "    %output% = %$counter%;\n" );
00417         }
00418         else
00419         {
00420             $code = '%output% = (';
00421             $counter = 0;
00422             $allStatic = true;
00423             foreach ( $parameters as $parameter )
00424             {
00425                 if ( !eZTemplateNodeTool::isStaticElement( $parameter ) )
00426                     $allStatic = false;
00427             }
00428             if ( $allStatic )
00429             {
00430                 switch ( $operatorName )
00431                 {
00432                     case 'lt':
00433                     {
00434                         $evalStatus = ( eZTemplateNodeTool::elementStaticValue( $parameters[0] ) <
00435                                         eZTemplateNodeTool::elementStaticValue( $parameters[1] ) );
00436                     } break;
00437 
00438                     case 'le':
00439                     {
00440                         $evalStatus = ( eZTemplateNodeTool::elementStaticValue( $parameters[0] ) <=
00441                                         eZTemplateNodeTool::elementStaticValue( $parameters[1] ) );
00442                     } break;
00443 
00444                     case 'gt':
00445                     {
00446                         $evalStatus = ( eZTemplateNodeTool::elementStaticValue( $parameters[0] ) >
00447                                         eZTemplateNodeTool::elementStaticValue( $parameters[1] ) );
00448                     } break;
00449 
00450                     case 'ge':
00451                     {
00452                         $evalStatus = ( eZTemplateNodeTool::elementStaticValue( $parameters[0] ) >=
00453                                         eZTemplateNodeTool::elementStaticValue( $parameters[1] ) );
00454                     } break;
00455 
00456                     case 'eq':
00457                     {
00458                         $staticParameters = array();
00459                         foreach ( $parameters as $parameter )
00460                         {
00461                             $staticParameters[] = eZPHPCreator::variableText( eZTemplateNodeTool::elementStaticValue( $parameter ),
00462                                                                               0, 0, false );
00463                         }
00464                         eval( '$evalStatus = ( ' . implode( ' == ', $staticParameters ) . ' );' );
00465                     } break;
00466 
00467                     case 'ne':
00468                     {
00469                         $staticParameters = array();
00470                         foreach ( $parameters as $parameter )
00471                         {
00472                             $staticParameters[] = eZPHPCreator::variableText( eZTemplateNodeTool::elementStaticValue( $parameter ),
00473                                                                               0, 0, false );
00474                         }
00475                         eval( '$evalStatus = ( ' . implode( ' != ', $staticParameters ) . ' );' );
00476                     } break;
00477                     break;
00478                 }
00479                 $newElements[] = eZTemplateNodeTool::createBooleanElement( $evalStatus );
00480                 return $newElements;
00481             }
00482 
00483             foreach ( $parameters as $parameter )
00484             {
00485                 if ( !eZTemplateNodeTool::isStaticElement( $parameter ) )
00486                     $allStatic = false;
00487                 if ( $counter++ )
00488                 {
00489                     $code .= " $operator";
00490                 }
00491                 $code .= " ( %$counter% )";
00492                 $values[] = $parameter;
00493             }
00494             $code .= " );\n";
00495         }
00496         $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values );
00497         return $newElements;
00498     }
00499 
00500     function negateTransformation( $operatorName, &$node, $tpl, &$resourceData,
00501                                    $element, $lastElement, $elementList, $elementTree, &$parameters )
00502     {
00503         $values = array();
00504         $function = $operatorName;
00505 
00506         if ( ( count( $parameters ) != 1) )
00507         {
00508             return false;
00509         }
00510         $newElements = array();
00511 
00512         $values[] = $parameters[0];
00513         $code = "%output% = !( %1% );\n";
00514 
00515         $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values );
00516         return $newElements;
00517     }
00518 
00519     function trueFalseTransformation( $operatorName, &$node, $tpl, &$resourceData,
00520                                       $element, $lastElement, $elementList, $elementTree, &$parameters )
00521     {
00522         $values = array();
00523         if ( ( count( $parameters ) != 0 ) )
00524         {
00525             return false;
00526         }
00527         $newElements = array();
00528 
00529         $value = false;
00530         if ( $operatorName == $this->TrueName )
00531             $value = true;
00532         $newElements[] = eZTemplateNodeTool::createBooleanElement( $value );
00533         return $newElements;
00534     }
00535 
00536     function chooseTransformation( $operatorName, &$node, $tpl, &$resourceData,
00537                                    $element, $lastElement, $elementList, $elementTree, &$parameters )
00538     {
00539         $values = array();
00540         $function = $operatorName;
00541 
00542         if ( ( count( $parameters ) < 2) )
00543         {
00544             return false;
00545         }
00546 
00547         $tmpValues = false;
00548         $newElements = array();
00549         if ( eZTemplateNodeTool::isStaticElement( $parameters[0] ) )
00550         {
00551             $selected = eZTemplateNodeTool::elementStaticValue( $parameters[0] );
00552 
00553             if ( $selected < 0 or $selected > ( count( $parameters ) - 1 ) )
00554             {
00555                 return false;
00556             }
00557 
00558             return $parameters[$selected + 1];
00559         }
00560         else
00561         {
00562             $values[] = $parameters[0];
00563             $array = $parameters;
00564             unset( $array[0] );
00565 
00566             $count = count( $parameters ) - 1;
00567             $operatorNameText = eZPHPCreator::variableText( $operatorName );
00568 
00569             if ( count( $parameters ) == ( 2 + 1 ) )
00570             {
00571                 $code = "%output% = %1% ? %3% : %2%;\n";
00572                 $values[] = $parameters[1];
00573                 $values[] = $parameters[2];
00574             }
00575             else
00576             {
00577                 $code = ( "if ( %1% < 0 and\n" .
00578                           "     %1% >= $count )\n" .
00579                           "{\n" .
00580                           "    \$tpl->error( $operatorNameText, \"Index \" . %1% . \" out of range\" );\n" .
00581                           "     %output% = false;\n" .
00582                           "}\n" );
00583                 $code .= "else switch ( %1% )\n{\n";
00584                 $valueNumber = 2;
00585                 for ( $i = 0; $i < $count; ++$i )
00586                 {
00587                     $parameterNumber = $i + 1;
00588                     $code .= "    case $i:";
00589                     if ( eZTemplateNodeTool::isStaticElement( $parameters[$parameterNumber] ) )
00590                     {
00591                         $value = eZTemplateNodeTool::elementStaticValue( $parameters[$parameterNumber] );
00592                         $valueText = eZPHPCreator::variableText( $value, 0, 0, false );
00593                         $code .= " %output% = $valueText; break;\n";
00594                     }
00595                     else
00596                     {
00597                         $code .= "\n    {\n";
00598                         $code .= "%code$valueNumber%\n";
00599                         $code .= "%output% = %$valueNumber%;\n";
00600                         $code .= "    } break;\n";
00601                         $values[] = $parameters[$parameterNumber];
00602                         ++$valueNumber;
00603                     }
00604                 }
00605                 $code .= "}\n";
00606             }
00607         }
00608         $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values, eZTemplateNodeTool::extractVariableNodePlacement( $node ), false );
00609         return $newElements;
00610     }
00611 
00612     /*!
00613      * Returns the 'count' value as described in the introduction or 'false' in
00614      * case of an unsupported type
00615      */
00616     function getValueCount( $val )
00617     {
00618         $val_cnt = false;
00619 
00620         if ( is_array( $val ) )
00621         {
00622             $val_cnt = count( $val );
00623         }
00624         else if ( is_null( $val ) )
00625         {
00626             $val_cnt = 0;
00627         }
00628         else if ( is_bool( $val ) )
00629         {
00630             $val_cnt = (int)$val;
00631         }
00632         else if ( is_object( $val ) and
00633                   method_exists( $val, "attributes" ) )
00634         {
00635             $val_cnt = count( $val->attributes() );
00636         }
00637         else if ( is_numeric( $val ) )
00638         {
00639             $val_cnt = $val;
00640         }
00641         else if ( is_string( $val ) )
00642         {
00643             $val_cnt = strlen( $val );
00644         }
00645         return $val_cnt;
00646     }
00647 
00648     /*!
00649      Examines the input value and outputs a boolean value. See class documentation for more information.
00650     */
00651     function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$value, $namedParameters,
00652                      $placement )
00653     {
00654         if ( $operatorName == $this->LtName or $operatorName == $this->GtName or
00655              $operatorName == $this->LeName or $operatorName == $this->GeName )
00656         {
00657             $val = $namedParameters["threshold"];
00658 
00659             if ( ( $val_cnt = $this->getValueCount( $val ) ) === false )
00660             {
00661                 $tpl->warning( $operatorName, "Unsupported input type: " . gettype( $val ) . "( $val ), must be either array, attribute object or numerical", $placement );
00662                 return;
00663             }
00664         }
00665         switch ( $operatorName )
00666         {
00667             case $this->TrueName:
00668             case $this->FalseName:
00669             {
00670                 $value = ( $operatorName == $this->TrueName );
00671             } break;
00672             case $this->NeName:
00673             {
00674                 if ( count( $operatorParameters ) >= 1 )
00675                 {
00676                     if ( count( $operatorParameters ) == 1 )
00677                     {
00678                         $lastOperand = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
00679                         $value = ( $lastOperand != $value );
00680                     }
00681                     else
00682                     {
00683                         $similar = false;
00684                         $value = false;
00685                         $lastOperand = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
00686                         for ( $i = 1; $i < count( $operatorParameters ); ++$i )
00687                         {
00688                             $operand = $tpl->elementValue( $operatorParameters[$i], $rootNamespace, $currentNamespace, $placement );
00689                             if ( $operand != $lastOperand )
00690                             {
00691                                 $value = true;
00692                                 break;
00693                             }
00694                             unset( $lastOperand );
00695                             $lastOperand =& $operand;
00696                         }
00697                     }
00698                 }
00699                 else
00700                 {
00701                     $value = false;
00702                     $tpl->warning( $operatorName, "Requires one parameter for input checking or two or more for parameter checking", $placement );
00703                 }
00704             } break;
00705             case $this->EqName:
00706             {
00707                 if ( count( $operatorParameters ) >= 1 )
00708                 {
00709                     if ( count( $operatorParameters ) == 1 )
00710                     {
00711                         $lastOperand = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
00712                         $value = ( $lastOperand == $value );
00713                     }
00714                     else
00715                     {
00716                         $similar = false;
00717                         $value = true;
00718                         $lastOperand = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
00719                         for ( $i = 1; $i < count( $operatorParameters ); ++$i )
00720                         {
00721                             $operand = $tpl->elementValue( $operatorParameters[$i], $rootNamespace, $currentNamespace, $placement );
00722                             if ( $operand != $lastOperand )
00723                             {
00724                                 $value = false;
00725                                 break;
00726                             }
00727                             unset( $lastOperand );
00728                             $lastOperand =& $operand;
00729                         }
00730                     }
00731                 }
00732                 else
00733                 {
00734                     $value = false;
00735                     $tpl->warning( $operatorName, "Requires one parameter for input checking or two or more for parameter checking", $placement );
00736                 }
00737             } break;
00738             case $this->OrName:
00739             {
00740                 for ( $i = 0; $i < count( $operatorParameters ); ++$i )
00741                 {
00742                     $operand = $tpl->elementValue( $operatorParameters[$i], $rootNamespace, $currentNamespace, $placement );
00743                     $operand_logic = false;
00744                     if ( is_array( $operand ) )
00745                         $operand_logic = count( $operand ) > 0;
00746                     else if ( is_numeric( $operand ) )
00747                         $operand_logic = $operand != 0;
00748                     else if ( is_null( $operand ) )
00749                         $operand_logic = false;
00750                     else if ( is_object( $operand ) )
00751                         $operand_logic = ( method_exists( $operand, "attributes" ) and
00752                                            method_exists( $operand, "attribute" ) );
00753                     else if ( is_bool( $operand ) )
00754                         $operand_logic = $operand;
00755                     else if ( is_string( $operand ) )
00756                         $operand_logic =  strlen(trim($operand)) > 0;
00757                     if ( $operand_logic )
00758                     {
00759                         $value = $operand;
00760                         return;
00761                     }
00762                 }
00763                 $value = false;
00764             } break;
00765             case $this->AndName:
00766             {
00767                 $operand = null;
00768                 for ( $i = 0; $i < count( $operatorParameters ); ++$i )
00769                 {
00770                     $operand = $tpl->elementValue( $operatorParameters[$i], $rootNamespace, $currentNamespace, $placement );
00771                     $operand_logic = false;
00772                     if ( is_array( $operand ) )
00773                         $operand_logic = count( $operand ) > 0;
00774                     else if ( is_numeric( $operand ) )
00775                         $operand_logic = $operand != 0;
00776                     else if ( is_null( $operand ) )
00777                         $operand_logic = false;
00778                     else if ( is_object( $operand ) )
00779                         $operand_logic = ( method_exists( $operand, "attributes" ) and
00780                                            method_exists( $operand, "attribute" ) );
00781                     else if ( is_bool( $operand ) )
00782                         $operand_logic = $operand;
00783                     else if ( is_string( $operand ) )
00784                         $operand_logic =  strlen(trim($operand)) > 0;
00785                     if ( !$operand_logic )
00786                     {
00787                         $value = false;
00788                         return;
00789                     }
00790                 }
00791                 $value = $operand;
00792             } break;
00793             case $this->ChooseName:
00794             {
00795                 if ( is_array( $value ) or
00796                      ( is_object( $value ) and
00797                        method_exists( $value, "attributes" ) ) )
00798                 {
00799                     $tpl->error( $operatorName, "Only supports numeric and boolean values", $placement );
00800                     return;
00801                 }
00802                 else if ( is_numeric( $value ) )
00803                     $index = $value;
00804                 else if ( is_null( $value ) )
00805                     $index = 0;
00806                 else
00807                     $index = $value ? 1 : 0;
00808                 if ( $index < 0 or
00809                      $index > count( $operatorParameters ) - 1 )
00810                 {
00811                     $tpl->error( $operatorName, "Index $index out of range 0 => " . ( count( $operatorParameters ) - 1 ),
00812                                  $placement );
00813                     $value = false;
00814                     return;
00815                 }
00816                 $value = $tpl->elementValue( $operatorParameters[$index], $rootNamespace, $currentNamespace, $placement );
00817             } break;
00818             case $this->LtName:
00819             case $this->GtName:
00820             case $this->LeName:
00821             case $this->GeName:
00822             {
00823                 if ( $value !== null )
00824                 {
00825                     $operandA = $value;
00826                     $operandB = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
00827                 }
00828                 else
00829                 {
00830                     $operandA = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
00831                     $operandB = $tpl->elementValue( $operatorParameters[1], $rootNamespace, $currentNamespace, $placement );
00832                 }
00833 
00834                 if ( ( $cnt = $this->getValueCount( $operandA ) ) === false )
00835                 {
00836                     $tpl->warning( $operatorName, "Unsupported input type: " . gettype( $operandA ) . "( $operandA ), must be either array, attribute object or numerical", $placement );
00837                     return;
00838                 }
00839                 if ( ( $val_cnt = $this->getValueCount( $operandB ) ) === false )
00840                 {
00841                     $tpl->warning( $operatorName, "Unsupported input type: " . gettype( $operandB ) . "( $operandB ), must be either array, attribute object or numerical", $placement );
00842                     return;
00843                 }
00844                 if ( $operatorName == $this->LtName )
00845                     $value = ( $cnt < $val_cnt );
00846                 else if ( $operatorName == $this->GtName )
00847                     $value = ( $cnt > $val_cnt );
00848                 else if ( $operatorName == $this->LeName )
00849                     $value = ( $cnt <= $val_cnt );
00850                 else if ( $operatorName == $this->GeName )
00851                     $value = ( $cnt >= $val_cnt );
00852             } break;
00853             case $this->NullName:
00854             {
00855                 $value = is_null( $value );
00856             } break;
00857             case $this->NotName:
00858             {
00859                 if ( $value === null and isset( $operatorParameters[0] ) )
00860                 {
00861                     $operand = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace, $placement );
00862                 }
00863                 else
00864                 {
00865                     $operand = $value;
00866                 }
00867                 if ( is_array( $operand ) )
00868                     $operand = ( count( $operand ) == 0 );
00869                 else if ( is_null( $operand ) )
00870                     $operand = true;
00871                 else if ( is_object( $operand ) and
00872                           method_exists( $operand, "attributes" ) )
00873                     $operand = ( count( $operand->attributes() ) == 0 );
00874                 else if ( is_numeric( $operand ) )
00875                     $operand = ( $operand == 0 );
00876                 else if ( is_string( $operand ) )
00877                     $operand = ( strlen( $operand ) == 0 );
00878                 else
00879                     $operand = !$operand;
00880                 $value = $operand;
00881             } break;
00882         }
00883     }
00884 
00885     /// \privatesection
00886     /// The array of operators
00887     public $Operators;
00888     /// The "less than" name
00889     public $LtName;
00890     /// The "greater than" name
00891     public $GtName;
00892     /// The "less than or equal" name
00893     public $LeName;
00894     /// The "greater than or equal" name
00895     public $GeName;
00896     /// The "equal" name
00897     public $EqName;
00898     /// The "not equal" name
00899     public $NeName;
00900     /// The "null" name
00901     public $NullName;
00902     /// The "not" name
00903     public $NotName;
00904     /// The "or" name
00905     public $OrName;
00906     /// The "and" name
00907     public $AndName;
00908     /// The "true" name
00909     public $TrueName;
00910     /// The "false" name
00911     public $FalseName;
00912     /// The "choose" name
00913     public $ChooseName;
00914 };
00915 
00916 ?>