diff --git a/CRM/Activity/Form/Task/PDFLetterCommon.php b/CRM/Activity/Form/Task/PDFLetterCommon.php index 7af6c006cfe0..3e16f72b0c33 100644 --- a/CRM/Activity/Form/Task/PDFLetterCommon.php +++ b/CRM/Activity/Form/Task/PDFLetterCommon.php @@ -42,35 +42,30 @@ public static function postProcess(&$form) { * Produce the document from the activities * This uses the new token processor * - * @param array $activityIds array of activity ids - * @param string $html_message message text with tokens - * @param array $formValues formValues from the form + * @param array $activityIds array of activity ids + * @param string $html_message message text with tokens + * @param array $formValues formValues from the form * * @return string + * + * @throws \API_Exception */ public static function createDocument($activityIds, $html_message, $formValues) { - $tp = self::createTokenProcessor(); - $tp->addMessage('body_html', $html_message, 'text/html'); - - foreach ($activityIds as $activityId) { - $tp->addRow()->context('activity_id', $activityId); + $result = \Civi\Api4\MessageTemplate::GetContent() + ->setHtmlStringToParse($html_message) + ->setEntities(['Activity']) + ->setEntityIds(['Activity' => $activityIds]) + ->setCheckPermissions(FALSE) + ->execute(); + $html = []; + foreach ($result as $htmDetail) { + $html[] = $htmDetail['html_string']; } - $tp->evaluate(); - - return self::renderFromRows($tp->getRows(), 'body_html', $formValues); - } - /** - * Create a token processor - * - * @return \Civi\Token\TokenProcessor - */ - public static function createTokenProcessor() { - return new TokenProcessor(\Civi::dispatcher(), [ - 'controller' => get_class(), - 'smarty' => FALSE, - 'schema' => ['activity_id'], - ]); + if (!empty($formValues['is_unit_test'])) { + return $html; + } + self::outputFromHtml($html, $formValues); } } diff --git a/CRM/Core/Form/Task/PDFLetterCommon.php b/CRM/Core/Form/Task/PDFLetterCommon.php index 49776534e041..265f467a296e 100644 --- a/CRM/Core/Form/Task/PDFLetterCommon.php +++ b/CRM/Core/Form/Task/PDFLetterCommon.php @@ -327,33 +327,6 @@ public static function formatMessage(&$message) { $message = implode($newLineOperators['p']['oper'], $htmlMsg); } - /** - * Render html from rows - * - * @param $rows - * @param string $msgPart - * The name registered with the TokenProcessor - * @param array $formValues - * The values submitted through the form - * - * @return array - * If formValues['is_unit_test'] is true, otherwise outputs document to browser - */ - public static function renderFromRows($rows, $msgPart, $formValues) { - $html = []; - foreach ($rows as $row) { - $html[] = $row->render($msgPart); - } - - if (!empty($formValues['is_unit_test'])) { - return $html; - } - - if (!empty($html)) { - self::outputFromHtml($formValues, $html); - } - } - /** * List the available tokens * @return array of token name => label diff --git a/Civi/Api4/Action/MessageTemplate/GetContent.php b/Civi/Api4/Action/MessageTemplate/GetContent.php new file mode 100644 index 000000000000..1284c778123e --- /dev/null +++ b/Civi/Api4/Action/MessageTemplate/GetContent.php @@ -0,0 +1,152 @@ + get_class(), + 'smarty' => FALSE, + 'schema' => ['Activity' => 'activity_id'], + ]); + $textFields = [ + 'msg_html' => $this->getMessageHtml(), + 'msg_subject' => $this->getMessageSubject(), + 'msg_text' => $this->getMessageText(), + 'html_string' => $this->getHtmlStringToParse(), + 'text_string' => $this->getTextStringToParse() + ]; + foreach ($textFields as $fieldKey => $textField) { + $html = []; + if (empty($textField)) { + continue; + } + foreach ($this->getEntityIds() as $entity => $ids) { + foreach ($ids as $id) { + $tokenProcessor->addRow()->context(strtolower($entity) . '_id', $id); + } + } + $tokenProcessor->addMessage($fieldKey, $textField, $this->getFormat($fieldKey)); + $tokenProcessor->evaluate(); + foreach ($tokenProcessor->getRows() as $row) { + /* @var \Civi\Token\TokenRow $row */ + $html = $row->render($fieldKey, $row); + } + $result[] = [$fieldKey => $html]; + } + } + + /** + * Get the format to pass in for the given key. + * + * @param string $key + * + * @return string + */ + protected function getFormat($key) { + $formats = [ + 'msg_subject' => 'text/plain', + 'msg_text' => 'text/plain', + 'msg_html' => 'text/html', + 'html_string' => 'text/html', + 'text_string' => 'text/plain', + ]; + return$formats[$key]; + } + +} diff --git a/Civi/Api4/Generic/AbstractAction.php b/Civi/Api4/Generic/AbstractAction.php index 905c9a1911d6..7a90e1c0a343 100644 --- a/Civi/Api4/Generic/AbstractAction.php +++ b/Civi/Api4/Generic/AbstractAction.php @@ -14,8 +14,6 @@ * * @package CRM * @copyright CiviCRM LLC https://civicrm.org/licensing - * $Id$ - * */ namespace Civi\Api4\Generic; diff --git a/Civi/Api4/MessageTemplate.php b/Civi/Api4/MessageTemplate.php index 10101db1538f..eb7a5f848869 100644 --- a/Civi/Api4/MessageTemplate.php +++ b/Civi/Api4/MessageTemplate.php @@ -27,4 +27,13 @@ */ class MessageTemplate extends Generic\DAOEntity { + /** + * @return \Civi\Api4\Action\MessageTemplate\GetContent + * + * @throws \API_Exception + */ + public static function GetContent() { + return new Action\MessageTemplate\GetContent(__CLASS__, __FUNCTION__); + } + }