Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

eztemplateblockfunction.php
Go to the documentation of this file.
1 <?php
49 class eZTemplateBlockFunction
50 {
51 const SCOPE_RELATIVE = 1;
52 const SCOPE_ROOT = 2;
53 const SCOPE_GLOBAL = 3;
54
60 public function __construct( $blockName = 'set-block',
61 $appendBlockName = 'append-block',
62 $onceName = 'run-once' )
63 {
64 $this->BlockName = $blockName;
65 $this->AppendBlockName = $appendBlockName;
66 $this->OnceName = $onceName;
67 }
68
73 function functionList()
74 {
75 return array( $this->BlockName, $this->AppendBlockName, $this->OnceName );
76 }
77
78 function functionTemplateHints()
79 {
80 return array( $this->BlockName => array( 'parameters' => true,
81 'static' => false,
82 'transform-children' => true,
83 'tree-transformation' => true,
84 'transform-parameters' => true ),
85 $this->AppendBlockName => array( 'parameters' => true,
86 'static' => false,
87 'transform-children' => true,
88 'tree-transformation' => true,
89 'transform-parameters' => true ),
90 $this->OnceName => array( 'parameters' => false,
91 'static' => false,
92 'transform-children' => true,
93 'tree-transformation' => true ) );
94 }
95
96 function templateNodeTransformation( $functionName, &$node,
97 $tpl, $parameters, $privateData )
98 {
99 if ( $functionName == $this->BlockName or
100 $functionName == $this->AppendBlockName )
101 {
102 if ( !isset( $parameters['variable'] ) )
103 return false;
104
105 $scope = eZTemplate::NAMESPACE_SCOPE_RELATIVE;
106 if ( isset( $parameters['scope'] ) )
107 {
108 if ( !eZTemplateNodeTool::isConstantElement( $parameters['scope'] ) )
109 return false;
110 $scopeText = eZTemplateNodeTool::elementConstantValue( $parameters['scope'] );
111 if ( $scopeText == 'relative' )
112 $scope = eZTemplate::NAMESPACE_SCOPE_RELATIVE;
113 else if ( $scopeText == 'root' )
114 $scope = eZTemplate::NAMESPACE_SCOPE_LOCAL;
115 else if ( $scopeText == 'global' )
116 $scope = eZTemplate::NAMESPACE_SCOPE_GLOBAL;
117 }
118
119 $name = '';
120 if ( isset( $parameters['name'] ) )
121 {
122 if ( !eZTemplateNodeTool::isConstantElement( $parameters['name'] ) )
123 return false;
124 $name = eZTemplateNodeTool::elementConstantValue( $parameters['name'] );
125 }
126 $variableName = eZTemplateNodeTool::elementConstantValue( $parameters['variable'] );
127
128 $newNodes = array();
129
130 $children = eZTemplateNodeTool::extractFunctionNodeChildren( $node );
131
132 $newNodes[] = eZTemplateNodeTool::createOutputVariableIncreaseNode();
133 if ( is_array( $children ) )
134 {
135 $newNodes = array_merge( $newNodes, $children );
136 }
137 $newNodes[] = eZTemplateNodeTool::createAssignFromOutputVariableNode( 'blockText' );
138 if ( $functionName == $this->AppendBlockName )
139 {
140 $data = array( eZTemplateNodeTool::createVariableElement( $variableName, $name, $scope ) );
141 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $data, false, array(),
142 'blockData' );
143
144 // This block checks whether the append-block variable is an array or not.
145 // TODO: This is a temporary solution and should also check whether the template variable exists.
146 // This new solution requires probably writing the createVariableElement and createVariableNode your self.
147 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( is_null ( \$blockData ) ) \$blockData = array();" );
148 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( is_array ( \$blockData ) ) \$blockData[] = \$blockText;" );
149 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "else eZDebug::writeError( \"Variable '$variableName' is already in use.\" );" );
150 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, 'blockData', false, array(),
151 array( $name, $scope, $variableName ), false, true, true );
152 $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( 'blockData' );
153 }
154 else
155 {
156 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, 'blockText', false, array(),
157 array( $name, $scope, $variableName ), false, true, true );
158 }
159 $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( 'blockText' );
160 $newNodes[] = eZTemplateNodeTool::createOutputVariableDecreaseNode();
161
162 return $newNodes;
163 }
164 else if ( $functionName == $this->OnceName )
165 {
166 $functionPlacement = eZTemplateNodeTool::extractFunctionNodePlacement( $node );
167 $key = $this->placementKey( $functionPlacement );
168 $newNodes = array();
169 if ( $key !== false )
170 {
171 $keyText = eZPHPCreator::variableText( $key, 0, 0, false );
172 $placementText = eZPHPCreator::variableText( $functionPlacement, 0, 0, false );
173 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( !isset( \$GLOBALS['eZTemplateRunOnceKeys'][$keyText] ) )\n" .
174 "{\n" .
175 " \$GLOBALS['eZTemplateRunOnceKeys'][$keyText] = $placementText;" );
176 $children = eZTemplateNodeTool::extractFunctionNodeChildren( $node );
177 $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode( 4 );
178 $newNodes = array_merge( $newNodes, $children );
179 $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode( 4 );
180 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}" );
181 }
182 return $newNodes;
183 }
184 return false;
185 }
186
190 function process( $tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
191 {
192 switch ( $functionName )
193 {
194 case $this->BlockName:
195 case $this->AppendBlockName:
196 {
197 $children = $functionChildren;
198 $parameters = $functionParameters;
199
200 $scope = eZTemplateBlockFunction::SCOPE_RELATIVE;
201 if ( isset( $parameters["scope"] ) )
202 {
203 $scopeText = $tpl->elementValue( $parameters["scope"], $rootNamespace, $currentNamespace, $functionPlacement );
204 if ( $scopeText == 'relative' )
205 $scope = eZTemplateBlockFunction::SCOPE_RELATIVE;
206 else if ( $scopeText == 'root' )
207 $scope = eZTemplateBlockFunction::SCOPE_ROOT;
208 else if ( $scopeText == 'global' )
209 $scope = eZTemplateBlockFunction::SCOPE_GLOBAL;
210 else
211 $tpl->warning( $functionName, "Scope value '$scopeText' is not valid, use either 'relative', 'root' or 'global'", $functionPlacement );
212 }
213
214 $name = null;
215 if ( isset( $parameters["name"] ) )
216 $name = $tpl->elementValue( $parameters["name"], $rootNamespace, $currentNamespace, $functionPlacement );
217 if ( $name === null )
218 {
219 if ( $scope == eZTemplateBlockFunction::SCOPE_RELATIVE )
220 $name = $currentNamespace;
221 else if ( $scope == eZTemplateBlockFunction::SCOPE_ROOT )
222 $name = $rootNamespace;
223 else
224 $name = '';
225 }
226 else
227 {
228 if ( $scope == eZTemplateBlockFunction::SCOPE_RELATIVE and
229 $currentNamespace != '' )
230 $name = "$currentNamespace:$name";
231 else if ( $scope == eZTemplateBlockFunction::SCOPE_ROOT and
232 $rootNamespace != '' )
233 $name = "$rootNamespace:$name";
234 }
235 $variableItem = null;
236 if ( isset( $parameters["variable"] ) )
237 {
238 $hasLoopItemParameter = true;
239 $variableItem = $tpl->elementValue( $parameters["variable"], $rootNamespace, $currentNamespace, $functionPlacement );
240 }
241 else
242 {
243 $tpl->missingParameter( $functionName, 'variable' );
244 return;
245 }
246
247 $childTextElements = array();
248 if ( is_array( $children ) )
249 {
250 foreach ( array_keys( $children ) as $childKey )
251 {
252 $child =& $children[$childKey];
253 $tpl->processNode( $child, $childTextElements, $rootNamespace, $name );
254 }
255 }
256 $text = implode( '', $childTextElements );
257 if ( $functionName == $this->AppendBlockName )
258 {
259 $textArray = array();
260 if ( $tpl->hasVariable( $variableItem, $name ) )
261 {
262 $textArray = $tpl->variable( $variableItem, $name );
263 if ( !is_array( $textArray ) )
264 {
265 $tpl->warning( $functionName, "Variable '$variableItem' is already in use.", $functionPlacement );
266 return;
267 }
268 }
269 $textArray[] = $text;
270 $tpl->setVariable( $variableItem, $textArray, $name );
271 }
272 else
273 $tpl->setVariable( $variableItem, $text, $name );
274 } break;
275
276 case $this->OnceName:
277 {
278 $key = $this->placementKey( $functionPlacement );
279 if ( $key !== false and !$this->hasPlacementKey( $key ) )
280 {
281 $this->registerPlacementKey( $key, $functionPlacement );
282
283 if ( is_array( $functionChildren ) )
284 {
285 foreach ( array_keys( $functionChildren ) as $childKey )
286 {
287 $child =& $functionChildren[$childKey];
288 $tpl->processNode( $child, $textElements, $rootNamespace, $currentNamespace );
289 }
290 }
291 }
292 } break;
293 }
294 }
295
296 function resetFunction( $functionName )
297 {
298 if ( $functionName == $this->OnceName )
299 {
300 unset( $GLOBALS['eZTemplateRunOnceKeys'] );
301 }
302 }
303
308 function placementKey( $placement )
309 {
310 if ( isset( $placement[0] ) and
311 isset( $placement[1] ) and
312 isset( $placement[2] ) )
313 {
314 $input = $placement[0][0] . ',' . $placement[0][1] . "\n";
315 $input .= $placement[1][0] . ',' . $placement[1][1] . "\n";
316 $input .= $placement[2];
317 return md5( $input );
318 }
319 return false;
320 }
321
325 function hasPlacementKey( $key )
326 {
327 return isset( $GLOBALS['eZTemplateRunOnceKeys'][$key] );
328 }
329
333 function registerPlacementKey( $key, $placement )
334 {
335 return $GLOBALS['eZTemplateRunOnceKeys'][$key] = $placement;
336 }
337
341 function hasChildren()
342 {
343 return true;
344 }
345
348 public $BlockName;
349 public $AppendBlockName;
350 public $OnceName;
351 }
352
353 ?>
eZTemplateBlockFunction
Advanced block handling.
Definition: eztemplateblockfunction.php:50
eZTemplateBlockFunction\hasChildren
hasChildren()
Definition: eztemplateblockfunction.php:341
eZTemplateBlockFunction\$BlockName
$BlockName
Definition: eztemplateblockfunction.php:348
eZTemplateBlockFunction\SCOPE_ROOT
const SCOPE_ROOT
Definition: eztemplateblockfunction.php:52
eZTemplateBlockFunction\$AppendBlockName
$AppendBlockName
Definition: eztemplateblockfunction.php:349
eZTemplateBlockFunction\registerPlacementKey
registerPlacementKey( $key, $placement)
Definition: eztemplateblockfunction.php:333
eZTemplateBlockFunction\hasPlacementKey
hasPlacementKey( $key)
Definition: eztemplateblockfunction.php:325
eZTemplateBlockFunction\functionTemplateHints
functionTemplateHints()
Definition: eztemplateblockfunction.php:78
eZTemplateBlockFunction\resetFunction
resetFunction( $functionName)
Definition: eztemplateblockfunction.php:296
eZTemplateBlockFunction\$OnceName
$OnceName
Definition: eztemplateblockfunction.php:350
eZTemplateBlockFunction\functionList
functionList()
Definition: eztemplateblockfunction.php:73
eZTemplateBlockFunction\__construct
__construct( $blockName='set-block', $appendBlockName='append-block', $onceName='run-once')
Definition: eztemplateblockfunction.php:60
eZTemplateBlockFunction\templateNodeTransformation
templateNodeTransformation( $functionName, &$node, $tpl, $parameters, $privateData)
Definition: eztemplateblockfunction.php:96
eZTemplateBlockFunction\SCOPE_RELATIVE
const SCOPE_RELATIVE
Definition: eztemplateblockfunction.php:51
eZTemplateBlockFunction\process
process( $tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace)
Definition: eztemplateblockfunction.php:190
eZTemplateBlockFunction\placementKey
placementKey( $placement)
Definition: eztemplateblockfunction.php:308
eZTemplateBlockFunction\SCOPE_GLOBAL
const SCOPE_GLOBAL
Definition: eztemplateblockfunction.php:53
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\createOutputVariableIncreaseNode
static createOutputVariableIncreaseNode( $parameters=array())
Definition: eztemplatenodetool.php:493
eZTemplateNodeTool\extractFunctionNodePlacement
static extractFunctionNodePlacement(&$node)
Definition: eztemplatenodetool.php:660
eZTemplateNodeTool\createOutputVariableDecreaseNode
static createOutputVariableDecreaseNode( $parameters=array())
Definition: eztemplatenodetool.php:498
eZTemplateNodeTool\createVariableElement
static createVariableElement( $variableName, $namespaceName, $namespaceScope=eZTemplate::NAMESPACE_SCOPE_LOCAL, $variablePlacement=false)
Definition: eztemplatenodetool.php:159
eZTemplateNodeTool\createAssignFromOutputVariableNode
static createAssignFromOutputVariableNode( $variableName, $parameters=array(), $assignmentType=eZPHPCreator::VARIABLE_ASSIGNMENT)
Definition: eztemplatenodetool.php:488
eZTemplateNodeTool\extractFunctionNodeChildren
static extractFunctionNodeChildren(&$node)
Definition: eztemplatenodetool.php:669
eZTemplateNodeTool\isConstantElement
static isConstantElement( $elements)
Definition: eztemplatenodetool.php:233
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+.