Merge pull request #12639 from aniesshsethh/issue_314
[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 = ['activity_date_time' => 'activity'];
58
59 foreach ($fields as $field) {
60 foreach ($this->getSearchesWithField($field) as $savedSearch) {
61 $formValues = $savedSearch['form_values'];
62 $isRelative = $hasRelative = FALSE;
63 $relativeFieldName = $field . '_relative';
64
65 if (!empty($relativeDateMappings[$field]) && isset($formValues['relative_dates'])) {
66 if (!empty($formValues['relative_dates'][$relativeDateMappings[$field]])) {
67 $formValues[] = [$relativeFieldName, '=', $savedSearch['form_values']['relative_dates'][$relativeDateMappings[$field]]];
68 unset($formValues['relative_dates'][$relativeDateMappings[$field]]);
69 $isRelative = TRUE;
70 }
71 }
72 foreach ($formValues as $index => $formValue) {
73 if (!isset($formValue[0])) {
74 // Any actual criteria will have this key set but skip any weird lines
75 continue;
76 }
77 if (in_array($formValue[0], $fieldPossibilities)) {
78 if ($isRelative) {
79 unset($formValues[$index]);
80 }
81 else {
82 $isHigh = substr($formValue[0], -5, 5) === '_high';
83 $formValues[$index][2] = $this->getConvertedDateValue($formValue[2], $isHigh);
84 }
85 }
86 }
87 if (!$isRelative) {
88 if (!in_array($relativeFieldName, $relativeFieldNames)) {
89 $relativeFieldNames[] = $relativeFieldName;
90 $formValues[] = [$relativeFieldName, '=', 0];
91 }
92 }
93 if ($formValues !== $savedSearch['form_values']) {
94 civicrm_api3('SavedSearch', 'create', ['id' => $savedSearch['id'], 'form_values' => $formValues]);
95 }
96 }
97 }
98 }
99
100 /**
101 * Conversion routine for a form change change from = string to IN array.
102 *
103 * For example a checkbox expected [$fieldName, '=', 1]
104 * whereas select expects [$fieldName, 'IN', [1]]
105 *
106 * @param string $field
107 */
108 public function convertEqualsStringToInArray($field) {
109 foreach ($this->getSearchesWithField($field) as $savedSearch) {
110 $formValues = $savedSearch['form_values'];
111 foreach ($formValues as $index => $formValue) {
112 if ($formValue[0] === $field && !is_array($formValue[2]) && $formValue[1] === '=') {
113 $formValues[$index][1] = 'IN';
114 $formValues[$index][2] = [$formValue[2]];
115 }
116 }
117
118 if ($formValues !== $savedSearch['form_values']) {
119 civicrm_api3('SavedSearch', 'create', ['id' => $savedSearch['id'], 'form_values' => $formValues]);
120 }
121 }
122 }
123
124 /**
125 * Get converted date value.
126 *
127 * @param string $dateValue
128 * @param bool $isEndOfDay
129 * Is this the upper value in a search range? If so alter the time to
130 * get the end of day if none set.
131 *
132 * @return string
133 * $dateValue
134 */
135 protected function getConvertedDateValue($dateValue, $isEndOfDay) {
136 if (date('Y-m-d', strtotime($dateValue)) !== $dateValue
137 && date('Y-m-d H:i:s', strtotime($dateValue)) !== $dateValue
138 ) {
139 $dateValue = date('Y-m-d H:i:s', strtotime(CRM_Utils_Date::processDate($dateValue)));
140 if ($isEndOfDay) {
141 $dateValue = str_replace('00:00:00', '23:59:59', $dateValue);
142 }
143 }
144 return $dateValue;
145 }
146
147 /**
148 * Rename a smartgroup field.
149 *
150 * @param string $oldName
151 * @param string $newName
152 */
153 public function renameField($oldName, $newName) {
154 foreach ($this->getSearchesWithField($oldName) as $savedSearch) {
155 $formValues = $savedSearch['form_values'];
156 foreach ($formValues as $index => $formValue) {
157 if ($formValue[0] === $oldName) {
158 $formValues[$index][0] = $newName;
159 }
160 }
161
162 if ($formValues !== $savedSearch['form_values']) {
163 civicrm_api3('SavedSearch', 'create', ['id' => $savedSearch['id'], 'form_values' => $formValues]);
164 }
165 }
166 }
167
168 /**
169 * Rename pairs of fields
170 *
171 * @param array $pairs
172 * Array or arrays of pairs - e.g
173 * [
174 * ['old' => 'activity_date', 'new' => 'activity_date_time'],
175 * ['old' => 'activity_date_low', 'new' => 'activity_date_time_low'],
176 * ['old' => 'activity_date_high', 'new' => 'activity_date_time_high'],
177 * ['old' => 'activity_date_relative', 'new' => 'activity_date_time_relative'],
178 * ]
179 */
180 public function renameFields($pairs) {
181 foreach ($pairs as $pair) {
182 $this->renameField($pair['old'], $pair['new']);
183 }
184 }
185
186 /**
187 * @param $field
188 * @return mixed
189 */
190 protected function getSearchesWithField($field) {
191 $savedSearches = civicrm_api3('SavedSearch', 'get', [
192 'options' => ['limit' => 0],
193 'form_values' => ['LIKE' => "%{$field}%"],
194 ])['values'];
195 return $savedSearches;
196
197 }
198
199 }