3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
33 class CRM_Upgrade_Incremental_MessageTemplates
{
36 * Version we are upgrading to.
40 protected $upgradeVersion;
45 public function getUpgradeVersion() {
46 return $this->upgradeVersion
;
50 * @param string $upgradeVersion
52 public function setUpgradeVersion($upgradeVersion) {
53 $this->upgradeVersion
= $upgradeVersion;
57 * CRM_Upgrade_Incremental_MessageTemplates constructor.
59 * @param string $upgradeVersion
61 public function __construct($upgradeVersion) {
62 $this->setUpgradeVersion($upgradeVersion);
66 * Get any templates that have been updated.
70 protected function getTemplateUpdates() {
73 'version' => '5.4.alpha1',
74 'upgrade_descriptor' => ts('Use email greeting at top where available'),
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'],
86 'version' => '5.7.alpha1',
87 'upgrade_descriptor' => ts('Fix invoice number (human readable) instead of id (reference)'),
88 'label' => ts('Contributions - Invoice'),
90 ['name' => 'contribution_invoice_receipt', 'type' => 'html'],
97 * Get any required template updates.
101 public function getTemplatesToUpdate() {
102 $templates = $this->getTemplateUpdates();
104 foreach ($templates as $templateArray) {
105 if ($templateArray['version'] === $this->getUpgradeVersion()) {
106 foreach ($templateArray['templates'] as $template) {
107 $return[$template['name'] . '_' . $template['type']] = array_merge($template, $templateArray);
115 * Get the upgrade messages.
117 public function getUpgradeMessages() {
118 $updates = $this->getTemplatesToUpdate();
121 foreach ($updates as $key => $value) {
123 $templateLabel = civicrm_api3('OptionValue', 'getvalue', [
125 'name' => $value['name'],
126 'options' => ['limit' => 1],
129 catch (Exception
$e) {
130 if (!empty($value['label'])) {
131 $templateLabel = $value['label'];
134 $messages[$templateLabel] = $value['upgrade_descriptor'];
140 * Update message templates.
142 public function updateTemplates() {
143 $templates = $this->getTemplatesToUpdate();
144 foreach ($templates as $template) {
145 $workFlowID = CRM_Core_DAO
::singleValueQuery("SELECT MAX(id) as id FROM civicrm_option_value WHERE name = %1", [
146 1 => [$template['name'], 'String'],
148 $content = file_get_contents(\Civi
::paths()->getPath('[civicrm.root]/xml/templates/message_templates/' . $template['name'] . '_' . $template['type'] . '.tpl'));
149 $templatesToUpdate = [];
150 if (!empty($workFlowID)) {
151 $templatesToUpdate[] = CRM_Core_DAO
::singleValueQuery("SELECT id FROM civicrm_msg_template WHERE workflow_id = $workFlowID AND is_reserved = 1");
152 $defaultTemplateID = CRM_Core_DAO
::singleValueQuery("
153 SELECT default_template.id FROM civicrm_msg_template reserved
154 LEFT JOIN civicrm_msg_template default_template
155 ON reserved.workflow_id = default_template.workflow_id
156 WHERE reserved.workflow_id = $workFlowID
157 AND reserved.is_reserved = 1 AND default_template.is_default = 1 AND reserved.id <> default_template.id
158 AND reserved.msg_{$template['type']} = default_template.msg_{$template['type']}
160 if ($defaultTemplateID) {
161 $templatesToUpdate[] = $defaultTemplateID;
164 CRM_Core_DAO
::executeQuery("
165 UPDATE civicrm_msg_template SET msg_{$template['type']} = %1 WHERE id IN (" . implode(',', $templatesToUpdate) . ")", [
166 1 => [$content, 'String']