| [ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // Definition of eZTemplateCompiledLoop class 4 // 5 // Created on: <17-Mar-2005 11:26:59 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 /*! 31 \class eZTemplateCompiledLoop eztemplatecompiledloop.php 32 \ingroup eZTemplateFunctions 33 \brief Common code for compiling the loop functions 34 */ 35 class eZTemplateCompiledLoop 36 { 37 function eZTemplateCompiledLoop( $name, &$newNodes, &$parameters, &$nodePlacement, $uniqid, 38 &$node, &$tpl, &$privateData ) 39 { 40 $this->Name =& $name; 41 $this->Parameters =& $parameters; 42 $this->NodePlacement =& $nodePlacement; 43 $this->UniqID =& $uniqid; 44 $this->NewNodes =& $newNodes; 45 $this->Node =& $node; 46 $this->Tpl =& $tpl; 47 $this->PrivateData =& $privateData; 48 } 49 50 /*! 51 * Returns true if sequence has been specified for the loop in its parameters. 52 */ 53 function hasSequence() 54 { 55 return isset( $this->Parameters['sequence_var'] ); 56 } 57 58 /*! 59 * Destroys PHP and template variables defined by the loop. 60 */ 61 function cleanup() 62 { 63 if ( $this->hasSequence() ) 64 $this->destroySequenceVars(); 65 } 66 67 /*! 68 \private 69 */ 70 function destroySequenceVars() 71 { 72 $fName = $this->Name; 73 $uniqid = $this->UniqID; 74 $this->NewNodes[] = eZTemplateNodeTool::createVariableUnsetNode( "$fName}_sequence_array_$uniqid" ); 75 $this->NewNodes[] = eZTemplateNodeTool::createVariableUnsetNode( "$fName}_sequence_var_$uniqid" ); 76 $this->NewNodes[] = eZTemplateNodeTool::createVariableUnsetNode( $this->Parameters['sequence_var'][0][1] ); 77 } 78 79 80 /*! 81 * Create PHP and template variables representing sequence specified for the loop. 82 */ 83 function createSequenceVars() 84 { 85 if ( !$this->hasSequence() ) 86 return; 87 88 $fName = $this->Name; 89 $uniqid = $this->UniqID; 90 $this->NewNodes[] = eZTemplateNodeTool::createCodePieceNode( "// creating sequence variables for \{$fName} loop" ); 91 $this->NewNodes[] = eZTemplateNodeTool::createVariableNode( false, 92 $this->Parameters['sequence_array'], 93 $this->NodePlacement, 94 array( 'treat-value-as-non-object' => true, 'text-result' => false ), 95 "$fName}_sequence_array_$uniqid" ); 96 $this->NewNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$$fName}_sequence_var_$uniqid = current( \$$fName}_sequence_array_$uniqid );\n" ); 97 } 98 99 /*! 100 * Export current sequence value to the template variable specified in loop parameters. 101 */ 102 function setCurrentSequenceValue() 103 { 104 if ( !$this->hasSequence() ) 105 return; 106 107 $fName = $this->Name; 108 $uniqid = $this->UniqID; 109 $seqVar = "$fName}_sequence_var_$uniqid"; 110 $this->NewNodes[] = eZTemplateNodeTool::createCodePieceNode( "// setting current sequence value" ); 111 $this->NewNodes[] = eZTemplateNodeTool::createVariableNode( false, $seqVar, $this->NodePlacement, array(), 112 $this->Parameters['sequence_var'][0][1], 113 false, true, true ); 114 } 115 116 /*! 117 * Increments loop sequence. 118 */ 119 function iterateSequence() 120 { 121 if ( !$this->hasSequence() ) 122 return; 123 124 $fName = $this->Name; 125 $uniqid = $this->UniqID; 126 $seqArray = "$fName}_sequence_array_$uniqid"; 127 $seqVar = "$fName}_sequence_var_$uniqid"; 128 $alterSeqValCode = 129 "if ( ( \$$seqVar = next( \$$seqArray ) ) === false )\n" . 130 "{\n" . 131 " reset( \$$seqArray );\n" . 132 " \$$seqVar = current( \$$seqArray );\n" . 133 "}\n"; 134 $this->NewNodes[] = eZTemplateNodeTool::createCodePieceNode( "// sequence iteration" ); 135 $this->NewNodes[] = eZTemplateNodeTool::createCodePieceNode( $alterSeqValCode ); 136 } 137 138 139 /* 140 * Compiles loop children (=code residing between start and end tags of the loop). 141 * Besides, does special handling of {break}, {continue}, {skip} and {delimiter} functions. 142 * \return true if the caller loop should break, false otherwise 143 */ 144 function processChildren() 145 { 146 // process the loop body 147 $children = eZTemplateNodeTool::extractFunctionNodeChildren( $this->Node ); 148 $transformedChildren = eZTemplateCompiler::processNodeTransformationNodes( $this->Tpl, $this->Node, $children, $this->PrivateData ); 149 150 $childrenNodes = array(); 151 $delimiter = null; 152 153 if ( is_array( $transformedChildren ) ) 154 { 155 foreach ( array_keys( $transformedChildren ) as $childKey ) 156 { 157 $child =& $transformedChildren[$childKey]; 158 159 if ( $child[0] == EZ_TEMPLATE_NODE_FUNCTION ) // check child type 160 { 161 $childFunctionName = $child[2]; 162 if ( $childFunctionName == 'delimiter' ) 163 { 164 // save delimiter for it to be processed below 165 $delimiter =& $child; 166 continue; 167 } 168 elseif ( $childFunctionName == 'break' ) 169 { 170 $childrenNodes[] = eZTemplateNodeTool::createCodePieceNode( "break;\n" ); 171 continue; 172 } 173 elseif ( $childFunctionName == 'continue' ) 174 { 175 $childrenNodes[] = eZTemplateNodeTool::createCodePieceNode( "continue;\n" ); 176 continue; 177 } 178 elseif ( $childFunctionName == 'skip' ) 179 { 180 $childrenNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$skipDelimiter = true;\ncontinue;\n" ); 181 continue; 182 } 183 } 184 185 $childrenNodes[] = $child; 186 } 187 } 188 189 if ( $delimiter ) // if delimiter is specified 190 { 191 $delimiterNodes = eZTemplateNodeTool::extractNodes( $children, 192 array( 'match' => array( 'type' => 'equal', 193 'matches' => array( array( 'match-keys' => array( 0 ), 194 'match-with' => EZ_TEMPLATE_NODE_FUNCTION ), 195 array( 'match-keys' => array( 2 ), 196 'match-with' => 'delimiter' ) ) ) ) ); 197 $delimiterNode = false; 198 if ( count( $delimiterNodes ) > 0 ) 199 $delimiterNode = $delimiterNodes[0]; 200 201 $delimiterChildren = eZTemplateNodeTool::extractFunctionNodeChildren( $delimiterNode ); 202 $delimiterParameters = eZTemplateNodeTool::extractFunctionNodeParameters( $delimiterNode ); 203 204 $checkModulo = array(); 205 $checkModuloEnd = array(); 206 $delemiterModuloValue = array(); 207 if ( isset( $delimiterParameters['modulo'] ) ) 208 { 209 switch ( $this->Name ) 210 { 211 case 'foreach': 212 { 213 $delimiterModulo = $delimiterParameters['modulo']; 214 $delimiterModulo = eZTemplateCompiler::processElementTransformationList( $this->Tpl, $delimiterModulo, $delimiterModulo, $this->PrivateData ); 215 // Get unique index 216 $currentIndex = "\$fe_i_$this->UniqID"; 217 218 if ( eZTemplateNodeTool::isStaticElement( $delimiterModulo ) ) 219 { 220 $moduloValue = (int)eZTemplateNodeTool::elementStaticValue( $delimiterModulo ); 221 $matchCode = "( ( $currentIndex ) % $moduloValue ) == 0"; 222 } 223 else 224 { 225 $delemiterModuloValue[] = eZTemplateNodeTool::createVariableNode( false, $delimiterModulo, eZTemplateNodeTool::extractFunctionNodePlacement( $this->Node ), 226 array( 'spacing' => 0 ), 'moduloValue' ); 227 $matchCode = "( ( $currentIndex ) % \$moduloValue ) == 0"; 228 } 229 $checkModulo[] = eZTemplateNodeTool::createCodePieceNode( "if ( $matchCode ) // Check modulo\n{" ); 230 $checkModulo[] = eZTemplateNodeTool::createSpacingIncreaseNode( 4 ); 231 232 $checkModuloEnd[] = eZTemplateNodeTool::createSpacingDecreaseNode( 4 ); 233 $checkModuloEnd[] = eZTemplateNodeTool::createCodePieceNode( "}\n" ); 234 } 235 } 236 } 237 $delimiterNodes = array(); 238 $delimiterNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( \$skipDelimiter )\n" . 239 " \$skipDelimiter = false;\n" . 240 "else\n" . 241 "{ // delimiter begins" ); 242 $delimiterNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode(); 243 if ( is_array( $delimiter[1] ) ) // if delimiter has children 244 { 245 // If modulo is specified 246 $delimiterNodes = array_merge( $delimiterNodes, $checkModulo ); 247 248 foreach ( $delimiter[1] as $delimiterChild ) 249 $delimiterNodes[] = $delimiterChild; 250 251 // Set end of checking for modulo 252 $delimiterNodes = array_merge( $delimiterNodes, $checkModuloEnd ); 253 } 254 255 $delimiterNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode(); 256 $delimiterNodes[] = eZTemplateNodeTool::createCodePieceNode( "} // delimiter ends\n" ); 257 258 // we place its code right before other loop children, 259 // if delemiter and modulo are specified and value of modulo is not static 260 // $delemiterModuloValue is initialization of variable 261 // we should place initialization of moduloValue before checking for delimiter 262 $childrenNodes = array_merge( $delemiterModuloValue, $delimiterNodes, $childrenNodes ); 263 } 264 265 $this->NewNodes = array_merge( $this->NewNodes, $childrenNodes ); 266 } 267 268 /*! 269 * Generates loop body. 270 */ 271 function processBody() 272 { 273 // export current sequence value to the specified template variable <$sequence_var> 274 $this->setCurrentSequenceValue(); 275 276 // process the loop body 277 $this->processChildren(); 278 279 $this->iterateSequence(); 280 } 281 282 /*! 283 * create PHP and template variables needed for the loop. 284 */ 285 function initVars() 286 { 287 // initialize delimiter processing 288 $this->NewNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$skipDelimiter = true;" ); 289 290 // initialize sequence 291 $this->createSequenceVars(); 292 } 293 294 /// 295 /// \privatesection 296 /// 297 var $Name; 298 var $Parameters; 299 var $NodePlacement; 300 var $UniqID; 301 var $NewNodes; 302 var $Node; 303 var $Tpl; 304 var $PrivateData; 305 306 } 307 308 ?>
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 |