-
-
Notifications
You must be signed in to change notification settings - Fork 812
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ebb452
commit 6420d73
Showing
5 changed files
with
179 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters