|
eZ Publish
[4.0]
|
00001 <?php 00002 // 00003 // Definition of eZTemplateControlOperator 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 eZTemplateControlOperator eztemplatetypeoperator.php 00033 \ingroup eZTemplateOperators 00034 \brief Operators for checking variable type 00035 00036 Usage: 00037 // Evalue condition and if true return body 00038 cond(is_set($var),$var, 00039 true(),2) 00040 // Return first element that is set 00041 first_set($var1,$var2,$var3,0) 00042 00043 */ 00044 00045 class eZTemplateControlOperator 00046 { 00047 /*! 00048 Initializes the operator class with the various operator names. 00049 */ 00050 function eZTemplateControlOperator( /*! The name array */ 00051 $condName = 'cond', 00052 $firstSetName = 'first_set' ) 00053 { 00054 $this->Operators = array( $condName, $firstSetName ); 00055 $this->CondName = $condName; 00056 $this->FirstSetName = $firstSetName; 00057 } 00058 00059 /*! 00060 Returns the operators in this class. 00061 */ 00062 function operatorList() 00063 { 00064 return $this->Operators; 00065 } 00066 00067 function operatorTemplateHints() 00068 { 00069 return array( $this->CondName => array( 'input' => false, 00070 'output' => true, 00071 'parameters' => true, 00072 'element-transformation' => true, 00073 'transform-parameters' => true, 00074 'input-as-parameter' => false, 00075 'element-transformation-func' => 'condTransform' ), 00076 $this->FirstSetName => array( 'input' => false, 00077 'output' => true, 00078 'parameters' => true, 00079 'element-transformation' => true, 00080 'transform-parameters' => true, 00081 'input-as-parameter' => false, 00082 'element-transformation-func' => 'condTransform' ) ); 00083 } 00084 00085 /*! 00086 \reimp 00087 */ 00088 function condTransform( $operatorName, &$node, $tpl, &$resourceData, 00089 $element, $lastElement, $elementList, $elementTree, &$parameters ) 00090 { 00091 switch( $operatorName ) 00092 { 00093 case $this->CondName: 00094 { 00095 $paramCount = count( $parameters ); 00096 $clauseCount = floor( $paramCount / 2 ); 00097 $hasDefaultClause = ( $paramCount % 2 ) != 0; 00098 00099 if ( $paramCount == 1 ) 00100 { 00101 return $parameters[0]; 00102 } 00103 00104 $values = array(); 00105 $code = ''; 00106 $spacing = 0; 00107 $spacingCode = ''; 00108 for ( $i = 0; $i < $clauseCount; ++$i ) 00109 { 00110 $prevSpacingCode = $spacingCode; 00111 $spacingCode = str_repeat( " ", $spacing*4 ); 00112 if ( $i > 0 ) 00113 { 00114 $code .= $prevSpacingCode . "else\n" . $prevSpacingCode . "{\n"; 00115 } 00116 00117 $values[] = $parameters[$i*2]; 00118 if ( $i > 0 ) 00119 { 00120 $code .= $spacingCode . "%code" . count( $values ) . "%\n"; 00121 } 00122 $code .= $spacingCode. 'if ( %' . count( $values ) . "% )\n" . $spacingCode . "{\n"; 00123 00124 if ( !eZTemplateNodeTool::isStaticElement( $parameters[$i*2 + 1] ) ) 00125 { 00126 $values[] = $parameters[$i*2 + 1]; 00127 $code .= ( $spacingCode . " %code" . count( $values ) . "%\n" . 00128 $spacingCode . " %output% = %" . count( $values ) . "%;\n" ); 00129 } 00130 else 00131 { 00132 $code .= $spacingCode . ' %output% = ' . eZPHPCreator::variableText( eZTemplateNodeTool::elementStaticValue( $parameters[$i*2 + 1] ), 0, 0, false ) . ';' . "\n"; 00133 } 00134 $code .= $spacingCode . "}\n"; 00135 ++$spacing; 00136 } 00137 $bracketCount = $clauseCount - 1; 00138 if ( $hasDefaultClause ) 00139 { 00140 ++$bracketCount; 00141 $values[] = $parameters[$paramCount - 1]; 00142 if ( $clauseCount > 0 ) 00143 { 00144 $code .= $spacingCode . "else\n" . $spacingCode . "{\n" . $spacingCode . " %code" . count( $values ) . "%\n "; 00145 } 00146 00147 $code .= $spacingCode . '%output% = %' . count( $values ) . "%;\n"; 00148 } 00149 for ( $clauseIndex = 0; $clauseIndex < $bracketCount; ++$clauseIndex ) 00150 { 00151 $spacingCode = str_repeat( " ", ( $bracketCount - $clauseIndex - 1 ) *4 ); 00152 $code .= $spacingCode . "}\n"; 00153 } 00154 00155 return array( eZTemplateNodeTool::createCodePieceElement( $code, $values ) ); 00156 } break; 00157 00158 case $this->FirstSetName: 00159 { 00160 $values = array(); 00161 $code = ''; 00162 $spacing = 0; 00163 $spacingCode = ''; 00164 $nestCount = 0; 00165 for( $i = 0; $i < count( $parameters ); ++$i ) 00166 { 00167 if ( $i != 0 ) 00168 { 00169 $code .= "$spacingCode}\n" . $spacingCode . "else\n$spacingCode{\n"; 00170 } 00171 $spacingCode = str_repeat( ' ', $spacing*4 ); 00172 ++$spacing; 00173 00174 if ( eZTemplateNodeTool::isStaticElement( $parameters[$i] ) ) 00175 { 00176 $code .= "$spacingCode%output% = " . eZPHPCreator::variableText( eZTemplateNodeTool::elementStaticValue( $parameters[$i] ), 0, 0, false ) . ";\n"; 00177 break; 00178 } 00179 ++$nestCount; 00180 00181 $values[] = $parameters[$i]; 00182 $code .= ( $spacingCode . "%code" . count( $values ) . "%\n" . 00183 $spacingCode . 'if ( isset( %' . count( $values ) . "% ) )\n" . 00184 $spacingCode . "{\n" . 00185 $spacingCode . " %output% = %" . count( $values ) . '%;' . "\n" ); 00186 } 00187 for ( $i = 0; $i < $nestCount; ++$i ) 00188 { 00189 $spacing = $nestCount - $i - 1; 00190 $spacingCode = str_repeat( ' ', $spacing*4 ); 00191 $code .= $spacingCode . "}\n"; 00192 } 00193 00194 return array( eZTemplateNodeTool::createCodePieceElement( $code, $values ) ); 00195 } break; 00196 } 00197 } 00198 00199 /*! 00200 \return true to tell the template engine that the parameter list exists per operator type. 00201 */ 00202 function namedParameterPerOperator() 00203 { 00204 return true; 00205 } 00206 00207 /*! 00208 See eZTemplateOperator::namedParameterList 00209 */ 00210 function namedParameterList() 00211 { 00212 return array(); 00213 } 00214 00215 /*! 00216 Examines the input value and outputs a boolean value. See class documentation for more information. 00217 */ 00218 function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$value, $namedParameters, 00219 $placement ) 00220 { 00221 switch ( $operatorName ) 00222 { 00223 case $this->CondName: 00224 { 00225 $parameterCount = count( $operatorParameters ); 00226 $clauseCount = floor( $parameterCount / 2 ); 00227 $clauseMod = $parameterCount % 2; 00228 $conditionSuccess = false; 00229 for ( $i = 0; $i < $clauseCount; ++$i ) 00230 { 00231 $condition = $tpl->elementValue( $operatorParameters[$i*2], $rootNamespace, $currentNamespace, $placement ); 00232 if ( $condition ) 00233 { 00234 $body = $tpl->elementValue( $operatorParameters[$i*2 + 1], $rootNamespace, $currentNamespace, $placement ); 00235 $conditionSuccess = true; 00236 $value = $body; 00237 break; 00238 } 00239 } 00240 if ( !$conditionSuccess and 00241 $clauseMod > 0 ) 00242 { 00243 $condition = $tpl->elementValue( $operatorParameters[count($operatorParameters) - 1], $rootNamespace, $currentNamespace, $placement ); 00244 if ( $condition ) 00245 { 00246 $conditionSuccess = true; 00247 $value = $condition; 00248 } 00249 } 00250 } break; 00251 case $this->FirstSetName: 00252 { 00253 if ( count( $operatorParameters ) > 0 ) 00254 { 00255 for ( $i = 0; $i < count( $operatorParameters ); ++$i ) 00256 { 00257 $operand = $tpl->elementValue( $operatorParameters[$i], $rootNamespace, $currentNamespace, $placement, true ); 00258 if ( $operand !== null ) 00259 { 00260 $value = $operand; 00261 return; 00262 } 00263 } 00264 } 00265 $value = null; 00266 } break; 00267 } 00268 } 00269 00270 /// The array of operators 00271 public $Operators; 00272 }; 00273 00274 ?>