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; |
89ab2f67 | 18 | use Civi\WorkflowMessage\Traits\LocalizationTrait; |
92f656cb TO |
19 | use Civi\WorkflowMessage\Traits\ReflectiveWorkflowTrait; |
20 | ||
21 | /** | |
22 | * Generic base-class for describing the inputs for a workflow email template. | |
23 | * | |
24 | * @method $this setContactId(int|null $contactId) | |
25 | * @method int|null getContactId() | |
8bfe8c6d TO |
26 | * @method $this setContact(array|null $contact) |
27 | * @method array|null getContact() | |
8cbb82e5 TO |
28 | * |
29 | * @support template-only | |
30 | * GenericWorkflowMessage should aim for "full" support, but it's prudent to keep | |
31 | * it flexible for the first few months. Consider updating to "full" after Dec 2021. | |
92f656cb TO |
32 | */ |
33 | class GenericWorkflowMessage implements WorkflowMessageInterface { | |
34 | ||
35 | // Implement getFields(), import(), export(), validate() - All methods based on inspecting class properties (`ReflectionClass`). | |
36 | // Define stub methods exportExtraTokenContext(), exportExtraTplParams(), etc. | |
37 | use ReflectiveWorkflowTrait; | |
38 | ||
39 | // Implement __call() - Public and protected properties are automatically given a default getter/setter. These may be overridden/customized. | |
40 | use MagicGetterSetterTrait; | |
41 | ||
1972897e TO |
42 | // Implement assertValid(), renderTemplate(), sendTemplate() - Sugary stub methods that delegate to real APIs. |
43 | use FinalHelperTrait; | |
44 | ||
02a47bd1 TO |
45 | // Implement setTo(), setReplyTo(), etc |
46 | use AddressingTrait; | |
47 | ||
89ab2f67 TO |
48 | // Implement setLocale(), etc |
49 | use LocalizationTrait; | |
50 | ||
92f656cb TO |
51 | /** |
52 | * WorkflowMessage constructor. | |
53 | * | |
54 | * @param array $imports | |
55 | * List of values to import. | |
56 | * Ex: ['tplParams' => [...tplValues...], 'tokenContext' => [...tokenData...]] | |
57 | * Ex: ['modelProps' => [...classProperties...]] | |
58 | */ | |
59 | public function __construct(array $imports = []) { | |
60 | WorkflowMessage::importAll($this, $imports); | |
61 | } | |
62 | ||
63 | /** | |
64 | * The contact receiving this message. | |
65 | * | |
8bfe8c6d | 66 | * @var int|null |
92f656cb | 67 | * @scope tokenContext |
8bfe8c6d | 68 | * @fkEntity Contact |
92f656cb TO |
69 | */ |
70 | protected $contactId; | |
71 | ||
8bfe8c6d TO |
72 | /** |
73 | * @var array|null | |
74 | * @scope tokenContext | |
75 | */ | |
76 | protected $contact; | |
77 | ||
78 | /** | |
79 | * Must provide either 'int $contactId' or 'array $contact' | |
80 | * | |
81 | * @param array $errors | |
82 | * @see ReflectiveWorkflowTrait::validate() | |
83 | */ | |
84 | protected function validateExtra_contact(array &$errors) { | |
85 | if (empty($this->contactId) && empty($this->contact['id'])) { | |
86 | $errors[] = [ | |
87 | 'severity' => 'error', | |
88 | 'fields' => ['contactId', 'contact'], | |
89 | 'name' => 'missingContact', | |
90 | 'message' => ts('Message template requires one of these fields (%1)', ['contactId, contact']), | |
91 | ]; | |
92 | } | |
93 | if (!empty($this->contactId) && !empty($this->contact)) { | |
94 | $errors[] = [ | |
95 | 'severity' => 'warning', | |
96 | 'fields' => ['contactId', 'contact'], | |
97 | 'name' => 'missingContact', | |
98 | 'message' => ts('Passing both (%1) may lead to ambiguous behavior.', ['contactId, contact']), | |
99 | ]; | |
100 | } | |
101 | } | |
102 | ||
b8fe55cd EM |
103 | /** |
104 | * Define tokens to be exported as smarty values. | |
105 | * | |
106 | * @param array $export | |
107 | */ | |
108 | protected function exportExtraTokenContext(array &$export): void { | |
109 | // Tax term is exposed at the generic level as so many templates use it | |
110 | // (e.g. Membership, participant, pledge as well as contributions). | |
111 | $export['smartyTokenAlias']['taxTerm'] = 'domain.tax_term'; | |
112 | } | |
113 | ||
92f656cb | 114 | } |