Skip to content

Commit

Permalink
Add new api to parse messages
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Apr 24, 2020
1 parent 2ebb452 commit 6420d73
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 52 deletions.
41 changes: 18 additions & 23 deletions CRM/Activity/Form/Task/PDFLetterCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
27 changes: 0 additions & 27 deletions CRM/Core/Form/Task/PDFLetterCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
152 changes: 152 additions & 0 deletions Civi/Api4/Action/MessageTemplate/GetContent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php


namespace Civi\Api4\Action\MessageTemplate;

use Civi\Api4\Generic\Result;
use Civi\Token\TokenProcessor;

/**
* Class GetContent.
*
* Get the content of an email for the given template text, rendering tokens.
*
* @method int setMessageTemplateId(int $messageTemplateID) Set Message Template ID.
* @method int getMessageTemplateId(int $messageTemplateID) Get Message Template ID.
* @method string setMessageSubject(string $messageSubject) Set Message Subject
* @method string getMessageSubject(string $messageSubject) Get Message Subject
* @method string setMessageHtml(string $messageHtml) Set Message Html
* @method string getMessageHtml string $messageHtml) Get Message Html
* @method string setMessageText(string $messageHtml) Set Message Text
* @method string getMessageText string $messageHtml) Get Message Text
* @method string getHtmlStringToParse(string $stringToParse) Get Html String to parse - ad hoc text.
* @method string setHtmlStringToParse(string $stringToParse) Set Html String to parse - ad hoc text.
* @method string getTextStringToParse(string $stringToParse) Get String to parse - ad hoc text.
* @method string setTextStringToParse(string $stringToParse) Set String to parse - ad hoc text.
* @method array setEntities(string $entities) Set entity/entities.
* @method array getEntities(string $entities) Get entity/entities.
* @method array setEntityIds(array $entityIds) Set entity IDs
* @method array getEntityIds(array $entityIds) Get entity IDs
*/
class GetContent extends \Civi\Api4\Generic\AbstractAction {

/**
* ID of message template.
*
* It is necessary to pass this or at least one of message_subject, message_text or message_html.
*
* @var int
*/
protected $messageTemplateId;

/**
* Ad hoc html string to parse.
*
* This is easy to use if not using an actual template field
*
* @var string
*/
protected $htmlStringToParse;

/**
* Ad hoc string to parse.
*
* This is easy to use if not using an actual template field
*
* @var string
*/
protected $textStringToParse;

/**
* String to be returned as the subject.
*
* @var string
*/
protected $messageSubject;

/**
* String to be returned as the subject.
*
* @var string
*/
protected $messageText;

/**
* String to be returned as the subject.
*
* @var string
*/
protected $messageHtml;

/**
* Entities for which tokens need to be resolved.
*
* This is required if tokens related to the entity are to be parsed and the entity cannot
* be derived from the message_template.
*
* @var array
*/
protected $entities;

/**
* An array of one of more ids for which the html should be rendered.
*
* @var array
*/
protected $entityIds = [];

/**
* @inheritDoc
*/
public function _run(Result $result) {
$tokenProcessor = new TokenProcessor(\Civi::dispatcher(), [
'controller' => 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];
}

}
2 changes: 0 additions & 2 deletions Civi/Api4/Generic/AbstractAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
* $Id$
*
*/

namespace Civi\Api4\Generic;
Expand Down
9 changes: 9 additions & 0 deletions Civi/Api4/MessageTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__);
}

}

0 comments on commit 6420d73

Please sign in to comment.