[ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // Definition of eZTemplateForFunction class 4 // 5 // Created on: <21-Feb-2005 12:38:26 vs> 6 // 7 // SOFTWARE NAME: eZ publish 8 // SOFTWARE RELEASE: 3.9.0 9 // BUILD VERSION: 17785 10 // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS 11 // SOFTWARE LICENSE: GNU General Public License v2.0 12 // NOTICE: > 13 // This program is free software; you can redistribute it and/or 14 // modify it under the terms of version 2.0 of the GNU General 15 // Public License as published by the Free Software Foundation. 16 // 17 // This program is distributed in the hope that it will be useful, 18 // but WITHOUT ANY WARRANTY; without even the implied warranty of 19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 // GNU General Public License for more details. 21 // 22 // You should have received a copy of version 2.0 of the GNU General 23 // Public License along with this program; if not, write to the Free 24 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 25 // MA 02110-1301, USA. 26 // 27 // 28 29 /*! 30 \class eZTemplateForFunction eztemplateforfunction.php 31 \ingroup eZTemplateFunctions 32 \brief FOR loop 33 34 Syntax: 35 \code 36 {for <number> to <number> as $itemVar [sequence <array> as $seqVar]} 37 [{delimiter}...{/delimiter}] 38 [{break}] 39 [{continue}] 40 [{skip}] 41 {/for} 42 \endcode 43 44 Examples: 45 \code 46 {for 1 to 5 as $i} 47 i: {$i}<br/> 48 {/for} 49 50 {for 5 to 1 as $i} 51 i: {$i}<br/> 52 {/for} 53 \endcode 54 */ 55 56 define ( 'EZ_TEMPLATE_FOR_FUNCTION_NAME', 'for' ); 57 class eZTemplateForFunction 58 { 59 /*! 60 * Returns an array of the function names, required for eZTemplate::registerFunctions. 61 */ 62 function &functionList() 63 { 64 $functionList = array( EZ_TEMPLATE_FOR_FUNCTION_NAME ); 65 return $functionList; 66 } 67 68 /*! 69 * Returns the attribute list. 70 * key: parameter name 71 * value: can have children 72 */ 73 function attributeList() 74 { 75 return array( 'delimiter' => true, 76 'break' => false, 77 'continue' => false, 78 'skip' => false ); 79 } 80 81 82 /*! 83 * Returns the array with hits for the template compiler. 84 */ 85 function functionTemplateHints() 86 { 87 return array( EZ_TEMPLATE_FOR_FUNCTION_NAME => array( 'parameters' => true, 88 'static' => false, 89 'transform-parameters' => true, 90 'tree-transformation' => true ) ); 91 } 92 93 /*! 94 * Compiles the function and its children into PHP code. 95 */ 96 function templateNodeTransformation( $functionName, &$node, 97 &$tpl, $parameters, $privateData ) 98 { 99 // {for <first_val> to <last_val> as $<loop_var> [sequence <sequence_array> as $<sequence_var>]} 100 101 $newNodes = array(); 102 $tpl->ForCounter++; 103 $nodePlacement = eZTemplateNodeTool::extractFunctionNodePlacement( $node ); 104 $uniqid = md5( $nodePlacement[2] ) . "_" . $tpl->ForCounter; 105 106 require_once ( 'lib/eztemplate/classes/eztemplatecompiledloop.php' ); 107 $loop = new eZTemplateCompiledLoop( EZ_TEMPLATE_FOR_FUNCTION_NAME, 108 $newNodes, $parameters, $nodePlacement, $uniqid, 109 $node, $tpl, $privateData ); 110 111 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "// for begins" ); 112 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameters['first_val'], $nodePlacement, array( 'treat-value-as-non-object' => true ), "for_firstval_$uniqid" ); 113 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameters['last_val'], $nodePlacement, array( 'treat-value-as-non-object' => true ), "for_lastval_$uniqid" ); 114 115 $loop->initVars(); 116 117 // loop header 118 $modifyLoopCounterCode = "\$for_firstval_$uniqid < \$for_lastval_$uniqid ? \$for_i_$uniqid}++ : \$for_i_$uniqid}--"; // . ";\n"; 119 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "for ( \$for_i_$uniqid = \$for_firstval_$uniqid ; ; $modifyLoopCounterCode )\n{" ); 120 $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode(); 121 // Check for index 122 $indexArray = isset( $parameters['loop_var'][0][1] ) ? $parameters['loop_var'][0][1] : array( "", 2, "default_index_$uniqid" ); 123 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, "for_i_$uniqid", $nodePlacement, 124 array( 'text-result' => true ), $indexArray, false, true, true ); 125 126 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( !( \$for_firstval_$uniqid < \$for_lastval_$uniqid ? " . 127 "\$for_i_$uniqid <= \$for_lastval_$uniqid : " . 128 "\$for_i_$uniqid >= \$for_lastval_$uniqid ) )\n" . 129 " break;\n" ); 130 131 $loop->processBody(); 132 133 // loop footer 134 $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode(); 135 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "} // for" ); 136 $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( $indexArray ); 137 $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( "for_firstval_$uniqid" ); 138 $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( "for_lastval_$uniqid" ); 139 $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( "for_i_$uniqid" ); 140 $loop->cleanup(); 141 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "// for ends\n" ); 142 143 return $newNodes; 144 } 145 146 /*! 147 * Actually executes the function and its children (in processed mode). 148 */ 149 function process( &$tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace ) 150 { 151 /* 152 * Check function parameters 153 */ 154 155 require_once ( 'lib/eztemplate/classes/eztemplateloop.php' ); 156 $loop = new eZTemplateLoop( EZ_TEMPLATE_FOR_FUNCTION_NAME, 157 $functionParameters, $functionChildren, $functionPlacement, 158 $tpl, $textElements, $rootNamespace, $currentNamespace ); 159 160 if ( !$loop->initialized() ) 161 return; 162 163 $loop->parseScalarParamValue( 'first_val', $firstVal, $firstValIsProxy ); 164 $loop->parseScalarParamValue( 'last_val', $lastVal, $lastValIsProxy ); 165 166 if ( $firstValIsProxy || $lastValIsProxy ) 167 { 168 $tpl->error( EZ_TEMPLATE_FOR_FUNCTION_NAME, 169 "Proxy objects ({section} loop iterators) cannot be used to specify the range \n" . 170 "(this will lead to indefinite loops in compiled mode).\n" . 171 "Please explicitly dereference the proxy object like this: \$current_node.item." ); 172 return; 173 } 174 175 $loop->parseParamVarName( 'loop_var' , $loopVarName ); 176 177 if ( is_null( $firstVal ) || is_null( $lastVal ) || !$loopVarName ) 178 { 179 $tpl->error( EZ_TEMPLATE_FOR_FUNCTION_NAME, "Wrong arguments passed." ); 180 return; 181 } 182 183 if ( !is_numeric( $firstVal ) || !is_numeric( $lastVal ) ) 184 { 185 $tpl->error( EZ_TEMPLATE_FOR_FUNCTION_NAME, "Both 'from' and 'to' values can only be numeric." ); 186 return; 187 } 188 189 $loop->initLoopVariable( $loopVarName ); 190 191 /* 192 * Everything is ok, run the 'for' loop itself 193 */ 194 for ( $i = $firstVal; $firstVal < $lastVal ? $i <= $lastVal : $i >= $lastVal; ) 195 { 196 // set loop variable 197 $tpl->setVariable( $loopVarName, $i, $rootNamespace ); 198 199 $loop->setSequenceVar(); // set sequence variable (if specified) 200 $loop->processDelimiter(); 201 $loop->resetIteration(); 202 203 if ( $loop->processChildren() ) 204 break; 205 206 // increment loop variable here for delimiter to be processed correctly 207 $firstVal < $lastVal ? $i++ : $i--; 208 209 $loop->incrementSequence(); 210 } // for 211 212 $loop->cleanup(); 213 } 214 215 /*! 216 * Returns true, telling the template parser that the function can have children. 217 */ 218 function hasChildren() 219 { 220 return true; 221 } 222 } 223 224 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |