| 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\LocalizationTrait; |
| 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() |
| 26 | * @method $this setContact(array|null $contact) |
| 27 | * @method array|null getContact() |
| 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. |
| 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 | |
| 42 | // Implement assertValid(), renderTemplate(), sendTemplate() - Sugary stub methods that delegate to real APIs. |
| 43 | use FinalHelperTrait; |
| 44 | |
| 45 | // Implement setTo(), setReplyTo(), etc |
| 46 | use AddressingTrait; |
| 47 | |
| 48 | // Implement setLocale(), etc |
| 49 | use LocalizationTrait; |
| 50 | |
| 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 | * |
| 66 | * @var int|null |
| 67 | * @scope tokenContext |
| 68 | * @fkEntity Contact |
| 69 | */ |
| 70 | protected $contactId; |
| 71 | |
| 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 | |
| 103 | } |