Merge pull request #21307 from civicrm/5.41
[civicrm-core.git] / Civi / WorkflowMessage / GenericWorkflowMessage.php
CommitLineData
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
13namespace Civi\WorkflowMessage;
14
15use Civi\Schema\Traits\MagicGetterSetterTrait;
02a47bd1 16use Civi\WorkflowMessage\Traits\AddressingTrait;
1972897e 17use Civi\WorkflowMessage\Traits\FinalHelperTrait;
92f656cb
TO
18use 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()
92f656cb
TO
27 */
28class GenericWorkflowMessage implements WorkflowMessageInterface {
29
30 // Implement getFields(), import(), export(), validate() - All methods based on inspecting class properties (`ReflectionClass`).
31 // Define stub methods exportExtraTokenContext(), exportExtraTplParams(), etc.
32 use ReflectiveWorkflowTrait;
33
34 // Implement __call() - Public and protected properties are automatically given a default getter/setter. These may be overridden/customized.
35 use MagicGetterSetterTrait;
36
1972897e
TO
37 // Implement assertValid(), renderTemplate(), sendTemplate() - Sugary stub methods that delegate to real APIs.
38 use FinalHelperTrait;
39
02a47bd1
TO
40 // Implement setTo(), setReplyTo(), etc
41 use AddressingTrait;
42
92f656cb
TO
43 /**
44 * WorkflowMessage constructor.
45 *
46 * @param array $imports
47 * List of values to import.
48 * Ex: ['tplParams' => [...tplValues...], 'tokenContext' => [...tokenData...]]
49 * Ex: ['modelProps' => [...classProperties...]]
50 */
51 public function __construct(array $imports = []) {
52 WorkflowMessage::importAll($this, $imports);
53 }
54
55 /**
56 * The contact receiving this message.
57 *
8bfe8c6d 58 * @var int|null
92f656cb 59 * @scope tokenContext
8bfe8c6d 60 * @fkEntity Contact
92f656cb
TO
61 */
62 protected $contactId;
63
8bfe8c6d
TO
64 /**
65 * @var array|null
66 * @scope tokenContext
67 */
68 protected $contact;
69
70 /**
71 * Must provide either 'int $contactId' or 'array $contact'
72 *
73 * @param array $errors
74 * @see ReflectiveWorkflowTrait::validate()
75 */
76 protected function validateExtra_contact(array &$errors) {
77 if (empty($this->contactId) && empty($this->contact['id'])) {
78 $errors[] = [
79 'severity' => 'error',
80 'fields' => ['contactId', 'contact'],
81 'name' => 'missingContact',
82 'message' => ts('Message template requires one of these fields (%1)', ['contactId, contact']),
83 ];
84 }
85 if (!empty($this->contactId) && !empty($this->contact)) {
86 $errors[] = [
87 'severity' => 'warning',
88 'fields' => ['contactId', 'contact'],
89 'name' => 'missingContact',
90 'message' => ts('Passing both (%1) may lead to ambiguous behavior.', ['contactId, contact']),
91 ];
92 }
93 }
94
92f656cb 95}