Commit | Line | Data |
---|---|---|
e10c0223 C |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | Copyright CiviCRM LLC. All rights reserved. | | |
5 | | | | |
6 | | This work is published under the GNU AGPLv3 license with some | | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
9 | +--------------------------------------------------------------------+ | |
10 | */ | |
11 | ||
12 | /** | |
13 | * Upgrade logic for FiveThirtyFour */ | |
14 | class CRM_Upgrade_Incremental_php_FiveThirtyFour extends CRM_Upgrade_Incremental_Base { | |
15 | ||
16 | /** | |
17 | * Compute any messages which should be displayed beforeupgrade. | |
18 | * | |
19 | * Note: This function is called iteratively for each upcoming | |
20 | * revision to the database. | |
21 | * | |
22 | * @param string $preUpgradeMessage | |
23 | * @param string $rev | |
24 | * a version number, e.g. '4.4.alpha1', '4.4.beta3', '4.4.0'. | |
25 | * @param null $currentVer | |
26 | */ | |
27 | public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) { | |
3d839125 | 28 | if ($rev === '5.34.alpha1') { |
29 | $xoauth2Value = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_MailSettings', 'protocol', 'IMAP_XOAUTH2'); | |
30 | if (!empty($xoauth2Value)) { | |
31 | if ($this->isXOAUTH2InUse($xoauth2Value)) { | |
32 | // Leaving out ts() since it's unlikely this message will ever | |
33 | // be displayed to anyone. | |
34 | $preUpgradeMessage .= '<p>This site appears to be using the IMAP_XOAUTH2 mail protocol which was part of pre-5.32 work towards OAUTH2 but was never functional and never released as an active option value. Please post at https://lab.civicrm.org/dev/core/-/issues/2264 describing how you are using this value.</p>'; | |
35 | } | |
36 | } | |
37 | } | |
e10c0223 C |
38 | } |
39 | ||
40 | /** | |
41 | * Compute any messages which should be displayed after upgrade. | |
42 | * | |
43 | * @param string $postUpgradeMessage | |
44 | * alterable. | |
45 | * @param string $rev | |
46 | * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs. | |
47 | */ | |
48 | public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) { | |
3d839125 | 49 | if ($rev === '5.34.alpha1') { |
50 | $xoauth2Value = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_MailSettings', 'protocol', 'IMAP_XOAUTH2'); | |
51 | if (!empty($xoauth2Value)) { | |
52 | if ($this->isXOAUTH2InUse($xoauth2Value)) { | |
53 | // Leaving out ts() since it's unlikely this message will ever | |
54 | // be displayed to anyone. | |
55 | $postUpgradeMessage .= '<div class="crm-error"><ul><li>This site appears to be using the IMAP_XOAUTH2 mail protocol which was part of pre-5.32 work towards OAUTH2 but was never functional and never released as an active option value. Please post at https://lab.civicrm.org/dev/core/-/issues/2264 describing how you are using this value.</li></ul></div>'; | |
56 | } | |
57 | } | |
58 | } | |
e10c0223 C |
59 | } |
60 | ||
61 | /* | |
62 | * Important! All upgrade functions MUST add a 'runSql' task. | |
63 | * Uncomment and use the following template for a new upgrade version | |
64 | * (change the x in the function name): | |
65 | */ | |
66 | ||
6a62a64e | 67 | /** |
68 | * Upgrade function. | |
69 | * | |
70 | * @param string $rev | |
71 | */ | |
72 | public function upgrade_5_34_alpha1(string $rev): void { | |
73 | $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev); | |
74 | $this->addTask('core-issue#365 - Add created_date to civicrm_action_schedule', 'addColumn', | |
75 | 'civicrm_action_schedule', 'created_date', "timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'When was the schedule reminder created.'"); | |
76 | ||
77 | $this->addTask('core-issue#365 - Add modified_date to civicrm_action_schedule', 'addColumn', | |
78 | 'civicrm_action_schedule', 'modified_date', "timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'When was the schedule reminder created.'"); | |
79 | ||
80 | $this->addTask('core-issue#365 - Add effective_start_date to civicrm_action_schedule', 'addColumn', | |
81 | 'civicrm_action_schedule', 'effective_start_date', "timestamp NULL COMMENT 'Earliest date to consider start events from.'"); | |
82 | ||
83 | $this->addTask('core-issue#365 - Add effective_end_date to civicrm_action_schedule', 'addColumn', | |
84 | 'civicrm_action_schedule', 'effective_end_date', "timestamp NULL COMMENT 'Latest date to consider end events from.'"); | |
c6d30d7f | 85 | |
86 | $this->addTask('Set defaults and required on financial type boolean fields', 'updateFinancialTypeTable'); | |
87 | $this->addTask('Set defaults and required on pledge fields', 'updatePledgeTable'); | |
3d839125 | 88 | |
89 | $this->addTask('Remove never used IMAP_XOAUTH2 option value', 'removeUnusedXOAUTH2'); | |
6a62a64e | 90 | } |
e10c0223 | 91 | |
c6d30d7f | 92 | /** |
93 | * Update financial type table to reflect recent schema changes. | |
94 | * | |
95 | * @param \CRM_Queue_TaskContext $ctx | |
96 | * | |
97 | * @return bool | |
98 | */ | |
99 | public static function updateFinancialTypeTable(CRM_Queue_TaskContext $ctx): bool { | |
100 | // Make sure there are no existing NULL values in the fields we are about to make required. | |
101 | CRM_Core_DAO::executeQuery(' | |
102 | UPDATE civicrm_financial_type | |
103 | SET is_active = COALESCE(is_active, 0), | |
104 | is_reserved = COALESCE(is_reserved, 0), | |
105 | is_deductible = COALESCE(is_deductible, 0) | |
106 | WHERE is_reserved IS NULL OR is_active IS NULL OR is_deductible IS NULL | |
107 | '); | |
108 | CRM_Core_DAO::executeQuery(" | |
109 | ALTER TABLE civicrm_financial_type | |
110 | MODIFY COLUMN `is_deductible` tinyint(4) DEFAULT 0 NOT NULL COMMENT 'Is this financial type tax-deductible? If true, contributions of this type may be fully OR partially deductible - non-deductible amount is stored in the Contribution record.', | |
111 | MODIFY COLUMN `is_reserved` tinyint(4) DEFAULT 0 NOT NULL COMMENT 'Is this a predefined system object?', | |
112 | MODIFY COLUMN `is_active` tinyint(4) DEFAULT 1 NOT NULL COMMENT 'Is this property active?' | |
113 | "); | |
114 | ||
115 | return TRUE; | |
116 | } | |
117 | ||
118 | /** | |
119 | * Update pledge table to reflect recent schema changes making fields required. | |
120 | * | |
121 | * @param \CRM_Queue_TaskContext $ctx | |
122 | * | |
123 | * @return bool | |
124 | */ | |
125 | public static function updatePledgeTable(CRM_Queue_TaskContext $ctx): bool { | |
126 | // Make sure there are no existing NULL values in the fields we are about to make required. | |
127 | CRM_Core_DAO::executeQuery(' | |
128 | UPDATE civicrm_pledge | |
129 | SET is_test = COALESCE(is_test, 0), | |
130 | frequency_unit = COALESCE(frequency_unit, "month"), | |
131 | # Cannot imagine this would be null but if it were... | |
132 | installments = COALESCE(installments, 0), | |
133 | # this does not seem plausible either. | |
134 | status_id = COALESCE(status_id, 1) | |
135 | WHERE is_test IS NULL OR frequency_unit IS NULL OR installments IS NULL OR status_id IS NULL | |
136 | '); | |
137 | CRM_Core_DAO::executeQuery(" | |
138 | ALTER TABLE civicrm_pledge | |
139 | MODIFY COLUMN `frequency_unit` varchar(8) DEFAULT 'month' NOT NULL COMMENT 'Time units for recurrence of pledge payments.', | |
140 | MODIFY COLUMN `installments` int(10) unsigned DEFAULT 1 NOT NULL COMMENT 'Total number of payments to be made.', | |
141 | MODIFY COLUMN `status_id` int(10) unsigned NOT NULL COMMENT 'Implicit foreign key to civicrm_option_values in the pledge_status option group.', | |
142 | MODIFY COLUMN `is_test` tinyint(4) DEFAULT 0 NOT NULL | |
143 | "); | |
144 | return TRUE; | |
145 | } | |
e10c0223 | 146 | |
3d839125 | 147 | /** |
148 | * This option value was never used, but check anyway if someone happens | |
149 | * to be using it and then ask them to report what they're doing with it. | |
150 | * There's no way to send a message to the user during the task, so we have | |
151 | * to check it here and also as a pre/post upgrade message. | |
152 | * Similar to removeGooglePlusOption from 5.23 except there we know some | |
153 | * people would have used it. | |
154 | */ | |
155 | public static function removeUnusedXOAUTH2(CRM_Queue_TaskContext $ctx) { | |
156 | $xoauth2Value = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_MailSettings', 'protocol', 'IMAP_XOAUTH2'); | |
157 | if (!empty($xoauth2Value)) { | |
158 | if (!self::isXOAUTH2InUse($xoauth2Value)) { | |
159 | CRM_Core_DAO::executeQuery("DELETE ov FROM civicrm_option_value ov | |
160 | INNER JOIN civicrm_option_group og | |
161 | ON (og.name = 'mail_protocol' AND ov.option_group_id = og.id) | |
162 | WHERE ov.value = %1", | |
163 | [1 => [$xoauth2Value, 'Positive']]); | |
164 | } | |
165 | } | |
166 | return TRUE; | |
167 | } | |
168 | ||
169 | /** | |
170 | * Determine if option value is enabled or used in mail settings. | |
171 | * @return bool | |
172 | */ | |
173 | private static function isXOAUTH2InUse($xoauth2Value) { | |
174 | $enabled = (bool) CRM_Core_DAO::SingleValueQuery("SELECT ov.is_active FROM civicrm_option_value ov | |
175 | INNER JOIN civicrm_option_group og | |
176 | ON (og.name = 'mail_protocol' AND ov.option_group_id = og.id) | |
177 | WHERE ov.value = %1", | |
178 | [1 => [$xoauth2Value, 'Positive']]); | |
179 | $usedInMailSettings = (bool) CRM_Core_DAO::SingleValueQuery("SELECT id FROM civicrm_mail_settings WHERE protocol = %1", [1 => [$xoauth2Value, 'Positive']]); | |
180 | return $enabled || $usedInMailSettings; | |
181 | } | |
182 | ||
e10c0223 | 183 | } |