3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
33 * Class to handled upgrading any saved searches with changed patterns.
35 class CRM_Upgrade_Incremental_SmartGroups
{
38 * Version we are upgrading to.
42 protected $upgradeVersion;
47 public function getUpgradeVersion() {
48 return $this->upgradeVersion
;
52 * @param string $upgradeVersion
54 public function setUpgradeVersion($upgradeVersion) {
55 $this->upgradeVersion
= $upgradeVersion;
59 * CRM_Upgrade_Incremental_MessageTemplates constructor.
61 * @param string $upgradeVersion
63 public function __construct($upgradeVersion) {
64 $this->setUpgradeVersion($upgradeVersion);
68 * Get any conversions required for saved smart groups.
72 public function getSmartGroupConversions() {
75 'version' => '5.11.alpha1',
76 'upgrade_descriptors' => [ts('Upgrade grant smart groups to datepicker format')],
78 'function' => 'datepickerConversion',
80 'grant_application_received_date',
81 'grant_decision_date',
82 'grant_money_transfer_date',
92 * @param array $fields
94 public function datePickerConversion($fields) {
95 $fieldPossibilities = [];
96 foreach ($fields as $field) {
97 $fieldPossibilities[] = $field;
98 $fieldPossibilities[] = $field . '_high';
99 $fieldPossibilities[] = $field . '_low';
102 foreach ($fields as $field) {
103 $savedSearches = civicrm_api3('SavedSearch', 'get', [
104 'options' => ['limit' => 0],
105 'form_values' => ['LIKE' => "%{$field}%"],
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]);
114 if ($formValues !== $savedSearch['form_values']) {
115 civicrm_api3('SavedSearch', 'create', ['id' => $savedSearch['id'], 'form_values' => $formValues]);
122 * Update message templates.
124 public function updateGroups() {
125 $conversions = $this->getSmartGroupConversionsToApply();
126 foreach ($conversions as $conversion) {
127 $function = $conversion['function'];
128 $this->{$function}($conversion['fields']);
133 * Get any required template updates.
137 public function getSmartGroupConversionsToApply() {
138 $conversions = $this->getSmartGroupConversions();
140 foreach ($conversions as $conversion) {
141 if ($conversion['version'] === $this->getUpgradeVersion()) {
142 $return[] = $conversion['actions'];
149 * Get converted date value.
151 * @param string $dateValue
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
160 $dateValue = date('Y-m-d H:i:s', strtotime(CRM_Utils_Date
::processDate($dateValue)));