Commit | Line | Data |
---|---|---|
92f656cb TO |
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; | |
02a47bd1 | 16 | use Civi\WorkflowMessage\Traits\AddressingTrait; |
1972897e | 17 | use Civi\WorkflowMessage\Traits\FinalHelperTrait; |
92f656cb TO |
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() | |
8bfe8c6d TO |
25 | * @method $this setContact(array|null $contact) |
26 | * @method array|null getContact() | |
8cbb82e5 TO |
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. | |
92f656cb TO |
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 | ||
1972897e TO |
41 | // Implement assertValid(), renderTemplate(), sendTemplate() - Sugary stub methods that delegate to real APIs. |
42 | use FinalHelperTrait; | |
43 | ||
02a47bd1 TO |
44 | // Implement setTo(), setReplyTo(), etc |
45 | use AddressingTrait; | |
46 | ||
92f656cb TO |
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 | * | |
8bfe8c6d | 62 | * @var int|null |
92f656cb | 63 | * @scope tokenContext |
8bfe8c6d | 64 | * @fkEntity Contact |
92f656cb TO |
65 | */ |
66 | protected $contactId; | |
67 | ||
8bfe8c6d TO |
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 | ||
92f656cb | 99 | } |