Merge pull request #15370 from JMAConsulting/core-692-2
[civicrm-core.git] / CRM / Upgrade / Incremental / SmartGroups.php
CommitLineData
7015248a 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 */
35class CRM_Upgrade_Incremental_SmartGroups {
36
37 /**
ac241c34 38 * Perform updates specified by upgrade function.
7015248a 39 */
ac241c34
CW
40 public function updateGroups($actions) {
41 foreach ($actions as $func => $fields) {
6e7cc0f5
SL
42 if ($func == 'renameField') {
43 foreach ($fields as $fieldConversion) {
44 $this->{$func}($fieldConversion['old'], $fieldConversion['new']);
45 }
46 }
47 else {
48 $this->{$func}($fields);
49 }
ac241c34 50 }
7015248a 51 }
52
53 /**
54 * Convert any
55 * @param array $fields
56 */
57 public function datePickerConversion($fields) {
929d5a15 58 $fieldPossibilities = $relativeFieldNames = [];
30898ee8 59 $relativeDateMappings = [
60 'activity_date_time' => 'activity',
61 'participant_register_date' => 'participant',
6e7cc0f5
SL
62 'receive_date' => 'contribution',
63 'contribution_cancel_date' => 'contribution_cancel',
1915f8d2
SL
64 'membership_join_date' => 'member_join',
65 'membership_start_date' => 'member_start',
66 'membership_end_date' => 'member_end',
67 'pledge_payment_scheduled_date' => 'pledge_payment',
68 'pledge_create_date' => 'pledge_create',
69 'pledge_end_date' => 'pledge_end',
70 'pledge_start_date' => 'pledge_start',
30898ee8 71 ];
7015248a 72
73 foreach ($fields as $field) {
b4d67eb2 74 foreach ($this->getSearchesWithField($field) as $savedSearch) {
1915f8d2
SL
75 // Only populate field possibilities as we go to convert each field
76 $fieldPossibilities[] = $field;
77 $fieldPossibilities[] = $field . '_high';
78 $fieldPossibilities[] = $field . '_low';
7015248a 79 $formValues = $savedSearch['form_values'];
929d5a15 80 $isRelative = $hasRelative = FALSE;
81 $relativeFieldName = $field . '_relative';
82
83 if (!empty($relativeDateMappings[$field]) && isset($formValues['relative_dates'])) {
84 if (!empty($formValues['relative_dates'][$relativeDateMappings[$field]])) {
85 $formValues[] = [$relativeFieldName, '=', $savedSearch['form_values']['relative_dates'][$relativeDateMappings[$field]]];
86 unset($formValues['relative_dates'][$relativeDateMappings[$field]]);
87 $isRelative = TRUE;
88 }
89 }
7015248a 90 foreach ($formValues as $index => $formValue) {
8e80025c 91 if (!isset($formValue[0])) {
92 // Any actual criteria will have this key set but skip any weird lines
93 continue;
94 }
30898ee8 95 if ($formValue[0] === $relativeFieldName && empty($formValue[2])) {
96 unset($formValues[$index]);;
97 }
98 elseif (in_array($formValue[0], $fieldPossibilities)) {
929d5a15 99 if ($isRelative) {
100 unset($formValues[$index]);
101 }
102 else {
103 $isHigh = substr($formValue[0], -5, 5) === '_high';
104 $formValues[$index][2] = $this->getConvertedDateValue($formValue[2], $isHigh);
105 }
106 }
107 }
108 if (!$isRelative) {
109 if (!in_array($relativeFieldName, $relativeFieldNames)) {
110 $relativeFieldNames[] = $relativeFieldName;
111 $formValues[] = [$relativeFieldName, '=', 0];
7015248a 112 }
113 }
114 if ($formValues !== $savedSearch['form_values']) {
115 civicrm_api3('SavedSearch', 'create', ['id' => $savedSearch['id'], 'form_values' => $formValues]);
116 }
117 }
118 }
119 }
120
b4d67eb2 121 /**
122 * Conversion routine for a form change change from = string to IN array.
123 *
124 * For example a checkbox expected [$fieldName, '=', 1]
125 * whereas select expects [$fieldName, 'IN', [1]]
126 *
127 * @param string $field
128 */
129 public function convertEqualsStringToInArray($field) {
130 foreach ($this->getSearchesWithField($field) as $savedSearch) {
131 $formValues = $savedSearch['form_values'];
132 foreach ($formValues as $index => $formValue) {
133 if ($formValue[0] === $field && !is_array($formValue[2]) && $formValue[1] === '=') {
134 $formValues[$index][1] = 'IN';
135 $formValues[$index][2] = [$formValue[2]];
136 }
137 }
138
139 if ($formValues !== $savedSearch['form_values']) {
140 civicrm_api3('SavedSearch', 'create', ['id' => $savedSearch['id'], 'form_values' => $formValues]);
141 }
142 }
143 }
144
7015248a 145 /**
146 * Get converted date value.
147 *
148 * @param string $dateValue
929d5a15 149 * @param bool $isEndOfDay
150 * Is this the upper value in a search range? If so alter the time to
151 * get the end of day if none set.
7015248a 152 *
153 * @return string
154 * $dateValue
155 */
929d5a15 156 protected function getConvertedDateValue($dateValue, $isEndOfDay) {
7015248a 157 if (date('Y-m-d', strtotime($dateValue)) !== $dateValue
158 && date('Y-m-d H:i:s', strtotime($dateValue)) !== $dateValue
159 ) {
160 $dateValue = date('Y-m-d H:i:s', strtotime(CRM_Utils_Date::processDate($dateValue)));
929d5a15 161 if ($isEndOfDay) {
162 $dateValue = str_replace('00:00:00', '23:59:59', $dateValue);
163 }
7015248a 164 }
165 return $dateValue;
166 }
167
7bab3351 168 /**
169 * Rename a smartgroup field.
170 *
171 * @param string $oldName
172 * @param string $newName
173 */
174 public function renameField($oldName, $newName) {
175 foreach ($this->getSearchesWithField($oldName) as $savedSearch) {
176 $formValues = $savedSearch['form_values'];
177 foreach ($formValues as $index => $formValue) {
178 if ($formValue[0] === $oldName) {
179 $formValues[$index][0] = $newName;
180 }
181 }
182
183 if ($formValues !== $savedSearch['form_values']) {
184 civicrm_api3('SavedSearch', 'create', ['id' => $savedSearch['id'], 'form_values' => $formValues]);
185 }
186 }
187 }
188
189 /**
190 * Rename pairs of fields
191 *
192 * @param array $pairs
193 * Array or arrays of pairs - e.g
194 * [
195 * ['old' => 'activity_date', 'new' => 'activity_date_time'],
196 * ['old' => 'activity_date_low', 'new' => 'activity_date_time_low'],
197 * ['old' => 'activity_date_high', 'new' => 'activity_date_time_high'],
198 * ['old' => 'activity_date_relative', 'new' => 'activity_date_time_relative'],
199 * ]
200 */
201 public function renameFields($pairs) {
202 foreach ($pairs as $pair) {
203 $this->renameField($pair['old'], $pair['new']);
204 }
205 }
206
b4d67eb2 207 /**
208 * @param $field
209 * @return mixed
210 */
211 protected function getSearchesWithField($field) {
212 $savedSearches = civicrm_api3('SavedSearch', 'get', [
213 'options' => ['limit' => 0],
214 'form_values' => ['LIKE' => "%{$field}%"],
215 ])['values'];
216 return $savedSearches;
7bab3351 217
b4d67eb2 218 }
219
7015248a 220}