00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 define( 'EZ_TEMPLATE_SET_SCOPE_RELATIVE', 1 );
00057 define( 'EZ_TEMPLATE_SET_SCOPE_ROOT', 2 );
00058 define( 'EZ_TEMPLATE_SET_SCOPE_GLOBAL', 3 );
00059
00060 class eZTemplateSetFunction
00061 {
00062
00063
00064
00065 function eZTemplateSetFunction( $setName = 'set', $letName = 'let', $defaultName = 'default' )
00066 {
00067 $this->SetName = $setName;
00068 $this->LetName = $letName;
00069 $this->DefaultName = $defaultName;
00070 }
00071
00072
00073
00074
00075 function functionList()
00076 {
00077 return array( $this->SetName, $this->LetName, $this->DefaultName );
00078 }
00079
00080 function functionTemplateStatistics( $functionName, &$node, &$tpl, $resourceData, $namespace, &$stats )
00081 {
00082 $newNamespace = $namespace;
00083 $parameters = eZTemplateNodeTool::extractFunctionNodeParameters( $node );
00084 if ( $functionName == $this->SetName or
00085 $functionName == $this->LetName or
00086 $functionName == $this->DefaultName )
00087 {
00088 if ( isset( $parameters['-name'] ) )
00089 {
00090 $nameData = $parameters['-name'];
00091 $nameDataInspection = eZTemplateCompiler::inspectVariableData( $tpl,
00092 $nameData, false,
00093 $resourceData );
00094 if ( $nameDataInspection['is-constant'] and
00095 !$nameDataInspection['has-operators'] and
00096 !$nameDataInspection['has-attributes'] )
00097 {
00098 $parameterNamespace = $nameDataInspection['new-data'][0][1];
00099 $newNamespace = $tpl->mergeNamespace( $namespace, $parameterNamespace );
00100 }
00101 }
00102 }
00103 if ( $functionName == $this->SetName )
00104 {
00105 foreach ( array_keys( $parameters ) as $name )
00106 {
00107 if ( $name == '-name' )
00108 continue;
00109 $parameter =& $parameters[$name];
00110 eZTemplateCompiler::setVariableStatistics( $stats, $newNamespace, $name, array( 'is_modified' => true ) );
00111 eZTemplateCompiler::calculateVariableNodeStatistics( $tpl, $parameter, false, $resourceData, $namespace, $stats );
00112 }
00113 }
00114 else if ( $functionName == $this->LetName )
00115 {
00116 foreach ( array_keys( $parameters ) as $name )
00117 {
00118 if ( $name == '-name' )
00119 continue;
00120 $parameter =& $parameters[$name];
00121 eZTemplateCompiler::setVariableStatistics( $stats, $newNamespace, $name, array( 'is_created' => true,
00122 'is_removed' => true ) );
00123 eZTemplateCompiler::calculateVariableNodeStatistics( $tpl, $parameter, false, $resourceData, $namespace, $stats );
00124 }
00125 }
00126 else if ( $functionName == $this->DefaultName )
00127 {
00128 foreach ( array_keys( $parameters ) as $name )
00129 {
00130 if ( $name == '-name' )
00131 continue;
00132 $parameter =& $parameters[$name];
00133 eZTemplateCompiler::setVariableStatistics( $stats, $newNamespace, $name, array( ) );
00134 eZTemplateCompiler::calculateVariableNodeStatistics( $tpl, $parameter, false, $resourceData, $namespace, $stats );
00135 }
00136 }
00137 if ( $functionName == $this->LetName or
00138 $functionName == $this->DefaultName )
00139 {
00140 $functionChildren = eZTemplateNodeTool::extractFunctionNodeChildren( $node );
00141 if ( is_array( $functionChildren ) )
00142 {
00143 eZTemplateCompiler::calculateVariableStatisticsChildren( $tpl, $functionChildren, $resourceData, $newNamespace, $stats );
00144 }
00145 }
00146 }
00147
00148
00149 function functionTemplateHints()
00150 {
00151 return array( $this->LetName => array( 'parameters' => true,
00152 'static' => false,
00153 'tree-transformation' => true,
00154 'transform-children' => true,
00155 'transform-parameters' => true ),
00156 $this->SetName => array( 'parameters' => true,
00157 'static' => false,
00158 'tree-transformation' => true,
00159 'transform-children' => true,
00160 'transform-parameters' => true ),
00161 $this->DefaultName => array( 'parameters' => true,
00162 'static' => false,
00163 'tree-transformation' => true,
00164 'transform-children' => true,
00165 'transform-parameters' => true ) );
00166 }
00167
00168 function templateNodeTransformation( $functionName, &$node,
00169 &$tpl, $parameters, $privateData )
00170 {
00171 switch( $functionName )
00172 {
00173 case $this->SetName:
00174 case $this->DefaultName:
00175 case $this->LetName:
00176 {
00177 $scope = EZ_TEMPLATE_NAMESPACE_SCOPE_RELATIVE;
00178 if ( isset( $parameters['-scope'] ) )
00179 {
00180 if ( !eZTemplateNodeTool::isStaticElement( $parameters['-scope'] ) )
00181 return false;
00182 $scopeText = eZTemplateNodeTool::elementStaticValue( $parameters['-scope'] );
00183 if ( $scopeText == 'relative' )
00184 $scope = EZ_TEMPLATE_NAMESPACE_SCOPE_RELATIVE;
00185 else if ( $scopeText == 'root' )
00186 $scope = EZ_TEMPLATE_NAMESPACE_SCOPE_LOCAL;
00187 else if ( $scopeText == 'global' )
00188 $scope = EZ_TEMPLATE_NAMESPACE_SCOPE_GLOBAL;
00189 }
00190
00191 $parameters = eZTemplateNodeTool::extractFunctionNodeParameters( $node );
00192 $namespaceValue = false;
00193 if ( isset( $parameters['-name'] ) )
00194 {
00195 if ( !eZTemplateNodeTool::isStaticElement( $parameters['-name'] ) )
00196 {
00197 return false;
00198 }
00199
00200 $namespaceValue = eZTemplateNodeTool::elementStaticValue( $parameters['-name'] );
00201 }
00202
00203 $variableList = array();
00204 $setVarNodes = array();
00205 foreach ( array_keys( $parameters ) as $parameterName )
00206 {
00207 if ( $parameterName == '-name' or $parameterName == '-scope' )
00208 {
00209 continue;
00210 }
00211
00212 $parameterData =& $parameters[$parameterName];
00213
00214 $setVarNodes[] = eZTemplateNodeTool::createVariableNode(
00215 false, $parameterData, eZTemplateNodeTool::extractFunctionNodePlacement( $node ),
00216 array(), array( $namespaceValue, $scope, $parameterName ),
00217 ( $functionName == $this->SetName ), ( $functionName != $this->DefaultName ),
00218 false, ( $functionName == $this->DefaultName ) );
00219
00220 if ( $functionName == $this->LetName or $functionName == $this->DefaultName )
00221 {
00222 $variableList[] = $parameterName;
00223 }
00224 }
00225
00226 if ( ( $functionName == $this->LetName or $functionName == $this->DefaultName ) and
00227 $namespaceValue )
00228 {
00229 $setVarNodes[] = eZTemplateNodeTool::createNamespaceChangeNode( $namespaceValue );
00230 }
00231
00232 if ( $functionName == $this->LetName or $functionName == $this->DefaultName )
00233 {
00234 $childNodes = eZTemplateNodeTool::extractFunctionNodeChildren( $node );
00235 }
00236 else
00237 {
00238 $childNodes = array();
00239 }
00240
00241 $unsetVarNodes = array();
00242
00243 if ( ( $functionName == $this->LetName or $functionName == $this->DefaultName ) and
00244 $namespaceValue )
00245 {
00246 $unsetVarNodes[] = eZTemplateNodeTool::createNamespaceRestoreNode();
00247 }
00248
00249 if ( $functionName == $this->LetName or $functionName == $this->DefaultName )
00250 {
00251 foreach( $variableList as $parameterName )
00252 {
00253 $unsetVarNodes[] = eZTemplateNodeTool::createVariableUnsetNode( array( $namespaceValue,
00254 EZ_TEMPLATE_NAMESPACE_SCOPE_RELATIVE,
00255 $parameterName ),
00256 array( 'remember_set' => $functionName == $this->DefaultName ) );
00257 }
00258 }
00259
00260 return array_merge( $setVarNodes, $childNodes, $unsetVarNodes );
00261 } break;
00262 }
00263
00264 }
00265
00266 function templateHookProcess( $functionName, $functionHookName, $functionHook,
00267 &$tpl, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
00268 {
00269 }
00270
00271 function defineVariables( &$tpl, $functionParameters, $functionPlacement, $name, $rootNamespace, &$currentNamespace )
00272 {
00273 $oldCurrentNamespace = $currentNamespace;
00274 $definedVariables = array();
00275 foreach ( array_keys( $functionParameters ) as $key )
00276 {
00277 $item =& $functionParameters[$key];
00278 switch ( $key )
00279 {
00280 case '-name':
00281 break;
00282
00283 default:
00284 {
00285 if ( !$tpl->hasVariable( $key, $name ) )
00286 {
00287 $itemValue =& $tpl->elementValue( $item, $rootNamespace, $currentNamespace, $functionPlacement );
00288 $tpl->setVariableRef( $key, $itemValue, $name );
00289 $definedVariables[] = $key;
00290 }
00291 else
00292 {
00293 $varname = $key;
00294 if ( $name != '' )
00295 $varname = "$name:$varname";
00296 $tpl->warning( $this->SetName, "Variable '$varname' already exists, cannot define" );
00297 }
00298 } break;
00299 }
00300 }
00301 $currentNamespace = $name;
00302 return array( $definedVariables,
00303 $oldCurrentNamespace );
00304 }
00305
00306 function createDefaultVariables( &$tpl, $functionParameters, $functionPlacement, $name, $rootNamespace, &$currentNamespace )
00307 {
00308 $oldCurrentNamespace = $currentNamespace;
00309 $definedVariables = array();
00310 foreach ( array_keys( $functionParameters ) as $key )
00311 {
00312 $item =& $functionParameters[$key];
00313 switch ( $key )
00314 {
00315 case '-name':
00316 break;
00317
00318 default:
00319 {
00320 if ( !$tpl->hasVariable( $key, $name ) )
00321 {
00322 $itemValue =& $tpl->elementValue( $item, $rootNamespace, $currentNamespace, $functionPlacement );
00323 $tpl->setVariableRef( $key, $itemValue, $name );
00324 $definedVariables[] = $key;
00325 }
00326 } break;
00327 }
00328 }
00329 $currentNamespace = $name;
00330 return array( $definedVariables,
00331 $oldCurrentNamespace );
00332 }
00333
00334 function cleanupVariables( &$tpl, $rootNamespace, &$currentNamespace, &$setData )
00335 {
00336 $definedVariables =& $setData[0];
00337 foreach ( $definedVariables as $variable )
00338 {
00339 $tpl->unsetVariable( $variable, $currentNamespace );
00340 }
00341 $currentNamespace = $setData[1];
00342 }
00343
00344
00345
00346
00347 function process( &$tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
00348 {
00349 if ( $functionName != $this->SetName and
00350 $functionName != $this->LetName and
00351 $functionName != $this->DefaultName )
00352 return null;
00353
00354 $children = $functionChildren;
00355 $parameters = $functionParameters;
00356
00357 $scope = EZ_TEMPLATE_SET_SCOPE_RELATIVE;
00358 if ( isset( $parameters['-scope'] ) )
00359 {
00360 $scopeText = $tpl->elementValue( $parameters['-scope'], $rootNamespace, $currentNamespace, $functionPlacement );
00361 if ( $scopeText == 'relative' )
00362 $scope = EZ_TEMPLATE_SET_SCOPE_RELATIVE;
00363 else if ( $scopeText == 'root' )
00364 $scope = EZ_TEMPLATE_SET_SCOPE_ROOT;
00365 else if ( $scopeText == 'global' )
00366 $scope = EZ_TEMPLATE_SET_SCOPE_GLOBAL;
00367 else
00368 $tpl->warning( $functionName, "Scope value '$scopeText' is not valid, use either 'relative', 'root' or 'global'" );
00369 }
00370
00371 $name = null;
00372 if ( isset( $parameters['-name'] ) )
00373 $name = $tpl->elementValue( $parameters['-name'], $rootNamespace, $currentNamespace, $functionPlacement );
00374 if ( $name === null )
00375 {
00376 if ( $scope == EZ_TEMPLATE_SET_SCOPE_RELATIVE )
00377 $name = $currentNamespace;
00378 else if ( $scope == EZ_TEMPLATE_SET_SCOPE_ROOT )
00379 $name = $rootNamespace;
00380 else
00381 $name = '';
00382 }
00383 else
00384 {
00385 if ( $scope == EZ_TEMPLATE_SET_SCOPE_RELATIVE and
00386 $currentNamespace != '' )
00387 $name = "$currentNamespace:$name";
00388 else if ( $scope == EZ_TEMPLATE_SET_SCOPE_ROOT and
00389 $rootNamespace != '' )
00390 $name = "$rootNamespace:$name";
00391 }
00392
00393 $definedVariables = array();
00394 if ( $functionName == $this->SetName )
00395 {
00396 foreach ( array_keys( $functionParameters ) as $key )
00397 {
00398 $item =& $functionParameters[$key];
00399 switch ( $key )
00400 {
00401 case '-name':
00402 case '-scope':
00403 break;
00404
00405 default:
00406 {
00407 if ( $tpl->hasVariable( $key, $name ) )
00408 {
00409 unset( $itemValue );
00410 $itemValue = $tpl->elementValue( $item, $rootNamespace, $currentNamespace, $functionPlacement );
00411 $tpl->setVariableRef( $key, $itemValue, $name );
00412 }
00413 else
00414 {
00415 $varname = $key;
00416 if ( $name != '' )
00417 $varname = "$name:$varname";
00418 $tpl->warning( $functionName, "Variable '$varname' doesn't exist, cannot set" );
00419 }
00420 } break;
00421 }
00422 }
00423 }
00424 else if ( $functionName == $this->DefaultName )
00425 {
00426 $definedVariables = eZTemplateSetFunction::createDefaultVariables( $tpl, $functionParameters, $functionPlacement, $name, $rootNamespace, $currentNamespace );
00427 }
00428 else
00429 {
00430 $definedVariables = eZTemplateSetFunction::defineVariables( $tpl, $functionParameters, $functionPlacement, $name, $rootNamespace, $currentNamespace );
00431 }
00432 if ( $functionName == $this->LetName or
00433 $functionName == $this->DefaultName )
00434 {
00435 if ( is_array( $functionChildren ) )
00436 {
00437 foreach ( array_keys( $functionChildren ) as $childKey )
00438 {
00439 $child =& $functionChildren[$childKey];
00440 $tpl->processNode( $child, $textElements, $rootNamespace, $name );
00441 }
00442 }
00443 eZTemplateSetFunction::cleanupVariables( $tpl, $rootNamespace, $currentNamespace, $definedVariables );
00444 }
00445 }
00446
00447
00448
00449
00450 function hasChildren()
00451 {
00452 return array( $this->SetName => false,
00453 $this->LetName => true,
00454 $this->DefaultName => true );
00455 }
00456
00457
00458 var $SetName;
00459 var $LetName;
00460 var $DefaultName;
00461 }
00462
00463 ?>