Merge pull request #14928 from lcdservices/dev-core-1158
[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 * $Id$
21 */
22
23 /**
24 * Smarty block function for printing the correct report section total
25 *
26 * Smarty param: string $key value of the current section column
27 * Smarty param: int $depth the depth of the current section
28 * (sections declared first have lesser depth, starting at 0)
29 *
30 * @param array $params
31 * Template call's parameters.
32 * @param CRM_Core_Smarty $smarty
33 * The Smarty object.
34 *
35 * @return string
36 * the string, translated by gettext
37 */
38 function smarty_function_sectionTotal($params, &$smarty) {
39 /* section totals are stored in template variable 'sectionTotals',
40 * which is a two-dimensional array keyed to a string which is a delimited
41 * concatenation (using CRM_Core_DAO::VALUE_SEPARATOR) of ordered permutations
42 * of section header values, e.g.,
43 * 'foo' => 10,
44 * 'foo[VALUE_SEAPARATOR]bar' => 5,
45 * 'foo[VALUE_SEAPARATOR]bar2' => 5
46 * Note: This array is created and assigned to the template in CRM_Report_Form::sectionTotals()
47 */
48
49 static $sectionValues = [];
50
51 // move back in the stack, if necessary
52 if (count($sectionValues) > $params['depth']) {
53 $sectionValues = array_slice($sectionValues, 0, $params['depth']);
54 }
55
56 // append the current value
57 $sectionValues[] = $params['key'];
58
59 // concatenate with pipes to build the right key
60 $totalsKey = implode(CRM_Core_DAO::VALUE_SEPARATOR, $sectionValues);
61
62 // return the corresponding total
63 return $smarty->_tpl_vars['sectionTotals'][$totalsKey];
64 }