4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
13 namespace Civi\WorkflowMessage\Traits
;
16 * Define a series of high-level, non-extensible helpers for WorkflowMessages,
17 * such as `renderTemplate()` or `sendTemplate()`.
19 * These helpers are convenient because it should be common to take a WorkflowMessage
20 * instance and pass it to a template. However, WorkflowMessage is the data-model
21 * for content of a message -- templating is outside the purview of WorkflowMessage.
22 * Consequently, there should not be any substantial templating logic here. Instead,
23 * these helpers MUST ONLY delegate out to a canonical renderer.
25 trait FinalHelperTrait
{
28 * @see \Civi\WorkflowMessage\WorkflowMessageInterface::export()
29 * @see \Civi\WorkflowMessage\Traits\ReflectiveWorkflowTrait::export()
31 abstract public function export(string $format = NULL): ?
array;
34 * @see \Civi\WorkflowMessage\WorkflowMessageInterface::validate()
35 * @see \Civi\WorkflowMessage\Traits\ReflectiveWorkflowTrait::validate()
37 abstract public function validate(): array;
40 * @see \Civi\WorkflowMessage\WorkflowMessageInterface::assertValid()
42 final public function assertValid($strict = FALSE) {
43 $validations = $this->validate();
45 $validations = array_filter($validations, function ($validation) {
46 return $validation['severity'] === 'error';
49 if (!empty($validations)) {
50 throw new \
CRM_Core_Exception(sprintf("Found %d validation error(s) in %s.", count($validations), get_class($this)));
56 * @see \Civi\WorkflowMessage\WorkflowMessageInterface::renderTemplate()
58 final public function renderTemplate(array $params = []): array {
59 $params['model'] = $this;
60 return \CRM_Core_BAO_MessageTemplate
::renderTemplate($params);
64 * @see \Civi\WorkflowMessage\WorkflowMessageInterface::sendTemplate()
66 final public function sendTemplate(array $params = []): array {
67 return \CRM_Core_BAO_MessageTemplate
::sendTemplate($params +
['model' => $this]);
71 // * Get the list of available tokens.
74 // * Ex: ['contact.first_name' => ['entity' => 'contact', 'field' => 'first_name', 'label' => ts('Last Name')]]
75 // * Array(string $dottedName => array('entity'=>string, 'field'=>string, 'label'=>string)).
77 //final public function getTokens(): array {
78 // $tp = new TokenProcessor(\Civi::dispatcher(), [
79 // 'controller' => static::CLASS,
81 // $tp->addRow($this->export('tokenContext'));
82 // return $tp->getTokens();