function templateNodeTransformation($functionName, &$node, $tpl, $parameters, $privateData) { // {for <first_val> to <last_val> as $<loop_var> [sequence <sequence_array> as $<sequence_var>]} $newNodes = array(); $tpl->ForCounter++; $nodePlacement = eZTemplateNodeTool::extractFunctionNodePlacement($node); $uniqid = md5($nodePlacement[2]) . "_" . $tpl->ForCounter; $loop = new eZTemplateCompiledLoop(eZTemplateForFunction::FUNCTION_NAME, $newNodes, $parameters, $nodePlacement, $uniqid, $node, $tpl, $privateData); $variableStack = "for_variable_stack_{$uniqid}"; $namesArray = array("for_firstval_{$uniqid}", "for_lastval_{$uniqid}", "for_i_{$uniqid}"); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("// for begins"); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("if ( !isset( \${$variableStack} ) ) \${$variableStack} = array();"); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("\$" . $variableStack . "[] = compact( '" . implode("', '", $namesArray) . "' );"); $newNodes[] = eZTemplateNodeTool::createVariableNode(false, $parameters['first_val'], $nodePlacement, array('treat-value-as-non-object' => true), "for_firstval_{$uniqid}"); $newNodes[] = eZTemplateNodeTool::createVariableNode(false, $parameters['last_val'], $nodePlacement, array('treat-value-as-non-object' => true), "for_lastval_{$uniqid}"); $loop->initVars(); // loop header $modifyLoopCounterCode = "\$for_firstval_{$uniqid} < \$for_lastval_{$uniqid} ? \$for_i_{$uniqid}++ : \$for_i_{$uniqid}--"; // . ";\n"; $newNodes[] = eZTemplateNodeTool::createCodePieceNode("for ( \$for_i_{$uniqid} = \$for_firstval_{$uniqid} ; ; {$modifyLoopCounterCode} )\n{"); $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode(); // Check for index $indexArray = isset($parameters['loop_var'][0][1]) ? $parameters['loop_var'][0][1] : array("", 2, "default_index_{$uniqid}"); $newNodes[] = eZTemplateNodeTool::createVariableNode(false, "for_i_{$uniqid}", $nodePlacement, array('text-result' => true), $indexArray, false, true, true); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("if ( !( \$for_firstval_{$uniqid} < \$for_lastval_{$uniqid} ? " . "\$for_i_{$uniqid} <= \$for_lastval_{$uniqid} : " . "\$for_i_{$uniqid} >= \$for_lastval_{$uniqid} ) )\n" . " break;\n"); $loop->processBody(); // loop footer $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode(); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("} // for"); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("if ( count( \${$variableStack} ) ) extract( array_pop( \${$variableStack} ) );\n"); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("else\n{\n"); $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode($indexArray); $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode("for_firstval_{$uniqid}"); $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode("for_lastval_{$uniqid}"); $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode("for_i_{$uniqid}"); $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode($variableStack); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("}\n"); $loop->cleanup(); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("// for ends\n"); return $newNodes; }
function templateNodeTransformation($functionName, &$node, $tpl, &$parameters, $privateData) { // {while <condition> [sequence <sequence_array> as $<sequence_var>]} $tpl->WhileCounter++; $newNodes = array(); $nodePlacement = eZTemplateNodeTool::extractFunctionNodePlacement($node); $uniqid = md5($nodePlacement[2]) . "_" . $tpl->WhileCounter; $loop = new eZTemplateCompiledLoop(eZTemplateWhileFunction::FUNCTION_NAME, $newNodes, $parameters, $nodePlacement, $uniqid, $node, $tpl, $privateData); $loop->initVars(); // loop header $newNodes[] = eZTemplateNodeTool::createCodePieceNode("while ( 1 ) // while\n{"); $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode(); $newNodes[] = eZTemplateNodeTool::createVariableNode(false, $parameters['condition'], $nodePlacement, array('treat-value-as-non-object' => true), "while_cond"); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("if ( ! \$while_cond ) break;\n"); $loop->processBody(); // loop footer $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode(); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("} // end of while\n"); $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode('while_cond'); $loop->cleanup(); return $newNodes; }
function templateNodeTransformation($functionName, &$node, $tpl, $parameters, $privateData) { /* {foreach <array> as [$keyVar =>] $itemVar [sequence <array> as $sequenceVar] [offset <offset>] [max <max>] [reverse] } */ $tpl->ForeachCounter++; $newNodes = array(); $nodePlacement = eZTemplateNodeTool::extractFunctionNodePlacement($node); $uniqid = md5($nodePlacement[2]) . "_" . $tpl->ForeachCounter; $loop = new eZTemplateCompiledLoop(eZTemplateForeachFunction::FUNCTION_NAME, $newNodes, $parameters, $nodePlacement, $uniqid, $node, $tpl, $privateData); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("// foreach begins"); $loop->initVars(); $array = "fe_array_{$uniqid}"; $arrayKeys = "fe_array_keys_{$uniqid}"; $nItems = "fe_n_items_{$uniqid}"; $nItemsProcessed = "fe_n_items_processed_{$uniqid}"; $i = "fe_i_{$uniqid}"; $key = "fe_key_{$uniqid}"; $val = "fe_val_{$uniqid}"; $offset = "fe_offset_{$uniqid}"; $max = "fe_max_{$uniqid}"; $reverse = "fe_reverse_{$uniqid}"; $firstVal = "fe_first_val_{$uniqid}"; $lastVal = "fe_last_val_{$uniqid}"; $variableStack = "fe_variable_stack_{$uniqid}"; $namesArray = array($array, $arrayKeys, $nItems, $nItemsProcessed, $i, $key, $val, $offset, $max, $reverse, $firstVal, $lastVal); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("if ( !isset( \${$variableStack} ) ) \${$variableStack} = array();"); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("\$" . $variableStack . "[] = compact( '" . implode("', '", $namesArray) . "' );"); $newNodes[] = eZTemplateNodeTool::createVariableNode(false, $parameters['array'], $nodePlacement, array('text-result' => false), $array); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("\${$arrayKeys} = is_array( \${$array} ) ? array_keys( \${$array} ) : array();"); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("\${$nItems} = count( \${$arrayKeys} );"); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("\${$nItemsProcessed} = 0;"); // process offset, max and reverse parameters if (isset($parameters['offset'])) { $newNodes[] = eZTemplateNodeTool::createVariableNode(false, $parameters['offset'], $nodePlacement, array('text-result' => false), $offset); } else { $newNodes[] = eZTemplateNodeTool::createCodePieceNode("\${$offset} = 0;"); } if (isset($parameters['max'])) { $newNodes[] = eZTemplateNodeTool::createVariableNode(false, $parameters['max'], $nodePlacement, array('text-result' => false), $max); } else { $newNodes[] = eZTemplateNodeTool::createCodePieceNode("\${$max} = \${$nItems} - \${$offset};"); } if (isset($parameters['reverse'])) { $newNodes[] = eZTemplateNodeTool::createVariableNode(false, $parameters['reverse'], $nodePlacement, array('text-result' => false), $reverse); } else { $newNodes[] = eZTemplateNodeTool::createCodePieceNode("\${$reverse} = false;"); } // fix definitely incorrect offset $newNodes[] = eZTemplateNodeTool::createCodePieceNode("if ( \${$offset} < 0 || \${$offset} >= \${$nItems} )\n{\n" . " \${$offset} = ( \${$offset} < 0 ) ? 0 : \${$nItems};\n" . " if ( \${$nItems} || \${$offset} < 0 )\n {\n" . " eZDebug::writeWarning(\"Invalid 'offset' parameter specified: '\${$offset}'. Array count: \${$nItems}\"); \n}\n}"); // fix definitely incorrect max $newNodes[] = eZTemplateNodeTool::createCodePieceNode("if ( \${$max} < 0 || \${$offset} + \${$max} > \${$nItems} )\n{\n" . " if ( \${$max} < 0 )\n eZDebug::writeWarning(\"Invalid 'max' parameter specified: \${$max}\");\n" . " \${$max} = \${$nItems} - \${$offset};\n}"); // initialize first and last indexes to iterate between them $newNodes[] = eZTemplateNodeTool::createCodePieceNode("if ( \${$reverse} )\n" . "{\n" . " \${$firstVal} = \${$nItems} - 1 - \${$offset};\n" . " \${$lastVal} = 0;\n" . "}\n" . "else\n" . "{\n" . " \${$firstVal} = \${$offset};\n" . " \${$lastVal} = \${$nItems} - 1;\n" . "}"); // generate loop header $newNodes[] = eZTemplateNodeTool::createCodePieceNode("// foreach"); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("for ( \${$i} = \${$firstVal}; " . "\${$nItemsProcessed} < \${$max} && ( \${$reverse} ? \${$i} >= \${$lastVal} : \${$i} <= \${$lastVal} ); " . "\${$reverse} ? \${$i}-- : \${$i}++ )\n" . "{"); $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode(); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("\${$key} = \${$arrayKeys}[\${$i}];"); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("\${$val} = \${$array}[\${$key}];"); // export $itemVar $newNodes[] = eZTemplateNodeTool::createVariableNode(false, "{$val}", $nodePlacement, array(), $parameters['item_var'][0][1], false, true, true); // export $keyVar (if specified) if (isset($parameters['key_var'])) { $newNodes[] = eZTemplateNodeTool::createVariableNode(false, "{$key}", $nodePlacement, array(), $parameters['key_var'][0][1], false, true, true); } $loop->processBody(); // generate loop footer $newNodes[] = eZTemplateNodeTool::createCodePieceNode("\${$nItemsProcessed}++;"); $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode(); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("} // foreach"); $loop->cleanup(); // unset the loop variables $newNodes[] = eZTemplateNodeTool::createCodePieceNode("if ( count( \${$variableStack} ) ) extract( array_pop( \${$variableStack} ) );\n"); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("else\n{\n"); $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode($array); $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode($arrayKeys); $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode($nItems); $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode($nItemsProcessed); $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode($i); $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode($key); $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode($val); $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode($offset); $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode($max); $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode($reverse); $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode($firstVal); $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode($lastVal); $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode($variableStack); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("}\n"); $newNodes[] = eZTemplateNodeTool::createCodePieceNode("// foreach ends"); return $newNodes; }