Skip to content

Commit

Permalink
Add upgrade function for message templates that does not involve copy…
Browse files Browse the repository at this point in the history
…ing the whole template
  • Loading branch information
eileenmcnaughton committed Jun 8, 2018
1 parent 5f5b9c2 commit 7e0ee04
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 1 deletion.
1 change: 1 addition & 0 deletions CRM/Upgrade/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@ public function setPreUpgradeMessage(&$preUpgradeMessage, $currentVer, $latestVe
foreach ($revisions as $rev) {
if (version_compare($currentVer, $rev) < 0) {
$versionObject = $this->incrementalPhpObject($rev);
CRM_Upgrade_Incremental_General::updateMessageTemplate($preUpgradeMessage, $rev);
if (is_callable(array($versionObject, 'setPreUpgradeMessage'))) {
$versionObject->setPreUpgradeMessage($preUpgradeMessage, $rev, $currentVer);
}
Expand Down
12 changes: 12 additions & 0 deletions CRM/Upgrade/Incremental/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,18 @@ public static function addColumn($ctx, $table, $column, $properties, $localizabl
return TRUE;
}

/**
* Do any relevant message template updates.
*
* @param CRM_Queue_TaskContext $ctx
* @param string $version
*/
public static function updateMessageTemplates($ctx, $version) {
$messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates($version);
$messageTemplateObject->updateTemplates();

}

/**
* Drop a column from a table if it exist.
*
Expand Down
26 changes: 25 additions & 1 deletion CRM/Upgrade/Incremental/General.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,37 @@ public static function setPreUpgradeMessage(&$preUpgradeMessage, $currentVer, $l
}
}

/**
* Perform any message template updates. 5.0+.
* @param $message
* @param $version
*/
public static function updateMessageTemplate(&$message, $version) {
if (version_compare($version, 5.0, '<')) {
return;
}
$messageObj = new CRM_Upgrade_Incremental_MessageTemplates($version);
$messages = $messageObj->getUpgradeMessages();
if (empty($messages)) {
return;
}
$message .= '<br />' . ts("The default copies of the message templates listed below will be updated to handle new features or correct a problem. Your installation has customized versions of these message templates, and you will need to apply the updates manually after running this upgrade. <a href='%1' style='color:white; text-decoration:underline; font-weight:bold;' target='_blank'>Click here</a> for detailed instructions. %2", array(
1 => 'http://wiki.civicrm.org/confluence/display/CRMDOC/Message+Templates#MessageTemplates-UpgradesandCustomizedSystemWorkflowTemplates',
2 => '<ul><l>' . implode('</li><li>', $messages) . '</li></ul>',
));

$messageObj->updateTemplates();
}

/**
* @param $message
* @param $latestVer
* @param $currentVer
*/
public static function checkMessageTemplate(&$message, $latestVer, $currentVer) {

if (version_compare($currentVer, 5.0, '>')) {
return;
}
$sql = "SELECT orig.workflow_id as workflow_id,
orig.msg_title as title
FROM civicrm_msg_template diverted JOIN civicrm_msg_template orig ON (
Expand Down
140 changes: 140 additions & 0 deletions CRM/Upgrade/Incremental/MessageTemplates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 5 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2018 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2018
*/
class CRM_Upgrade_Incremental_MessageTemplates {

/**
* Version we are upgrading to.
*
* @var string
*/
protected $upgradeVersion;

/**
* @return string
*/
public function getUpgradeVersion() {
return $this->upgradeVersion;
}

/**
* @param string $upgradeVersion
*/
public function setUpgradeVersion($upgradeVersion) {
$this->upgradeVersion = $upgradeVersion;
}

/**
* CRM_Upgrade_Incremental_MessageTemplates constructor.
*
* @param string $upgradeVersion
*/
public function __construct($upgradeVersion) {
$this->setUpgradeVersion($upgradeVersion);
}

/**
* Get any templates that have been updated.
*
* @return array
*/
protected function getTemplateUpdates() {
return [
[
'version' => '5.4.alpha1',
'template' => 'membership_online_receipt',
'upgrade_descriptor' => ts('Use email greeting at top where available'),
'type' => 'text',
],
];
}

/**
* Get any required template updates.
*
* @return array
*/
public function getTemplatesToUpdate() {
$templates = $this->getTemplateUpdates();
$return = [];
foreach ($templates as $template) {
if ($template['version'] === $this->getUpgradeVersion()) {
$return[$template['template'] . '_' . $template['type']] = $template;
}
}
return $return;
}

/**
* Get the upgrade messages.
*/
public function getUpgradeMessages() {
$updates = $this->getTemplatesToUpdate();
$messages = [];
foreach ($updates as $key => $value) {
$messages[$key] = $value['upgrade_descriptor'];
}
return $messages;
}

/**
* Update message templates.
*/
public function updateTemplates() {
$templates = $this->getTemplatesToUpdate();
foreach ($templates as $template) {
$workFlowID = CRM_Core_DAO::singleValueQuery("SELECT MAX(id) as id FROM civicrm_option_value WHERE name = %1", [
1 => [$template['template'], 'String'],
]);
$content = file_get_contents(\Civi::paths()->getPath('[civicrm.root]/xml/templates/message_templates/' . $template['template'] . '_' . $template['type'] . '.tpl'));
$templatesToUpdate = [];
$templatesToUpdate[] = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_msg_template WHERE workflow_id = $workFlowID AND is_reserved = 1");
$defaultTemplateID = CRM_Core_DAO::singleValueQuery("
SELECT default_template.id FROM civicrm_msg_template reserved
LEFT JOIN civicrm_msg_template default_template
ON reserved.workflow_id = default_template.workflow_id
WHERE reserved.workflow_id = $workFlowID
AND reserved.is_reserved = 1 AND default_template.is_default = 1 AND reserved.id <> default_template.id
");
if ($defaultTemplateID) {
$templatesToUpdate[] = $defaultTemplateID;
}

CRM_Core_DAO::executeQuery("
UPDATE civicrm_msg_template SET msg_{$template['type']} = %1 WHERE id IN (" . implode(',', $templatesToUpdate) . ")", [
1 => [$content, 'String']
]
);
}
}

}
70 changes: 70 additions & 0 deletions tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/**
* Class CRM_UF_Page_ProfileEditorTest
* @group headless
*/
class CRM_Upgrade_Incremental_Base_Test extends CiviUnitTestCase {

/**
* Test message upgrade process.
*/
public function testMessageTemplateUpgrade() {
$workFlowID = civicrm_api3('OptionValue', 'getvalue', ['return' => 'id', 'name' => 'membership_online_receipt', 'options' => ['limit' => 1, 'sort' => 'id DESC']]);

$templates = $this->callAPISuccess('MessageTemplate', 'get', ['workflow_id' => $workFlowID])['values'];
foreach ($templates as $template) {
$originalText = $template['msg_text'];
$this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => 'great what a cool member you are', 'id' => $template['id']]);
$msg_text = $this->callAPISuccessGetValue('MessageTemplate', ['id' => $template['id'], 'return' => 'msg_text']);
$this->assertEquals('great what a cool member you are', $msg_text);
}
$messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates('5.4.alpha1');
$messageTemplateObject->updateTemplates();

foreach ($templates as $template) {
$msg_text = $this->callAPISuccessGetValue('MessageTemplate', ['id' => $template['id'], 'return' => 'msg_text']);
$this->assertContains('{ts}Membership Information{/ts}', $msg_text);
if ($msg_text !== $originalText) {
// Reset value for future tests.
$this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => $originalText, 'id' => $template['id']]);
}
}
}

/**
* Test message upgrade process only edits the default if the template is customised.
*/
public function testMessageTemplateUpgradeAlreadyCustomised() {
$workFlowID = civicrm_api3('OptionValue', 'getvalue', ['return' => 'id', 'name' => 'membership_online_receipt', 'options' => ['limit' => 1, 'sort' => 'id DESC']]);

$templates = $this->callAPISuccess('MessageTemplate', 'get', ['workflow_id' => $workFlowID])['values'];
foreach ($templates as $template) {
if ($template['is_reserved']) {
$originalText = $template['msg_text'];
$this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => 'great what a cool member you are', 'id' => $template['id']]);
}
else {
$this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => 'great what a silly sausage you are', 'id' => $template['id']]);
}
}
$messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates('5.4.alpha1');
$messageTemplateObject->updateTemplates();

foreach ($templates as $template) {
$msg_text = $this->callAPISuccessGetValue('MessageTemplate', ['id' => $template['id'], 'return' => 'msg_text']);
if ($template['is_reserved']) {
$this->assertTrue(strstr($msg_text, '{ts}Membership Information{/ts}'));
}
else {
$this->assertEquals('great what a silly sausage you are', $msg_text);
}

if ($msg_text !== $originalText) {
// Reset value for future tests.
$this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => $originalText, 'id' => $template['id']]);
}
}
}

}

0 comments on commit 7e0ee04

Please sign in to comment.