[ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // Definition of eZTemplateSequenceFunction class 4 // 5 // Created on: <05-Mar-2002 13:55:25 amos> 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 eZTemplateSequenceFunction eztemplatesectionfunction.php 31 \ingroup eZTemplateFunctions 32 \brief Wrapped array looping in templates using function "sequence" 33 34 This class allows for creating arrays which are looped independently 35 of a section. This is useful if you want to create multiple sequences. 36 37 \code 38 // Example of template code 39 {* Init the sequence *} 40 {sequence name=seq loop=array(2,5,7)} 41 42 {* Use it *} 43 {$seq:item} 44 45 {* Iterate it *} 46 {sequence name=seq} 47 48 \endcode 49 */ 50 51 class eZTemplateSequenceFunction 52 { 53 /*! 54 Initializes the function with the function name $inc_name. 55 */ 56 function eZTemplateSequenceFunction() 57 { 58 $this->SequenceName = 'sequence'; 59 } 60 61 /*! 62 Returns an array of the function names, required for eZTemplate::registerFunctions. 63 */ 64 function &functionList() 65 { 66 $functionList = array( $this->SequenceName ); 67 return $functionList; 68 } 69 70 /*! 71 * Returns the array with hits for the template compiler. 72 */ 73 function functionTemplateHints() 74 { 75 return array( $this->SequenceName => array( 'parameters' => true, 76 'static' => false, 77 'transform-parameters' => true, 78 'tree-transformation' => true ) ); 79 } 80 81 function templateNodeSequenceCreate( &$node, &$tpl, $parameters, $nameValue, $loopValue ) 82 { 83 $newNodes = array(); 84 85 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameters['loop'], 86 eZTemplateNodeTool::extractFunctionNodePlacement( $node ), 87 array( 'text-result' => false ), '_array' ); 88 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$GLOBALS['eZTemplateSequence-$nameValue'] = array( 'iteration' => 0, 'index' => 0, 'loop' => \$_array );\n" ); 89 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$tpl->setVariable( 'item', \$GLOBALS['eZTemplateSequence-$nameValue']['loop'][0], '$nameValue' );\n" ); 90 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$tpl->setVariable( 'iteration', 0, '$nameValue' );\n" ); 91 $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( '_array' ); 92 93 return $newNodes; 94 } 95 96 function templateNodeSequenceIterate( &$node, &$tpl, $parameters, $nameValue ) 97 { 98 $newNodes = array(); 99 100 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$_seq_var = &\$GLOBALS['eZTemplateSequence-$nameValue'];\n" ); 101 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "++\$_seq_var['index'];\n++\$_seq_var['iteration'];" ); 102 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( \$_seq_var['index'] >= count( \$_seq_var['loop'] ) )\n\t\$_seq_var['index'] = 0;\n" ); 103 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$tpl->setVariable( 'item', \$_seq_var['loop'][\$_seq_var['index']], '$nameValue' );\n" ); 104 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$tpl->setVariable( 'iteration', \$_seq_var['iteration'], '$nameValue' );\n" ); 105 106 return $newNodes; 107 } 108 109 function templateNodeTransformation( $functionName, &$node, 110 &$tpl, $parameters, $privateData ) 111 { 112 $newNodes = array(); 113 $namespaceValue = false; 114 $varName = 'match'; 115 116 if ( !isset( $parameters['name'] ) ) 117 return false; 118 if ( !eZTemplateNodeTool::isStaticElement( $parameters['name'] ) ) 119 return false; 120 121 $nameData = $parameters['name']; 122 $nameValue = eZTemplateNodeTool::elementStaticValue( $nameData ); 123 124 if ( isset( $parameters['loop'] ) ) 125 { 126 $loopData = $parameters['loop']; 127 if ( !eZTemplateNodeTool::isStaticElement( $loopData ) ) 128 return false; 129 $loopValue = eZTemplateNodeTool::elementStaticValue( $loopData ); 130 131 $newNodes = $this->templateNodeSequenceCreate( $node, $tpl, $parameters, $nameValue, $loopValue ); 132 } 133 else 134 { 135 $newNodes = $this->templateNodeSequenceIterate( $node, $tpl, $parameters, $nameValue ); 136 } 137 138 return $newNodes; 139 } 140 141 /*! 142 Either initializes the sequence or iterates it. 143 */ 144 function process( &$tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace ) 145 { 146 $params = $functionParameters; 147 $loop = null; 148 if ( isset( $params["loop"] ) ) 149 { 150 $loop = $tpl->elementValue( $params["loop"], $rootNamespace, $currentNamespace, $functionPlacement ); 151 } 152 if ( $loop !== null and !is_array( $loop ) ) 153 { 154 $tpl->error( $functionName, "Can only loop arrays", $functionPlacement ); 155 return; 156 } 157 158 $name = ""; 159 if ( !isset( $params["name"] ) ) 160 { 161 $tpl->missingParameter( $functionName, "name" ); 162 return; 163 } 164 $name = $tpl->elementValue( $params["name"], $rootNamespace, $currentNamespace, $functionPlacement ); 165 166 $seq_var =& $GLOBALS["eZTemplateSequence-$name"]; 167 if ( !is_array( $seq_var ) ) 168 $seq_var = array(); 169 if ( $loop !== null ) 170 { 171 $seq_var["loop"] = $loop; 172 $seq_var["iteration"] = 0; 173 $seq_var["index"] = 0; 174 } 175 else 176 { 177 $index =& $seq_var["index"]; 178 $iteration =& $seq_var["iteration"]; 179 ++$iteration; 180 ++$index; 181 if ( $index >= count( $seq_var["loop"] ) ) 182 $index = 0 ; 183 } 184 $tpl->setVariable( "item", $seq_var["loop"][$seq_var["index"]], $name ); 185 $tpl->setVariable( "iteration", $seq_var["iteration"], $name ); 186 } 187 188 /*! 189 Returns false, telling the template parser that this is a single tag. 190 */ 191 function hasChildren() 192 { 193 return false; 194 } 195 196 /// Name of sequence function 197 var $SequenceName; 198 } 199 200 ?>
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 |