eZ Publish  [4.0]
eztemplatewhilefunction.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZTemplateWhileFunction class
00004 //
00005 // Created on: <18-Feb-2005 14:57:37 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 eZTemplateWhileFunction eztemplatewhilefunction.php
00033   \ingroup eZTemplateFunctions
00034   \brief WHILE loop
00035 
00036   Syntax:
00037 \code
00038     {while <condition> [sequence <array> as $seqVar] }
00039         [{delimiter}...{/delimiter}]
00040         [{break}]
00041         [{continue}]
00042         [{skip}]
00043     {/while}
00044 \endcode
00045 
00046   Example:
00047 \code
00048     {while ne( $var, false() ) }
00049         I like big trucks
00050     {/while}
00051 \endcode
00052 */
00053 
00054 class eZTemplateWhileFunction
00055 {
00056     const FUNCTION_NAME = 'while';
00057 
00058     /*!
00059      * Returns an array of the function names, required for eZTemplate::registerFunctions.
00060      */
00061     function &functionList()
00062     {
00063         $functionList = array( eZTemplateWhileFunction::FUNCTION_NAME );
00064         return $functionList;
00065     }
00066 
00067     /*!
00068      * Returns the attribute list.
00069      * key:   parameter name
00070      * value: can have children
00071      */
00072     function attributeList()
00073     {
00074         return array( 'delimiter' => true,
00075                       'break'     => false,
00076                       'continue'  => false,
00077                       'skip'      => false );
00078     }
00079 
00080 
00081     /*!
00082      * Returns the array with hits for the template compiler.
00083      */
00084     function functionTemplateHints()
00085     {
00086         return array( eZTemplateWhileFunction::FUNCTION_NAME => array( 'parameters' => true,
00087                                                                 'static' => false,
00088                                                                 'transform-parameters' => true,
00089                                                                 'tree-transformation' => true ) );
00090     }
00091 
00092     /*!
00093      * Compiles the function and its children into PHP code.
00094      */
00095     function templateNodeTransformation( $functionName, &$node,
00096                                          $tpl, &$parameters, $privateData )
00097     {
00098         // {while <condition> [sequence <sequence_array> as $<sequence_var>]}
00099 
00100         $tpl->WhileCounter++;
00101         $newNodes      = array();
00102         $nodePlacement = eZTemplateNodeTool::extractFunctionNodePlacement( $node );
00103         $uniqid        =  md5( $nodePlacement[2] ) . "_" . $tpl->WhileCounter;
00104 
00105         require_once( 'lib/eztemplate/classes/eztemplatecompiledloop.php' );
00106         $loop = new eZTemplateCompiledLoop( eZTemplateWhileFunction::FUNCTION_NAME,
00107                                             $newNodes, $parameters, $nodePlacement, $uniqid,
00108                                             $node, $tpl, $privateData );
00109 
00110 
00111         $loop->initVars();
00112 
00113         // loop header
00114         $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "while ( 1 ) // while\n{" );
00115         $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode();
00116         $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameters['condition'], $nodePlacement, array( 'treat-value-as-non-object' => true ),
00117                                                               "while_cond" );
00118         $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( ! \$while_cond ) break;\n" );
00119 
00120         $loop->processBody();
00121 
00122         // loop footer
00123         $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode();
00124         $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "} // end of while\n" );
00125         $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( 'while_cond' );
00126         $loop->cleanup();
00127 
00128         return $newNodes;
00129     }
00130 
00131     /*!
00132      * Actually executes the function and its children (in processed mode).
00133      */
00134     function process( $tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
00135     {
00136         if ( count( $functionParameters ) == 0 )
00137         {
00138             eZDebug::writeError( "Not enough arguments passed to 'while' function." );
00139             return;
00140         }
00141 
00142         require_once( 'lib/eztemplate/classes/eztemplateloop.php' );
00143         $loop = new eZTemplateLoop( eZTemplateWhileFunction::FUNCTION_NAME,
00144                                     $functionParameters, $functionChildren, $functionPlacement,
00145                                     $tpl, $textElements, $rootNamespace, $currentNamespace );
00146 
00147         if ( !$loop->initialized() )
00148             return;
00149 
00150         while ( $tpl->elementValue( $functionParameters['condition'], $rootNamespace, $currentNamespace, $functionPlacement ) )
00151         {
00152             $loop->setSequenceVar(); // set sequence variable (if specified)
00153             $loop->processDelimiter();
00154             $loop->resetIteration();
00155 
00156             if ( $loop->processChildren() )
00157                 break;
00158 
00159             $loop->incrementSequence();
00160         }
00161 
00162         $loop->cleanup();
00163     }
00164 
00165     /*!
00166      * Returns true, telling the template parser that the function can have children.
00167      */
00168     function hasChildren()
00169     {
00170         return true;
00171     }
00172 }
00173 
00174 ?>