|
eZ Publish
[4.0]
|
00001 <?php 00002 // 00003 // Definition of eZTemplateDefFunction class 00004 // 00005 // Created on: <28-Feb-2005 16:03:02 vs> 00006 // 00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00008 // SOFTWARE NAME: eZ Publish 00009 // SOFTWARE RELEASE: 4.0.x 00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS 00011 // SOFTWARE LICENSE: GNU General Public License v2.0 00012 // NOTICE: > 00013 // This program is free software; you can redistribute it and/or 00014 // modify it under the terms of version 2.0 of the GNU General 00015 // Public License as published by the Free Software Foundation. 00016 // 00017 // This program is distributed in the hope that it will be useful, 00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 // GNU General Public License for more details. 00021 // 00022 // You should have received a copy of version 2.0 of the GNU General 00023 // Public License along with this program; if not, write to the Free 00024 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00025 // MA 02110-1301, USA. 00026 // 00027 // 00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00029 // 00030 00031 /*! 00032 \class eZTemplateDefFunction eztemplatedeffunction.php 00033 \ingroup eZTemplateFunctions 00034 \brief Allows to define/undefine template variables in any place. 00035 00036 This class allows to execute on of two or more code pieces depending 00037 on a condition. 00038 00039 Syntax: 00040 \code 00041 {def $var1=<value1> [$var2=<value2> ...]} 00042 \endcode 00043 00044 Example: 00045 \code 00046 {def $i=10 $j=20} 00047 {def $s1='hello' $s2='world'} 00048 ... 00049 {set $i=$i+1} 00050 ... 00051 {undef $i} 00052 {undef $s1 $s2} 00053 {undef} 00054 \endcode 00055 */ 00056 00057 class eZTemplateDefFunction 00058 { 00059 const DEF_FUNCTION_NAME = 'def'; 00060 const UNDEF_FUNCTION_NAME = 'undef'; 00061 00062 /*! 00063 * Returns an array of the function names, required for eZTemplate::registerFunctions. 00064 */ 00065 function &functionList() 00066 { 00067 $functionList = array( eZTemplateDefFunction::DEF_FUNCTION_NAME, eZTemplateDefFunction::UNDEF_FUNCTION_NAME ); 00068 return $functionList; 00069 } 00070 00071 /*! 00072 * Returns the attribute list which is 'delimiter', 'elseif' and 'else'. 00073 * key: parameter name 00074 * value: can have children 00075 */ 00076 function attributeList() 00077 { 00078 return array(); 00079 } 00080 00081 00082 /*! 00083 * Returns the array with hits for the template compiler. 00084 */ 00085 function functionTemplateHints() 00086 { 00087 return array( eZTemplateDefFunction::DEF_FUNCTION_NAME => array( 'parameters' => true, 00088 'static' => false, 00089 'transform-parameters' => true, 00090 'tree-transformation' => true ), 00091 eZTemplateDefFunction::UNDEF_FUNCTION_NAME => array( 'parameters' => true, 00092 'static' => false, 00093 'transform-parameters' => true, 00094 'tree-transformation' => true ) ); 00095 } 00096 00097 /*! 00098 * Compiles the function into PHP code. 00099 */ 00100 function templateNodeTransformation( $functionName, &$node, 00101 $tpl, &$parameters, $privateData ) 00102 { 00103 $undef = ( $functionName == 'undef' ); 00104 $newNodes = array(); 00105 00106 if ( !$parameters ) 00107 { 00108 if ( !$undef ) 00109 // prevent execution of the function in processed mode 00110 return array( eZTemplateNodeTool::createCodePieceNode( "// an error occured in $functionName" ) ); 00111 00112 // {undef} called w/o arguments => destroy all local variables 00113 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "// undef all" ); 00114 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$tpl->unsetLocalVariables();" ); 00115 return $newNodes; 00116 } 00117 00118 $nodePlacement = eZTemplateNodeTool::extractFunctionNodePlacement( $node ); 00119 foreach ( array_keys( $parameters ) as $parameterName ) 00120 { 00121 $parameterData = $parameters[$parameterName]; 00122 if ( $undef ) 00123 { 00124 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "// undef \$$parameterName" ); 00125 // generates "$tpl->unsetLocalVariable();" 00126 $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( array( $namespaceValue = false, 00127 $scope = eZTemplate::NAMESPACE_SCOPE_LOCAL, 00128 $parameterName ), 00129 array( 'remember_set' => false, 'local-variable' => true ) ); 00130 } 00131 else 00132 { 00133 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "// def \$$parameterName" ); 00134 // generates "$tpl->setLocalVariable();" 00135 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameterData, $nodePlacement, array( 'local-variable' => true ), 00136 array( $namespaceValue = false, $scope = eZTemplate::NAMESPACE_SCOPE_LOCAL, $parameterName ), 00137 $onlyExisting = false, $overwrite = true, false, $rememberSet = false ); 00138 } 00139 } 00140 00141 return $newNodes; 00142 } 00143 00144 /*! 00145 * Actually executes the function (in processed mode). 00146 */ 00147 function process( $tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace ) 00148 { 00149 $undef = ( $functionName == eZTemplateDefFunction::UNDEF_FUNCTION_NAME ) ? true : false; 00150 00151 if ( $undef && !count( $functionParameters ) ) // if {undef} called w/o arguments 00152 { 00153 // destroy all variables defined in the current template using {def} 00154 $tpl->unsetLocalVariables(); 00155 } 00156 00157 foreach ( array_keys( $functionParameters ) as $key ) 00158 { 00159 $varName = $key; 00160 $param = $functionParameters[$varName]; 00161 $varValue = $tpl->elementValue( $param, $rootNamespace, $currentNamespace, $functionPlacement ); 00162 00163 00164 if ( $undef ) // {undef} 00165 { 00166 if ( !$tpl->hasLocalVariable( $varName, $rootNamespace ) ) 00167 $tpl->warning( eZTemplateDefFunction::UNDEF_FUNCTION_NAME, "Variable '$varName' is not defined with {def}." ); 00168 else 00169 $tpl->unsetLocalVariable( $varName, $rootNamespace ); 00170 00171 } 00172 else // {def} 00173 { 00174 if ( $tpl->hasVariable( $varName, $rootNamespace ) ) // if the variable already exists 00175 { 00176 // we don't create new variable but just assign value to the existing one. 00177 $tpl->warning( eZTemplateDefFunction::DEF_FUNCTION_NAME 00178 . ' in ' . $functionPlacement[2] 00179 . '[' . $functionPlacement[1][0] . ']' 00180 . ':' . $functionPlacement[1][1], 00181 "Variable '$varName' is already defined." ); 00182 $tpl->setVariable( $varName, $varValue, $rootNamespace ); 00183 } 00184 else 00185 // create a new local variable and assign a value to it. 00186 $tpl->setLocalVariable( $varName, $varValue, $rootNamespace ); 00187 00188 } 00189 } 00190 } 00191 00192 /*! 00193 * Returns false, telling the template parser that the function cannot have children. 00194 */ 00195 function hasChildren() 00196 { 00197 return false; 00198 } 00199 } 00200 00201 ?>