Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

eztemplateswitchfunction.php
Go to the documentation of this file.
1 <?php
42 class eZTemplateSwitchFunction
43 {
47 public function __construct()
48 {
49 $this->SwitchName = 'switch';
50 }
51
55 function functionList()
56 {
57 return array( $this->SwitchName );
58 }
59
60 function functionTemplateHints()
61 {
62 return array( $this->SwitchName => array( 'parameters' => true,
63 'static' => false,
64 'transform-children' => false,
65 'tree-transformation' => true,
66 'transform-parameters' => true ) );
67 }
68
72 function attributeList()
73 {
74 return array( "case" => true );
75 }
76
77 function templateNodeCaseTransformation( $tpl, &$newNodes, &$caseNodes, &$caseCounter, &$node, $privateData )
78 {
79 if ( $node[2] == 'case' )
80 {
81 if ( is_array( $node[3] ) && count( $node[3] ) )
82 {
83 if ( isset( $node[3]['match'] ) )
84 {
85 $match = $node[3]['match'];
86 $match = eZTemplateCompiler::processElementTransformationList( $tpl, $node, $match, $privateData );
87
88 $dynamicCase = false;
89 if ( eZTemplateNodeTool::isConstantElement( $match ) )
90 {
91 $matchValue = eZTemplateNodeTool::elementConstantValue( $match );
92 $caseText = eZPHPCreator::variableText( $matchValue, 0, 0, false );
93 }
94 else
95 {
96 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $match, false, array(), 'case' . $caseCounter );
97 $caseText = "\$case" . $caseCounter;
98 ++$caseCounter;
99 $dynamicCase = true;
100 }
101
102 $caseNodes[] = eZTemplateNodeTool::createCodePieceNode( " case $caseText:\n {" );
103 if ( $dynamicCase )
104 $caseNodes[] = eZTemplateNodeTool::createCodePieceNode( " unset( $caseText );" );
105 }
106 else if ( isset( $node[3]['in'] ) )
107 {
108 return false;
109 }
110 }
111 else
112 {
113 $caseNodes[] = eZTemplateNodeTool::createCodePieceNode( " default:\n {" );
114 }
115
116 $children = eZTemplateNodeTool::extractFunctionNodeChildren( $node );
117 if ( $children === false )
118 {
119 $children = array();
120 }
121 else
122 {
123 $children = eZTemplateCompiler::processNodeTransformationNodes( $tpl, $node, $children, $privateData );
124 }
125
126 $caseNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode( 8 );
127
128 $caseNodes = array_merge( $caseNodes, $children );
129 $caseNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode( 8 );
130
131 $caseNodes[] = eZTemplateNodeTool::createCodePieceNode( " } break;" );
132 }
133 }
134
135
136 function templateNodeTransformation( $functionName, &$node,
137 $tpl, $parameters, $privateData )
138 {
139 $newNodes = array();
140 $namespaceValue = false;
141 $varName = 'match';
142
143 if ( !isset( $parameters['match'] ) )
144 {
145 return false;
146 }
147
148 if ( isset( $parameters['name'] ) )
149 {
150 $nameData = $parameters['name'];
151 if ( !eZTemplateNodeTool::isConstantElement( $nameData ) )
152 return false;
153 $namespaceValue = eZTemplateNodeTool::elementConstantValue( $nameData );
154 }
155
156 if ( isset( $parameters['var'] ) )
157 {
158 $varData = $parameters['var'];
159 if ( !eZTemplateNodeTool::isConstantElement( $varData ) )
160 return false;
161 $varName = eZTemplateNodeTool::elementConstantValue( $varData );
162 }
163
164 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameters['match'], false, array(),
165 array( $namespaceValue, eZTemplate::NAMESPACE_SCOPE_RELATIVE, $varName ) );
166 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameters['match'],
167 eZTemplateNodeTool::extractFunctionNodePlacement( $node ),
168 array( 'variable-name' => 'match',
169 'text-result' => true ), 'match' );
170 // 'text-result' => false ) );
171 if ( isset( $parameters['name'] ) )
172 {
173 $newNodes[] = eZTemplateNodeTool::createNamespaceChangeNode( $parameters['name'] );
174 }
175
176 $tmpNodes = array();
177 $children = eZTemplateNodeTool::extractFunctionNodeChildren( $node );
178 $caseNodes = array();
179 $caseCounter = 1;
180 if ( is_array( $children ) )
181 {
182 foreach ( $children as $child )
183 {
184 $childType = $child[0];
185 if ( $childType == eZTemplate::NODE_FUNCTION )
186 {
187 if ( $this->templateNodeCaseTransformation( $tpl, $tmpNodes, $caseNodes, $caseCounter, $child, $privateData ) === false )
188 {
189 return false;
190 }
191 }
192 }
193 }
194 $newNodes = array_merge( $newNodes, $tmpNodes );
195 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "switch ( \$match )\n{" );
196 $newNodes = array_merge( $newNodes, $caseNodes );
197
198 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}" );
199 $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( 'match' );
200 if ( isset( $parameters['name'] ) )
201 {
202 $newNodes[] = eZTemplateNodeTool::createNamespaceRestoreNode();
203 }
204 $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( array( $namespaceValue, eZTemplate::NAMESPACE_SCOPE_RELATIVE, 'match' ) );
205
206 return $newNodes;
207 }
208
212 function process( $tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
213 {
214 $children = $functionChildren;
215 $params = $functionParameters;
216 $name = "";
217 if ( isset( $params["name"] ) )
218 $name = $tpl->elementValue( $params["name"], $rootNamespace, $currentNamespace, $functionPlacement );
219 $varName = false;
220 if ( isset( $params["var"] ) )
221 $varName = $tpl->elementValue( $params["var"], $rootNamespace, $currentNamespace, $functionPlacement );
222 if ( $currentNamespace != "" )
223 {
224 if ( $name != "" )
225 $name = "$currentNamespace:$name";
226 else
227 $name = $currentNamespace;
228 }
229 if ( isset( $params["match"] ) )
230 $match = $tpl->elementValue( $params["match"], $rootNamespace, $currentNamespace, $functionPlacement );
231 else
232 {
233 $tpl->missingParameter( $this->SwitchName, "match" );
234 return false;
235 }
236
237 $items = array();
238 $in_items = array();
239 $def = null;
240 $case = null;
241 if ( is_array( $children ) )
242 {
243 foreach( $children as $child )
244 {
245 $childType = $child[0];
246 if ( $childType == eZTemplate::NODE_FUNCTION )
247 {
248 switch ( $child[2] )
249 {
250 case "case":
251 {
252 $child_params = $child[3];
253 if ( isset( $child_params["match"] ) )
254 {
255 $child_match = $child_params["match"];
256 $child_match = $tpl->elementValue( $child_match, $rootNamespace, $currentNamespace, $functionPlacement );
257 if ( !isset( $items[$child_match] ) )
258 {
259 $items[$child_match] = $child;
260 if ( $case === null and
261 $match == $child_match )
262 {
263 $case = $child;
264 }
265 }
266 else
267 {
268 $tpl->warning( $this->SwitchName, "Match value $child_match already set, skipping", $functionPlacement );
269 }
270 }
271 else if ( isset( $child_params["in"] ) )
272 {
273 $key_name = null;
274 if ( isset( $child_params["key"] ) )
275 {
276 $child_key = $child_params["key"];
277 $key_name = $tpl->elementValue( $child_key, $rootNamespace, $currentNamespace, $functionPlacement );
278 }
279 $child_in = $child_params["in"];
280 $child_in = $tpl->elementValue( $child_in, $rootNamespace, $currentNamespace, $functionPlacement );
281 if ( !is_array( $child_in ) )
282 break;
283 if ( $case === null )
284 {
285 if ( $key_name === null )
286 {
287 if ( in_array( $match, $child_in ) )
288 {
289 $case = $child;
290 }
291 }
292 else
293 {
294 foreach( $child_in as $child_in_element )
295 {
296 if ( !is_array( $key_name ) )
297 $key_name_array = array( $key_name );
298 else
299 $key_name_array = $key_name;
300 $child_value = $tpl->variableAttribute( $child_in_element, $key_name );
301 if ( $child_value == $match )
302 {
303 $case = $child;
304 break;
305 }
306 }
307 }
308 }
309 }
310 else
311 {
312 $def = $child;
313 }
314 } break;
315 default:
316 {
317 $tpl->warning( $this->SwitchName, "Only case functions are allowed as children, found \""
318 . $child[2] . "\"", $functionPlacement );
319 } break;
320 }
321 }
322 else if ( $childType == eZTemplate::NODE_TEXT )
323 {
324 // Ignore text.
325 }
326 else
327 {
328 $tpl->warning( $this->SwitchName, "Only functions are allowed as children, found \""
329 . $childType . "\"", $functionPlacement );
330 }
331 }
332 }
333
334 if ( $case === null )
335 {
336 $case = $def;
337 }
338
339 if ( $case !== null )
340 {
341 if ( $varName !== false )
342 $tpl->setVariable( $varName, $match, $name );
343 else
344 $tpl->setVariable( "match", $match, $name );
345 $case_children = $case[1];
346 if ( $case_children )
347 {
348 foreach( $case_children as $case_child )
349 {
350 $tpl->processNode( $case_child, $textElements, $rootNamespace, $name );
351 }
352 }
353 }
354 else
355 $tpl->warning( $this->SwitchName, "No case match and no default case", $functionPlacement );
356 return;
357 }
358
362 function hasChildren()
363 {
364 return true;
365 }
366
368 public $SwitchName;
369 }
370
371 ?>
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\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\createNamespaceChangeNode
static createNamespaceChangeNode( $variableData, $parameters=array())
Definition: eztemplatenodetool.php:513
eZTemplateNodeTool\extractFunctionNodePlacement
static extractFunctionNodePlacement(&$node)
Definition: eztemplatenodetool.php:660
eZTemplateNodeTool\extractFunctionNodeChildren
static extractFunctionNodeChildren(&$node)
Definition: eztemplatenodetool.php:669
eZTemplateNodeTool\isConstantElement
static isConstantElement( $elements)
Definition: eztemplatenodetool.php:233
eZTemplateSwitchFunction
Handles conditional output in templates using function "switch".
Definition: eztemplateswitchfunction.php:43
eZTemplateSwitchFunction\templateNodeCaseTransformation
templateNodeCaseTransformation( $tpl, &$newNodes, &$caseNodes, &$caseCounter, &$node, $privateData)
Definition: eztemplateswitchfunction.php:77
eZTemplateSwitchFunction\$SwitchName
$SwitchName
The name of the switch function.
Definition: eztemplateswitchfunction.php:368
eZTemplateSwitchFunction\functionTemplateHints
functionTemplateHints()
Definition: eztemplateswitchfunction.php:60
eZTemplateSwitchFunction\templateNodeTransformation
templateNodeTransformation( $functionName, &$node, $tpl, $parameters, $privateData)
Definition: eztemplateswitchfunction.php:136
eZTemplateSwitchFunction\hasChildren
hasChildren()
Definition: eztemplateswitchfunction.php:362
eZTemplateSwitchFunction\__construct
__construct()
Definition: eztemplateswitchfunction.php:47
eZTemplateSwitchFunction\functionList
functionList()
Definition: eztemplateswitchfunction.php:55
eZTemplateSwitchFunction\process
process( $tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace)
Definition: eztemplateswitchfunction.php:212
eZTemplateSwitchFunction\attributeList
attributeList()
Definition: eztemplateswitchfunction.php:72
eZTemplate\NAMESPACE_SCOPE_RELATIVE
const NAMESPACE_SCOPE_RELATIVE
Definition: eztemplate.php:286
eZTemplate\NODE_TEXT
const NODE_TEXT
Definition: eztemplate.php:211
eZTemplate\NODE_FUNCTION
const NODE_FUNCTION
Definition: eztemplate.php:213
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.