Merge pull request #14425 from eileenmcnaughton/form_clean
[civicrm-core.git] / CRM / Upgrade / Incremental / SmartGroups.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 *
33 * Class to handled upgrading any saved searches with changed patterns.
34 */
35 class CRM_Upgrade_Incremental_SmartGroups {
36
37 /**
38 * Perform updates specified by upgrade function.
39 */
40 public function updateGroups($actions) {
41 foreach ($actions as $func => $fields) {
42 $this->{$func}($fields);
43 }
44 }
45
46 /**
47 * Convert any
48 * @param array $fields
49 */
50 public function datePickerConversion($fields) {
51 $fieldPossibilities = $relativeFieldNames = [];
52 foreach ($fields as $field) {
53 $fieldPossibilities[] = $field;
54 $fieldPossibilities[] = $field . '_high';
55 $fieldPossibilities[] = $field . '_low';
56 }
57 $relativeDateMappings = [
58 'activity_date_time' => 'activity',
59 'participant_register_date' => 'participant',
60 ];
61
62 foreach ($fields as $field) {
63 foreach ($this->getSearchesWithField($field) as $savedSearch) {
64 $formValues = $savedSearch['form_values'];
65 $isRelative = $hasRelative = FALSE;
66 $relativeFieldName = $field . '_relative';
67
68 if (!empty($relativeDateMappings[$field]) && isset($formValues['relative_dates'])) {
69 if (!empty($formValues['relative_dates'][$relativeDateMappings[$field]])) {
70 $formValues[] = [$relativeFieldName, '=', $savedSearch['form_values']['relative_dates'][$relativeDateMappings[$field]]];
71 unset($formValues['relative_dates'][$relativeDateMappings[$field]]);
72 $isRelative = TRUE;
73 }
74 }
75 foreach ($formValues as $index => $formValue) {
76 if (!isset($formValue[0])) {
77 // Any actual criteria will have this key set but skip any weird lines
78 continue;
79 }
80 if ($formValue[0] === $relativeFieldName && empty($formValue[2])) {
81 unset($formValues[$index]);;
82 }
83 elseif (in_array($formValue[0], $fieldPossibilities)) {
84 if ($isRelative) {
85 unset($formValues[$index]);
86 }
87 else {
88 $isHigh = substr($formValue[0], -5, 5) === '_high';
89 $formValues[$index][2] = $this->getConvertedDateValue($formValue[2], $isHigh);
90 }
91 }
92 }
93 if (!$isRelative) {
94 if (!in_array($relativeFieldName, $relativeFieldNames)) {
95 $relativeFieldNames[] = $relativeFieldName;
96 $formValues[] = [$relativeFieldName, '=', 0];
97 }
98 }
99 if ($formValues !== $savedSearch['form_values']) {
100 civicrm_api3('SavedSearch', 'create', ['id' => $savedSearch['id'], 'form_values' => $formValues]);
101 }
102 }
103 }
104 }
105
106 /**
107 * Conversion routine for a form change change from = string to IN array.
108 *
109 * For example a checkbox expected [$fieldName, '=', 1]
110 * whereas select expects [$fieldName, 'IN', [1]]
111 *
112 * @param string $field
113 */
114 public function convertEqualsStringToInArray($field) {
115 foreach ($this->getSearchesWithField($field) as $savedSearch) {
116 $formValues = $savedSearch['form_values'];
117 foreach ($formValues as $index => $formValue) {
118 if ($formValue[0] === $field && !is_array($formValue[2]) && $formValue[1] === '=') {
119 $formValues[$index][1] = 'IN';
120 $formValues[$index][2] = [$formValue[2]];
121 }
122 }
123
124 if ($formValues !== $savedSearch['form_values']) {
125 civicrm_api3('SavedSearch', 'create', ['id' => $savedSearch['id'], 'form_values' => $formValues]);
126 }
127 }
128 }
129
130 /**
131 * Get converted date value.
132 *
133 * @param string $dateValue
134 * @param bool $isEndOfDay
135 * Is this the upper value in a search range? If so alter the time to
136 * get the end of day if none set.
137 *
138 * @return string
139 * $dateValue
140 */
141 protected function getConvertedDateValue($dateValue, $isEndOfDay) {
142 if (date('Y-m-d', strtotime($dateValue)) !== $dateValue
143 && date('Y-m-d H:i:s', strtotime($dateValue)) !== $dateValue
144 ) {
145 $dateValue = date('Y-m-d H:i:s', strtotime(CRM_Utils_Date::processDate($dateValue)));
146 if ($isEndOfDay) {
147 $dateValue = str_replace('00:00:00', '23:59:59', $dateValue);
148 }
149 }
150 return $dateValue;
151 }
152
153 /**
154 * Rename a smartgroup field.
155 *
156 * @param string $oldName
157 * @param string $newName
158 */
159 public function renameField($oldName, $newName) {
160 foreach ($this->getSearchesWithField($oldName) as $savedSearch) {
161 $formValues = $savedSearch['form_values'];
162 foreach ($formValues as $index => $formValue) {
163 if ($formValue[0] === $oldName) {
164 $formValues[$index][0] = $newName;
165 }
166 }
167
168 if ($formValues !== $savedSearch['form_values']) {
169 civicrm_api3('SavedSearch', 'create', ['id' => $savedSearch['id'], 'form_values' => $formValues]);
170 }
171 }
172 }
173
174 /**
175 * Rename pairs of fields
176 *
177 * @param array $pairs
178 * Array or arrays of pairs - e.g
179 * [
180 * ['old' => 'activity_date', 'new' => 'activity_date_time'],
181 * ['old' => 'activity_date_low', 'new' => 'activity_date_time_low'],
182 * ['old' => 'activity_date_high', 'new' => 'activity_date_time_high'],
183 * ['old' => 'activity_date_relative', 'new' => 'activity_date_time_relative'],
184 * ]
185 */
186 public function renameFields($pairs) {
187 foreach ($pairs as $pair) {
188 $this->renameField($pair['old'], $pair['new']);
189 }
190 }
191
192 /**
193 * @param $field
194 * @return mixed
195 */
196 protected function getSearchesWithField($field) {
197 $savedSearches = civicrm_api3('SavedSearch', 'get', [
198 'options' => ['limit' => 0],
199 'form_values' => ['LIKE' => "%{$field}%"],
200 ])['values'];
201 return $savedSearches;
202
203 }
204
205 }