c22daf39416a6482624a85b3edd8efea07a8c5f8
[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 * 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) {
103 $savedSearches = civicrm_api3('SavedSearch', 'get', [
104 'options' => ['limit' => 0],
105 'form_values' => ['LIKE' => "%{$field}%"],
106 ])['values'];
107 foreach ($savedSearches as $savedSearch) {
108 $formValues = $savedSearch['form_values'];
109 foreach ($formValues as $index => $formValue) {
110 if (in_array($formValue[0], $fieldPossibilities)) {
111 $formValues[$index][2] = $this->getConvertedDateValue($formValue[2]);
112 }
113 }
114 if ($formValues !== $savedSearch['form_values']) {
115 civicrm_api3('SavedSearch', 'create', ['id' => $savedSearch['id'], 'form_values' => $formValues]);
116 }
117 }
118 }
119 }
120
121 /**
122 * Update message templates.
123 */
124 public function updateGroups() {
125 $conversions = $this->getSmartGroupConversionsToApply();
126 foreach ($conversions as $conversion) {
127 $function = $conversion['function'];
128 $this->{$function}($conversion['fields']);
129 }
130 }
131
132 /**
133 * Get any required template updates.
134 *
135 * @return array
136 */
137 public function getSmartGroupConversionsToApply() {
138 $conversions = $this->getSmartGroupConversions();
139 $return = [];
140 foreach ($conversions as $conversion) {
141 if ($conversion['version'] === $this->getUpgradeVersion()) {
142 $return[] = $conversion['actions'];
143 }
144 }
145 return $return;
146 }
147
148 /**
149 * Get converted date value.
150 *
151 * @param string $dateValue
152 *
153 * @return string
154 * $dateValue
155 */
156 protected function getConvertedDateValue($dateValue) {
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)));
161 }
162 return $dateValue;
163 }
164
165 }