Merge pull request #15746 from civicrm/5.19
[civicrm-core.git] / CRM / Upgrade / Incremental / MessageTemplates.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 CRM_Upgrade_Incremental_MessageTemplates {
34
35 /**
36 * Version we are upgrading to.
37 *
38 * @var string
39 */
40 protected $upgradeVersion;
41
42 /**
43 * @return string
44 */
45 public function getUpgradeVersion() {
46 return $this->upgradeVersion;
47 }
48
49 /**
50 * @param string $upgradeVersion
51 */
52 public function setUpgradeVersion($upgradeVersion) {
53 $this->upgradeVersion = $upgradeVersion;
54 }
55
56 /**
57 * CRM_Upgrade_Incremental_MessageTemplates constructor.
58 *
59 * @param string $upgradeVersion
60 */
61 public function __construct($upgradeVersion) {
62 $this->setUpgradeVersion($upgradeVersion);
63 }
64
65 /**
66 * Get any templates that have been updated.
67 *
68 * @return array
69 */
70 protected function getTemplateUpdates() {
71 return [
72 [
73 'version' => '5.4.alpha1',
74 'upgrade_descriptor' => ts('Use email greeting at top where available'),
75 'templates' => [
76 ['name' => 'membership_online_receipt', 'type' => 'text'],
77 ['name' => 'membership_online_receipt', 'type' => 'html'],
78 ['name' => 'contribution_online_receipt', 'type' => 'text'],
79 ['name' => 'contribution_online_receipt', 'type' => 'html'],
80 ['name' => 'event_online_receipt', 'type' => 'text'],
81 ['name' => 'event_online_receipt', 'type' => 'html'],
82 ['name' => 'event_online_receipt', 'type' => 'subject'],
83 ],
84 ],
85 [
86 'version' => '5.7.alpha1',
87 'upgrade_descriptor' => ts('Fix invoice number (human readable) instead of id (reference)'),
88 'label' => ts('Contributions - Invoice'),
89 'templates' => [
90 ['name' => 'contribution_invoice_receipt', 'type' => 'html'],
91 ],
92 ],
93 [
94 'version' => '5.10.alpha1',
95 'upgrade_descriptor' => ts('Show recurring cancel/update URLs in receipt based on payment processor capabilities'),
96 'label' => ts('Receipts - cancel/update subscription URLs'),
97 'templates' => [
98 ['name' => 'contribution_online_receipt', 'type' => 'text'],
99 ['name' => 'contribution_online_receipt', 'type' => 'html'],
100 ['name' => 'contribution_recurring_notify', 'type' => 'text'],
101 ['name' => 'contribution_recurring_notify', 'type' => 'html'],
102 ['name' => 'membership_online_receipt', 'type' => 'text'],
103 ['name' => 'membership_online_receipt', 'type' => 'html'],
104 ],
105 ],
106 [
107 'version' => '5.12.alpha1',
108 'upgrade_descriptor' => ts('Update payment notification to remove print text, use email greeting'),
109 'label' => ts('Payment notification'),
110 'templates' => [
111 ['name' => 'payment_or_refund_notification', 'type' => 'text'],
112 ['name' => 'payment_or_refund_notification', 'type' => 'html'],
113 ],
114 ],
115 [
116 'version' => '5.15.alpha1',
117 'upgrade_descriptor' => ts('Use email greeting and fix capitalization'),
118 'label' => ts('Pledge acknowledgement'),
119 'templates' => [
120 ['name' => 'pledge_acknowledge', 'type' => 'text'],
121 ['name' => 'pledge_acknowledge', 'type' => 'html'],
122 ],
123 ],
124 [
125 'version' => '5.20.alpha1',
126 'upgrade_descriptor' => ts('Fix missing Email greetings'),
127 'templates' => [
128 ['name' => 'contribution_dupalert', 'type' => 'subject'],
129 ['name' => 'contribution_invoice_receipt', 'type' => 'subject'],
130 ['name' => 'contribution_offline_receipt', 'type' => 'html'],
131 ['name' => 'contribution_offline_receipt', 'type' => 'subject'],
132 ['name' => 'contribution_offline_receipt', 'type' => 'text'],
133 ['name' => 'contribution_online_receipt', 'type' => 'subject'],
134 ['name' => 'contribution_recurring_billing', 'type' => 'html'],
135 ['name' => 'contribution_recurring_billing', 'type' => 'subject'],
136 ['name' => 'contribution_recurring_billing', 'type' => 'text'],
137 ['name' => 'contribution_recurring_cancelled', 'type' => 'html'],
138 ['name' => 'contribution_recurring_cancelled', 'type' => 'subject'],
139 ['name' => 'contribution_recurring_cancelled', 'type' => 'text'],
140 ['name' => 'contribution_recurring_edit', 'type' => 'html'],
141 ['name' => 'contribution_recurring_edit', 'type' => 'subject'],
142 ['name' => 'contribution_recurring_edit', 'type' => 'text'],
143 ['name' => 'contribution_recurring_notify', 'type' => 'html'],
144 ['name' => 'contribution_recurring_notify', 'type' => 'subject'],
145 ['name' => 'contribution_recurring_notify', 'type' => 'text'],
146 ['name' => 'event_offline_receipt', 'type' => 'html'],
147 ['name' => 'event_offline_receipt', 'type' => 'subject'],
148 ['name' => 'event_offline_receipt', 'type' => 'text'],
149 ['name' => 'event_online_receipt', 'type' => 'html'],
150 ['name' => 'event_online_receipt', 'type' => 'subject'],
151 ['name' => 'event_online_receipt', 'type' => 'text'],
152 ['name' => 'event_registration_receipt', 'type' => 'html'],
153 ['name' => 'event_registration_receipt', 'type' => 'subject'],
154 ['name' => 'event_registration_receipt', 'type' => 'text'],
155 ['name' => 'membership_autorenew_billing', 'type' => 'html'],
156 ['name' => 'membership_autorenew_billing', 'type' => 'subject'],
157 ['name' => 'membership_autorenew_billing', 'type' => 'text'],
158 ['name' => 'membership_autorenew_cancelled', 'type' => 'html'],
159 ['name' => 'membership_autorenew_cancelled', 'type' => 'subject'],
160 ['name' => 'membership_autorenew_cancelled', 'type' => 'text'],
161 ['name' => 'membership_offline_receipt', 'type' => 'html'],
162 ['name' => 'membership_offline_receipt', 'type' => 'subject'],
163 ['name' => 'membership_offline_receipt', 'type' => 'text'],
164 ['name' => 'membership_online_receipt', 'type' => 'subject'],
165 ['name' => 'participant_cancelled', 'type' => 'html'],
166 ['name' => 'participant_cancelled', 'type' => 'subject'],
167 ['name' => 'participant_cancelled', 'type' => 'text'],
168 ['name' => 'participant_confirm', 'type' => 'html'],
169 ['name' => 'participant_confirm', 'type' => 'subject'],
170 ['name' => 'participant_confirm', 'type' => 'text'],
171 ['name' => 'participant_expired', 'type' => 'html'],
172 ['name' => 'participant_expired', 'type' => 'subject'],
173 ['name' => 'participant_expired', 'type' => 'text'],
174 ['name' => 'participant_transferred', 'type' => 'html'],
175 ['name' => 'participant_transferred', 'type' => 'subject'],
176 ['name' => 'participant_transferred', 'type' => 'text'],
177 ['name' => 'payment_or_refund_notification', 'type' => 'html'],
178 ['name' => 'payment_or_refund_notification', 'type' => 'subject'],
179 ['name' => 'payment_or_refund_notification', 'type' => 'text'],
180 ['name' => 'pcp_notify', 'type' => 'subject'],
181 ['name' => 'pcp_owner_notify', 'type' => 'html'],
182 ['name' => 'pcp_owner_notify', 'type' => 'subject'],
183 ['name' => 'pcp_owner_notify', 'type' => 'text'],
184 ['name' => 'pcp_status_change', 'type' => 'subject'],
185 ['name' => 'pcp_supporter_notify', 'type' => 'html'],
186 ['name' => 'pcp_supporter_notify', 'type' => 'subject'],
187 ['name' => 'pcp_supporter_notify', 'type' => 'text'],
188 ['name' => 'petition_confirmation_needed', 'type' => 'html'],
189 ['name' => 'petition_confirmation_needed', 'type' => 'subject'],
190 ['name' => 'petition_confirmation_needed', 'type' => 'text'],
191 ['name' => 'petition_sign', 'type' => 'html'],
192 ['name' => 'petition_sign', 'type' => 'subject'],
193 ['name' => 'petition_sign', 'type' => 'text'],
194 ['name' => 'pledge_acknowledge', 'type' => 'subject'],
195 ['name' => 'pledge_acknowledge', 'type' => 'subject'],
196 ['name' => 'pledge_reminder', 'type' => 'html'],
197 ['name' => 'pledge_reminder', 'type' => 'subject'],
198 ['name' => 'pledge_reminder', 'type' => 'text'],
199 ['name' => 'uf_notify', 'type' => 'subject'],
200 ],
201 ],
202
203 ];
204 }
205
206 /**
207 * Get any required template updates.
208 *
209 * @return array
210 */
211 public function getTemplatesToUpdate() {
212 $templates = $this->getTemplateUpdates();
213 $return = [];
214 foreach ($templates as $templateArray) {
215 if ($templateArray['version'] === $this->getUpgradeVersion()) {
216 foreach ($templateArray['templates'] as $template) {
217 $return[$template['name'] . '_' . $template['type']] = array_merge($template, $templateArray);
218 }
219 }
220 }
221 return $return;
222 }
223
224 /**
225 * Get the upgrade messages.
226 */
227 public function getUpgradeMessages() {
228 $updates = $this->getTemplatesToUpdate();
229 $messages = [];
230 $templateLabel = '';
231 foreach ($updates as $key => $value) {
232 try {
233 $templateLabel = civicrm_api3('OptionValue', 'getvalue', [
234 'return' => 'label',
235 'name' => $value['name'],
236 'options' => ['limit' => 1],
237 ]);
238 }
239 catch (Exception $e) {
240 if (!empty($value['label'])) {
241 $templateLabel = $value['label'];
242 }
243 }
244 $messages[$templateLabel] = $value['upgrade_descriptor'];
245 }
246 return $messages;
247 }
248
249 /**
250 * Update message templates.
251 */
252 public function updateTemplates() {
253 $templates = $this->getTemplatesToUpdate();
254 foreach ($templates as $template) {
255 $workFlowID = CRM_Core_DAO::singleValueQuery("SELECT MAX(id) as id FROM civicrm_option_value WHERE name = %1", [
256 1 => [$template['name'], 'String'],
257 ]);
258 $content = file_get_contents(\Civi::paths()->getPath('[civicrm.root]/xml/templates/message_templates/' . $template['name'] . '_' . $template['type'] . '.tpl'));
259 $templatesToUpdate = [];
260 if (!empty($workFlowID)) {
261 $templatesToUpdate[] = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_msg_template WHERE workflow_id = $workFlowID AND is_reserved = 1");
262 $defaultTemplateID = CRM_Core_DAO::singleValueQuery("
263 SELECT default_template.id FROM civicrm_msg_template reserved
264 LEFT JOIN civicrm_msg_template default_template
265 ON reserved.workflow_id = default_template.workflow_id
266 WHERE reserved.workflow_id = $workFlowID
267 AND reserved.is_reserved = 1 AND default_template.is_default = 1 AND reserved.id <> default_template.id
268 AND reserved.msg_{$template['type']} = default_template.msg_{$template['type']}
269 ");
270 if ($defaultTemplateID) {
271 $templatesToUpdate[] = $defaultTemplateID;
272 }
273
274 CRM_Core_DAO::executeQuery("
275 UPDATE civicrm_msg_template SET msg_{$template['type']} = %1 WHERE id IN (" . implode(',', $templatesToUpdate) . ")", [
276 1 => [$content, 'String'],
277 ]
278 );
279 }
280 }
281 }
282
283 }