00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 define ( 'EZ_TEMPLATE_IF_FUNCTION_NAME', 'if' );
00058 class eZTemplateIfFunction
00059 {
00060
00061
00062
00063 function &functionList()
00064 {
00065
00066 $functionList = array( EZ_TEMPLATE_IF_FUNCTION_NAME );
00067 return $functionList;
00068 }
00069
00070
00071
00072
00073
00074
00075 function attributeList()
00076 {
00077 return array( 'elseif' => false,
00078 'else' => false );
00079 }
00080
00081
00082
00083
00084
00085 function functionTemplateHints()
00086 {
00087 return array( EZ_TEMPLATE_IF_FUNCTION_NAME => array( 'parameters' => true,
00088 'static' => false,
00089 'transform-parameters' => true,
00090 'tree-transformation' => true ) );
00091 }
00092
00093
00094
00095
00096 function templateNodeTransformation( $functionName, &$node,
00097 &$tpl, $parameters, $privateData )
00098 {
00099 $tpl->ElseifCounter++;
00100 $newNodes = array();
00101 $nodesToPrepend = array();
00102 $nodesToAppend = array();
00103 $nodePlacement = eZTemplateNodeTool::extractFunctionNodePlacement( $node );
00104 $uniqid = md5( $nodePlacement[2] ) . "_" . $tpl->ElseifCounter;
00105 $children = eZTemplateNodeTool::extractFunctionNodeChildren( $node );
00106
00107
00108 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "// if begins" );
00109 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameters['condition'], $nodePlacement, array( 'treat-value-as-non-object' => true ), 'if_cond' );
00110 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( \$if_cond )\n{" );
00111 $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode();
00112
00113 if ( is_array( $children ) )
00114 {
00115 foreach ( array_keys( $children ) as $childKey )
00116 {
00117 $child =& $children[$childKey];
00118
00119 if ( $child[0] == EZ_TEMPLATE_NODE_FUNCTION )
00120 {
00121 $childFunctionName =& $child[2];
00122 $childChildren = eZTemplateNodeTool::extractFunctionNodeChildren( $child );
00123 $childFunctionArgs =& $child[3];
00124
00125 if ( $childFunctionName == 'elseif' )
00126 {
00127 $compiledElseifCondition = eZTemplateCompiler::processElementTransformationList( $tpl, $child, $childFunctionArgs['condition'], $privateData );
00128 $nodesToPrepend[] = eZTemplateNodeTool::createVariableNode( false, $compiledElseifCondition,
00129 $nodePlacement,
00130 array( 'text-result' => true ),
00131 "elseif_cond_$uniqid" );
00132 $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode();
00133 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}\nelseif ( \$elseif_cond_$uniqid )\n{" );
00134 $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode();
00135 $nodesToAppend[] = eZTemplateNodeTool::createVariableUnsetNode( "elseif_cond_$uniqid" );
00136
00137 $uniqid = md5( $nodePlacement[2] ) . "_" . ++$tpl->ElseifCounter;
00138 }
00139 elseif ( $childFunctionName == 'else' )
00140 {
00141 $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode();
00142 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}\nelse\n{" );
00143 $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode();
00144 }
00145 elseif ( $childFunctionName == 'break' )
00146 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "break;\n" );
00147 elseif ( $childFunctionName == 'continue' )
00148 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "continue;\n" );
00149 elseif ( $childFunctionName == 'skip' )
00150 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$skipDelimiter = true;\ncontinue;\n" );
00151
00152
00153 if ( in_array( $childFunctionName, array( 'elseif', 'else', 'break', 'continue', 'skip' ) ) )
00154 continue;
00155 }
00156 $newNodes[] = $child;
00157 }
00158 }
00159
00160 $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode();
00161 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}" );
00162 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "unset( \$if_cond );" );
00163 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "// if ends\n" );
00164
00165 $newNodes = array_merge( $nodesToPrepend, $newNodes, $nodesToAppend );
00166
00167 return $newNodes;
00168 }
00169
00170
00171
00172
00173 function process( &$tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
00174 {
00175 if ( count( $functionParameters ) == 0 || !count( $functionParameters['condition'] ) )
00176 {
00177 eZDebug::writeError( "Not enough arguments passed to 'if' function." );
00178 return;
00179 }
00180
00181 $ifValue = $tpl->elementValue( $functionParameters['condition'], $rootNamespace, $currentNamespace, $functionPlacement );
00182
00183 $show = $ifValue;
00184 $showWasTrue = $show;
00185 $foundElse = false;
00186
00187 if ( !is_array( $functionChildren ) )
00188 {
00189 return;
00190 }
00191 foreach ( $functionChildren as $key => $child )
00192 {
00193 $childType = $child[0];
00194
00195
00196 if ( $childType == EZ_TEMPLATE_NODE_FUNCTION )
00197 {
00198 $childFunctionName =& $child[2];
00199 $childFunctionArgs =& $child[3];
00200 $childFunctionPlacement =& $child[4];
00201
00202 if ( $childFunctionName == 'else' )
00203 {
00204 if ( $foundElse )
00205 {
00206 eZDebug::writeError( "Duplicated 'else'" );
00207 $show = false;
00208 continue;
00209 }
00210
00211 $show = !$showWasTrue;
00212 $foundElse = true;
00213 continue;
00214 }
00215 elseif ( $childFunctionName == 'elseif' )
00216 {
00217 if ( $foundElse )
00218 {
00219 eZDebug::writeError( "There should be no more 'elseif' after 'else'" );
00220 $show = false;
00221 continue;
00222 }
00223
00224 if ( !isset( $childFunctionArgs['condition'] ) || !count( $childFunctionArgs['condition'] ) )
00225 $elseifValue = false;
00226 else
00227 $elseifValue = $tpl->elementValue( $childFunctionArgs['condition'], $rootNamespace, $currentNamespace, $functionPlacement );
00228
00229 if ( !$showWasTrue && $elseifValue )
00230 $show = $showWasTrue = true;
00231 else
00232 $show = false;
00233 continue;
00234 }
00235 elseif ( $childFunctionName == 'break' )
00236 {
00237 if ( !$show )
00238 continue;
00239 return array( 'breakFunctionFound' => 1 );
00240 }
00241 elseif ( $childFunctionName == 'continue' )
00242 {
00243 if ( !$show )
00244 continue;
00245 return array( 'continueFunctionFound' => 1 );
00246 }
00247 elseif ( $childFunctionName == 'skip' )
00248 {
00249 if ( !$show )
00250 continue;
00251 return array( 'skipFunctionFound' => 1 );
00252 }
00253 }
00254
00255 if ( $show )
00256 {
00257 $rslt = $tpl->processNode( $child, $textElements, $rootNamespace, $currentNamespace );
00258
00259
00260 if ( is_array( $rslt ) && ( array_key_exists( 'breakFunctionFound', $rslt ) ||
00261 array_key_exists( 'continueFunctionFound', $rslt ) ||
00262 array_key_exists( 'skipFunctionFound', $rslt ) ) )
00263 {
00264 return $rslt;
00265 }
00266 }
00267 }
00268 }
00269
00270
00271
00272
00273 function hasChildren()
00274 {
00275 return true;
00276 }
00277 }
00278
00279 ?>