Merge pull request #15818 from colemanw/fields
[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 * $Id$
21 */
22
23/**
24 * Smarty function for checking change in a property's value, for example
25 * when looping through an array.
26 *
6a488035
TO
27 * Smarty param: string $key unique identifier for this property (REQUIRED)
28 * Smarty param: mixed $value the current value of the property
29 * Smarty param: string $assign name of template variable to which to assign result
30 *
31 *
6a0b768e
TO
32 * @param array $params
33 * Template call's parameters.
16b10e64 34 * @param CRM_Core_Smarty $smarty
6a0b768e 35 * The Smarty object.
6a488035
TO
36 *
37 * @return NULL
38 */
39function smarty_function_isValueChange($params, &$smarty) {
be2fb01f 40 static $values = [];
6a488035
TO
41
42 if (empty($params['key'])) {
43 $smarty->trigger_error("Missing required parameter, 'key', in isValueChange plugin.");
389bcebf 44 return NULL;
6a488035
TO
45 }
46
47 $is_changed = FALSE;
48
85d48a9e 49 if (!array_key_exists($params['key'], $values) || strcasecmp($params['value'], $values[$params['key']]) !== 0) {
6a488035
TO
50 // if we have a new value
51
52 $is_changed = TRUE;
53
54 $values[$params['key']] = $params['value'];
55
56 // clear values on all properties added below/after this property
57 $clear = FALSE;
58 foreach ($values as $k => $dontcare) {
59 if ($clear) {
60 unset($values[$k]);
61 }
62 elseif ($params['key'] == $k) {
63 $clear = TRUE;
64 }
65 }
66 }
67
68 if ($params['assign']) {
69 $smarty->assign($params['assign'], $is_changed);
70 }
71
389bcebf 72 return NULL;
6a488035 73}