diff --git a/phpBB/phpbb/template/base.php b/phpBB/phpbb/template/base.php index 41c0a01ba8..d502aceab8 100644 --- a/phpBB/phpbb/template/base.php +++ b/phpBB/phpbb/template/base.php @@ -104,6 +104,27 @@ abstract class base implements template return $this; } + /** + * {@inheritdoc} + */ + public function retrieve_vars(array $vararray) + { + $result = array(); + foreach ($vararray as $varname) + { + $result[$varname] = $this->retrieve_var($varname); + } + return $result; + } + + /** + * {@inheritdoc} + */ + public function retrieve_var($varname) + { + return $this->context->retrieve_var($varname); + } + /** * {@inheritdoc} */ @@ -124,6 +145,14 @@ abstract class base implements template return $this; } + /** + * {@inheritdoc} + */ + public function retrieve_block_vars($blockname, array $vararray) + { + return $this->context->retrieve_block_vars($blockname, $vararray); + } + /** * {@inheritdoc} */ diff --git a/phpBB/phpbb/template/context.php b/phpBB/phpbb/template/context.php index 5e4f71a2a9..392efd5933 100644 --- a/phpBB/phpbb/template/context.php +++ b/phpBB/phpbb/template/context.php @@ -86,6 +86,17 @@ class context return true; } + /** + * Retreive a single scalar value from a single key. + * + * @param string $varname Variable name + * @return mixed Variable value, or null if not set + */ + public function retrieve_var($varname) + { + return isset($this->rootref[$varname]) ? $this->rootref[$varname] : null; + } + /** * Returns a reference to template data array. * @@ -263,6 +274,68 @@ class context return true; } + /** + * Retrieve key variable pairs from the specified block + * + * @param string $blockname Name of block to retrieve $vararray from + * @param array $vararray An array of variable names, empty array retrieves all vars + * @return array of hashes with variable name as key and retrieved value or null as value + */ + public function retrieve_block_vars($blockname, array $vararray) + { + // 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; + } + $block = $block[$name]; + $block = $block[$index]; + } + + $result = array(); + if ($vararray === array()) + { + // The calculated vars that depend on the block position are excluded from the complete block returned results + $excluded_vars = array('S_FIRST_ROW', 'S_LAST_ROW', 'S_BLOCK_NAME', 'S_NUM_ROWS', 'S_ROW_COUNT', 'S_ROW_NUM'); + + foreach ($block as $varname => $varvalue) + { + if ($varname === strtoupper($varname) && !is_array($varvalue) && !in_array($varname, $excluded_vars)) + { + $result[$varname] = $varvalue; + } + } + } + else + { + foreach ($vararray as $varname) + { + $result[$varname] = isset($block[$varname]) ? $block[$varname] : null; + } + } + return $result; + } + /** * Find the index for a specified key in the innermost specified block * diff --git a/phpBB/phpbb/template/template.php b/phpBB/phpbb/template/template.php index d1ec442e9a..df83d5bc43 100644 --- a/phpBB/phpbb/template/template.php +++ b/phpBB/phpbb/template/template.php @@ -127,6 +127,22 @@ interface template */ public function append_var($varname, $varval); + /** + * Retrieve multiple template values + * + * @param array $vararray An array with variable names + * @return array A hash of variable name => value pairs (value is null if not set) + */ + public function retrieve_vars(array $vararray); + + /** + * Retreive a single scalar value from a single key. + * + * @param string $varname Variable name + * @return mixed Variable value, or null if not set + */ + public function retrieve_var($varname); + /** * Assign key variable pairs from an array to a specified block * @param string $blockname Name of block to assign $vararray to @@ -143,6 +159,14 @@ interface template */ public function assign_block_vars_array($blockname, array $block_vars_array); + /** + * Retrieve variable values from an specified block + * @param string $blockname Name of block to retrieve $vararray from + * @param array $vararray An array with variable names, empty array gets all vars + * @return array A hash of variable name => value pairs (value is null if not set) + */ + public function retrieve_block_vars($blockname, array $vararray); + /** * Change already assigned key variable pair (one-dimensional - single loop entry) * diff --git a/tests/template/template_test.php b/tests/template/template_test.php index f86572cae8..9f2124418d 100644 --- a/tests/template/template_test.php +++ b/tests/template/template_test.php @@ -597,6 +597,37 @@ class phpbb_template_template_test extends phpbb_template_template_test_case $this->assertEquals($expecting, $this->display('append_var')); } + public function test_retrieve_data() + { + $this->template->set_filenames(array('test' => 'loop_nested.html')); + + $this->template->assign_var('TEST_MORE', false); + + // @todo Change this + $this->template->assign_vars(array('ONE' => true, 'TWO' => 'two', 'THREE' => 3)); + $this->template->assign_block_vars('outer', array('POSITION' => 'O1')); + $this->template->assign_block_vars('outer.middle', array('POSITION' => 'O1M1')); + $this->template->assign_block_vars('outer', array('POSITION' => 'O2')); + $this->template->assign_block_vars('outer.middle', array('POSITION' => 'O2M1')); + $this->template->assign_block_vars('outer.middle', array('POSITION' => 'O2M2')); + $this->template->assign_block_vars('outer', array('POSITION' => 'O3')); + $this->template->assign_block_vars('outer.middle', array('POSITION' => 'O3M1')); + $this->template->assign_block_vars('outer.middle', array('POSITION' => 'O3M2', 'ONE' => true, 'TWO' => 'two', 'THREE' => 3)); + $this->template->assign_block_vars('outer.middle', array('POSITION' => 'O3M3')); + + $expect = 'outer - 0middle - 0outer - 1middle - 0middle - 1outer - 2middle - 0middle - 1middle - 2'; + $this->assertEquals($expect, str_replace(array("\n", "\r", "\t"), '', $this->display('test')), 'Ensuring template is built correctly before modification'); + + $this->assertEquals(true, $this->template->retrieve_var('ONE'), 'Retrieve a single value from the template'); + $this->assertEquals(null, $this->template->retrieve_var('FOUR'), 'Retrieve a non_existent value from the template'); + $this->assertEquals(array('TWO' => 'two', 'THREE' => 3, 'FOUR' => null), $this->template->retrieve_vars(array('TWO','THREE', 'FOUR')), 'Retrieve several variables from the template'); + + $this->assertEquals(array('POSITION' => 'O3', 'SIZE' => null), $this->template->retrieve_block_vars('outer', array('POSITION', 'SIZE')), 'Retrieve vars from a block in the template'); + $this->assertEquals(array('POSITION' => 'O2M1'), $this->template->retrieve_block_vars('outer[1].middle[0]', array('POSITION')), 'Retrieve single var from a nested indexed block in the template'); + $this->assertEquals(array('S_ROW_NUM' => 2), $this->template->retrieve_block_vars('outer.middle', array('S_ROW_NUM')), 'Retrieve automatic var from a block in the template'); + $this->assertEquals(array('POSITION' => 'O3M2', 'ONE' => true, 'TWO' => 'two', 'THREE' => 3), $this->template->retrieve_block_vars('outer[2].middle[1]', array()), 'Retrieve all vars from a block in the template'); + } + public function test_php() { global $phpbb_root_path;