From 63ed9ac261efa8d15469d75e1ca0b538f1b5c63d Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 22 Jun 2023 22:45:08 -0700 Subject: [PATCH] (REF) Move `loadTemplate()` from MsgTpl BAO to `TemplateTrait` --- CRM/Core/BAO/MessageTemplate.php | 71 ------------------ Civi/WorkflowMessage/Traits/TemplateTrait.php | 75 ++++++++++++++++++- 2 files changed, 74 insertions(+), 72 deletions(-) diff --git a/CRM/Core/BAO/MessageTemplate.php b/CRM/Core/BAO/MessageTemplate.php index 3a68dfabce..21ea726d67 100644 --- a/CRM/Core/BAO/MessageTemplate.php +++ b/CRM/Core/BAO/MessageTemplate.php @@ -470,77 +470,6 @@ class CRM_Core_BAO_MessageTemplate extends CRM_Core_DAO_MessageTemplate implemen ])->fetchMap('name', 'id'); } - /** - * Load the specified template. - * - * @param string $workflowName - * @param bool $isTest - * @param int|null $messageTemplateID - * @param string $groupName - * @param array|null $messageTemplateOverride - * Optionally, record with msg_subject, msg_text, msg_html. - * If omitted, the record will be loaded from workflowName/messageTemplateID. - * @param string|null $language - * - * @return array - * @throws \CRM_Core_Exception - * @internal - */ - public static function loadTemplate(string $workflowName, bool $isTest, int $messageTemplateID = NULL, $groupName = NULL, ?array $messageTemplateOverride = NULL, ?string $language = NULL): array { - $base = ['msg_subject' => NULL, 'msg_text' => NULL, 'msg_html' => NULL, 'pdf_format_id' => NULL]; - if (!$workflowName && !$messageTemplateID && !$messageTemplateOverride) { - throw new CRM_Core_Exception(ts("Message template not specified. No option value, ID, or template content.")); - } - - $apiCall = MessageTemplate::get(FALSE) - ->setLanguage($language) - ->setTranslationMode('fuzzy') - ->addSelect('msg_subject', 'msg_text', 'msg_html', 'pdf_format_id', 'id') - ->addWhere('is_default', '=', 1); - - if ($messageTemplateID) { - $apiCall->addWhere('id', '=', (int) $messageTemplateID); - $result = $apiCall->execute(); - } - elseif ($workflowName) { - $apiCall->addWhere('workflow_name', '=', $workflowName); - $result = $apiCall->execute(); - } - else { - // Don't bother with query. We know there's nothing. - $result = new \Civi\Api4\Generic\Result(); - } - $messageTemplate = array_merge($base, $result->first() ?: [], $messageTemplateOverride ?: []); - if (empty($messageTemplate['id']) && empty($messageTemplateOverride)) { - if ($messageTemplateID) { - throw new CRM_Core_Exception(ts('No such message template: id=%1.', [1 => $messageTemplateID])); - } - throw new CRM_Core_Exception(ts('No message template with workflow name %1.', [1 => $workflowName])); - } - - $mailContent = [ - 'subject' => $messageTemplate['msg_subject'], - 'text' => $messageTemplate['msg_text'], - 'html' => $messageTemplate['msg_html'], - 'format' => $messageTemplate['pdf_format_id'], - // Workflow name is the field in the message templates table that denotes the - // workflow the template is used for. This is intended to eventually - // replace the non-standard option value/group implementation - see - // https://github.com/civicrm/civicrm-core/pull/17227 and the longer - // discussion on https://github.com/civicrm/civicrm-core/pull/17180 - 'workflow_name' => $workflowName, - // Note messageTemplateID is the id but when present we also know it was specifically requested. - 'messageTemplateID' => $messageTemplateID, - // Group name & valueName are deprecated parameters. At some point it will not be passed out. - // https://github.com/civicrm/civicrm-core/pull/17180 - 'groupName' => $groupName, - 'workflow' => $workflowName, - 'isTest' => $isTest, - ]; - - return [$mailContent, $messageTemplate['actual_language'] ?? NULL]; - } - /** * Mark these fields as translatable. * diff --git a/Civi/WorkflowMessage/Traits/TemplateTrait.php b/Civi/WorkflowMessage/Traits/TemplateTrait.php index 9ba6d84578..f0aeba9cf9 100644 --- a/Civi/WorkflowMessage/Traits/TemplateTrait.php +++ b/Civi/WorkflowMessage/Traits/TemplateTrait.php @@ -11,6 +11,8 @@ namespace Civi\WorkflowMessage\Traits; +use Civi\Api4\MessageTemplate; + /** * @method getTemplate(): ?array * @method setTemplate(?array $value): $this @@ -56,9 +58,80 @@ trait TemplateTrait { if (empty($language) && !empty($model->getContactID())) { $language = \Civi\Api4\Contact::get(FALSE)->addWhere('id', '=', $this->getContactID())->addSelect('preferred_language')->execute()->first()['preferred_language']; } - [$mailContent, $translatedLanguage] = \CRM_Core_BAO_MessageTemplate::loadTemplate((string) $model->getWorkflowName(), $model->getIsTest(), $model->getTemplateId(), $model->getGroupName(), $model->getTemplate(), $language); + [$mailContent, $translatedLanguage] = self::loadTemplate((string) $model->getWorkflowName(), $model->getIsTest(), $model->getTemplateId(), $model->getGroupName(), $model->getTemplate(), $language); $model->setLocale($translatedLanguage ?? $model->getLocale()); return $mailContent; } + /** + * Load the specified template. + * + * @param string $workflowName + * @param bool $isTest + * @param int|null $messageTemplateID + * @param string $groupName + * @param array|null $messageTemplateOverride + * Optionally, record with msg_subject, msg_text, msg_html. + * If omitted, the record will be loaded from workflowName/messageTemplateID. + * @param string|null $language + * + * @return array + * @throws \CRM_Core_Exception + * @internal + */ + private static function loadTemplate(string $workflowName, bool $isTest, int $messageTemplateID = NULL, $groupName = NULL, ?array $messageTemplateOverride = NULL, ?string $language = NULL): array { + $base = ['msg_subject' => NULL, 'msg_text' => NULL, 'msg_html' => NULL, 'pdf_format_id' => NULL]; + if (!$workflowName && !$messageTemplateID && !$messageTemplateOverride) { + throw new CRM_Core_Exception(ts("Message template not specified. No option value, ID, or template content.")); + } + + $apiCall = MessageTemplate::get(FALSE) + ->setLanguage($language) + ->setTranslationMode('fuzzy') + ->addSelect('msg_subject', 'msg_text', 'msg_html', 'pdf_format_id', 'id') + ->addWhere('is_default', '=', 1); + + if ($messageTemplateID) { + $apiCall->addWhere('id', '=', (int) $messageTemplateID); + $result = $apiCall->execute(); + } + elseif ($workflowName) { + $apiCall->addWhere('workflow_name', '=', $workflowName); + $result = $apiCall->execute(); + } + else { + // Don't bother with query. We know there's nothing. + $result = new \Civi\Api4\Generic\Result(); + } + $messageTemplate = array_merge($base, $result->first() ?: [], $messageTemplateOverride ?: []); + if (empty($messageTemplate['id']) && empty($messageTemplateOverride)) { + if ($messageTemplateID) { + throw new CRM_Core_Exception(ts('No such message template: id=%1.', [1 => $messageTemplateID])); + } + throw new CRM_Core_Exception(ts('No message template with workflow name %1.', [1 => $workflowName])); + } + + $mailContent = [ + 'subject' => $messageTemplate['msg_subject'], + 'text' => $messageTemplate['msg_text'], + 'html' => $messageTemplate['msg_html'], + 'format' => $messageTemplate['pdf_format_id'], + // Workflow name is the field in the message templates table that denotes the + // workflow the template is used for. This is intended to eventually + // replace the non-standard option value/group implementation - see + // https://github.com/civicrm/civicrm-core/pull/17227 and the longer + // discussion on https://github.com/civicrm/civicrm-core/pull/17180 + 'workflow_name' => $workflowName, + // Note messageTemplateID is the id but when present we also know it was specifically requested. + 'messageTemplateID' => $messageTemplateID, + // Group name & valueName are deprecated parameters. At some point it will not be passed out. + // https://github.com/civicrm/civicrm-core/pull/17180 + 'groupName' => $groupName, + 'workflow' => $workflowName, + 'isTest' => $isTest, + ]; + + return [$mailContent, $messageTemplate['actual_language'] ?? NULL]; + } + } -- 2.25.1