Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

eztemplatesetfunction.php
Go to the documentation of this file.
1 <?php
36 class eZTemplateSetFunction
37 {
38 const SCOPE_RELATIVE = 1;
39 const SCOPE_ROOT = 2;
40 const SCOPE_GLOBAL = 3;
41
49 public function __construct( $setName = 'set', $letName = 'let', $defaultName = 'default' )
50 {
51 $this->SetName = $setName;
52 $this->LetName = $letName;
53 $this->DefaultName = $defaultName;
54 }
55
59 function functionList()
60 {
61 return array( $this->SetName, $this->LetName, $this->DefaultName );
62 }
63
64 function functionTemplateHints()
65 {
66 return array( $this->LetName => array( 'parameters' => true,
67 'static' => false,
68 'tree-transformation' => true,
69 'transform-children' => true,
70 'transform-parameters' => true ),
71 $this->SetName => array( 'parameters' => true,
72 'static' => false,
73 'tree-transformation' => true,
74 'transform-children' => true,
75 'transform-parameters' => true ),
76 $this->DefaultName => array( 'parameters' => true,
77 'static' => false,
78 'tree-transformation' => true,
79 'transform-children' => true,
80 'transform-parameters' => true ) );
81 }
82
83 function templateNodeTransformation( $functionName, &$node,
84 $tpl, $parameters, $privateData )
85 {
86 switch( $functionName )
87 {
88 case $this->SetName:
89 case $this->DefaultName:
90 case $this->LetName:
91 {
92 $scope = eZTemplate::NAMESPACE_SCOPE_RELATIVE;
93 if ( isset( $parameters['-scope'] ) )
94 {
95 if ( !eZTemplateNodeTool::isConstantElement( $parameters['-scope'] ) )
96 return false;
97 $scopeText = eZTemplateNodeTool::elementConstantValue( $parameters['-scope'] );
98 if ( $scopeText == 'relative' )
99 $scope = eZTemplate::NAMESPACE_SCOPE_RELATIVE;
100 else if ( $scopeText == 'root' )
101 $scope = eZTemplate::NAMESPACE_SCOPE_LOCAL;
102 else if ( $scopeText == 'global' )
103 $scope = eZTemplate::NAMESPACE_SCOPE_GLOBAL;
104 }
105
106 $parameters = eZTemplateNodeTool::extractFunctionNodeParameters( $node );
107 $namespaceValue = false;
108 if ( isset( $parameters['-name'] ) )
109 {
110 if ( !eZTemplateNodeTool::isConstantElement( $parameters['-name'] ) )
111 {
112 return false;
113 }
114
115 $namespaceValue = eZTemplateNodeTool::elementConstantValue( $parameters['-name'] );
116 }
117
118 $variableList = array();
119 $setVarNodes = array();
120 foreach ( array_keys( $parameters ) as $parameterName )
121 {
122 if ( $parameterName == '-name' or $parameterName == '-scope' )
123 {
124 continue;
125 }
126
127 $parameterData =& $parameters[$parameterName];
128
129 $setVarNodes[] = eZTemplateNodeTool::createVariableNode(
130 false, $parameterData, eZTemplateNodeTool::extractFunctionNodePlacement( $node ),
131 array(), array( $namespaceValue, $scope, $parameterName ),
132 ( $functionName == $this->SetName ), ( $functionName != $this->DefaultName ),
133 false, ( $functionName == $this->DefaultName ) );
134
135 if ( $functionName == $this->LetName or $functionName == $this->DefaultName )
136 {
137 $variableList[] = $parameterName;
138 }
139 }
140
141 if ( ( $functionName == $this->LetName or $functionName == $this->DefaultName ) and
142 $namespaceValue )
143 {
144 $setVarNodes[] = eZTemplateNodeTool::createNamespaceChangeNode( $namespaceValue );
145 }
146
147 if ( $functionName == $this->LetName or $functionName == $this->DefaultName )
148 {
149 $childNodes = eZTemplateNodeTool::extractFunctionNodeChildren( $node );
150 if ( !is_array( $childNodes ) )
151 {
152 $childNodes = array();
153 }
154 }
155 else
156 {
157 $childNodes = array();
158 }
159
160 $unsetVarNodes = array();
161
162 if ( ( $functionName == $this->LetName or $functionName == $this->DefaultName ) and
163 $namespaceValue )
164 {
165 $unsetVarNodes[] = eZTemplateNodeTool::createNamespaceRestoreNode();
166 }
167
168 if ( $functionName == $this->LetName or $functionName == $this->DefaultName )
169 {
170 foreach( $variableList as $parameterName )
171 {
172 $unsetVarNodes[] = eZTemplateNodeTool::createVariableUnsetNode( array( $namespaceValue,
173 eZTemplate::NAMESPACE_SCOPE_RELATIVE,
174 $parameterName ),
175 array( 'remember_set' => $functionName == $this->DefaultName ) );
176 }
177 }
178
179 return array_merge( $setVarNodes, $childNodes, $unsetVarNodes );
180 } break;
181 }
182
183 }
184
185 function templateHookProcess( $functionName, $functionHookName, $functionHook,
186 $tpl, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
187 {
188 }
189
190 function defineVariables( $tpl, $functionParameters, $functionPlacement, $name, $rootNamespace, &$currentNamespace )
191 {
192 $oldCurrentNamespace = $currentNamespace;
193 $definedVariables = array();
194 foreach ( array_keys( $functionParameters ) as $key )
195 {
196 $item =& $functionParameters[$key];
197 switch ( $key )
198 {
199 case '-name':
200 break;
201
202 default:
203 {
204 if ( !$tpl->hasVariable( $key, $name ) )
205 {
206 $itemValue = $tpl->elementValue( $item, $rootNamespace, $currentNamespace, $functionPlacement );
207 $tpl->setVariable( $key, $itemValue, $name );
208 $definedVariables[] = $key;
209 }
210 else
211 {
212 $varname = $key;
213 if ( $name != '' )
214 $varname = "$name:$varname";
215 $tpl->warning( $this->SetName, "Variable '$varname' already exists, cannot define" );
216 }
217 } break;
218 }
219 }
220 $currentNamespace = $name;
221 return array( $definedVariables,
222 $oldCurrentNamespace );
223 }
224
225 function createDefaultVariables( $tpl, $functionParameters, $functionPlacement, $name, $rootNamespace, &$currentNamespace )
226 {
227 $oldCurrentNamespace = $currentNamespace;
228 $definedVariables = array();
229 foreach ( array_keys( $functionParameters ) as $key )
230 {
231 $item =& $functionParameters[$key];
232 switch ( $key )
233 {
234 case '-name':
235 break;
236
237 default:
238 {
239 if ( !$tpl->hasVariable( $key, $name ) )
240 {
241 $itemValue = $tpl->elementValue( $item, $rootNamespace, $currentNamespace, $functionPlacement );
242 $tpl->setVariable( $key, $itemValue, $name );
243 $definedVariables[] = $key;
244 }
245 } break;
246 }
247 }
248 $currentNamespace = $name;
249 return array( $definedVariables,
250 $oldCurrentNamespace );
251 }
252
253 function cleanupVariables( $tpl, $rootNamespace, &$currentNamespace, $setData )
254 {
255 $definedVariables = $setData[0];
256 foreach ( $definedVariables as $variable )
257 {
258 $tpl->unsetVariable( $variable, $currentNamespace );
259 }
260 $currentNamespace = $setData[1];
261 }
262
266 function process( $tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
267 {
268 if ( $functionName != $this->SetName and
269 $functionName != $this->LetName and
270 $functionName != $this->DefaultName )
271 return null;
272
273 $children = $functionChildren;
274 $parameters = $functionParameters;
275
276 $scope = eZTemplateSetFunction::SCOPE_RELATIVE;
277 if ( isset( $parameters['-scope'] ) )
278 {
279 $scopeText = $tpl->elementValue( $parameters['-scope'], $rootNamespace, $currentNamespace, $functionPlacement );
280 if ( $scopeText == 'relative' )
281 $scope = eZTemplateSetFunction::SCOPE_RELATIVE;
282 else if ( $scopeText == 'root' )
283 $scope = eZTemplateSetFunction::SCOPE_ROOT;
284 else if ( $scopeText == 'global' )
285 $scope = eZTemplateSetFunction::SCOPE_GLOBAL;
286 else
287 $tpl->warning( $functionName, "Scope value '$scopeText' is not valid, use either 'relative', 'root' or 'global'" );
288 }
289
290 $name = null;
291 if ( isset( $parameters['-name'] ) )
292 $name = $tpl->elementValue( $parameters['-name'], $rootNamespace, $currentNamespace, $functionPlacement );
293 if ( $name === null )
294 {
295 if ( $scope == eZTemplateSetFunction::SCOPE_RELATIVE )
296 $name = $currentNamespace;
297 else if ( $scope == eZTemplateSetFunction::SCOPE_ROOT )
298 $name = $rootNamespace;
299 else
300 $name = '';
301 }
302 else
303 {
304 if ( $scope == eZTemplateSetFunction::SCOPE_RELATIVE and
305 $currentNamespace != '' )
306 $name = "$currentNamespace:$name";
307 else if ( $scope == eZTemplateSetFunction::SCOPE_ROOT and
308 $rootNamespace != '' )
309 $name = "$rootNamespace:$name";
310 }
311
312 $definedVariables = array();
313 if ( $functionName == $this->SetName )
314 {
315 foreach ( array_keys( $functionParameters ) as $key )
316 {
317 $item =& $functionParameters[$key];
318 switch ( $key )
319 {
320 case '-name':
321 case '-scope':
322 break;
323
324 default:
325 {
326 if ( $tpl->hasVariable( $key, $name ) )
327 {
328 unset( $itemValue );
329 $itemValue = $tpl->elementValue( $item, $rootNamespace, $currentNamespace, $functionPlacement );
330 $tpl->setVariable( $key, $itemValue, $name );
331 }
332 else
333 {
334 $varname = $key;
335 if ( $name != '' )
336 $varname = "$name:$varname";
337 $tpl->warning( $functionName, "Variable '$varname' doesn't exist, cannot set" );
338 }
339 } break;
340 }
341 }
342 }
343 else if ( $functionName == $this->DefaultName )
344 {
345 $definedVariables = eZTemplateSetFunction::createDefaultVariables( $tpl, $functionParameters, $functionPlacement, $name, $rootNamespace, $currentNamespace );
346 }
347 else
348 {
349 $definedVariables = eZTemplateSetFunction::defineVariables( $tpl, $functionParameters, $functionPlacement, $name, $rootNamespace, $currentNamespace );
350 }
351 if ( $functionName == $this->LetName or
352 $functionName == $this->DefaultName )
353 {
354 if ( is_array( $functionChildren ) )
355 {
356 foreach ( array_keys( $functionChildren ) as $childKey )
357 {
358 $child =& $functionChildren[$childKey];
359 $tpl->processNode( $child, $textElements, $rootNamespace, $name );
360 }
361 }
362 eZTemplateSetFunction::cleanupVariables( $tpl, $rootNamespace, $currentNamespace, $definedVariables );
363 }
364 }
365
369 function hasChildren()
370 {
371 return array( $this->SetName => false,
372 $this->LetName => true,
373 $this->DefaultName => true );
374 }
375
377 public $SetName;
378 public $LetName;
379 public $DefaultName;
380 }
381
382 ?>
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\createVariableUnsetNode
static createVariableUnsetNode( $variableName, $parameters=array())
Definition: eztemplatenodetool.php:462
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\extractFunctionNodeParameters
static extractFunctionNodeParameters(&$node)
Definition: eztemplatenodetool.php:678
eZTemplateNodeTool\extractFunctionNodeChildren
static extractFunctionNodeChildren(&$node)
Definition: eztemplatenodetool.php:669
eZTemplateNodeTool\isConstantElement
static isConstantElement( $elements)
Definition: eztemplatenodetool.php:233
eZTemplateSetFunction
Sets template variables code using function 'set'.
Definition: eztemplatesetfunction.php:37
eZTemplateSetFunction\$DefaultName
$DefaultName
Definition: eztemplatesetfunction.php:379
eZTemplateSetFunction\SCOPE_RELATIVE
const SCOPE_RELATIVE
Definition: eztemplatesetfunction.php:38
eZTemplateSetFunction\hasChildren
hasChildren()
Definition: eztemplatesetfunction.php:369
eZTemplateSetFunction\createDefaultVariables
createDefaultVariables( $tpl, $functionParameters, $functionPlacement, $name, $rootNamespace, &$currentNamespace)
Definition: eztemplatesetfunction.php:225
eZTemplateSetFunction\SCOPE_ROOT
const SCOPE_ROOT
Definition: eztemplatesetfunction.php:39
eZTemplateSetFunction\templateHookProcess
templateHookProcess( $functionName, $functionHookName, $functionHook, $tpl, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace)
Definition: eztemplatesetfunction.php:185
eZTemplateSetFunction\SCOPE_GLOBAL
const SCOPE_GLOBAL
Definition: eztemplatesetfunction.php:40
eZTemplateSetFunction\$LetName
$LetName
Definition: eztemplatesetfunction.php:378
eZTemplateSetFunction\functionList
functionList()
Definition: eztemplatesetfunction.php:59
eZTemplateSetFunction\functionTemplateHints
functionTemplateHints()
Definition: eztemplatesetfunction.php:64
eZTemplateSetFunction\templateNodeTransformation
templateNodeTransformation( $functionName, &$node, $tpl, $parameters, $privateData)
Definition: eztemplatesetfunction.php:83
eZTemplateSetFunction\process
process( $tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace)
Definition: eztemplatesetfunction.php:266
eZTemplateSetFunction\$SetName
$SetName
The name of the set function.
Definition: eztemplatesetfunction.php:377
eZTemplateSetFunction\defineVariables
defineVariables( $tpl, $functionParameters, $functionPlacement, $name, $rootNamespace, &$currentNamespace)
Definition: eztemplatesetfunction.php:190
eZTemplateSetFunction\cleanupVariables
cleanupVariables( $tpl, $rootNamespace, &$currentNamespace, $setData)
Definition: eztemplatesetfunction.php:253
eZTemplateSetFunction\__construct
__construct( $setName='set', $letName='let', $defaultName='default')
Definition: eztemplatesetfunction.php:49
eZTemplate\NAMESPACE_SCOPE_RELATIVE
const NAMESPACE_SCOPE_RELATIVE
Definition: eztemplate.php:286
eZTemplate\NAMESPACE_SCOPE_GLOBAL
const NAMESPACE_SCOPE_GLOBAL
Definition: eztemplate.php:284
eZTemplate\NAMESPACE_SCOPE_LOCAL
const NAMESPACE_SCOPE_LOCAL
Definition: eztemplate.php:285
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.