fields = $fields; } /** * Check for required fields. * * @param \Civi\FlexMailer\Event\CheckSendableEvent $e */ public function onCheckSendable(CheckSendableEvent $e) { if (!$this->isActive()) { return; } foreach ($this->fields as $field) { // Parentheses indicate multiple options. Ex: '(body_html|body_text)' if ($field{0} === '(') { $alternatives = explode('|', substr($field, 1, -1)); $fieldTitle = implode(' or ', array_map(function ($x) { return "\"$x\""; }, $alternatives)); $found = $this->hasAny($e->getMailing(), $alternatives); } else { $fieldTitle = "\"$field\""; $found = !empty($e->getMailing()->{$field}); } if (!$found) { $e->setError($field, E::ts('Field %1 is required.', array( 1 => $fieldTitle, ))); } unset($found); } } /** * Determine if $object has any of the given properties. * * @param mixed $object * @param array $alternatives * @return bool */ protected function hasAny($object, $alternatives) { foreach ($alternatives as $alternative) { if (!empty($object->{$alternative})) { return TRUE; } } return FALSE; } /** * Get the list of required fields. * * @return array * Ex: array('subject', 'from_name', '(body_html|body_text)'). */ public function getFields() { return $this->fields; } /** * Set the list of required fields. * * @param array $fields * Ex: array('subject', 'from_name', '(body_html|body_text)'). * @return RequiredFields */ public function setFields($fields) { $this->fields = $fields; return $this; } }