1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-07-30 21:40:43 +02:00

Merge branch '3.2.x' into ticket/15068

This commit is contained in:
javiexin
2017-03-09 16:12:25 +01:00
committed by GitHub
1652 changed files with 75596 additions and 44938 deletions

View File

@@ -336,6 +336,89 @@ class context
return $result;
}
/**
* Find the index for a specified key in the innermost specified block
*
* @param string $blockname the blockname, for example 'loop'
* @param mixed $key Key to search for
*
* array: KEY => VALUE [the key/value pair to search for within the loop to determine the correct position]
*
* int: Position [the position to search for]
*
* If key is false the position is set to 0
* If key is true the position is set to the last entry
*
* @return mixed false if not found, index position otherwise; be sure to test with ===
*/
public function find_key_index($blockname, $key)
{
// For nested block, $blockcount > 0, for top-level block, $blockcount == 0
$blocks = explode('.', $blockname);
$blockcount = sizeof($blocks) - 1;
$block = $this->tpldata;
for ($i = 0; $i < $blockcount; $i++)
{
if (($pos = strpos($blocks[$i], '[')) !== false)
{
$name = substr($blocks[$i], 0, $pos);
if (strpos($blocks[$i], '[]') === $pos)
{
$index = sizeof($block[$name]) - 1;
}
else
{
$index = min((int) substr($blocks[$i], $pos + 1, -1), sizeof($block[$name]) - 1);
}
}
else
{
$name = $blocks[$i];
$index = sizeof($block[$name]) - 1;
}
if (!isset($block[$name]))
{
return false;
}
$block = $block[$name];
if (!isset($block[$index]))
{
return false;
}
$block = $block[$index];
}
if (!isset($block[$blocks[$i]]))
{
return false;
}
$block = $block[$blocks[$i]]; // Traverse the last block
// Change key to zero (change first position) if false and to last position if true
if ($key === false || $key === true)
{
return ($key === false) ? 0 : sizeof($block) - 1;
}
// Get correct position if array given
if (is_array($key))
{
// Search array to get correct position
list($search_key, $search_value) = @each($key);
foreach ($block as $i => $val_ary)
{
if ($val_ary[$search_key] === $search_value)
{
return $i;
}
}
}
return (is_int($key) && ((0 <= $key) && ($key < sizeof($block)))) ? $key : false;
}
/**
* Change already assigned key variable pair (one-dimensional - single loop entry)
*
@@ -366,45 +449,49 @@ class context
public function alter_block_array($blockname, array $vararray, $key = false, $mode = 'insert')
{
$this->num_rows_is_set = false;
if (strpos($blockname, '.') !== false)
// For nested block, $blockcount > 0, for top-level block, $blockcount == 0
$blocks = explode('.', $blockname);
$blockcount = sizeof($blocks) - 1;
$block = &$this->tpldata;
for ($i = 0; $i < $blockcount; $i++)
{
// Nested block.
$blocks = explode('.', $blockname);
$blockcount = sizeof($blocks) - 1;
$block = &$this->tpldata;
for ($i = 0; $i < $blockcount; $i++)
if (($pos = strpos($blocks[$i], '[')) !== false)
{
if (($pos = strpos($blocks[$i], '[')) !== false)
{
$name = substr($blocks[$i], 0, $pos);
$name = substr($blocks[$i], 0, $pos);
if (strpos($blocks[$i], '[]') === $pos)
{
$index = sizeof($block[$name]) - 1;
}
else
{
$index = min((int) substr($blocks[$i], $pos + 1, -1), sizeof($block[$name]) - 1);
}
if (strpos($blocks[$i], '[]') === $pos)
{
$index = sizeof($block[$name]) - 1;
}
else
{
$name = $blocks[$i];
$index = sizeof($block[$name]) - 1;
$index = min((int) substr($blocks[$i], $pos + 1, -1), sizeof($block[$name]) - 1);
}
$block = &$block[$name];
$block = &$block[$index];
}
else
{
$name = $blocks[$i];
$index = sizeof($block[$name]) - 1;
}
$block = &$block[$name];
$block = &$block[$index];
}
$name = $blocks[$i];
$block = &$block[$blocks[$i]]; // Traverse the last block
}
else
// If last block does not exist and we are inserting, and not searching for key, we create it empty; otherwise, nothing to do
if (!isset($block[$name]))
{
// Top-level block.
$block = &$this->tpldata[$blockname];
if ($mode != 'insert' || is_array($key))
{
return false;
}
$block[$name] = array();
}
$block = &$block[$name]; // Now we can traverse the last block
// Change key to zero (change first position) if false and to last position if true
if ($key === false || $key === true)
{
@@ -438,20 +525,21 @@ class context
if ($mode == 'insert')
{
// Make sure we are not exceeding the last iteration
if ($key >= sizeof($this->tpldata[$blockname]))
if ($key >= sizeof($block))
{
$key = sizeof($this->tpldata[$blockname]);
unset($this->tpldata[$blockname][($key - 1)]['S_LAST_ROW']);
$key = sizeof($block);
unset($block[($key - 1)]['S_LAST_ROW']);
$vararray['S_LAST_ROW'] = true;
}
else if ($key === 0)
if ($key <= 0)
{
unset($this->tpldata[$blockname][0]['S_FIRST_ROW']);
$key = 0;
unset($block[0]['S_FIRST_ROW']);
$vararray['S_FIRST_ROW'] = true;
}
// Assign S_BLOCK_NAME
$vararray['S_BLOCK_NAME'] = $blockname;
$vararray['S_BLOCK_NAME'] = $name;
// Re-position template blocks
for ($i = sizeof($block); $i > $key; $i--)
@@ -471,6 +559,12 @@ class context
// Which block to change?
if ($mode == 'change')
{
// If key is out of bounds, do not change anything
if ($key > sizeof($block) || $key < 0)
{
return false;
}
if ($key == sizeof($block))
{
$key--;