Components - Use extracted function
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FiveFortyThree.php
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 FiveFortyThree
14 */
15 class CRM_Upgrade_Incremental_php_FiveFortyThree extends CRM_Upgrade_Incremental_Base {
16
17 /**
18 * Compute any messages which should be displayed beforeupgrade.
19 *
20 * Note: This function is called iteratively for each incremental upgrade step.
21 * There must be a concrete step (eg 'X.Y.Z.mysql.tpl' or 'upgrade_X_Y_Z()').
22 *
23 * @param string $preUpgradeMessage
24 * @param string $rev
25 * a version number, e.g. '4.4.alpha1', '4.4.beta3', '4.4.0'.
26 * @param null $currentVer
27 */
28 public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL): void {
29 if ($rev === '5.43.alpha1' && CRM_Core_Component::isEnabled('CiviCase')) {
30 $preUpgradeMessage .= '<p>' . ts('Minor changes have been made to how the tokens to render case.is_deleted, case.created_date and case.modified_date. See https://docs.civicrm.org/sysadmin/en/latest/upgrade/version-specific/ for more') . '</p>';
31 }
32 }
33
34 /**
35 * Upgrade function.
36 *
37 * @param string $rev
38 */
39 public function upgrade_5_43_alpha1(string $rev): void {
40 $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
41 $this->addTask('Fix DB Collation if needed on the relationship cache table', 'fixRelationshipCacheTableCollation');
42 $this->addTask('Make mapping field foreign key cascade delete', 'alterMappingFK');
43 $this->addTask('Replace legacy displayName smarty token in Online contribution workflow template',
44 'updateMessageToken', 'contribution_online_receipt', '$displayName', 'contact.display_name', $rev
45 );
46 $this->addTask('Replace legacy first_name smarty token in Online contribution workflow template',
47 'updateMessageToken', 'contribution_online_receipt', '$first_name', 'contact.first_name', $rev
48 );
49 $this->addTask('Replace legacy last_name smarty token in Online contribution workflow template',
50 'updateMessageToken', 'contribution_online_receipt', '$last_name', 'contact.last_name', $rev
51 );
52 $this->addTask('Replace membership status token in action schedule',
53 'updateActionScheduleToken', 'membership.status', 'membership.status_id:label', $rev
54 );
55 $this->addTask('Replace membership type token in action schedule',
56 'updateActionScheduleToken', 'membership.type', 'membership.membership_type_id:label', $rev
57 );
58 $this->addTask('Replace event type token in action schedule',
59 'updateActionScheduleToken', 'event.event_type_id', 'event.event_type_id:label', $rev
60 );
61 $this->addTask('Replace event balance in action schedule',
62 'updateActionScheduleToken', 'event.balance', 'participant.balance', $rev
63 );
64 $this->addTask('Replace event fee amount in action schedule',
65 'updateActionScheduleToken', 'event.fee_amount', 'participant.fee_amount', $rev
66 );
67 $this->addTask('Replace contact.preferred_communication_method in action schedule',
68 'updateActionScheduleToken', 'contact.preferred_communication_method', 'contact.preferred_communication_method:label', $rev
69 );
70 $this->addTask('Replace event event_id in action schedule',
71 'updateActionScheduleToken', 'event.event_id', 'event.id', $rev
72 );
73 $this->addTask('Replace duplicate event title token in event badges',
74 'updatePrintLabelToken', 'participant.event_title', 'event.title', $rev
75 );
76 $this->addTask('Replace duplicate event start date token in event badges',
77 'updatePrintLabelToken', 'participant.event_start_date', 'event.start_date', $rev
78 );
79 $this->addTask('Replace duplicate event end date token in event badges',
80 'updatePrintLabelToken', 'participant.event_end_date', 'event.end_date', $rev
81 );
82 $this->addTask('Update participant status id token in event badges',
83 'updatePrintLabelToken', 'participant.participant_status_id', 'participant.status_id', $rev
84 );
85 $this->addTask('Update participant role id token in event badges',
86 'updatePrintLabelToken', 'participant.participant_role_id', 'participant.role_id', $rev
87 );
88 $this->addTask('Update participant role label token in event badges',
89 'updatePrintLabelToken', 'participant.participant_role', 'participant.role_id:label', $rev
90 );
91 $this->addTask('Update participant register date token in event badges',
92 'updatePrintLabelToken', 'participant.participant_register_date', 'participant.register_date', $rev
93 );
94 $this->addTask('Update participant source token in event badges',
95 'updatePrintLabelToken', 'participant.participant_source', 'participant.source', $rev
96 );
97 $this->addTask('Update participant fee level token in event badges',
98 'updatePrintLabelToken', 'participant.participant_fee_level', 'participant.fee_level', $rev
99 );
100 $this->addTask('Update participant fee amount token in event badges',
101 'updatePrintLabelToken', 'participant.participant_fee_amount', 'participant.fee_amount', $rev
102 );
103 $this->addTask('Update participant registered by id token in event badges',
104 'updatePrintLabelToken', 'participant.participant_registered_by_id', 'participant.registered_by_id', $rev
105 );
106 $this->addTask('Update preferred_communication_method token in saved message templates',
107 'updateMessageToken', '', 'contact.preferred_communication_method', 'contact.preferred_communication_method:label', $rev
108 );
109 $this->addTask('Update contribution status token in saved message templates',
110 'updateMessageToken', '', 'contribution.contribution_status', 'contribution.contribution_status_id:label', $rev
111 );
112 $this->addTask('Update campaign token in saved message templates',
113 'updateMessageToken', '', 'contribution.campaign', 'contribution.campaign_id:label', $rev);
114 $this->addTask('Update start date token in event badges',
115 'updatePrintLabelToken', 'event.start_date', 'event.start_date|crmDate:"%B %E%f', $rev
116 );
117 $this->addTask('Update end date token in event badges',
118 'updatePrintLabelToken', 'event.end_date', 'event.end_date|crmDate:"%B %E%f', $rev
119 );
120 $this->addTask('Update event id token in event badges',
121 'updatePrintLabelToken', 'event.event_id', 'participant.event_id', $rev
122 );
123 // prefix_id was a short lived variant around civicrm 4.4 - it's unlikely any
124 // cases exist, but they will resolve as an integer if not updated
125 $this->addTask('Update legacy prefix id token in greetings (use {contact.prefix_id:label})',
126 'updateGreetingOptions', 'contact.prefix_id', 'contact.prefix_id:label', $rev
127 );
128 // suffix_id was a short lived variant around civicrm 4.4 - it's unlikely any
129 // cases exist, but they will resolve as an integer if not updated
130 $this->addTask('Update legacy suffix id token in greetings (use {contact.suffix_id:label})',
131 'updateGreetingOptions', 'contact.suffix_id', 'contact.suffix_id:label', $rev
132 );
133 // individual_prefix still works but is now deprecated
134 $this->addTask('Update individual_prefix token in greetings (use {contact.prefix_id:label})',
135 'updateGreetingOptions', 'contact.individual_prefix', 'contact.prefix_id:label', $rev
136 );
137 // individual_prefix still works but is now deprecated
138 $this->addTask('Update individual_suffix token in greetings (use {contact.suffix_id:label})',
139 'updateGreetingOptions', 'contact.individual_suffix', 'contact.suffix_id:label', $rev
140 );
141
142 $this->addTask('Set note_date field to NOT NULL', 'setNoteDateFieldNotNull');
143 }
144
145 /**
146 * @param \CRM_Queue_TaskContext $ctx
147 *
148 * @return bool
149 */
150 public static function alterMappingFK(CRM_Queue_TaskContext $ctx): bool {
151 CRM_Core_BAO_SchemaHandler::safeRemoveFK('civicrm_mapping_field', 'FK_civicrm_mapping_field_mapping_id');
152 CRM_Core_DAO::executeQuery('
153 ALTER TABLE civicrm_mapping_field
154 ADD CONSTRAINT `FK_civicrm_mapping_field_mapping_id`
155 FOREIGN KEY (`mapping_id`)
156 REFERENCES `civicrm_mapping`(`id`) ON DELETE CASCADE
157 ', [], TRUE, NULL, FALSE, FALSE);
158 return TRUE;
159 }
160
161 public static function fixRelationshipCacheTableCollation():bool {
162 $contactTableCollation = CRM_Core_BAO_SchemaHandler::getInUseCollation();
163 $dao = CRM_Core_DAO::executeQuery('SHOW TABLE STATUS LIKE \'civicrm_relationship_cache\'');
164 $dao->fetch();
165 $relationshipCacheCollation = $dao->Collation;
166 $characterSet = 'utf8';
167 if (stripos($contactTableCollation, 'utf8mb4') !== FALSE) {
168 $characterSet = 'utf8mb4';
169 }
170 if ($contactTableCollation !== $relationshipCacheCollation) {
171 CRM_Core_BAO_SchemaHandler::migrateUtf8mb4(($characterSet === 'utf8mb4' ? FALSE : TRUE), ['%civicrm_relationship_cache%']);
172 }
173 return TRUE;
174 }
175
176 /**
177 * Ensure note_date is NOT NULL
178 *
179 * Since note_date is exposed in the UI, we want to ensure an empty value
180 * results in CURRENT_TIME, not a NULL date.
181 */
182 public static function setNoteDateFieldNotNull(): bool {
183 $i18nRewrite = FALSE;
184 $query = "UPDATE civicrm_note SET note_date = created_date WHERE note_date IS NULL";
185 CRM_Core_DAO::executeQuery($query, [], TRUE, NULL, FALSE, $i18nRewrite);
186 $query = "ALTER TABLE civicrm_note CHANGE COLUMN `note_date` `note_date` timestamp NOT NULL DEFAULT current_timestamp() COMMENT 'Date attached to the note'";
187 CRM_Core_DAO::executeQuery($query, [], TRUE, NULL, FALSE, $i18nRewrite);
188 return TRUE;
189 }
190
191 }