Commit | Line | Data |
---|---|---|
bdf67e28 SL |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | Copyright CiviCRM LLC. All rights reserved. | | |
5 | | | | |
6 | | This work is published under the GNU AGPLv3 license with some | | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
9 | +--------------------------------------------------------------------+ | |
10 | */ | |
11 | namespace Civi\FlexMailer\Listener; | |
12 | ||
13 | use CRM_Flexmailer_ExtensionUtil as E; | |
14 | use Civi\FlexMailer\Event\CheckSendableEvent; | |
15 | ||
16 | /** | |
17 | * Class RequiredFields | |
18 | * @package Civi\FlexMailer\Listener | |
19 | * | |
20 | * The RequiredFields listener checks that all mandatory fields have a value. | |
21 | */ | |
22 | class RequiredFields extends BaseListener { | |
23 | ||
24 | /** | |
25 | * @var array | |
26 | * Ex: array('subject', 'from_name', '(body_html|body_text)'). | |
27 | */ | |
28 | private $fields; | |
29 | ||
30 | /** | |
31 | * RequiredFields constructor. | |
32 | * @param array $fields | |
33 | */ | |
34 | public function __construct($fields) { | |
35 | $this->fields = $fields; | |
36 | } | |
37 | ||
38 | /** | |
39 | * Check for required fields. | |
40 | * | |
41 | * @param \Civi\FlexMailer\Event\CheckSendableEvent $e | |
42 | */ | |
43 | public function onCheckSendable(CheckSendableEvent $e) { | |
44 | if (!$this->isActive()) { | |
45 | return; | |
46 | } | |
47 | ||
48 | foreach ($this->fields as $field) { | |
49 | // Parentheses indicate multiple options. Ex: '(body_html|body_text)' | |
50 | if ($field{0} === '(') { | |
51 | $alternatives = explode('|', substr($field, 1, -1)); | |
52 | $fieldTitle = implode(' or ', array_map(function ($x) { | |
53 | return "\"$x\""; | |
54 | }, $alternatives)); | |
55 | $found = $this->hasAny($e->getMailing(), $alternatives); | |
56 | } | |
57 | else { | |
58 | $fieldTitle = "\"$field\""; | |
59 | $found = !empty($e->getMailing()->{$field}); | |
60 | } | |
61 | ||
62 | if (!$found) { | |
63 | $e->setError($field, E::ts('Field %1 is required.', array( | |
64 | 1 => $fieldTitle, | |
65 | ))); | |
66 | } | |
67 | unset($found); | |
68 | } | |
69 | } | |
70 | ||
71 | /** | |
72 | * Determine if $object has any of the given properties. | |
73 | * | |
74 | * @param mixed $object | |
75 | * @param array $alternatives | |
76 | * @return bool | |
77 | */ | |
78 | protected function hasAny($object, $alternatives) { | |
79 | foreach ($alternatives as $alternative) { | |
80 | if (!empty($object->{$alternative})) { | |
81 | return TRUE; | |
82 | } | |
83 | } | |
84 | return FALSE; | |
85 | } | |
86 | ||
87 | /** | |
88 | * Get the list of required fields. | |
89 | * | |
90 | * @return array | |
91 | * Ex: array('subject', 'from_name', '(body_html|body_text)'). | |
92 | */ | |
93 | public function getFields() { | |
94 | return $this->fields; | |
95 | } | |
96 | ||
97 | /** | |
98 | * Set the list of required fields. | |
99 | * | |
100 | * @param array $fields | |
101 | * Ex: array('subject', 'from_name', '(body_html|body_text)'). | |
102 | * @return RequiredFields | |
103 | */ | |
104 | public function setFields($fields) { | |
105 | $this->fields = $fields; | |
106 | return $this; | |
107 | } | |
108 | ||
109 | } |