eZ Publish  [4.0]
eztemplatedofunction.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZTemplateDoFunction class
00004 //
00005 // Created on: <25-Feb-2005 13:04:30 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 eZTemplateDoFunction eztemplatedofunction.php
00033   \ingroup eZTemplateFunctions
00034   \brief DO..WHILE loop
00035 
00036   Syntax:
00037 \code
00038     {do}
00039         [{delimiter}...{/delimiter}]
00040         [{break}]
00041         [{continue}]
00042         [{skip}]
00043     {/do while <condition> [sequence <array> as $seqVar]}
00044 \endcode
00045 
00046   Example:
00047 \code
00048     {do}
00049         One more beer, please.
00050     {/do while eq( $drunk, false() )}
00051 \endcode
00052 */
00053 
00054 class eZTemplateDoFunction
00055 {
00056     const FUNCTION_NAME = 'do';
00057 
00058     /*!
00059      * Returns an array of the function names, required for eZTemplate::registerFunctions().
00060      */
00061     function &functionList()
00062     {
00063         $functionList = array( eZTemplateDoFunction::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( eZTemplateDoFunction::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         // {/do while <condition> [sequence <sequence_array> as $<sequence_var>]}
00099 
00100         $tpl->DoCounter++;
00101         $newNodes      = array();
00102         $nodePlacement = eZTemplateNodeTool::extractFunctionNodePlacement( $node );
00103         $uniqid        = md5( $nodePlacement[2] ) . "_" . $tpl->DoCounter;
00104 
00105         // initialize loop
00106         require_once( 'lib/eztemplate/classes/eztemplatecompiledloop.php' );
00107         $loop = new eZTemplateCompiledLoop( eZTemplateDoFunction::FUNCTION_NAME,
00108                                             $newNodes, $parameters, $nodePlacement, $uniqid,
00109                                             $node, $tpl, $privateData );
00110 
00111         $loop->initVars();
00112 
00113         // loop header
00114         $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "while ( 1 ) // do..while\n{" );
00115         $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode();
00116 
00117         $loop->processBody();
00118 
00119         // loop footer
00120         $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameters['condition'], $nodePlacement, array( 'treat-value-as-non-object' => true ), 'do_cond' );
00121         $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( ! \$do_cond ) break;" );
00122         $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode();
00123         $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "} // do..while" );
00124         $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( 'do_cond' );
00125 
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         require_once( 'lib/eztemplate/classes/eztemplateloop.php' );
00137         $loop = new eZTemplateLoop( eZTemplateDoFunction::FUNCTION_NAME,
00138                                     $functionParameters, $functionChildren, $functionPlacement,
00139                                     $tpl, $textElements, $rootNamespace, $currentNamespace );
00140 
00141         if ( !$loop->initialized() )
00142             return;
00143 
00144         if ( !isset( $functionParameters['condition'] ) )
00145         {
00146             eZDebug::writeError( "Not enough arguments passed to 'do' function"  );
00147             return;
00148         }
00149 
00150         do
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         } while ( $tpl->elementValue( $functionParameters['condition'], $rootNamespace, $currentNamespace, $functionPlacement ) );
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 ?>