eZ Publish  [4.0]
eztemplatesequencefunction.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZTemplateSequenceFunction class
00004 //
00005 // Created on: <05-Mar-2002 13:55:25 amos>
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 eZTemplateSequenceFunction eztemplatesectionfunction.php
00033   \ingroup eZTemplateFunctions
00034   \brief Wrapped array looping in templates using function "sequence"
00035 
00036   This class allows for creating arrays which are looped independently
00037   of a section. This is useful if you want to create multiple sequences.
00038 
00039 \code
00040 // Example of template code
00041 {* Init the sequence *}
00042 {sequence name=seq loop=array(2,5,7)}
00043 
00044 {* Use it *}
00045 {$seq:item}
00046 
00047 {* Iterate it *}
00048 {sequence name=seq}
00049 
00050 \endcode
00051 */
00052 
00053 class eZTemplateSequenceFunction
00054 {
00055     /*!
00056      Initializes the function with the function name $inc_name.
00057     */
00058     function eZTemplateSequenceFunction()
00059     {
00060         $this->SequenceName = 'sequence';
00061     }
00062 
00063     /*!
00064      Returns an array of the function names, required for eZTemplate::registerFunctions.
00065     */
00066     function &functionList()
00067     {
00068         $functionList = array( $this->SequenceName );
00069         return $functionList;
00070     }
00071 
00072     /*!
00073      * Returns the array with hits for the template compiler.
00074      */
00075     function functionTemplateHints()
00076     {
00077         return array( $this->SequenceName => array( 'parameters' => true,
00078                                                     'static' => false,
00079                                                     'transform-parameters' => true,
00080                                                     'tree-transformation' => true ) );
00081     }
00082 
00083     function templateNodeSequenceCreate( &$node, $tpl, $parameters, $nameValue, $loopValue )
00084     {
00085         $newNodes = array();
00086 
00087         $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameters['loop'],
00088                                                               eZTemplateNodeTool::extractFunctionNodePlacement( $node ),
00089                                                               array( 'text-result' => false ), '_array' );
00090         $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$GLOBALS['eZTemplateSequence-$nameValue'] = array( 'iteration' => 0, 'index' => 0, 'loop' => \$_array );\n" );
00091         $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$tpl->setVariable( 'item', \$GLOBALS['eZTemplateSequence-$nameValue']['loop'][0],  \$namespace );\n" );
00092         $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$tpl->setVariable( 'iteration', 0, \$namespace );\n" );
00093         $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( '_array' );
00094 
00095         return $newNodes;
00096     }
00097 
00098     function templateNodeSequenceIterate( &$node, $tpl, $parameters, $nameValue )
00099     {
00100         $newNodes = array();
00101 
00102         $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$_seq_var = &\$GLOBALS['eZTemplateSequence-$nameValue'];\n" );
00103         $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "++\$_seq_var['index'];\n++\$_seq_var['iteration'];" );
00104         $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( \$_seq_var['index'] >= count( \$_seq_var['loop'] ) )\n\t\$_seq_var['index'] = 0;\n" );
00105         $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$tpl->setVariable( 'item', \$_seq_var['loop'][\$_seq_var['index']], \$namespace );\n" );
00106         $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$tpl->setVariable( 'iteration', \$_seq_var['iteration'], \$namespace );\n" );
00107 
00108         return $newNodes;
00109     }
00110 
00111     function templateNodeTransformation( $functionName, &$node,
00112                                          $tpl, $parameters, $privateData )
00113     {
00114         $newNodes = array();
00115         $namespaceValue = false;
00116         $varName = 'match';
00117 
00118         if ( !isset( $parameters['name'] ) )
00119             return false;
00120         if ( !eZTemplateNodeTool::isStaticElement( $parameters['name'] ) )
00121             return false;
00122 
00123         $nameData = $parameters['name'];
00124         $nameValue = eZTemplateNodeTool::elementStaticValue( $nameData );
00125 
00126         $nameSpaceNode = eZTemplateNodeTool::createCodePieceNode( "\$namespace = \$rootNamespace;
00127 if ( \$namespace == '' )
00128     \$namespace = \"$nameValue\";
00129 else
00130     \$namespace .= ':$nameValue';
00131 " );
00132         if ( isset( $parameters['loop'] ) )
00133         {
00134             $loopData = $parameters['loop'];
00135             if ( !eZTemplateNodeTool::isStaticElement( $loopData ) )
00136                 return false;
00137             $loopValue = eZTemplateNodeTool::elementStaticValue( $loopData );
00138 
00139             $newNodes = $this->templateNodeSequenceCreate( $node, $tpl, $parameters, $nameValue, $loopValue );
00140         }
00141         else
00142         {
00143             $newNodes = $this->templateNodeSequenceIterate( $node, $tpl, $parameters, $nameValue );
00144         }
00145         $retNodes = array_merge( array( $nameSpaceNode ),  $newNodes );
00146         return $retNodes;
00147     }
00148 
00149     /*!
00150      Either initializes the sequence or iterates it.
00151     */
00152     function process( $tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
00153     {
00154         $params = $functionParameters;
00155         $loop = null;
00156         if ( isset( $params["loop"] ) )
00157         {
00158             $loop = $tpl->elementValue( $params["loop"], $rootNamespace, $currentNamespace, $functionPlacement );
00159         }
00160         if ( $loop !== null and !is_array( $loop ) )
00161         {
00162             $tpl->error( $functionName, "Can only loop arrays", $functionPlacement );
00163             return;
00164         }
00165 
00166         $name = "";
00167         if ( !isset( $params["name"] ) )
00168         {
00169             $tpl->missingParameter( $functionName, "name" );
00170             return;
00171         }
00172         $name = $tpl->elementValue( $params["name"], $rootNamespace, $currentNamespace, $functionPlacement );
00173         $seq_var =& $GLOBALS["eZTemplateSequence-$name"];
00174         if ( !is_array( $seq_var ) )
00175             $seq_var = array();
00176         if ( $loop !== null )
00177         {
00178             $seq_var["loop"] = $loop;
00179             $seq_var["iteration"] = 0;
00180             $seq_var["index"] = 0;
00181         }
00182         else
00183         {
00184             $index =& $seq_var["index"];
00185             $iteration =& $seq_var["iteration"];
00186             ++$iteration;
00187             ++$index;
00188             if ( $index >= count( $seq_var["loop"] ) )
00189                 $index = 0 ;
00190         }
00191         $tpl->setVariable( "item", $seq_var["loop"][$seq_var["index"]], $tpl->mergeNamespace( $rootNamespace, $name ) );
00192         $tpl->setVariable( "iteration", $seq_var["iteration"], $tpl->mergeNamespace( $rootNamespace, $name ) );
00193     }
00194 
00195     /*!
00196      Returns false, telling the template parser that this is a single tag.
00197     */
00198     function hasChildren()
00199     {
00200         return false;
00201     }
00202 
00203     /// Name of sequence function
00204     public $SequenceName;
00205 }
00206 
00207 ?>