From 6fdc7c60dad906cf5466d23cea48c74af7a80d1b Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sun, 24 Dec 2023 12:00:48 +1300 Subject: [PATCH] Update MessageTemplates generation to not use weird old methods --- CRM/Core/CodeGen/Util/MessageTemplates.php | 283 +++++++++++++++++---- CRM/Core/Smarty/plugins/block.ts.php | 10 +- setup/src/Setup/Template.php | 1 - xml/templates/civicrm_msg_template.tpl | 63 ++--- 4 files changed, 265 insertions(+), 92 deletions(-) diff --git a/CRM/Core/CodeGen/Util/MessageTemplates.php b/CRM/Core/CodeGen/Util/MessageTemplates.php index 2df3165642..2a2dfa46b3 100644 --- a/CRM/Core/CodeGen/Util/MessageTemplates.php +++ b/CRM/Core/CodeGen/Util/MessageTemplates.php @@ -6,7 +6,7 @@ class CRM_Core_CodeGen_Util_MessageTemplates { public static function assignSmartyVariables($smarty): void { // Assigns for message templates - $ogNames = [ + $smarty->assign('optionGroupNames', [ 'case' => ts('Message Template Workflow for Cases', ['escape' => 'sql']), 'contribution' => ts('Message Template Workflow for Contributions', ['escape' => 'sql']), 'event' => ts('Message Template Workflow for Events', ['escape' => 'sql']), @@ -16,67 +16,248 @@ class CRM_Core_CodeGen_Util_MessageTemplates { 'pledge' => ts('Message Template Workflow for Pledges', ['escape' => 'sql']), 'uf' => ts('Message Template Workflow for Profiles', ['escape' => 'sql']), 'petition' => ts('Message Template Workflow for Petition', ['escape' => 'sql']), - ]; - $ovNames = [ - 'case' => [ - 'case_activity' => ts('Cases - Send Copy of an Activity', ['escape' => 'sql']), - ], - 'contribution' => [ - 'contribution_dupalert' => ts('Contributions - Duplicate Organization Alert', ['escape' => 'sql']), - 'contribution_offline_receipt' => ts('Contributions - Receipt (off-line)', ['escape' => 'sql']), - 'contribution_online_receipt' => ts('Contributions - Receipt (on-line)', ['escape' => 'sql']), - 'contribution_invoice_receipt' => ts('Contributions - Invoice', ['escape' => 'sql']), - 'contribution_recurring_notify' => ts('Contributions - Recurring Start and End Notification', ['escape' => 'sql']), - 'contribution_recurring_cancelled' => ts('Contributions - Recurring Cancellation Notification', ['escape' => 'sql']), - 'contribution_recurring_billing' => ts('Contributions - Recurring Billing Updates', ['escape' => 'sql']), - 'contribution_recurring_edit' => ts('Contributions - Recurring Updates', ['escape' => 'sql']), - 'pcp_notify' => ts('Personal Campaign Pages - Admin Notification', ['escape' => 'sql']), - 'pcp_status_change' => ts('Personal Campaign Pages - Supporter Status Change Notification', ['escape' => 'sql']), - 'pcp_supporter_notify' => ts('Personal Campaign Pages - Supporter Welcome', ['escape' => 'sql']), - 'pcp_owner_notify' => ts('Personal Campaign Pages - Owner Notification', ['escape' => 'sql']), - 'payment_or_refund_notification' => ts('Additional Payment Receipt or Refund Notification', ['escape' => 'sql']), - ], - 'event' => [ - 'event_offline_receipt' => ts('Events - Registration Confirmation and Receipt (off-line)', ['escape' => 'sql']), - 'event_online_receipt' => ts('Events - Registration Confirmation and Receipt (on-line)', ['escape' => 'sql']), - 'event_registration_receipt' => ts('Events - Receipt only', ['escape' => 'sql']), - 'participant_cancelled' => ts('Events - Registration Cancellation Notice', ['escape' => 'sql']), - 'participant_confirm' => ts('Events - Registration Confirmation Invite', ['escape' => 'sql']), - 'participant_expired' => ts('Events - Pending Registration Expiration Notice', ['escape' => 'sql']), - 'participant_transferred' => ts('Events - Registration Transferred Notice', ['escape' => 'sql']), + ]); + + $templates = [ + 'case_activity' => [ + 'option_group_name' => 'case', + 'title' => ts('Cases - Send Copy of an Activity', ['escape' => 'sql']), + 'weight' => 1, + 'value' => 1, + ], + 'contribution_dupalert' => [ + 'option_group_name' => 'contribution', + 'title' => ts('Contributions - Duplicate Organization Alert', ['escape' => 'sql']), + 'weight' => 1, + 'value' => 1, + ], + 'contribution_offline_receipt' => [ + 'option_group_name' => 'contribution', + 'title' => ts('Contributions - Receipt (off-line)', ['escape' => 'sql']), + 'weight' => 2, + 'value' => 2, + ], + 'contribution_online_receipt' => [ + 'option_group_name' => 'contribution', + 'title' => ts('Contributions - Receipt (on-line)', ['escape' => 'sql']), + 'weight' => 3, + 'value' => 3, + ], + 'contribution_invoice_receipt' => [ + 'option_group_name' => 'contribution', + 'title' => ts('Contributions - Invoice', ['escape' => 'sql']), + 'weight' => 4, + 'value' => 4, + ], + 'contribution_recurring_notify' => [ + 'option_group_name' => 'contribution', + 'title' => ts('Contributions - Recurring Start and End Notification', ['escape' => 'sql']), + 'weight' => 5, + 'value' => 5, + ], + 'contribution_recurring_cancelled' => [ + 'option_group_name' => 'contribution', + 'title' => ts('Contributions - Recurring Cancellation Notification', ['escape' => 'sql']), + 'weight' => 6, + 'value' => 6, + ], + 'contribution_recurring_billing' => [ + 'option_group_name' => 'contribution', + 'title' => ts('Contributions - Recurring Billing Updates', ['escape' => 'sql']), + 'weight' => 7, + 'value' => 7, + ], + 'contribution_recurring_edit' => [ + 'title' => ts('Contributions - Recurring Updates', ['escape' => 'sql']), + 'option_group_name' => 'contribution', + 'weight' => 8, + 'value' => 8, + ], + 'pcp_notify' => [ + 'title' => ts('Personal Campaign Pages - Admin Notification', ['escape' => 'sql']), + 'option_group_name' => 'contribution', + 'weight' => 9, + 'value' => 9, + ], + 'pcp_status_change' => [ + 'title' => ts('Personal Campaign Pages - Supporter Status Change Notification', ['escape' => 'sql']), + 'option_group_name' => 'contribution', + 'weight' => 10, + 'value' => 10, + ], + 'pcp_supporter_notify' => [ + 'title' => ts('Personal Campaign Pages - Supporter Welcome', ['escape' => 'sql']), + 'option_group_name' => 'contribution', + 'weight' => 11, + 'value' => 11, + ], + 'pcp_owner_notify' => [ + 'title' => ts('Personal Campaign Pages - Owner Notification', ['escape' => 'sql']), + 'option_group_name' => 'contribution', + 'weight' => 12, + 'value' => 12, + ], + 'payment_or_refund_notification' => [ + 'title' => ts('Additional Payment Receipt or Refund Notification', ['escape' => 'sql']), + 'option_group_name' => 'contribution', + 'weight' => 13, + 'value' => 13, + ], + 'event_offline_receipt' => [ + 'option_group_name' => 'event', + 'title' => ts('Events - Registration Confirmation and Receipt (off-line)', ['escape' => 'sql']), + 'weight' => 1, + 'value' => 1, + ], + 'event_online_receipt' => [ + 'option_group_name' => 'event', + 'title' => ts('Events - Registration Confirmation and Receipt (on-line)', ['escape' => 'sql']), + 'weight' => 2, + 'value' => 2, + ], + 'event_registration_receipt' => [ + 'option_group_name' => 'event', + 'title' => ts('Events - Receipt only', ['escape' => 'sql']), + 'weight' => 3, + 'value' => 3, + ], + 'participant_cancelled' => [ + 'option_group_name' => 'event', + 'title' => ts('Events - Registration Cancellation Notice', ['escape' => 'sql']), + 'weight' => 4, + 'value' => 4, + ], + 'participant_confirm' => [ + 'option_group_name' => 'event', + 'title' => ts('Events - Registration Confirmation Invite', ['escape' => 'sql']), + 'weight' => 5, + 'value' => 5, + ], + 'participant_expired' => [ + 'option_group_name' => 'event', + 'title' => ts('Events - Pending Registration Expiration Notice', ['escape' => 'sql']), + 'weight' => 6, + 'value' => 6, + ], + 'participant_transferred' => [ + 'option_group_name' => 'event', + 'title' => ts('Events - Registration Transferred Notice', ['escape' => 'sql']), + 'weight' => 7, + 'value' => 7, ], 'friend' => [ - 'friend' => ts('Tell-a-Friend Email', ['escape' => 'sql']), + 'option_group_name' => 'friend', + 'title' => ts('Tell-a-Friend Email', ['escape' => 'sql']), + 'weight' => 1, + 'value' => 1, + ], + 'membership_offline_receipt' => [ + 'option_group_name' => 'membership', + 'title' => ts('Memberships - Signup and Renewal Receipts (off-line)', ['escape' => 'sql']), + 'weight' => 1, + 'value' => 1, + ], + 'membership_online_receipt' => [ + 'option_group_name' => 'membership', + 'title' => ts('Memberships - Receipt (on-line)', ['escape' => 'sql']), + 'weight' => 2, + 'value' => 2, + ], + 'membership_autorenew_cancelled' => [ + 'option_group_name' => 'membership', + 'title' => ts('Memberships - Auto-renew Cancellation Notification', ['escape' => 'sql']), + 'weight' => 3, + 'value' => 3, ], - 'membership' => [ - 'membership_offline_receipt' => ts('Memberships - Signup and Renewal Receipts (off-line)', ['escape' => 'sql']), - 'membership_online_receipt' => ts('Memberships - Receipt (on-line)', ['escape' => 'sql']), - 'membership_autorenew_cancelled' => ts('Memberships - Auto-renew Cancellation Notification', ['escape' => 'sql']), - 'membership_autorenew_billing' => ts('Memberships - Auto-renew Billing Updates', ['escape' => 'sql']), + 'membership_autorenew_billing' => [ + 'option_group_name' => 'membership', + 'title' => ts('Memberships - Auto-renew Billing Updates', ['escape' => 'sql']), + 'weight' => 4, + 'value' => 4, ], - 'meta' => [ - 'test_preview' => ts('Test-drive - Receipt Header', ['escape' => 'sql']), + 'test_preview' => [ + 'option_group_name' => 'meta', + 'title' => ts('Test-drive - Receipt Header', ['escape' => 'sql']), + 'weight' => 1, + 'value' => 1, ], - 'pledge' => [ - 'pledge_acknowledge' => ts('Pledges - Acknowledgement', ['escape' => 'sql']), - 'pledge_reminder' => ts('Pledges - Payment Reminder', ['escape' => 'sql']), + 'pledge_acknowledge' => [ + 'option_group_name' => 'pledge', + 'title' => ts('Pledges - Acknowledgement', ['escape' => 'sql']), + 'weight' => 1, + 'value' => 1, ], - 'uf' => [ - 'uf_notify' => ts('Profiles - Admin Notification', ['escape' => 'sql']), + 'pledge_reminder' => [ + 'option_group_name' => 'pledge', + 'title' => ts('Pledges - Payment Reminder', ['escape' => 'sql']), + 'weight' => 2, + 'value' => 2, ], - 'petition' => [ - 'petition_sign' => ts('Petition - signature added', ['escape' => 'sql']), - 'petition_confirmation_needed' => ts('Petition - need verification', ['escape' => 'sql']), + 'uf_notify' => [ + 'option_group_name' => 'uf', + 'title' => ts('Profiles - Admin Notification', ['escape' => 'sql']), + 'weight' => 1, + 'value' => 1, + ], + 'petition_sign' => [ + 'option_group_name' => 'petition', + 'title' => ts('Petition - signature added', ['escape' => 'sql']), + 'weight' => 1, + 'value' => 1, + ], + 'petition_confirmation_needed' => [ + 'option_group_name' => 'petition', + 'title' => ts('Petition - need verification', ['escape' => 'sql']), + 'weight' => 2, + 'value' => 2, ], ]; - $smarty->assign('ogNames', $ogNames); - $smarty->assign('ovNames', $ovNames); - $dir = $smarty->get_template_vars()['gencodeXmlDir'] . '/templates/message_templates/sample'; - $templates = []; - foreach (preg_grep('/\.tpl$/', scandir($dir)) as $filename) { - $templates[] = ['name' => basename($filename, '.tpl'), 'filename' => "$dir/$filename"]; + // @todo - if used mgd files we could also use revert (& would not need the reserved version in the db). + $directory = self::getDirectory($smarty); + foreach (array_keys($templates) as $name) { + $templates[$name]['msg_html'] = file_get_contents($directory . '/' . $name . '_html.tpl'); + $templates[$name]['msg_text'] = file_get_contents($directory . '/' . $name . '_text.tpl') ?: ''; + $templates[$name]['subject'] = file_get_contents($directory . '/' . $name . '_subject.tpl'); + $templates[$name]['name'] = $name; } + $templates['Sample CiviMail Newsletter Template'] = [ + 'title' => ts('Sample CiviMail Newsletter Template', ['escape' => 'sql']), + 'subject' => ts('Sample CiviMail Newsletter', ['escape' => 'sql']), + 'msg_html' => file_get_contents($directory . '/sample/Sample CiviMail Newsletter.tpl'), + 'weight' => 1, + 'name' => 'Sample CiviMail Newsletter', + 'option_group_name' => '', + ]; + $templates['Sample Responsive Design Newsletter - Single Column Template'] = [ + 'title' => ts('Sample Responsive Design Newsletter - Single Column Template', ['escape' => 'sql']), + 'subject' => ts('Sample Responsive Design Newsletter - Single Column', ['escape' => 'sql']), + 'msg_html' => file_get_contents($directory . '/sample/Sample Responsive Design Newsletter - Single Column.tpl'), + 'weight' => 2, + 'name' => 'Sample Responsive Design Newsletter - Single Column', + 'option_group_name' => '', + ]; + $templates['Sample Responsive Design Newsletter - Two Column Template'] = [ + 'title' => ts('Sample Responsive Design Newsletter - Two Column Template', ['escape' => 'sql']), + 'subject' => ts('Sample Responsive Design Newsletter - Two Column', ['escape' => 'sql']), + 'msg_html' => file_get_contents($directory . '/sample/Sample Responsive Design Newsletter - Two Column.tpl'), + 'weight' => 3, + 'name' => 'Sample Responsive Design Newsletter - Two Column', + 'option_group_name' => '', + ]; $smarty->assign('templates', $templates); } + /** + * Transition function to get the directory as we switch from Smarty 2 to 3. + * @param $smarty + * + * @return string + */ + protected static function getDirectory($smarty) { + if (method_exists($smarty, 'getTemplateDir')) { + return $smarty->getTemplateDir() . '/message_templates/'; + } + return $smarty->template_dir . '/message_templates/'; + } + } diff --git a/CRM/Core/Smarty/plugins/block.ts.php b/CRM/Core/Smarty/plugins/block.ts.php index cb077675de..3c0016c6de 100644 --- a/CRM/Core/Smarty/plugins/block.ts.php +++ b/CRM/Core/Smarty/plugins/block.ts.php @@ -37,7 +37,15 @@ */ function smarty_block_ts($params, $text, &$smarty, &$repeat) { if (!$repeat) { - if (!isset($params['domain']) && $extensionKey = $smarty->get_template_vars('extensionKey')) { + $extensionKey = ''; + if (method_exists($smarty, 'getTemplateVars')) { + $extensionKey = $smarty->getTemplateVars('extensionKey'); + } + else { + // Transitional support for Smarty2 still being used in GenCode. + $extensionKey = $smarty->get_template_vars('extensionKey'); + } + if (!isset($params['domain']) && $extensionKey) { $params['domain'] = is_array($extensionKey) ? $extensionKey : [$extensionKey, NULL]; } return _ts($text, $params); diff --git a/setup/src/Setup/Template.php b/setup/src/Setup/Template.php index 7ead78bcd6..7678169227 100644 --- a/setup/src/Setup/Template.php +++ b/setup/src/Setup/Template.php @@ -10,7 +10,6 @@ class Template { public function __construct($srcPath, $fileType) { $this->filetype = $fileType; - $this->smarty = \Civi\Setup\SmartyUtil::createSmarty($srcPath); $this->assign('generated', "DO NOT EDIT. Generated by Installer"); diff --git a/xml/templates/civicrm_msg_template.tpl b/xml/templates/civicrm_msg_template.tpl index 22f55527c1..4459733415 100644 --- a/xml/templates/civicrm_msg_template.tpl +++ b/xml/templates/civicrm_msg_template.tpl @@ -10,48 +10,33 @@ -- {$generated} -- +{foreach from=$optionGroupNames key=name item=description} INSERT INTO civicrm_option_group - (name, {localize field='title'}title{/localize}, {localize field='description'}description{/localize}, is_reserved, is_active) VALUES -{foreach from=$ogNames key=name item=description name=for_groups} - ('msg_tpl_workflow_{$name}', {localize}'{$description}'{/localize}, {localize}'{$description}'{/localize}, 1, 1) {if $smarty.foreach.for_groups.last};{else},{/if} + (name, {localize field='title'}title{/localize}, {localize field='description'}description{/localize}, is_reserved, is_active) + VALUES + ('msg_tpl_workflow_{$name}', {localize}'{$description}'{/localize}, {localize}'{$description}'{/localize}, 1, 1); {/foreach} -{foreach from=$ogNames key=name item=description} - SELECT @tpl_ogid_{$name} := MAX(id) FROM civicrm_option_group WHERE name = 'msg_tpl_workflow_{$name}'; -{/foreach} +{foreach from=$templates key=templateName item=template} + {if $template.option_group_name} + INSERT INTO civicrm_option_value + (option_group_id, name, {localize field='label'}label{/localize}, value, weight) + VALUES + ((SELECT MAX(id) FROM civicrm_option_group WHERE name = 'msg_tpl_workflow_{$template.option_group_name}') , '{$templateName}', {localize}'{$template.title}'{/localize}, {$template.value}, {$template.weight}); -INSERT INTO civicrm_option_value - (option_group_id, name, {localize field='label'}label{/localize}, value, weight) VALUES -{foreach from=$ovNames key=gName item=ovs name=for_groups} -{foreach from=$ovs key=vName item=label name=for_values} - (@tpl_ogid_{$gName}, '{$vName}', {localize}'{$label}'{/localize}, {$smarty.foreach.for_values.iteration}, {$smarty.foreach.for_values.iteration}) {if $smarty.foreach.for_groups.last and $smarty.foreach.for_values.last};{else},{/if} -{/foreach} -{/foreach} - -{foreach from=$ovNames key=gName item=ovs} -{foreach from=$ovs key=vName item=label} - SELECT @tpl_ovid_{$vName} := MAX(id) FROM civicrm_option_value WHERE option_group_id = @tpl_ogid_{$gName} AND name = '{$vName}'; -{/foreach} -{/foreach} - -INSERT INTO civicrm_msg_template - (msg_title, msg_subject, msg_text, msg_html, workflow_name, workflow_id, is_default, is_reserved) VALUES -{foreach from=$ovNames key=gName item=ovs name=for_groups} -{foreach from=$ovs key=vName item=title name=for_values} - {assign var="subject_file_name" value=$vName|cat:'_subject'} - {assign var="html_file_name" value=$vName|cat:'_html'} - {assign var="text_file_name" value=$vName|cat:'_text'} - {fetch assign=subject file="$gencodeXmlDir/templates/message_templates/$subject_file_name.tpl"} - {fetch assign=text file="$gencodeXmlDir/templates/message_templates/$text_file_name.tpl"} - {fetch assign=html file="$gencodeXmlDir/templates/message_templates/$html_file_name.tpl"} - ('{$title}', '{$subject|escape:"quotes"}', '{$text|escape:"quotes"}', '{$html|escape:"quotes"}', '{$vName}', @tpl_ovid_{$vName}, 1, 0), - ('{$title}', '{$subject|escape:"quotes"}', '{$text|escape:"quotes"}', '{$html|escape:"quotes"}', '{$vName}', @tpl_ovid_{$vName}, 0, 1) {if $smarty.foreach.for_groups.last and $smarty.foreach.for_values.last};{else},{/if} -{/foreach} -{/foreach} + INSERT INTO civicrm_msg_template + (msg_title, msg_subject, msg_text, msg_html, workflow_name, workflow_id, is_default, is_reserved) + VALUES + ('{$template.title}', '{$template.subject|escape:"quotes"}', '{$template.msg_text|escape:"quotes"}', '{$template.msg_html|escape:"quotes"}', '{$template.name}', (SELECT id FROM civicrm_option_value WHERE name = '{$template.name}'), 1, 0); -{foreach from=$templates item=tpl} - {fetch assign=content file=$tpl.filename} -INSERT INTO civicrm_msg_template - (msg_title, msg_subject, msg_text, msg_html, workflow_id, is_default, is_reserved) VALUES - ('{$tpl.name} Template', '{$tpl.name}', '', '{$content|escape:"quotes"}' ,NULL, 1, 0); + INSERT INTO civicrm_msg_template + (msg_title, msg_subject, msg_text, msg_html, workflow_name, workflow_id, is_default, is_reserved) + VALUES + ('{$template.title}', '{$template.subject|escape:"quotes"}', '{$template.msg_text|escape:"quotes"}', '{$template.msg_html|escape:"quotes"}', '{$template.name}', (SELECT id FROM civicrm_option_value WHERE name = '{$template.name}'), 0, 1); + {else} + INSERT INTO civicrm_msg_template + (msg_text, msg_title, msg_subject, msg_html) + VALUES + ('', '{$template.title}', '{$template.subject|escape:"quotes"}', '{$template.msg_html|escape:"quotes"}'); + {/if} {/foreach} -- 2.25.1