Merge pull request #22532 from seamuslee001/dev_core_3034
[civicrm-core.git] / CRM / Core / Smarty / plugins / function.isValueChange.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 * CiviCRM's Smarty looped value change plugin
14 *
15 * Checks for change in value of given key
16 *
17 * @package CRM
18 * @author Allen Shaw <allen@nswebsolutions.com>
ca5cec67 19 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
20 */
21
22/**
23 * Smarty function for checking change in a property's value, for example
24 * when looping through an array.
25 *
6a488035
TO
26 * Smarty param: string $key unique identifier for this property (REQUIRED)
27 * Smarty param: mixed $value the current value of the property
28 * Smarty param: string $assign name of template variable to which to assign result
29 *
30 *
6a0b768e
TO
31 * @param array $params
32 * Template call's parameters.
16b10e64 33 * @param CRM_Core_Smarty $smarty
6a0b768e 34 * The Smarty object.
6a488035
TO
35 *
36 * @return NULL
37 */
38function smarty_function_isValueChange($params, &$smarty) {
be2fb01f 39 static $values = [];
6a488035
TO
40
41 if (empty($params['key'])) {
42 $smarty->trigger_error("Missing required parameter, 'key', in isValueChange plugin.");
389bcebf 43 return NULL;
6a488035
TO
44 }
45
46 $is_changed = FALSE;
47
85d48a9e 48 if (!array_key_exists($params['key'], $values) || strcasecmp($params['value'], $values[$params['key']]) !== 0) {
6a488035
TO
49 // if we have a new value
50
51 $is_changed = TRUE;
52
53 $values[$params['key']] = $params['value'];
54
55 // clear values on all properties added below/after this property
56 $clear = FALSE;
57 foreach ($values as $k => $dontcare) {
58 if ($clear) {
59 unset($values[$k]);
60 }
61 elseif ($params['key'] == $k) {
62 $clear = TRUE;
63 }
64 }
65 }
66
67 if ($params['assign']) {
68 $smarty->assign($params['assign'], $is_changed);
69 }
70
389bcebf 71 return NULL;
6a488035 72}