00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 class eZTemplateLoopSequence
00034 {
00035 function eZTemplateLoopSequence( &$array )
00036 {
00037 $this->ArrayRef =& $array;
00038 $this->CurVal = current( $this->ArrayRef );
00039 }
00040
00041 function val()
00042 {
00043 return $this->CurVal;
00044 }
00045
00046 function next()
00047 {
00048 if( ( $this->CurVal = next( $this->ArrayRef ) ) === false )
00049 {
00050 reset( $this->ArrayRef );
00051 $this->CurVal = current( $this->ArrayRef );
00052 }
00053 }
00054
00055 var $ArrayRef;
00056 var $CurVal;
00057 }
00058
00059
00060
00061
00062
00063
00064 class eZTemplateLoop
00065 {
00066 function eZTemplateLoop( $functionName, &$functionParameters, &$functionChildren, &$functionPlacement,
00067 &$tpl, &$textElements, &$rootNamespace, &$currentNamespace )
00068 {
00069 $this->SkipDelimiter = true;
00070 $this->SkipSequenceIncrement = false;
00071 $this->Delimiter = null;
00072 $this->Initialized = true;
00073 $this->SequenceVarName = null;
00074 $this->Sequence = null;
00075 $this->LoopVariablesNames = array();
00076
00077
00078 $this->FunctionName = $functionName;
00079 $this->FunctionParameters =& $functionParameters;
00080 $this->FunctionChildren =& $functionChildren;
00081
00082 $this->Tpl =& $tpl;
00083 $this->TextElements =& $textElements;
00084 $this->RootNamespace =& $rootNamespace;
00085 $this->CurrentNamespace =& $currentNamespace;
00086 $this->FunctionPlacement =& $functionPlacement;
00087
00088 $this->Initialized = $this->processFunctionParameters();
00089 }
00090
00091
00092
00093
00094 function processFunctionParameters()
00095 {
00096 $params =& $this->FunctionParameters;
00097
00098 if ( !isset( $params['sequence_array'] ) || !count( $params['sequence_array'] ) )
00099 return true;
00100
00101 $this->parseParamVarName( 'sequence_var', $seqVarName );
00102
00103 if ( !$seqVarName )
00104 {
00105 $this->Tpl->error( $this->FunctionName, "Empty sequence variable name." );
00106 return false;
00107 }
00108
00109 $this->initLoopVariable( $seqVarName );
00110
00111 $seqArray = $this->Tpl->elementValue( $params['sequence_array'],
00112 $this->RootNamespace, $this->CurrentNamespace, $this->FunctionPlacement );
00113
00114 $this->Sequence = new eZTemplateLoopSequence( $seqArray );
00115 $this->SequenceVarName = $seqVarName;
00116
00117 return true;
00118 }
00119
00120
00121
00122
00123 function initialized()
00124 {
00125 return $this->Initialized;
00126 }
00127
00128
00129
00130
00131 function setSequenceVar()
00132 {
00133 if ( !$this->hasSequence() )
00134 return;
00135
00136 $this->Tpl->setVariable( $this->SequenceVarName, $this->Sequence->val(), $this->RootNamespace );
00137 }
00138
00139
00140
00141
00142
00143 function resetIteration()
00144 {
00145 $this->SkipDelimiter = false;
00146 $this->SkipSequenceIncrement = false;
00147 }
00148
00149
00150
00151
00152 function incrementSequence()
00153 {
00154 if ( $this->hasSequence() && !$this->SkipSequenceIncrement )
00155 $this->Sequence->next();
00156 }
00157
00158
00159
00160
00161 function hasSequence()
00162 {
00163 return !is_null( $this->Sequence );
00164 }
00165
00166
00167
00168
00169
00170 function cleanup()
00171 {
00172
00173 foreach ( $this->LoopVariablesNames as $varName )
00174 $this->Tpl->unsetVariable( $varName, $this->RootNamespace );
00175 }
00176
00177
00178
00179
00180
00181
00182
00183
00184 function processChildren()
00185 {
00186 if ( !is_array( $this->FunctionChildren ) )
00187 {
00188 return false;
00189 }
00190 foreach ( array_keys( $this->FunctionChildren ) as $childKey )
00191 {
00192 $child =& $this->FunctionChildren[$childKey];
00193
00194 if ( $child[0] == EZ_TEMPLATE_NODE_FUNCTION )
00195 {
00196 $childFunctionName =& $child[2];
00197
00198 if ( $childFunctionName == 'break' )
00199 return true;
00200 elseif ( $childFunctionName == 'continue' )
00201 {
00202 $this->SkipSequenceIncrement = true;
00203 break;
00204 }
00205 elseif ( $childFunctionName == 'skip' )
00206 {
00207 $this->SkipSequenceIncrement = true;
00208 $this->SkipDelimiter = true;
00209 break;
00210 }
00211 elseif ( $childFunctionName == 'delimiter' )
00212 {
00213 if ( is_null( $this->Delimiter ) )
00214 $this->Delimiter =& $child;
00215 continue;
00216 }
00217 }
00218
00219 $rslt = $this->Tpl->processNode( $child, $this->TextElements, $this->RootNamespace, $this->CurrentNamespace );
00220
00221
00222 if ( is_array( $rslt ) )
00223 {
00224 if ( array_key_exists( 'breakFunctionFound', $rslt ) )
00225 return true;
00226 elseif ( array_key_exists( 'continueFunctionFound', $rslt ) )
00227 {
00228 $this->SkipSequenceIncrement = true;
00229 break;
00230 }
00231 elseif ( array_key_exists( 'skipFunctionFound', $rslt ) )
00232 {
00233 $this->SkipSequenceIncrement = true;
00234 $this->SkipDelimiter = true;
00235 break;
00236 }
00237 }
00238
00239 }
00240
00241 return false;
00242 }
00243
00244
00245
00246
00247
00248
00249
00250 function processDelimiter( $index = false )
00251 {
00252 if ( is_null( $this->Delimiter ) || $this->SkipDelimiter )
00253 return false;
00254
00255 $delimiterChildren =& $this->Delimiter[1];
00256 $delimiterParameters =& $this->Delimiter[3];
00257 $delimiterMatch = true;
00258
00259 if ( isset( $delimiterParameters["modulo"] ) and $index !== false )
00260 {
00261 $delimiterModulo = $delimiterParameters["modulo"];
00262 $modulo = $this->Tpl->elementValue( $delimiterModulo, $this->RootNamespace, $this->FunctionName, $this->FunctionPlacement );
00263 $modulo = trim( $modulo );
00264 if ( is_numeric( $modulo ) )
00265 $delimiterMatch = ( $index % $modulo ) == 0;
00266 }
00267 if ( $delimiterMatch )
00268 {
00269 foreach ( array_keys( $delimiterChildren ) as $key )
00270 $this->Tpl->processNode( $delimiterChildren[$key], $this->TextElements, $this->RootNamespace, $this->CurrentNamespace );
00271 }
00272
00273 return false;
00274 }
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284 function parseParamVarName( $paramName, &$dst )
00285 {
00286 $dst = null;
00287
00288 if ( !isset( $this->FunctionParameters[$paramName] ) ||
00289 !count( $this->FunctionParameters[$paramName] ) )
00290 return false;
00291
00292 list( $varNsName, $varNsType, $varName ) = $this->FunctionParameters[$paramName][0][1];
00293
00294 if ( $varNsType != EZ_TEMPLATE_NAMESPACE_SCOPE_LOCAL || $varNsName )
00295 {
00296 $this->Tpl->error( $this->FunctionName,
00297 'Loop variables can be defined in root namespace only (e.g. $foo, but not $#foo or $:foo.)' );
00298 return false;
00299 }
00300
00301 $dst = $varName;
00302 return true;
00303 }
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313 function parseScalarParamValue( $paramName, &$dst, &$isProxyObject )
00314 {
00315 $dst = null;
00316
00317 if ( !isset( $this->FunctionParameters[$paramName] ) || !count( $this->FunctionParameters[$paramName] ) )
00318 return false;
00319
00320
00321 $dst = $this->Tpl->elementValue( $this->FunctionParameters[$paramName], $this->RootNamespace,
00322 $this->CurrentNamespace, $this->FunctionPlacement, false, true );
00323
00324
00325 if ( isset( $this->FunctionParameters[$paramName]['proxy-object-found'] ) )
00326 {
00327 $isProxyObject = true;
00328 unset( $this->FunctionParameters[$paramName]['proxy-object-found'] );
00329 }
00330 else
00331 $isProxyObject = false;
00332
00333 return true;
00334 }
00335
00336
00337
00338
00339
00340
00341
00342
00343 function parseParamValue( $paramName, &$dst )
00344 {
00345 $dst = null;
00346
00347 if ( !isset( $this->FunctionParameters[$paramName] ) || !count( $this->FunctionParameters[$paramName] ) )
00348 return false;
00349
00350 $dst = $this->Tpl->elementValue( $this->FunctionParameters[$paramName], $this->RootNamespace,
00351 $this->CurrentNamespace, $this->FunctionPlacement );
00352 return true;
00353 }
00354
00355
00356
00357
00358
00359
00360
00361 function initLoopVariable( $varName )
00362 {
00363 if ( $this->Tpl->hasVariable( $varName, $this->RootNamespace ) )
00364 $this->Tpl->warning( $this->FunctionName, "Variable '$varName' already exists." );
00365 else
00366 $this->LoopVariablesNames[] = $varName;
00367 }
00368
00369
00370
00371
00372
00373 var $FunctionName;
00374 var $FunctionParameters;
00375 var $FunctionChildren;
00376 var $FunctionPlacement;
00377
00378 var $SkipDelimiter;
00379 var $SkipSequenceIncrement;
00380 var $delimiter;
00381
00382 var $Tpl;
00383 var $TextElements;
00384 var $RootNamespace;
00385 var $CurrentNamespace;
00386
00387 var $Initialized;
00388 var $Sequence;
00389 var $SequenceVarName;
00390
00391
00392
00393
00394
00395 var $LoopVariablesNames;
00396 }
00397
00398 ?>