Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

eztemplateiffunction.php
Go to the documentation of this file.
1 <?php
37 class eZTemplateIfFunction
38 {
39 const FUNCTION_NAME = 'if';
40
44 function functionList()
45 {
46 //eZDebug::writeDebug( "if::functionList()" );
47 $functionList = array( eZTemplateIfFunction::FUNCTION_NAME );
48 return $functionList;
49 }
50
56 function attributeList()
57 {
58 return array( 'elseif' => false,
59 'else' => false );
60 }
61
62
66 function functionTemplateHints()
67 {
68 return array( eZTemplateIfFunction::FUNCTION_NAME => array( 'parameters' => true,
69 'static' => false,
70 'transform-parameters' => true,
71 'tree-transformation' => true ) );
72 }
73
77 function templateNodeTransformation( $functionName, &$node,
78 $tpl, $parameters, $privateData )
79 {
80 $tpl->ElseifCounter++;
81 $newNodes = array();
82 $nodesToPrepend = array();
83 $nodesToAppend = array();
84 $nodePlacement = eZTemplateNodeTool::extractFunctionNodePlacement( $node );
85 $uniqid = md5( $nodePlacement[2] ) . "_" . $tpl->ElseifCounter;
86 $children = eZTemplateNodeTool::extractFunctionNodeChildren( $node );
87
88
89 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "// if begins" );
90 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameters['condition'], $nodePlacement, array( 'treat-value-as-non-object' => true ), 'if_cond' );
91 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( \$if_cond )\n{" );
92 $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode();
93
94 if ( is_array( $children ) )
95 {
96 foreach ( array_keys( $children ) as $childKey )
97 {
98 $child =& $children[$childKey];
99
100 if ( $child[0] == eZTemplate::NODE_FUNCTION )
101 {
102 $childFunctionName =& $child[2];
103 $childChildren = eZTemplateNodeTool::extractFunctionNodeChildren( $child );
104 $childFunctionArgs =& $child[3];
105
106 if ( $childFunctionName == 'elseif' )
107 {
108 $compiledElseifCondition = eZTemplateCompiler::processElementTransformationList( $tpl, $child, $childFunctionArgs['condition'], $privateData );
109 $nodesToPrepend[] = eZTemplateNodeTool::createVariableNode( false, $compiledElseifCondition,
110 $nodePlacement,
111 array( 'text-result' => true ),
112 "elseif_cond_$uniqid" );
113 $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode();
114 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}\nelseif ( \$elseif_cond_$uniqid )\n{" );
115 $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode();
116 $nodesToAppend[] = eZTemplateNodeTool::createVariableUnsetNode( "elseif_cond_$uniqid" );
117 // increment unique elseif counter
118 $uniqid = md5( $nodePlacement[2] ) . "_" . ++$tpl->ElseifCounter;
119 }
120 elseif ( $childFunctionName == 'else' )
121 {
122 $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode();
123 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}\nelse\n{" );
124 $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode();
125 }
126 elseif ( $childFunctionName == 'break' )
127 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "break;\n" );
128 elseif ( $childFunctionName == 'continue' )
129 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "continue;\n" );
130 elseif ( $childFunctionName == 'skip' )
131 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$skipDelimiter = true;\ncontinue;\n" );
132
133 // let other functions (ones not listed in the conditions above) be transformed
134 if ( in_array( $childFunctionName, array( 'elseif', 'else', 'break', 'continue', 'skip' ) ) )
135 continue;
136 }
137 $newNodes[] = $child;
138 }
139 }
140
141 $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode();
142 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}" );
143 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "unset( \$if_cond );" );
144 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "// if ends\n" );
145
146 $newNodes = array_merge( $nodesToPrepend, $newNodes, $nodesToAppend );
147
148 return $newNodes;
149 }
150
154 function process( $tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
155 {
156 if ( count( $functionParameters ) == 0 || !count( $functionParameters['condition'] ) )
157 {
158 eZDebug::writeError( "Not enough arguments passed to 'if' function." );
159 return;
160 }
161
162 $ifValue = $tpl->elementValue( $functionParameters['condition'], $rootNamespace, $currentNamespace, $functionPlacement );
163
164 $show = $ifValue; // whether to show the child being currently processing
165 $showWasTrue = $show; // true if $show has been assigned 'true' value at least once in the current {if}...{/if} block.
166 $foundElse = false; // true if 'else' has been met once in the current {if}...{/if} block.
167
168 if ( !is_array( $functionChildren ) )
169 {
170 return;
171 }
172 foreach ( $functionChildren as $key => $child )
173 {
174 $childType = $child[0];
175
176 // parse 'elseif', 'else' functions
177 if ( $childType == eZTemplate::NODE_FUNCTION )
178 {
179 $childFunctionName =& $child[2];
180 $childFunctionArgs =& $child[3];
181 $childFunctionPlacement =& $child[4];
182
183 if ( $childFunctionName == 'else' )
184 {
185 if ( $foundElse )
186 {
187 eZDebug::writeError( "Duplicated 'else'" );
188 $show = false;
189 continue;
190 }
191
192 $show = !$showWasTrue;
193 $foundElse = true;
194 continue;
195 }
196 elseif ( $childFunctionName == 'elseif' )
197 {
198 if ( $foundElse )
199 {
200 eZDebug::writeError( "There should be no more 'elseif' after 'else'" );
201 $show = false;
202 continue;
203 }
204
205 if ( !isset( $childFunctionArgs['condition'] ) || !count( $childFunctionArgs['condition'] ) ) // no arguments passes to 'elseif' (should not happen)
206 $elseifValue = false;
207 else
208 $elseifValue = $tpl->elementValue( $childFunctionArgs['condition'], $rootNamespace, $currentNamespace, $functionPlacement );
209
210 if ( !$showWasTrue && $elseifValue )
211 $show = $showWasTrue = true;
212 else
213 $show = false;
214 continue;
215 }
216 elseif ( $childFunctionName == 'break' )
217 {
218 if ( !$show )
219 continue;
220 return array( 'breakFunctionFound' => 1 );
221 }
222 elseif ( $childFunctionName == 'continue' )
223 {
224 if ( !$show )
225 continue;
226 return array( 'continueFunctionFound' => 1 );
227 }
228 elseif ( $childFunctionName == 'skip' )
229 {
230 if ( !$show )
231 continue;
232 return array( 'skipFunctionFound' => 1 );
233 }
234 }
235
236 if ( $show )
237 {
238 $rslt = $tpl->processNode( $child, $textElements, $rootNamespace, $currentNamespace );
239
240 // handle 'break', 'continue' and 'skip'
241 if ( is_array( $rslt ) && ( array_key_exists( 'breakFunctionFound', $rslt ) ||
242 array_key_exists( 'continueFunctionFound', $rslt ) ||
243 array_key_exists( 'skipFunctionFound', $rslt ) ) )
244 {
245 return $rslt;
246 }
247 }
248 }
249 }
250
254 function hasChildren()
255 {
256 return true;
257 }
258 }
259
260 ?>
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZTemplateCompiler\processElementTransformationList
static processElementTransformationList( $tpl, &$node, $elements, &$privateData)
Definition: eztemplatecompiler.php:1211
eZTemplateIfFunction
Conditional execution in templates.
Definition: eztemplateiffunction.php:38
eZTemplateIfFunction\functionList
functionList()
Definition: eztemplateiffunction.php:44
eZTemplateIfFunction\templateNodeTransformation
templateNodeTransformation( $functionName, &$node, $tpl, $parameters, $privateData)
Definition: eztemplateiffunction.php:77
eZTemplateIfFunction\attributeList
attributeList()
Definition: eztemplateiffunction.php:56
eZTemplateIfFunction\functionTemplateHints
functionTemplateHints()
Definition: eztemplateiffunction.php:66
eZTemplateIfFunction\FUNCTION_NAME
const FUNCTION_NAME
Definition: eztemplateiffunction.php:39
eZTemplateIfFunction\process
process( $tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace)
Definition: eztemplateiffunction.php:154
eZTemplateIfFunction\hasChildren
hasChildren()
Definition: eztemplateiffunction.php:254
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\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\extractFunctionNodePlacement
static extractFunctionNodePlacement(&$node)
Definition: eztemplatenodetool.php:660
eZTemplateNodeTool\extractFunctionNodeChildren
static extractFunctionNodeChildren(&$node)
Definition: eztemplatenodetool.php:669
eZTemplate\NODE_FUNCTION
const NODE_FUNCTION
Definition: eztemplate.php:213
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.