Add upgrade routine to convert on_hold to an array for sites with
[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 /**
38 * Version we are upgrading to.
39 *
40 * @var string
41 */
42 protected $upgradeVersion;
43
44 /**
45 * @return string
46 */
47 public function getUpgradeVersion() {
48 return $this->upgradeVersion;
49 }
50
51 /**
52 * @param string $upgradeVersion
53 */
54 public function setUpgradeVersion($upgradeVersion) {
55 $this->upgradeVersion = $upgradeVersion;
56 }
57
58 /**
59 * CRM_Upgrade_Incremental_MessageTemplates constructor.
60 *
61 * @param string $upgradeVersion
62 */
63 public function __construct($upgradeVersion) {
64 $this->setUpgradeVersion($upgradeVersion);
65 }
66
67 /**
68 * Get any conversions required for saved smart groups.
69 *
70 * @return array
71 */
72 public function getSmartGroupConversions() {
73 return [
74 [
75 'version' => '5.11.alpha1',
76 'upgrade_descriptors' => [ts('Upgrade grant smart groups to datepicker format')],
77 'actions' => [
78 'function' => 'datepickerConversion',
79 'fields' => [
80 'grant_application_received_date',
81 'grant_decision_date',
82 'grant_money_transfer_date',
83 'grant_due_date'
84 ]
85 ]
86 ]
87 ];
88 }
89
90 /**
91 * Convert any
92 * @param array $fields
93 */
94 public function datePickerConversion($fields) {
95 $fieldPossibilities = [];
96 foreach ($fields as $field) {
97 $fieldPossibilities[] = $field;
98 $fieldPossibilities[] = $field . '_high';
99 $fieldPossibilities[] = $field . '_low';
100 }
101
102 foreach ($fields as $field) {
abe5b62c 103 foreach ($this->getSearchesWithField($field) as $savedSearch) {
7015248a 104 $formValues = $savedSearch['form_values'];
105 foreach ($formValues as $index => $formValue) {
106 if (in_array($formValue[0], $fieldPossibilities)) {
107 $formValues[$index][2] = $this->getConvertedDateValue($formValue[2]);
108 }
109 }
110 if ($formValues !== $savedSearch['form_values']) {
111 civicrm_api3('SavedSearch', 'create', ['id' => $savedSearch['id'], 'form_values' => $formValues]);
112 }
113 }
114 }
115 }
116
abe5b62c 117 /**
118 * Conversion routine for a form change change from = string to IN array.
119 *
120 * For example a checkbox expected [$fieldName, '=', 1]
121 * whereas select expects [$fieldName, 'IN', [1]]
122 *
123 * @param string $field
124 */
125 public function convertEqualsStringToInArray($field) {
126 foreach ($this->getSearchesWithField($field) as $savedSearch) {
127 $formValues = $savedSearch['form_values'];
128 foreach ($formValues as $index => $formValue) {
129 if ($formValue[0] === $field && !is_array($formValue[2]) && $formValue[1] === '=') {
130 $formValues[$index][1] = 'IN';
131 $formValues[$index][2] = [$formValue[2]];
132 }
133 }
134
135 if ($formValues !== $savedSearch['form_values']) {
136 civicrm_api3('SavedSearch', 'create', ['id' => $savedSearch['id'], 'form_values' => $formValues]);
137 }
138 }
139 }
140
7015248a 141 /**
142 * Update message templates.
143 */
144 public function updateGroups() {
145 $conversions = $this->getSmartGroupConversionsToApply();
146 foreach ($conversions as $conversion) {
147 $function = $conversion['function'];
148 $this->{$function}($conversion['fields']);
149 }
150 }
151
152 /**
153 * Get any required template updates.
154 *
155 * @return array
156 */
157 public function getSmartGroupConversionsToApply() {
158 $conversions = $this->getSmartGroupConversions();
159 $return = [];
160 foreach ($conversions as $conversion) {
161 if ($conversion['version'] === $this->getUpgradeVersion()) {
162 $return[] = $conversion['actions'];
163 }
164 }
165 return $return;
166 }
167
168 /**
169 * Get converted date value.
170 *
171 * @param string $dateValue
172 *
173 * @return string
174 * $dateValue
175 */
176 protected function getConvertedDateValue($dateValue) {
177 if (date('Y-m-d', strtotime($dateValue)) !== $dateValue
178 && date('Y-m-d H:i:s', strtotime($dateValue)) !== $dateValue
179 ) {
180 $dateValue = date('Y-m-d H:i:s', strtotime(CRM_Utils_Date::processDate($dateValue)));
181 }
182 return $dateValue;
183 }
184
abe5b62c 185 /**
186 * @param $field
187 * @return mixed
188 */
189 protected function getSearchesWithField($field) {
190 $savedSearches = civicrm_api3('SavedSearch', 'get', [
191 'options' => ['limit' => 0],
192 'form_values' => ['LIKE' => "%{$field}%"],
193 ])['values'];
194 return $savedSearches;
195 }
196
7015248a 197}