eZ Publish  [trunk]
eztemplatedofunction.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZTemplateDoFunction class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package lib
00009  */
00010 
00011 /*!
00012   \class eZTemplateDoFunction eztemplatedofunction.php
00013   \ingroup eZTemplateFunctions
00014   \brief DO..WHILE loop
00015 
00016   Syntax:
00017 \code
00018     {do}
00019         [{delimiter}...{/delimiter}]
00020         [{break}]
00021         [{continue}]
00022         [{skip}]
00023     {/do while <condition> [sequence <array> as $seqVar]}
00024 \endcode
00025 
00026   Example:
00027 \code
00028     {do}
00029         One more beer, please.
00030     {/do while eq( $drunk, false() )}
00031 \endcode
00032 */
00033 
00034 class eZTemplateDoFunction
00035 {
00036     const FUNCTION_NAME = 'do';
00037 
00038     /*!
00039      * Returns an array of the function names, required for eZTemplate::registerFunctions().
00040      */
00041     function functionList()
00042     {
00043         $functionList = array( eZTemplateDoFunction::FUNCTION_NAME );
00044         return $functionList;
00045     }
00046 
00047     /*!
00048      * Returns the attribute list
00049      * key:   parameter name
00050      * value: can have children
00051      */
00052     function attributeList()
00053     {
00054         return array( 'delimiter' => true,
00055                       'break'     => false,
00056                       'continue'  => false,
00057                       'skip'      => false );
00058     }
00059 
00060 
00061     /*!
00062      * Returns the array with hits for the template compiler.
00063      */
00064     function functionTemplateHints()
00065     {
00066         return array( eZTemplateDoFunction::FUNCTION_NAME => array( 'parameters' => true,
00067                                                              'static' => false,
00068                                                              'transform-parameters' => true,
00069                                                              'tree-transformation' => true ) );
00070     }
00071 
00072     /*!
00073      * Compiles the function and its children into PHP code.
00074      */
00075     function templateNodeTransformation( $functionName, &$node,
00076                                          $tpl, $parameters, $privateData )
00077     {
00078         // {/do while <condition> [sequence <sequence_array> as $<sequence_var>]}
00079 
00080         $tpl->DoCounter++;
00081         $newNodes      = array();
00082         $nodePlacement = eZTemplateNodeTool::extractFunctionNodePlacement( $node );
00083         $uniqid        = md5( $nodePlacement[2] ) . "_" . $tpl->DoCounter;
00084 
00085         // initialize loop
00086         $loop = new eZTemplateCompiledLoop( eZTemplateDoFunction::FUNCTION_NAME,
00087                                             $newNodes, $parameters, $nodePlacement, $uniqid,
00088                                             $node, $tpl, $privateData );
00089 
00090         $loop->initVars();
00091 
00092         // loop header
00093         $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "while ( 1 ) // do..while\n{" );
00094         $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode();
00095 
00096         $loop->processBody();
00097 
00098         // loop footer
00099         $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameters['condition'], $nodePlacement, array( 'treat-value-as-non-object' => true ), 'do_cond' );
00100         $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( ! \$do_cond ) break;" );
00101         $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode();
00102         $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "} // do..while" );
00103         $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( 'do_cond' );
00104 
00105         $loop->cleanup();
00106 
00107         return $newNodes;
00108     }
00109 
00110     /*!
00111      * Actually executes the function and its children (in processed mode).
00112      */
00113     function process( $tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
00114     {
00115         $loop = new eZTemplateLoop( eZTemplateDoFunction::FUNCTION_NAME,
00116                                     $functionParameters, $functionChildren, $functionPlacement,
00117                                     $tpl, $textElements, $rootNamespace, $currentNamespace );
00118 
00119         if ( !$loop->initialized() )
00120             return;
00121 
00122         if ( !isset( $functionParameters['condition'] ) )
00123         {
00124             eZDebug::writeError( "Not enough arguments passed to 'do' function"  );
00125             return;
00126         }
00127 
00128         do
00129         {
00130             $loop->setSequenceVar(); // set sequence variable (if specified)
00131             $loop->processDelimiter();
00132             $loop->resetIteration();
00133 
00134             if ( $loop->processChildren() )
00135                 break;
00136 
00137             $loop->incrementSequence();
00138         } while ( $tpl->elementValue( $functionParameters['condition'], $rootNamespace, $currentNamespace, $functionPlacement ) );
00139 
00140         $loop->cleanup();
00141     }
00142 
00143     /*!
00144      * Returns true, telling the template parser that the function can have children.
00145      */
00146     function hasChildren()
00147     {
00148         return true;
00149     }
00150 }
00151 
00152 ?>