Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

eztemplatesectionfunction.php
Go to the documentation of this file.
1 <?php
57 class eZTemplateSectionFunction
58 {
64 public function __construct( $name = "section" )
65 {
66 $this->Name = $name;
67 }
68
73 function attributeList()
74 {
75 return array( "delimiter" => true,
76 "section-exclude" => false,
77 "section-include" => false,
78 $this->Name . "-else" => false );
79 }
80
85 function functionList()
86 {
87 return array( $this->Name );
88 }
89
90 function functionTemplateHints()
91 {
92 return array( $this->Name => array( 'parameters' => true,
93 'static' => false,
94 'transform-children' => false,
95 'tree-transformation' => true,
96 'transform-parameters' => true ) );
97 }
98
99 function templateNodeTransformation( $functionName, &$node,
100 $tpl, $parameters, $privateData )
101 {
102 $useLastValue = false;
103 if ( isset( $parameters['last-value'] ) and
104 !eZTemplateNodeTool::isConstantElement( $parameters['last-value'] ) )
105 return false;
106 if ( isset( $parameters['name'] ) and
107 !eZTemplateNodeTool::isConstantElement( $parameters['name'] ) )
108 return false;
109 if ( isset( $parameters['var'] ) and
110 !eZTemplateNodeTool::isConstantElement( $parameters['var'] ) )
111 return false;
112 if ( isset( $parameters['reverse'] ) and
113 !eZTemplateNodeTool::isConstantElement( $parameters['reverse'] ) )
114 return false;
115
116 $varName = false;
117 if ( isset( $parameters['var'] ) )
118 $varName = eZTemplateNodeTool::elementConstantValue( $parameters['var'] );
119 if ( isset( $parameters['last-value'] ) )
120 $useLastValue = (bool)eZTemplateNodeTool::elementConstantValue( $parameters['last-value'] );
121 if ( !$varName )
122 $useLastValue = false;
123 $reverseLoop = false;
124 if ( isset( $parameters['reverse'] ) )
125 $reverseLoop = eZTemplateNodeTool::elementConstantValue( $parameters['reverse'] );
126
127 $useLoop = isset( $parameters['loop'] );
128 $allowLoop = true;
129 $newNodes = array();
130
131 $maxText = "false";
132 $useMax = false;
133 $maxPopText = false;
134 if ( isset( $parameters['max'] ) )
135 {
136 if ( eZTemplateNodeTool::isConstantElement( $parameters['max'] ) )
137 {
138 $maxValue = eZTemplateNodeTool::elementConstantValue( $parameters['max'] );
139 if ( $maxValue > 0 )
140 {
141 $maxText = eZPHPCreator::variableText( $maxValue );
142 $useMax = true;
143 }
144 else
145 return array( eZTemplateNodeTool::createTextNode( '' ) );
146 }
147 else
148 {
149 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameters['max'], eZTemplateNodeTool::extractFunctionNodePlacement( $node ),
150 array(), 'max' );
151 $maxText = "\$max";
152 $maxPopText = ", \$max";
153 $useMax = true;
154 }
155 }
156
157 // Controls whether the 'if' statement with brackets is added
158 $useShow = false;
159 // Controls whether main nodes are handled, also controls delimiter and filters
160 $useMain = true;
161 // Controls wether else nodes are handled
162 $useElse = false;
163
164 $spacing = 0;
165 if ( isset( $parameters['show'] ) )
166 {
167 if ( eZTemplateNodeTool::isConstantElement( $parameters['show'] ) )
168 {
169 $showValue = eZTemplateNodeTool::elementConstantValue( $parameters['show'] );
170 if ( $showValue )
171 {
172 $useMain = true;
173 $useElse = false;
174 $useShow = false;
175 }
176 else
177 {
178 $useMain = false;
179 $useElse = true;
180 $useShow = false;
181 }
182 $newNodes[] = eZTemplateNodeTool::createTextNode( '' );
183 }
184 else
185 {
186 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameters['show'], eZTemplateNodeTool::extractFunctionNodePlacement( $node ),
187 array(), 'show' );
188 $spacing = 4;
189 $useElse = true;
190 $useShow = true;
191 }
192 }
193
194 $children = eZTemplateNodeTool::extractFunctionNodeChildren( $node );
195 if ( $useShow )
196 {
197 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( \$show )\n{\n" );
198 $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode( $spacing );
199 $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( 'show' );
200 }
201 if ( isset( $parameters['name'] ) and !$useLoop )
202 $newNodes[] = eZTemplateNodeTool::createNamespaceChangeNode( $parameters['name'] );
203 $mainNodes = eZTemplateNodeTool::extractNodes( $children,
204 array( 'match' => array( 'type' => 'before',
205 'matches' => array( array( 'match-keys' => array( 0 ),
206 'match-with' => eZTemplate::NODE_FUNCTION ),
207 array( 'match-keys' => array( 2 ),
208 'match-with' => 'section-else' ) ),
209 'filter' => array( array( array( 'match-keys' => array( 0 ),
210 'match-with' => eZTemplate::NODE_FUNCTION ),
211 array( 'match-keys' => array( 2 ),
212 'match-with' => array( 'delimiter', 'section-exclude', 'section-include' ) ) ) ) ) ) );
213 $delimiterNodes = eZTemplateNodeTool::extractNodes( $children,
214 array( 'match' => array( 'type' => 'equal',
215 'matches' => array( array( 'match-keys' => array( 0 ),
216 'match-with' => eZTemplate::NODE_FUNCTION ),
217 array( 'match-keys' => array( 2 ),
218 'match-with' => 'delimiter' ) ) ) ) );
219 $filterNodes = eZTemplateNodeTool::extractNodes( $children,
220 array( 'match' => array( 'type' => 'equal',
221 'matches' => array( array( 'match-keys' => array( 0 ),
222 'match-with' => eZTemplate::NODE_FUNCTION ),
223 array( 'match-keys' => array( 2 ),
224 'match-with' => array( 'section-exclude', 'section-include' ) ) ) ) ) );
225
226 $delimiterNode = false;
227 if ( count( $delimiterNodes ) > 0 )
228 $delimiterNode = $delimiterNodes[0];
229
230 if ( $useMain )
231 {
232 // Avoid transformation if the nodes will not be used, saves time
233 $mainNodes = eZTemplateCompiler::processNodeTransformationNodes( $tpl, $node, $mainNodes, $privateData );
234 }
235
236 if ( $useLoop and $useMain )
237 {
238 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameters['loop'], eZTemplateNodeTool::extractFunctionNodePlacement( $node ),
239 array(), 'loopItem' );
240 $hasSequence = false;
241 if ( isset( $parameters['sequence'] ) )
242 {
243 $sequenceParameter = $parameters['sequence'];
244 $hasSequence = true;
245 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $sequenceParameter, eZTemplateNodeTool::extractFunctionNodePlacement( $node ),
246 array(), 'sequence' );
247 }
248
249 if ( isset( $parameters['name'] ) )
250 $newNodes[] = eZTemplateNodeTool::createNamespaceChangeNode( $parameters['name'] );
251
252 $code = ( "if ( !isset( \$sectionStack ) )\n" .
253 " \$sectionStack = array();\n" );
254
255 $variableValuePushText = '';
256 $variableValuePopText = '';
257 if ( $varName )
258 {
259 $code .= ( "\$variableValue = new eZTemplateSectionIterator();\n" .
260 "\$lastVariableValue = false;\n" );
261 $variableValuePushText = "&\$variableValue, ";
262 $variableValuePopText = "\$variableValue, ";
263 }
264 $code .= ( "\$index = 0;\n" .
265 "\$currentIndex = 1;\n" );
266
267
268 $arrayCode = '';
269 $numericCode = '';
270 $stringCode = '';
271 $offsetText = '0';
272 if ( isset( $parameters['offset'] ) )
273 {
274 $offsetParameter = $parameters['offset'];
275 if ( eZTemplateNodeTool::isConstantElement( $offsetParameter ) )
276 {
277 $iterationValue = (int)eZTemplateNodeTool::elementConstantValue( $offsetParameter );
278 if ( $iterationValue > 0 )
279 {
280 $arrayCode = " \$loopKeys = array_splice( \$loopKeys, $iterationValue );\n";
281 }
282 $offsetText = $iterationValue;
283 }
284 else
285 {
286 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $offsetParameter, eZTemplateNodeTool::extractFunctionNodePlacement( $node ),
287 array(), 'offset' );
288 $arrayCode = ( " if ( \$offset > 0 )\n" .
289 " \$loopKeys = array_splice( \$loopKeys, \$offset );\n" );
290 $offsetText = "\$offset";
291 }
292 }
293
294 // Initialization for array
295 $code .= ( "if ( is_array( \$loopItem ) )\n{\n" .
296 " \$loopKeys = array_keys( \$loopItem );\n" );
297 if ( $reverseLoop )
298 $code .= " \$loopKeys = array_reverse( \$loopKeys );\n";
299 $code .= $arrayCode;
300 $code .= " \$loopCount = count( \$loopKeys );\n";
301 $code .= "}\n";
302
303 // Initialization for numeric
304 $code .= ( "else if ( is_numeric( \$loopItem ) )\n{\n" .
305 " \$loopKeys = false;\n" .
306 $numericCode .
307 " if ( \$loopItem < 0 )\n" .
308 " \$loopCountValue = -\$loopItem;\n" .
309 " else\n" .
310 " \$loopCountValue = \$loopItem;\n" .
311 " \$loopCount = \$loopCountValue - $offsetText;\n" .
312 "}\n" );
313
314 // Initialization for string
315 $code .= ( "else if ( is_string( \$loopItem ) )\n{\n" .
316 " \$loopKeys = false;\n" .
317 $stringCode .
318 " \$loopCount = strlen( \$loopItem ) - $offsetText;\n" .
319 "}\n" );
320 // Fallback for no item
321 $code .= ( "else\n{\n" .
322 " \$loopKeys = false;\n" .
323 " \$loopCount = 0;\n" .
324 "}" );
325 // Initialization end
326
327
328
329 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( $code );
330 $code = ( "while ( \$index < \$loopCount )\n" .
331 "{\n" );
332 if ( $useMax )
333 $code .= ( " if ( \$currentIndex > $maxText )\n" .
334 " break;\n" .
335 " unset( \$item );\n" );
336
337
338 // Iterator check for array
339 $code .= ( " if ( is_array( \$loopItem ) )\n" .
340 " {\n" .
341 " \$loopKey = \$loopKeys[\$index];\n" .
342 " unset( \$item );\n" .
343 " \$item = \$loopItem[\$loopKey];\n" .
344 " }\n" );
345
346 // Iterator check for numeric
347 $code .= ( " else if ( is_numeric( \$loopItem ) )\n" .
348 " {\n" .
349 " unset( \$item );\n" );
350 if ( $reverseLoop )
351 $code .= " \$item = \$loopCountValue - \$index - $offsetText;\n";
352 else
353 $code .= " \$item = \$index + $offsetText + 1;\n";
354 $code .= ( " if ( \$loopItem < 0 )\n" .
355 " \$item = -\$item;\n" );
356 if ( $reverseLoop )
357 $code .= " \$loopKey = \$loopCountValue - \$index - $offsetText - 1;\n";
358 else
359 $code .= " \$loopKey = \$index + $offsetText;\n";
360 $code .= " }\n";
361
362 // Iterator check for string
363 $code .= ( " else if ( is_string( \$loopItem ) )\n" .
364 " {\n" .
365 " unset( \$item );\n" );
366 if ( $reverseLoop )
367 $code .= " \$loopKey = \$loopCount - \$index - $offsetText + 1;\n";
368 else
369 $code .= " \$loopKey = \$index + $offsetText;\n";
370 $code .= ( " \$item = \$loopItem[\$loopKey];\n" .
371 " }\n" );
372 // Iterator check end
373
374 $code .= ( " unset( \$last );\n" .
375 " \$last = false;\n" );
376 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( $code );
377 $code = '';
378 if ( $useLastValue )
379 {
380 $code .= ( " if ( \$currentIndex > 1 )\n" .
381 " {\n" .
382 " \$last = \$lastVariableValue;\n" .
383 " \$variableValue = new eZTemplateSectionIterator();\n" .
384 " }\n" );
385 }
386 if ( $varName )
387 {
388 $code .= " \$variableValue->setIteratorValues( \$item, \$loopKey, \$currentIndex - 1, \$currentIndex, false, \$last );";
389 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( $code );
390 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, 'variableValue', eZTemplateNodeTool::extractFunctionNodePlacement( $node ),
391 array( 'spacing' => 4 ), array( '', eZTemplate::NAMESPACE_SCOPE_RELATIVE, $varName ), false, true, true );
392 }
393 else
394 {
395 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, 'loopKey', eZTemplateNodeTool::extractFunctionNodePlacement( $node ),
396 array( 'spacing' => 4 ), array( '', eZTemplate::NAMESPACE_SCOPE_RELATIVE, 'key' ), false, true, true );
397 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, 'item', eZTemplateNodeTool::extractFunctionNodePlacement( $node ),
398 array( 'spacing' => 4 ), array( '', eZTemplate::NAMESPACE_SCOPE_RELATIVE, 'item' ), false, true, true );
399 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$currentIndexInc = \$currentIndex - 1;\n" );
400 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, 'currentIndexInc', eZTemplateNodeTool::extractFunctionNodePlacement( $node ),
401 array( 'spacing' => 4 ), array( '', eZTemplate::NAMESPACE_SCOPE_RELATIVE, 'index' ), false, true, true );
402 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, 'currentIndex', eZTemplateNodeTool::extractFunctionNodePlacement( $node ),
403 array( 'spacing' => 4 ), array( '', eZTemplate::NAMESPACE_SCOPE_RELATIVE, 'number' ), false, true, true );
404 }
405
406 $mainSpacing = 0;
407 $hasFilter = false;
408 if ( count( $filterNodes ) > 0 )
409 {
410 $newFilterNodes = array();
411 $matchValue = true;
412 $hasDynamicFilter = false;
413 foreach ( $filterNodes as $filterNode )
414 {
415 $filterParameters = eZTemplateNodeTool::extractFunctionNodeParameters( $filterNode );
416 if ( !isset( $filterParameters['match'] ) )
417 continue;
418 $hasFilter = true;
419 $filterParameterMatch = $filterParameters['match'];
420 $filterParameterMatch = eZTemplateCompiler::processElementTransformationList( $tpl, $filterNode, $filterParameterMatch, $privateData );
421 if ( eZTemplateNodeTool::isConstantElement( $filterParameterMatch ) )
422 {
423 $matchValue = eZTemplateNodeTool::elementConstantValue( $filterParameterMatch );
424 if ( eZTemplateNodeTool::extractFunctionNodeName( $filterNode ) == 'section-exclude' )
425 {
426 if ( $matchValue )
427 $matchValue = false;
428 }
429 else
430 {
431 if ( $matchValue )
432 $matchValue = true;
433 }
434 $newFilterNodes = array();
435 $hasDynamicFilter = false;
436 }
437 else
438 {
439 $newFilterNodes[] = eZTemplateNodeTool::createVariableNode( false, $filterParameterMatch, eZTemplateNodeTool::extractFunctionNodePlacement( $filterNode ),
440 array( 'spacing' => 4 ), 'tmpMatchValue' );
441 if ( eZTemplateNodeTool::extractFunctionNodeName( $filterNode ) == 'section-exclude' )
442 {
443 $newFilterNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( \$tmpMatchValue )\n \$matchValue = false;",
444 array( 'spacing' => 4 ) );
445 }
446 else
447 {
448 $newFilterNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( \$tmpMatchValue )\n \$matchValue = true;",
449 array( 'spacing' => 4 ) );
450 }
451 $hasDynamicFilter = true;
452 }
453 }
454 if ( $hasFilter )
455 {
456 $mainSpacing += 4;
457 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $matchValue, eZTemplateNodeTool::extractFunctionNodePlacement( $filterNode ),
458 array( 'spacing' => 4 ), 'matchValue' );
459 if ( $hasDynamicFilter )
460 {
461 $newNodes = array_merge( $newNodes, $newFilterNodes );
462 }
463 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( \$matchValue )\n{\n", array( 'spacing' => 4 ) );
464 }
465 }
466
467 $sequencePopText = '';
468 if ( $hasSequence )
469 {
470 $sequencePopText = ", \$sequence";
471 if ( $varName )
472 {
473 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( is_array( \$sequence ) )\n" .
474 "{\n" .
475 " \$sequenceValue = array_shift( \$sequence );\n" .
476 " \$variableValue->setSequence( \$sequenceValue );\n" .
477 " \$sequence[] = \$sequenceValue;\n" .
478 " unset( \$sequenceValue );\n" .
479 "}", array( 'spacing' => $mainSpacing + 4 ) );
480 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, 'variableValue', eZTemplateNodeTool::extractFunctionNodePlacement( $node ),
481 array( 'spacing' => 4 ), array( '', eZTemplate::NAMESPACE_SCOPE_RELATIVE, $varName ), false, true, true );
482 }
483 else
484 {
485 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( is_array( \$sequence ) )\n" .
486 "{\n" .
487 " \$sequenceValue = array_shift( \$sequence );\n", array( 'spacing' => $mainSpacing + 4 ) );
488 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, 'sequenceValue', eZTemplateNodeTool::extractFunctionNodePlacement( $node ),
489 array( 'spacing' => $mainSpacing + 4 ), array( '', eZTemplate::NAMESPACE_SCOPE_RELATIVE, 'sequence' ), false, true, true );
490 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( " \$sequence[] = \$sequenceValue;\n" .
491 " unset( \$sequenceValue );\n" .
492 "}", array( 'spacing' => $mainSpacing + 4 ) );
493 }
494 }
495 $code = ( "\$sectionStack[] = array( " . $variableValuePushText . "&\$loopItem, \$loopKeys, \$loopCount, \$currentIndex, \$index" . $sequencePopText . $maxPopText . " );\n" .
496 "unset( \$loopItem, \$loopKeys );\n" );
497 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( $code, array( 'spacing' => $mainSpacing + 4 ) );
498
499 $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode( $mainSpacing + 4 );
500
501 if ( $delimiterNode )
502 {
503 $delimiterChildren = eZTemplateNodeTool::extractFunctionNodeChildren( $delimiterNode );
504 $delimiterParameters = eZTemplateNodeTool::extractFunctionNodeParameters( $delimiterNode );
505 $delimiterChildren = eZTemplateCompiler::processNodeTransformationNodes( $tpl, $node, $delimiterChildren, $privateData );
506 $delimiterModulo = false;
507 $matchCode = false;
508 $useModulo = true;
509 if ( isset( $delimiterParameters['match'] ) )
510 {
511 $delimiterMatch = $delimiterParameters['match'];
512 $delimiterMatch = eZTemplateCompiler::processElementTransformationList( $tpl, $delimiterNode, $delimiterMatch, $privateData );
513 if ( eZTemplateNodeTool::isConstantElement( $delimiterMatch ) )
514 {
515 $moduloValue = eZTemplateNodeTool::elementConstantValue( $delimiterMatch );
516 $useModulo = false;
517 }
518 else
519 {
520 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $delimiterMatch, eZTemplateNodeTool::extractFunctionNodePlacement( $node ),
521 array( 'spacing' => 0 ), 'matchValue' );
522 $matchCode = " and \$matchValue";
523 }
524 }
525 else if ( isset( $delimiterParameters['modulo'] ) )
526 {
527 $delimiterModulo = $delimiterParameters['modulo'];
528 $delimiterModulo = eZTemplateCompiler::processElementTransformationList( $tpl, $delimiterModulo, $delimiterModulo, $privateData );
529 if ( eZTemplateNodeTool::isConstantElement( $delimiterModulo ) )
530 {
531 $moduloValue = (int)eZTemplateNodeTool::elementConstantValue( $delimiterModulo );
532 $matchCode = " and ( ( \$currentIndex - 1 ) % $moduloValue ) == 0";
533 }
534 else
535 {
536 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $delimiterModulo, eZTemplateNodeTool::extractFunctionNodePlacement( $node ),
537 array( 'spacing' => 0 ), 'moduloValue' );
538 $matchCode = " and ( ( \$currentIndex - 1 ) % \$moduloValue ) == 0";
539 }
540 }
541 if ( $useModulo )
542 {
543 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( \$currentIndex > 1$matchCode )\n{" );
544
545 $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode( 4 );
546 $newNodes = array_merge( $newNodes, $delimiterChildren );
547 $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode( 4 );
548
549 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}\n" );
550 }
551 }
552 $newNodes = array_merge( $newNodes, $mainNodes );
553 $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode( $mainSpacing + 4 );
554
555 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "list( " . $variableValuePopText . "\$loopItem, \$loopKeys, \$loopCount, \$currentIndex, \$index" . $sequencePopText. $maxPopText . " ) = array_pop( \$sectionStack );",
556 array( 'spacing' => $mainSpacing + 4 ) );
557 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "++\$currentIndex;\n", array( 'spacing' => $mainSpacing + 4 ) );
558 if ( $varName )
559 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$lastVariableValue = \$variableValue;", array( 'spacing' => $mainSpacing + 4 ) );
560 if ( $hasFilter )
561 {
562 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( " }" );
563 $mainSpacing -= 4;
564 }
565 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "++\$index;\n", array( 'spacing' => $mainSpacing + 4 ) );
566 $code = ( "}\n" .
567 "unset( \$loopKeys, \$loopCount, \$index, \$last, \$loopIndex, \$loopItem" );
568 if ( $hasSequence )
569 $code .= ", \$sequence";
570 $code .= " );";
571 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( $code );
572 }
573 else if ( $useMain )
574 {
575 $newNodes = array_merge( $newNodes, $mainNodes );
576 }
577
578 if ( isset( $parameters['name'] ) )
579 $newNodes[] = eZTemplateNodeTool::createNamespaceRestoreNode();
580
581 if ( $useShow )
582 {
583 $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode( $spacing );
584 }
585
586 if ( $useElse )
587 {
588 $elseNodes = eZTemplateNodeTool::extractNodes( $children,
589 array( 'match' => array( 'type' => 'after',
590 'matches' => array( array( 'match-keys' => array( 0 ),
591 'match-with' => eZTemplate::NODE_FUNCTION ),
592 array( 'match-keys' => array( 2 ),
593 'match-with' => 'section-else' ) )
594 ) ) );
595 $elseNodes = eZTemplateCompiler::processNodeTransformationNodes( $tpl, $node, $elseNodes, $privateData );
596 if ( count( $elseNodes ) > 0 )
597 {
598 if ( $useShow )
599 {
600 // This is needed if a 'if ( $show )' was used earlier
601 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}\nelse\n{\n" );
602 $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode( $spacing );
603 $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( 'show' );
604 }
605
606 if ( isset( $parameters['name'] ) )
607 $newNodes[] = eZTemplateNodeTool::createNamespaceChangeNode( $parameters['name'] );
608 $newNodes = array_merge( $newNodes, $elseNodes );
609 if ( isset( $parameters['name'] ) )
610 $newNodes[] = eZTemplateNodeTool::createNamespaceRestoreNode();
611
612 if ( $useShow )
613 {
614 // This is needed if a 'if ( $show )' was used earlier
615 $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode( $spacing );
616 }
617 }
618
619 if ( $useShow )
620 {
621 // This is needed if a 'if ( $show )' was used earlier
622 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}\n" );
623 }
624 }
625 return $newNodes;
626 }
627
631 function process( $tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
632 {
633 $children = $functionChildren;
634 $parameters = $functionParameters;
635 $name = null;
636 if ( isset( $parameters["name"] ) )
637 $name = $tpl->elementValue( $parameters["name"], $rootNamespace, $currentNamespace, $functionPlacement );
638 if ( $name === null )
639 {
640 $name = $currentNamespace;
641 }
642 else
643 {
644 if ( $currentNamespace != "" )
645 $name = "$currentNamespace:$name";
646 }
647
648 $loopItem = null;
649 $hasLoopItemParameter = false;
650 if ( isset( $parameters["loop"] ) )
651 {
652 $hasLoopItemParameter = true;
653 $loopItem = $tpl->elementValue( $parameters["loop"], $rootNamespace, $currentNamespace, $functionPlacement );
654 }
655
656 $variableIterator = null;
657 if ( isset( $parameters['var'] ) )
658 {
659 $variableIterator = $tpl->elementValue( $parameters['var'], $rootNamespace, $currentNamespace, $functionPlacement );
660 }
661
662 $noLastValue = true;
663 if ( isset( $parameters['last-value'] ) )
664 {
665 $lastValue = $tpl->elementValue( $parameters['last-value'], $rootNamespace, $currentNamespace, $functionPlacement );
666 $noLastValue = !$lastValue;
667 }
668
669 $reverseLoop = false;
670 if ( isset( $parameters['reverse'] ) )
671 {
672 $reverseLoop = $tpl->elementValue( $parameters['reverse'], $rootNamespace, $currentNamespace, $functionPlacement );
673 }
674 if ( $hasLoopItemParameter and $loopItem === null )
675 return;
676
677 $showItem = null;
678 $showSet = isset( $parameters["show"] );
679 if ( $showSet )
680 $showItem = $tpl->elementValue( $parameters["show"], $rootNamespace, $currentNamespace, $functionPlacement );
681
682 $sequenceStructure = null;
683 if ( isset( $parameters["sequence"] ) )
684 $sequenceStructure = $tpl->elementValue( $parameters["sequence"], $rootNamespace, $currentNamespace, $functionPlacement );
685
686 $iterationMaxCount = false;
687 if ( isset( $parameters["max"] ) )
688 {
689 $iterationMaxCount = $tpl->elementValue( $parameters["max"], $rootNamespace, $currentNamespace, $functionPlacement );
690 if ( is_array( $iterationMaxCount ) )
691 {
692 $iterationMaxCount = count( $iterationMaxCount );
693 }
694 else if ( !is_numeric( $iterationMaxCount ) )
695 {
696 $tpl->warning( $functionName, "Wrong parameter type '" . gettype( $iterationMaxCount ) . "' for 'max', use either numericals or arrays", $functionPlacement );
697 }
698 $iterationMaxCount = (int)$iterationMaxCount;
699 }
700
701 $iterationOffset = false;
702 if ( isset( $parameters["offset"] ) )
703 {
704 $iterationOffset = $tpl->elementValue( $parameters["offset"], $rootNamespace, $currentNamespace, $functionPlacement );
705 if ( is_array( $iterationOffset ) )
706 {
707 $iterationOffset = count( $iterationOffset );
708 }
709 else if ( !is_numeric( $iterationOffset ) )
710 {
711 $tpl->warning( $functionName, "Wrong parameter type '" . gettype( $iterationOffset ) . "' for 'offset', use either numericals or arrays", $functionPlacement );
712 }
713 $iterationOffset = (int)$iterationOffset;
714 if ( $iterationOffset < 0 )
715 {
716 $tpl->warning( $functionName, "The 'offset' parameter can only be negative, $iterationOffset is not accepteed, the value will be reset to 0", $functionPlacement );
717 $iterationOffset = 0;
718 }
719 }
720
721 $elseName = $functionName . "-else";
722 $delimiterStructure = null;
723 $filterStructure = array();
724 $else = null;
725 $shown = 1;
726 $items = array();
727 $items[0] = array();
728 $items[1] = array();
729 if ( is_array( $children ) )
730 {
731 foreach ( array_keys( $children ) as $childKey )
732 {
733 $child =& $children[$childKey];
734 $childType = $child[0];
735 if ( $childType == eZTemplate::NODE_FUNCTION )
736 {
737 switch ( $child[2] )
738 {
739 case "delimiter":
740 {
741 if ( $shown === 1 and $delimiterStructure === null )
742 {
743 $delimiterStructure =& $child;
744 }
745 } break;
746 case "section-exclude":
747 case "section-include":
748 {
749 if ( $shown === 1 )
750 $filterStructure[] =& $child;
751 } break;
752 case $elseName:
753 {
754 $else =& $child;
755 $shown = 0;
756 } break;
757 default:
758 {
759 $items[$shown][] =& $child;
760 } break;
761 }
762 }
763 else
764 {
765 $items[$shown][] =& $child;
766 }
767 }
768 }
769
770 $canShowBlock = true;
771 if( $showSet and ( ( is_array( $showItem ) and count( $showItem ) == 0 ) or
772 ( is_numeric( $showItem ) and $showItem == 0 ) or
773 ( is_string( $showItem ) > 0 and strlen( $showItem ) == 0 ) or
774 !$showItem ) )
775 $canShowBlock = false;
776
777 if ( ( !$showSet or ( $showSet and $canShowBlock ) ) and $loopItem === null )
778 {
779 $this->processChildrenOnce( $textElements, $items[1], $tpl, $rootNamespace, $name );
780 }
781 else
782 {
783 $iteratorData = array( 'iterator' => false );
784 $showMainBody = true;
785 if ( $showSet )
786 {
787 if( !$canShowBlock )
788 $showMainBody = false;
789 }
790 if ( $showMainBody )
791 {
792 $isFirstRun = true;
793 $index = 0;
794 if ( is_array( $loopItem ) )
795 {
796 $array =& $loopItem;
797 $arrayKeys = array_keys( $array );
798 if ( $reverseLoop )
799 $arrayKeys = array_reverse( $arrayKeys );
800 if ( $iterationOffset !== false )
801 $arrayKeys = array_splice( $arrayKeys, $iterationOffset );
802 $currentCount = 0;
803 foreach ( $arrayKeys as $key )
804 {
805 unset( $item );
806 $item =& $array[$key];
807 $usedElement = $this->processChildren( $textElements, $items[1], $key, $item, $index, $isFirstRun,
808 $delimiterStructure, $sequenceStructure, $filterStructure,
809 $tpl, $rootNamespace, $name, $functionPlacement,
810 $variableIterator, $noLastValue,
811 $iteratorData );
812 if ( $usedElement )
813 {
814 if ( $iterationMaxCount !== false )
815 {
816 ++$currentCount;
817 if ( $currentCount >= $iterationMaxCount )
818 break;
819 }
820 }
821 }
822 }
823 else if ( is_numeric( $loopItem ) )
824 {
825 $value = $loopItem;
826 $count = $value;
827 if ( $value < 0 )
828 $count = -$count;
829 $loopStart = 0;
830 if ( $iterationOffset !== false )
831 $loopStart = $iterationOffset;
832 $currentCount = 0;
833 for ( $i = $loopStart; $i < $count; ++$i )
834 {
835 unset( $iterator );
836 if ( $reverseLoop )
837 $iterator = ( $count - $i ) - 1;
838 else
839 $iterator = $i;
840 unset( $key );
841 unset( $item );
842 if ( $value < 0 )
843 {
844 $key = $iterator;
845 $item = -$iterator - 1;
846 }
847 else
848 {
849 $key = $iterator;
850 $item = $iterator + 1;
851 }
852 $usedElement = $this->processChildren( $textElements, $items[1], $key, $item, $index, $isFirstRun,
853 $delimiterStructure, $sequenceStructure, $filterStructure,
854 $tpl, $rootNamespace, $name, $functionPlacement,
855 $variableIterator, $noLastValue,
856 $iteratorData );
857 if ( $usedElement )
858 {
859 if ( $iterationMaxCount !== false )
860 {
861 ++$currentCount;
862 if ( $currentCount >= $iterationMaxCount )
863 break;
864 }
865 }
866 }
867 }
868 else if ( is_string( $loopItem ) )
869 {
870 $text =& $loopItem;
871 $stringLength = strlen( $text );
872 $loopStart = 0;
873 if ( $iterationOffset !== false )
874 $loopStart = $iterationOffset;
875 $currentCount = 0;
876 for ( $i = $loopStart; $i < $stringLength; ++$i )
877 {
878 if ( $reverseLoop )
879 $iterator = ($stringLength - $i) - 1;
880 else
881 $iterator = $i;
882 unset( $key );
883 unset( $item );
884 $key = $iterator;
885 $item = $text[$iterator];
886 $usedElement = $this->processChildren( $textElements, $items[1], $key, $item, $index, $isFirstRun,
887 $delimiterStructure, $sequenceStructure, $filterStructure,
888 $tpl, $rootNamespace, $name, $functionPlacement,
889 $variableIterator, $noLastValue,
890 $iteratorData );
891 if ( $usedElement )
892 {
893 if ( $iterationMaxCount !== false )
894 {
895 ++$currentCount;
896 if ( $currentCount >= $iterationMaxCount )
897 break;
898 }
899 }
900 }
901 }
902 if ( !$isFirstRun )
903 {
904 if ( $variableIterator !== null )
905 {
906 $tpl->unsetVariable( $variableIterator, $name );
907 }
908 else
909 {
910 $tpl->unsetVariable( "key", $name );
911 $tpl->unsetVariable( "item", $name );
912 $tpl->unsetVariable( "index", $name );
913 $tpl->unsetVariable( "number", $name );
914 if ( $sequenceStructure !== null and is_array( $sequenceStructure ) )
915 $tpl->unsetVariable( "sequence", $name );
916 }
917 }
918 }
919 else
920 {
921 $this->processChildrenOnce( $textElements, $items[0], $tpl, $rootNamespace, $name );
922 }
923 }
924 }
925
931 function processChildrenOnce( &$textElements, &$children, $tpl, $rootNamespace, $name )
932 {
933 foreach ( array_keys( $children ) as $childKey )
934 {
935 $child =& $children[$childKey];
936 $tpl->processNode( $child, $textElements, $rootNamespace, $name );
937 }
938 return true;
939 }
940
947 function processChildren( &$textElements,
948 &$children, $key, &$item, &$index, &$isFirstRun,
949 &$delimiterStructure, &$sequenceStructure, &$filterStructure,
950 $tpl, $rootNamespace, $name, $functionPlacement,
951 &$variableIterator, $noLastValue,
952 &$iteratorData )
953 {
954 if ( $variableIterator !== null )
955 {
956 unset( $last );
957 if ( !$noLastValue and $iteratorData['iterator'] !== false )
958 {
959 $last =& $iteratorData['iterator'];
960 }
961 else
962 {
963 $last = false;
964 }
965 unset( $iteratorData['iterator'] );
966 $iteratorData['iterator'] = new eZTemplateSectionIterator();
967 $iteratorData['iterator']->setIteratorValues( $item, $key, $index, $index + 1, false, $last );
968 unset( $last );
969
970 $iteratorObject =& $iteratorData['iterator'];
971 $tpl->setVariable( $variableIterator, $iteratorObject, $name );
972 }
973 else
974 {
975 $tpl->setVariable( "key", $key, $name );
976 $tpl->setVariable( "item", $item, $name );
977 $tpl->setVariable( "index", $index, $name );
978 $tpl->setVariable( "number", $index + 1, $name );
979 }
980 if ( count( $filterStructure ) > 0 )
981 {
982 $filterCount = count( $filterStructure );
983 $includeElement = true;
984 for ( $i = 0; $i < $filterCount; ++$i )
985 {
986 $filterElement =& $filterStructure[$i];
987 $filterParameters =& $filterElement[3];
988 $filterName = $filterElement[2];
989 $filterMatch = null;
990 if ( isset( $filterParameters["match"] ) )
991 {
992 $filterMatch = $tpl->elementValue( $filterParameters["match"], $rootNamespace, $name, $functionPlacement );
993 if ( $filterMatch )
994 $includeElement = $filterName == "section-exclude" ? false : true;
995 }
996 else
997 $tpl->missingParameter( "section:$filterName", "match" );
998 }
999 if ( !$includeElement )
1000 return false;
1001 }
1002 if ( $delimiterStructure !== null and !$isFirstRun )
1003 {
1004 $delimiterParameters = $delimiterStructure[3];
1005 $delimiterMatch = true;
1006 if ( isset( $delimiterParameters["modulo"] ) )
1007 {
1008 $delimiterModulo = $delimiterParameters["modulo"];
1009 $modulo = $tpl->elementValue( $delimiterModulo, $rootNamespace, $name, $functionPlacement );
1010 $modulo = trim( $modulo );
1011 if ( is_numeric( $modulo ) )
1012 $delimiterMatch = ( $index % $modulo ) == 0;
1013 }
1014 if ( isset( $delimiterParameters["match"] ) )
1015 {
1016 $delimiterMatchParameter = $delimiterParameters["match"];
1017 $delimiterMatch = $tpl->elementValue( $delimiterMatchParameter, $rootNamespace, $name, $functionPlacement );
1018 }
1019 if ( $delimiterMatch )
1020 {
1021 $delimiterChildren =& $delimiterStructure[1];
1022 if ( is_array( $delimiterChildren ) )
1023 {
1024 foreach ( array_keys( $delimiterChildren ) as $delimiterChildKey )
1025 {
1026 $delimiterChild =& $delimiterChildren[$delimiterChildKey];
1027 $tpl->processNode( $delimiterChild, $textElements, $rootNamespace, $name );
1028 }
1029 }
1030 }
1031 }
1032 $isFirstRun = false;
1033 if ( $sequenceStructure !== null and is_array( $sequenceStructure ) )
1034 {
1035 $sequenceValue = array_shift( $sequenceStructure );
1036 if ( $variableIterator !== null )
1037 {
1038 $iteratorData['iterator']->setSequence( $sequenceValue );
1039 }
1040 else
1041 {
1042 $tpl->setVariable( "sequence", $sequenceValue, $name );
1043 }
1044 $sequenceStructure[] = $sequenceValue;
1045 }
1046 foreach ( array_keys( $children ) as $childKey )
1047 {
1048 $child =& $children[$childKey];
1049 $tpl->processNode( $child, $textElements, $rootNamespace, $name );
1050 }
1051 ++$index;
1052 return true;
1053 }
1054
1058 function hasChildren()
1059 {
1060 return true;
1061 }
1062
1065 public $Name;
1066 }
1067
1068 ?>
eZTemplateCompiler\processElementTransformationList
static processElementTransformationList( $tpl, &$node, $elements, &$privateData)
Definition: eztemplatecompiler.php:1211
eZTemplateCompiler\processNodeTransformationNodes
static processNodeTransformationNodes( $tpl, &$node, &$nodes, &$privateData)
Definition: eztemplatecompiler.php:1012
eZTemplateNodeTool\createVariableNode
static createVariableNode( $originalNode=false, $variableData=false, $variablePlacement=false, $parameters=array(), $variableAssignmentName=false, $onlyExisting=false, $overWrite=true, $assignFromVariable=false, $rememberSet=false)
Definition: eztemplatenodetool.php:395
eZTemplateNodeTool\elementConstantValue
static elementConstantValue( $elements)
Definition: eztemplatenodetool.php:197
eZTemplateNodeTool\extractNodes
static extractNodes( $nodeList, $parameters=array())
Definition: eztemplatenodetool.php:543
eZTemplateNodeTool\createSpacingDecreaseNode
static createSpacingDecreaseNode( $spacing=4, $parameters=array())
Definition: eztemplatenodetool.php:508
eZTemplateNodeTool\createVariableUnsetNode
static createVariableUnsetNode( $variableName, $parameters=array())
Definition: eztemplatenodetool.php:462
eZTemplateNodeTool\createCodePieceNode
static createCodePieceNode( $codePiece, $parameters=array())
Definition: eztemplatenodetool.php:457
eZTemplateNodeTool\createSpacingIncreaseNode
static createSpacingIncreaseNode( $spacing=4, $parameters=array())
Definition: eztemplatenodetool.php:503
eZTemplateNodeTool\createNamespaceRestoreNode
static createNamespaceRestoreNode( $parameters=array())
Definition: eztemplatenodetool.php:522
eZTemplateNodeTool\createTextNode
static createTextNode( $text)
Definition: eztemplatenodetool.php:442
eZTemplateNodeTool\createNamespaceChangeNode
static createNamespaceChangeNode( $variableData, $parameters=array())
Definition: eztemplatenodetool.php:513
eZTemplateNodeTool\extractFunctionNodePlacement
static extractFunctionNodePlacement(&$node)
Definition: eztemplatenodetool.php:660
eZTemplateNodeTool\extractFunctionNodeParameters
static extractFunctionNodeParameters(&$node)
Definition: eztemplatenodetool.php:678
eZTemplateNodeTool\extractFunctionNodeName
static extractFunctionNodeName(&$node)
Definition: eztemplatenodetool.php:705
eZTemplateNodeTool\extractFunctionNodeChildren
static extractFunctionNodeChildren(&$node)
Definition: eztemplatenodetool.php:669
eZTemplateNodeTool\isConstantElement
static isConstantElement( $elements)
Definition: eztemplatenodetool.php:233
eZTemplateSectionFunction
Advanced block handling in templates using function "section".
Definition: eztemplatesectionfunction.php:58
eZTemplateSectionFunction\attributeList
attributeList()
Definition: eztemplatesectionfunction.php:73
eZTemplateSectionFunction\$Name
$Name
Definition: eztemplatesectionfunction.php:1065
eZTemplateSectionFunction\process
process( $tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace)
Definition: eztemplatesectionfunction.php:631
eZTemplateSectionFunction\functionTemplateHints
functionTemplateHints()
Definition: eztemplatesectionfunction.php:90
eZTemplateSectionFunction\processChildren
processChildren(&$textElements, &$children, $key, &$item, &$index, &$isFirstRun, &$delimiterStructure, &$sequenceStructure, &$filterStructure, $tpl, $rootNamespace, $name, $functionPlacement, &$variableIterator, $noLastValue, &$iteratorData)
Definition: eztemplatesectionfunction.php:947
eZTemplateSectionFunction\processChildrenOnce
processChildrenOnce(&$textElements, &$children, $tpl, $rootNamespace, $name)
Definition: eztemplatesectionfunction.php:931
eZTemplateSectionFunction\templateNodeTransformation
templateNodeTransformation( $functionName, &$node, $tpl, $parameters, $privateData)
Definition: eztemplatesectionfunction.php:99
eZTemplateSectionFunction\hasChildren
hasChildren()
Definition: eztemplatesectionfunction.php:1058
eZTemplateSectionFunction\functionList
functionList()
Definition: eztemplatesectionfunction.php:85
eZTemplateSectionFunction\__construct
__construct( $name="section")
Definition: eztemplatesectionfunction.php:64
eZTemplateSectionIterator
The iterator item in a section loop which works as a proxy.
Definition: eztemplatesectioniterator.php:28
eZTemplate\NAMESPACE_SCOPE_RELATIVE
const NAMESPACE_SCOPE_RELATIVE
Definition: eztemplate.php:286
eZTemplate\NODE_FUNCTION
const NODE_FUNCTION
Definition: eztemplate.php:213
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.