0c072a485e10c8ee3a8d81539fd29a94981d223c
[civicrm-core.git] / Civi / WorkflowMessage / GenericWorkflowMessage.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13 namespace Civi\WorkflowMessage;
14
15 use Civi\Schema\Traits\MagicGetterSetterTrait;
16 use Civi\WorkflowMessage\Traits\AddressingTrait;
17 use Civi\WorkflowMessage\Traits\FinalHelperTrait;
18 use Civi\WorkflowMessage\Traits\ReflectiveWorkflowTrait;
19
20 /**
21 * Generic base-class for describing the inputs for a workflow email template.
22 *
23 * @method $this setContactId(int|null $contactId)
24 * @method int|null getContactId()
25 * @method $this setContact(array|null $contact)
26 * @method array|null getContact()
27 *
28 * @support template-only
29 * GenericWorkflowMessage should aim for "full" support, but it's prudent to keep
30 * it flexible for the first few months. Consider updating to "full" after Dec 2021.
31 */
32 class GenericWorkflowMessage implements WorkflowMessageInterface {
33
34 // Implement getFields(), import(), export(), validate() - All methods based on inspecting class properties (`ReflectionClass`).
35 // Define stub methods exportExtraTokenContext(), exportExtraTplParams(), etc.
36 use ReflectiveWorkflowTrait;
37
38 // Implement __call() - Public and protected properties are automatically given a default getter/setter. These may be overridden/customized.
39 use MagicGetterSetterTrait;
40
41 // Implement assertValid(), renderTemplate(), sendTemplate() - Sugary stub methods that delegate to real APIs.
42 use FinalHelperTrait;
43
44 // Implement setTo(), setReplyTo(), etc
45 use AddressingTrait;
46
47 /**
48 * WorkflowMessage constructor.
49 *
50 * @param array $imports
51 * List of values to import.
52 * Ex: ['tplParams' => [...tplValues...], 'tokenContext' => [...tokenData...]]
53 * Ex: ['modelProps' => [...classProperties...]]
54 */
55 public function __construct(array $imports = []) {
56 WorkflowMessage::importAll($this, $imports);
57 }
58
59 /**
60 * The contact receiving this message.
61 *
62 * @var int|null
63 * @scope tokenContext
64 * @fkEntity Contact
65 */
66 protected $contactId;
67
68 /**
69 * @var array|null
70 * @scope tokenContext
71 */
72 protected $contact;
73
74 /**
75 * Must provide either 'int $contactId' or 'array $contact'
76 *
77 * @param array $errors
78 * @see ReflectiveWorkflowTrait::validate()
79 */
80 protected function validateExtra_contact(array &$errors) {
81 if (empty($this->contactId) && empty($this->contact['id'])) {
82 $errors[] = [
83 'severity' => 'error',
84 'fields' => ['contactId', 'contact'],
85 'name' => 'missingContact',
86 'message' => ts('Message template requires one of these fields (%1)', ['contactId, contact']),
87 ];
88 }
89 if (!empty($this->contactId) && !empty($this->contact)) {
90 $errors[] = [
91 'severity' => 'warning',
92 'fields' => ['contactId', 'contact'],
93 'name' => 'missingContact',
94 'message' => ts('Passing both (%1) may lead to ambiguous behavior.', ['contactId, contact']),
95 ];
96 }
97 }
98
99 }