3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
11 namespace Civi\FlexMailer\Listener
;
13 use CRM_Flexmailer_ExtensionUtil
as E
;
14 use Civi\FlexMailer\Event\CheckSendableEvent
;
17 * Class RequiredFields
18 * @package Civi\FlexMailer\Listener
20 * The RequiredFields listener checks that all mandatory fields have a value.
22 class RequiredFields
extends BaseListener
{
26 * Ex: array('subject', 'from_name', '(body_html|body_text)').
31 * RequiredFields constructor.
32 * @param array $fields
34 public function __construct($fields) {
35 $this->fields
= $fields;
39 * Check for required fields.
41 * @param \Civi\FlexMailer\Event\CheckSendableEvent $e
43 public function onCheckSendable(CheckSendableEvent
$e) {
44 if (!$this->isActive()) {
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) {
55 $found = $this->hasAny($e->getMailing(), $alternatives);
58 $fieldTitle = "\"$field\"";
59 $found = !empty($e->getMailing()->{$field});
63 $e->setError($field, E
::ts('Field %1 is required.', array(
72 * Determine if $object has any of the given properties.
74 * @param mixed $object
75 * @param array $alternatives
78 protected function hasAny($object, $alternatives) {
79 foreach ($alternatives as $alternative) {
80 if (!empty($object->{$alternative})) {
88 * Get the list of required fields.
91 * Ex: array('subject', 'from_name', '(body_html|body_text)').
93 public function getFields() {
98 * Set the list of required fields.
100 * @param array $fields
101 * Ex: array('subject', 'from_name', '(body_html|body_text)').
102 * @return RequiredFields
104 public function setFields($fields) {
105 $this->fields
= $fields;