Set version to 5.4.beta1
[civicrm-core.git] / CRM / Upgrade / Incremental / MessageTemplates.php
CommitLineData
fe83c251 1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
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'],
fe83c251 82 ]
83 ],
84 ];
85 }
86
87 /**
88 * Get any required template updates.
89 *
90 * @return array
91 */
92 public function getTemplatesToUpdate() {
93 $templates = $this->getTemplateUpdates();
94 $return = [];
95 foreach ($templates as $templateArray) {
96 if ($templateArray['version'] === $this->getUpgradeVersion()) {
97 foreach ($templateArray['templates'] as $template) {
98 $return[$template['name'] . '_' . $template['type']] = array_merge($template, $templateArray);
99 }
100 }
101 }
102 return $return;
103 }
104
105 /**
106 * Get the upgrade messages.
107 */
108 public function getUpgradeMessages() {
109 $updates = $this->getTemplatesToUpdate();
110 $messages = [];
111 foreach ($updates as $key => $value) {
112 $templateLabel = civicrm_api3('OptionValue', 'getvalue', [
ac6fc3d1 113 'return' => 'label',
114 'name' => $value['name'],
115 'options' => ['limit' => 1],
fe83c251 116 ]);
117 $messages[$templateLabel] = $value['upgrade_descriptor'];
118 }
119 return $messages;
120 }
121
122 /**
123 * Update message templates.
124 */
125 public function updateTemplates() {
126 $templates = $this->getTemplatesToUpdate();
127 foreach ($templates as $template) {
128 $workFlowID = CRM_Core_DAO::singleValueQuery("SELECT MAX(id) as id FROM civicrm_option_value WHERE name = %1", [
129 1 => [$template['name'], 'String'],
130 ]);
131 $content = file_get_contents(\Civi::paths()->getPath('[civicrm.root]/xml/templates/message_templates/' . $template['name'] . '_' . $template['type'] . '.tpl'));
132 $templatesToUpdate = [];
133 $templatesToUpdate[] = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_msg_template WHERE workflow_id = $workFlowID AND is_reserved = 1");
134 $defaultTemplateID = CRM_Core_DAO::singleValueQuery("
135 SELECT default_template.id FROM civicrm_msg_template reserved
136 LEFT JOIN civicrm_msg_template default_template
137 ON reserved.workflow_id = default_template.workflow_id
138 WHERE reserved.workflow_id = $workFlowID
139 AND reserved.is_reserved = 1 AND default_template.is_default = 1 AND reserved.id <> default_template.id
140 ");
141 if ($defaultTemplateID) {
142 $templatesToUpdate[] = $defaultTemplateID;
143 }
144
145 CRM_Core_DAO::executeQuery("
146 UPDATE civicrm_msg_template SET msg_{$template['type']} = %1 WHERE id IN (" . implode(',', $templatesToUpdate) . ")", [
147 1 => [$content, 'String']
148 ]
149 );
150 }
151 }
152
153}