Add upgrade routine
[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) {
42 $this->{$func}($fields);
43 }
7015248a 44 }
45
46 /**
47 * Convert any
48 * @param array $fields
49 */
50 public function datePickerConversion($fields) {
51 $fieldPossibilities = [];
52 foreach ($fields as $field) {
53 $fieldPossibilities[] = $field;
54 $fieldPossibilities[] = $field . '_high';
55 $fieldPossibilities[] = $field . '_low';
56 }
57
58 foreach ($fields as $field) {
b4d67eb2 59 foreach ($this->getSearchesWithField($field) as $savedSearch) {
7015248a 60 $formValues = $savedSearch['form_values'];
61 foreach ($formValues as $index => $formValue) {
62 if (in_array($formValue[0], $fieldPossibilities)) {
63 $formValues[$index][2] = $this->getConvertedDateValue($formValue[2]);
64 }
65 }
66 if ($formValues !== $savedSearch['form_values']) {
67 civicrm_api3('SavedSearch', 'create', ['id' => $savedSearch['id'], 'form_values' => $formValues]);
68 }
69 }
70 }
71 }
72
b4d67eb2 73 /**
74 * Conversion routine for a form change change from = string to IN array.
75 *
76 * For example a checkbox expected [$fieldName, '=', 1]
77 * whereas select expects [$fieldName, 'IN', [1]]
78 *
79 * @param string $field
80 */
81 public function convertEqualsStringToInArray($field) {
82 foreach ($this->getSearchesWithField($field) as $savedSearch) {
83 $formValues = $savedSearch['form_values'];
84 foreach ($formValues as $index => $formValue) {
85 if ($formValue[0] === $field && !is_array($formValue[2]) && $formValue[1] === '=') {
86 $formValues[$index][1] = 'IN';
87 $formValues[$index][2] = [$formValue[2]];
88 }
89 }
90
91 if ($formValues !== $savedSearch['form_values']) {
92 civicrm_api3('SavedSearch', 'create', ['id' => $savedSearch['id'], 'form_values' => $formValues]);
93 }
94 }
95 }
96
7015248a 97 /**
98 * Get converted date value.
99 *
100 * @param string $dateValue
101 *
102 * @return string
103 * $dateValue
104 */
105 protected function getConvertedDateValue($dateValue) {
106 if (date('Y-m-d', strtotime($dateValue)) !== $dateValue
107 && date('Y-m-d H:i:s', strtotime($dateValue)) !== $dateValue
108 ) {
109 $dateValue = date('Y-m-d H:i:s', strtotime(CRM_Utils_Date::processDate($dateValue)));
110 }
111 return $dateValue;
112 }
113
7bab3351 114 /**
115 * Rename a smartgroup field.
116 *
117 * @param string $oldName
118 * @param string $newName
119 */
120 public function renameField($oldName, $newName) {
121 foreach ($this->getSearchesWithField($oldName) as $savedSearch) {
122 $formValues = $savedSearch['form_values'];
123 foreach ($formValues as $index => $formValue) {
124 if ($formValue[0] === $oldName) {
125 $formValues[$index][0] = $newName;
126 }
127 }
128
129 if ($formValues !== $savedSearch['form_values']) {
130 civicrm_api3('SavedSearch', 'create', ['id' => $savedSearch['id'], 'form_values' => $formValues]);
131 }
132 }
133 }
134
135 /**
136 * Rename pairs of fields
137 *
138 * @param array $pairs
139 * Array or arrays of pairs - e.g
140 * [
141 * ['old' => 'activity_date', 'new' => 'activity_date_time'],
142 * ['old' => 'activity_date_low', 'new' => 'activity_date_time_low'],
143 * ['old' => 'activity_date_high', 'new' => 'activity_date_time_high'],
144 * ['old' => 'activity_date_relative', 'new' => 'activity_date_time_relative'],
145 * ]
146 */
147 public function renameFields($pairs) {
148 foreach ($pairs as $pair) {
149 $this->renameField($pair['old'], $pair['new']);
150 }
151 }
152
b4d67eb2 153 /**
154 * @param $field
155 * @return mixed
156 */
157 protected function getSearchesWithField($field) {
158 $savedSearches = civicrm_api3('SavedSearch', 'get', [
159 'options' => ['limit' => 0],
160 'form_values' => ['LIKE' => "%{$field}%"],
161 ])['values'];
162 return $savedSearches;
7bab3351 163
b4d67eb2 164 }
165
7015248a 166}