Files
smarty/libs/sysplugins/smarty_internal_compile_foreach.php

347 lines
12 KiB
PHP
Raw Normal View History

<?php
/**
* Smarty Internal Plugin Compile Foreach
* Compiles the {foreach} {foreachelse} {/foreach} tags
2011-09-16 14:19:56 +00:00
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
2010-08-17 15:39:51 +00:00
/**
* Smarty Internal Plugin Compile Foreach Class
2011-09-16 14:19:56 +00:00
*
* @package Smarty
2011-09-16 14:19:56 +00:00
* @subpackage Compiler
*/
class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_ForeachSection
{
2011-09-16 14:19:56 +00:00
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Smarty_Internal_CompileBase
*/
public $required_attributes = array('from', 'item');
2015-07-01 03:30:08 +02:00
2011-09-16 14:19:56 +00:00
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Smarty_Internal_CompileBase
*/
public $optional_attributes = array('name', 'key', 'properties');
2015-07-01 03:30:08 +02:00
2011-09-16 14:19:56 +00:00
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Smarty_Internal_CompileBase
*/
public $shorttag_order = array('from', 'item', 'key', 'name');
2015-07-01 03:30:08 +02:00
/**
* counter
2015-07-01 03:30:08 +02:00
*
* @var int
*/
public $counter = 0;
/**
* Name of this tag
*
* @var string
*/
public $tagName = 'foreach';
/**
* Valid properties of $smarty.foreach.name.xxx variable
*
* @var array
*/
public $nameProperties = array('first', 'last', 'index', 'iteration', 'show', 'total');
/**
* Valid properties of $item@xxx variable
*
* @var array
*/
public $itemProperties = array('first', 'last', 'index', 'iteration', 'show', 'total', 'key');
/**
* Flag if tag had name attribute
*
* @var bool
*/
public $isNamed = false;
2015-07-01 03:30:08 +02:00
/**
* Compiles code for the {foreach} tag
2011-09-16 14:19:56 +00:00
*
2018-06-12 09:58:15 +02:00
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
*
* @return string compiled code
2015-07-01 03:30:08 +02:00
* @throws \SmartyCompilerException
* @throws \SmartyException
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
{
$compiler->loopNesting++;
// init
$this->isNamed = false;
// check and get attributes
2011-09-16 14:19:56 +00:00
$_attr = $this->getAttributes($compiler, $args);
$from = $_attr[ 'from' ];
$item = $compiler->getId($_attr[ 'item' ]);
2015-07-01 03:30:08 +02:00
if ($item === false) {
$item = $compiler->getVariableName($_attr[ 'item' ]);
2011-09-16 14:19:56 +00:00
}
$key = $name = null;
2015-07-01 03:30:08 +02:00
$attributes = array('item' => $item);
if (isset($_attr[ 'key' ])) {
$key = $compiler->getId($_attr[ 'key' ]);
2015-07-01 03:30:08 +02:00
if ($key === false) {
$key = $compiler->getVariableName($_attr[ 'key' ]);
2015-07-01 03:30:08 +02:00
}
$attributes[ 'key' ] = $key;
2011-09-16 14:19:56 +00:00
}
if (isset($_attr[ 'name' ])) {
$this->isNamed = true;
$name = $attributes[ 'name' ] = $compiler->getId($_attr[ 'name' ]);
2015-07-01 03:30:08 +02:00
}
foreach ($attributes as $a => $v) {
if ($v === false) {
$compiler->trigger_template_error("'{$a}' attribute/variable has illegal value", null, true);
2015-07-01 03:30:08 +02:00
}
}
$fromName = $compiler->getVariableName($_attr[ 'from' ]);
2015-07-01 03:30:08 +02:00
if ($fromName) {
foreach (array('item', 'key') as $a) {
if (isset($attributes[ $a ]) && $attributes[ $a ] === $fromName) {
2018-06-12 09:58:15 +02:00
$compiler->trigger_template_error(
"'{$a}' and 'from' may not have same variable name '{$fromName}'",
null,
true
2018-06-12 09:58:15 +02:00
);
2015-07-01 03:30:08 +02:00
}
}
}
$itemVar = "\$_smarty_tpl->tpl_vars['{$item}']";
$local = '$__foreach_' . $attributes[ 'item' ] . '_' . $this->counter++ . '_';
// search for used tag attributes
2015-07-01 03:30:08 +02:00
$itemAttr = array();
$namedAttr = array();
$this->scanForProperties($attributes, $compiler);
if (!empty($this->matchResults[ 'item' ])) {
$itemAttr = $this->matchResults[ 'item' ];
2011-09-16 14:19:56 +00:00
}
if (!empty($this->matchResults[ 'named' ])) {
$namedAttr = $this->matchResults[ 'named' ];
}
if (isset($_attr[ 'properties' ]) && preg_match_all('/[\'](.*?)[\']/', $_attr[ 'properties' ], $match)) {
foreach ($match[ 1 ] as $prop) {
if (in_array($prop, $this->itemProperties)) {
$itemAttr[ $prop ] = true;
} else {
$compiler->trigger_template_error("Invalid property '{$prop}'", null, true);
}
}
if ($this->isNamed) {
foreach ($match[ 1 ] as $prop) {
if (in_array($prop, $this->nameProperties)) {
$nameAttr[ $prop ] = true;
} else {
$compiler->trigger_template_error("Invalid property '{$prop}'", null, true);
}
}
}
}
if (isset($itemAttr[ 'first' ])) {
$itemAttr[ 'index' ] = true;
2015-07-01 03:30:08 +02:00
}
if (isset($namedAttr[ 'first' ])) {
$namedAttr[ 'index' ] = true;
}
if (isset($namedAttr[ 'last' ])) {
$namedAttr[ 'iteration' ] = true;
$namedAttr[ 'total' ] = true;
}
if (isset($itemAttr[ 'last' ])) {
$itemAttr[ 'iteration' ] = true;
$itemAttr[ 'total' ] = true;
2015-07-01 03:30:08 +02:00
}
if (isset($namedAttr[ 'show' ])) {
$namedAttr[ 'total' ] = true;
}
if (isset($itemAttr[ 'show' ])) {
$itemAttr[ 'total' ] = true;
}
2015-02-15 16:58:42 +01:00
$keyTerm = '';
if (isset($attributes[ 'key' ])) {
$keyTerm = "\$_smarty_tpl->tpl_vars['{$key}']->value => ";
}
if (isset($itemAttr[ 'key' ])) {
$keyTerm = "{$itemVar}->key => ";
2015-02-15 16:58:42 +01:00
}
if ($this->isNamed) {
$foreachVar = "\$_smarty_tpl->tpl_vars['__smarty_foreach_{$attributes['name']}']";
2015-07-01 03:30:08 +02:00
}
$needTotal = isset($itemAttr[ 'total' ]);
// Register tag
2018-06-12 09:58:15 +02:00
$this->openTag(
$compiler,
'foreach',
array('foreach', $compiler->nocache, $local, $itemVar, empty($itemAttr) ? 0 : 1)
2018-06-12 09:58:15 +02:00
);
// maybe nocache because of nocache variables
$compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
// generate output code
$output = "<?php\n";
$output .= "\$_from = \$_smarty_tpl->smarty->ext->_foreach->init(\$_smarty_tpl, $from, " .
2016-02-09 01:27:15 +01:00
var_export($item, true);
if ($name || $needTotal || $key) {
$output .= ', ' . var_export($needTotal, true);
2011-09-16 14:19:56 +00:00
}
if ($name || $key) {
$output .= ', ' . var_export($key, true);
}
if ($name) {
$output .= ', ' . var_export($name, true) . ', ' . var_export($namedAttr, true);
}
$output .= ");\n";
if (isset($itemAttr[ 'show' ])) {
$output .= "{$itemVar}->show = ({$itemVar}->total > 0);\n";
2011-09-16 14:19:56 +00:00
}
if (isset($itemAttr[ 'iteration' ])) {
$output .= "{$itemVar}->iteration = 0;\n";
}
if (isset($itemAttr[ 'index' ])) {
$output .= "{$itemVar}->index = -1;\n";
}
$output .= "{$itemVar}->do_else = true;\n";
$output .= "foreach (\$_from as {$keyTerm}{$itemVar}->value) {\n";
$output .= "{$itemVar}->do_else = false;\n";
if (isset($attributes[ 'key' ]) && isset($itemAttr[ 'key' ])) {
$output .= "\$_smarty_tpl->tpl_vars['{$key}']->value = {$itemVar}->key;\n";
2011-09-16 14:19:56 +00:00
}
if (isset($itemAttr[ 'iteration' ])) {
$output .= "{$itemVar}->iteration++;\n";
2011-09-16 14:19:56 +00:00
}
if (isset($itemAttr[ 'index' ])) {
$output .= "{$itemVar}->index++;\n";
}
if (isset($itemAttr[ 'first' ])) {
$output .= "{$itemVar}->first = !{$itemVar}->index;\n";
2011-09-16 14:19:56 +00:00
}
if (isset($itemAttr[ 'last' ])) {
$output .= "{$itemVar}->last = {$itemVar}->iteration === {$itemVar}->total;\n";
2011-09-16 14:19:56 +00:00
}
if (isset($foreachVar)) {
if (isset($namedAttr[ 'iteration' ])) {
$output .= "{$foreachVar}->value['iteration']++;\n";
2011-09-16 14:19:56 +00:00
}
if (isset($namedAttr[ 'index' ])) {
$output .= "{$foreachVar}->value['index']++;\n";
2015-02-15 16:58:42 +01:00
}
if (isset($namedAttr[ 'first' ])) {
$output .= "{$foreachVar}->value['first'] = !{$foreachVar}->value['index'];\n";
2011-09-16 14:19:56 +00:00
}
if (isset($namedAttr[ 'last' ])) {
$output .= "{$foreachVar}->value['last'] = {$foreachVar}->value['iteration'] === {$foreachVar}->value['total'];\n";
}
}
if (!empty($itemAttr)) {
$output .= "{$local}saved = {$itemVar};\n";
}
$output .= '?>';
return $output;
2011-09-16 14:19:56 +00:00
}
/**
* Compiles code for to restore saved template variables
*
* @param int $levels number of levels to restore
*
* @return string compiled code
*/
public function compileRestore($levels)
{
return "\$_smarty_tpl->smarty->ext->_foreach->restore(\$_smarty_tpl, {$levels});";
}
2011-09-16 14:19:56 +00:00
}
/**
* Smarty Internal Plugin Compile Foreachelse Class
2011-09-16 14:19:56 +00:00
*
* @package Smarty
2011-09-16 14:19:56 +00:00
* @subpackage Compiler
*/
class Smarty_Internal_Compile_Foreachelse extends Smarty_Internal_CompileBase
{
/**
* Compiles code for the {foreachelse} tag
2011-09-16 14:19:56 +00:00
*
2018-06-12 09:58:15 +02:00
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
{
// check and get attributes
2011-09-16 14:19:56 +00:00
$_attr = $this->getAttributes($compiler, $args);
list($openTag, $nocache, $local, $itemVar, $restore) = $this->closeTag($compiler, array('foreach'));
$this->openTag($compiler, 'foreachelse', array('foreachelse', $nocache, $local, $itemVar, 0));
2015-02-15 16:58:42 +01:00
$output = "<?php\n";
if ($restore === 2) {
$output .= "{$itemVar} = {$local}saved;\n";
}
$output .= "}\nif ({$itemVar}->do_else) {\n?>";
2015-02-15 16:58:42 +01:00
return $output;
2011-09-16 14:19:56 +00:00
}
}
/**
* Smarty Internal Plugin Compile Foreachclose Class
2011-09-16 14:19:56 +00:00
*
* @package Smarty
2011-09-16 14:19:56 +00:00
* @subpackage Compiler
*/
class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase
{
/**
* Compiles code for the {/foreach} tag
2011-09-16 14:19:56 +00:00
*
2018-06-12 09:58:15 +02:00
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
*
* @return string compiled code
* @throws \SmartyCompilerException
2018-06-12 09:58:15 +02:00
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
{
$compiler->loopNesting--;
// must endblock be nocache?
2011-09-16 14:19:56 +00:00
if ($compiler->nocache) {
$compiler->tag_nocache = true;
}
list(
$openTag, $compiler->nocache, $local, $itemVar, $restore
) = $this->closeTag($compiler, array('foreach', 'foreachelse'));
2015-02-15 16:58:42 +01:00
$output = "<?php\n";
if ($restore === 2) {
$output .= "{$itemVar} = {$local}saved;\n";
2015-02-15 16:58:42 +01:00
}
if ($restore > 0) {
$output .= "}\n";
}
2015-07-01 03:30:08 +02:00
$output .= "}\n";
/* @var Smarty_Internal_Compile_Foreach $foreachCompiler */
$foreachCompiler = $compiler->getTagCompiler('foreach');
$output .= $foreachCompiler->compileRestore(1);
$output .= "?>";
2015-02-15 16:58:42 +01:00
return $output;
2011-09-16 14:19:56 +00:00
}
}