Merge pull request #14465 from philmb/dev/core#1010
[civicrm-core.git] / CRM / Upgrade / Incremental / MessageTemplates.php
CommitLineData
fe83c251 1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
fe83c251 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
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
fe83c251 32 */
33class 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'],
e74e138c 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'],
2db5cabf 82 ['name' => 'event_online_receipt', 'type' => 'subject'],
3655bea4 83 ],
fe83c251 84 ],
9d404148 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'],
3655bea4 91 ],
f926d56f
MWMC
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'],
3655bea4 104 ],
1e477c5b 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'],
3655bea4 113 ],
1e477c5b 114 ],
3eb013ed
P
115 [
116 'version' => '5.14',
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 ],
fe83c251 124 ];
125 }
126
127 /**
128 * Get any required template updates.
129 *
130 * @return array
131 */
132 public function getTemplatesToUpdate() {
133 $templates = $this->getTemplateUpdates();
134 $return = [];
135 foreach ($templates as $templateArray) {
136 if ($templateArray['version'] === $this->getUpgradeVersion()) {
137 foreach ($templateArray['templates'] as $template) {
138 $return[$template['name'] . '_' . $template['type']] = array_merge($template, $templateArray);
139 }
140 }
141 }
142 return $return;
143 }
144
145 /**
146 * Get the upgrade messages.
147 */
148 public function getUpgradeMessages() {
149 $updates = $this->getTemplatesToUpdate();
150 $messages = [];
9d404148 151 $templateLabel = '';
fe83c251 152 foreach ($updates as $key => $value) {
9d404148 153 try {
154 $templateLabel = civicrm_api3('OptionValue', 'getvalue', [
155 'return' => 'label',
156 'name' => $value['name'],
157 'options' => ['limit' => 1],
158 ]);
159 }
160 catch (Exception $e) {
161 if (!empty($value['label'])) {
162 $templateLabel = $value['label'];
163 }
164 }
fe83c251 165 $messages[$templateLabel] = $value['upgrade_descriptor'];
166 }
167 return $messages;
168 }
169
170 /**
171 * Update message templates.
172 */
173 public function updateTemplates() {
174 $templates = $this->getTemplatesToUpdate();
175 foreach ($templates as $template) {
176 $workFlowID = CRM_Core_DAO::singleValueQuery("SELECT MAX(id) as id FROM civicrm_option_value WHERE name = %1", [
177 1 => [$template['name'], 'String'],
178 ]);
179 $content = file_get_contents(\Civi::paths()->getPath('[civicrm.root]/xml/templates/message_templates/' . $template['name'] . '_' . $template['type'] . '.tpl'));
180 $templatesToUpdate = [];
9d404148 181 if (!empty($workFlowID)) {
182 $templatesToUpdate[] = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_msg_template WHERE workflow_id = $workFlowID AND is_reserved = 1");
183 $defaultTemplateID = CRM_Core_DAO::singleValueQuery("
184 SELECT default_template.id FROM civicrm_msg_template reserved
185 LEFT JOIN civicrm_msg_template default_template
186 ON reserved.workflow_id = default_template.workflow_id
187 WHERE reserved.workflow_id = $workFlowID
188 AND reserved.is_reserved = 1 AND default_template.is_default = 1 AND reserved.id <> default_template.id
189 AND reserved.msg_{$template['type']} = default_template.msg_{$template['type']}
190 ");
191 if ($defaultTemplateID) {
192 $templatesToUpdate[] = $defaultTemplateID;
193 }
fe83c251 194
9d404148 195 CRM_Core_DAO::executeQuery("
196 UPDATE civicrm_msg_template SET msg_{$template['type']} = %1 WHERE id IN (" . implode(',', $templatesToUpdate) . ")", [
3655bea4
SL
197 1 => [$content, 'String'],
198 ]
9d404148 199 );
200 }
fe83c251 201 }
202 }
203
204}