Merge pull request #22532 from seamuslee001/dev_core_3034
[civicrm-core.git] / CRM / Core / Smarty / plugins / function.sectionTotal.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * CiviCRM's Smarty report section totals plugin
14 *
15 * Prints the correct report section total based on the given key and order in the section hierarchy
16 *
17 * @package CRM
18 * @author Allen Shaw <allen@nswebsolutions.com>
19 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 */
21
22 /**
23 * Smarty block function for printing the correct report section total
24 *
25 * Smarty param: string $key value of the current section column
26 * Smarty param: int $depth the depth of the current section
27 * (sections declared first have lesser depth, starting at 0)
28 *
29 * @param array $params
30 * Template call's parameters.
31 * @param CRM_Core_Smarty $smarty
32 * The Smarty object.
33 *
34 * @return string
35 * the string, translated by gettext
36 */
37 function smarty_function_sectionTotal($params, &$smarty) {
38 /* section totals are stored in template variable 'sectionTotals',
39 * which is a two-dimensional array keyed to a string which is a delimited
40 * concatenation (using CRM_Core_DAO::VALUE_SEPARATOR) of ordered permutations
41 * of section header values, e.g.,
42 * 'foo' => 10,
43 * 'foo[VALUE_SEAPARATOR]bar' => 5,
44 * 'foo[VALUE_SEAPARATOR]bar2' => 5
45 * Note: This array is created and assigned to the template in CRM_Report_Form::sectionTotals()
46 */
47
48 static $sectionValues = [];
49
50 // move back in the stack, if necessary
51 if (count($sectionValues) > $params['depth']) {
52 $sectionValues = array_slice($sectionValues, 0, $params['depth']);
53 }
54
55 // append the current value
56 $sectionValues[] = $params['key'];
57
58 // concatenate with pipes to build the right key
59 $totalsKey = implode(CRM_Core_DAO::VALUE_SEPARATOR, $sectionValues);
60
61 // return the corresponding total
62 return $smarty->_tpl_vars['sectionTotals'][$totalsKey];
63 }