adds logic for presence of email recipient fields

This commit is contained in:
Andrew Aitken-Fincham 2018-02-13 16:24:53 +00:00
parent 46460c0e7c
commit 8c34519644
4 changed files with 45 additions and 9 deletions

View File

@ -163,7 +163,6 @@ class UserDefinedFormController extends PageController
*/
public function generateConditionalJavascript()
{
$default = '';
$rules = '';
$form = $this->data();
$formFields = $form->Fields();
@ -325,14 +324,6 @@ JS
$email->addData($key, $value);
}
$email->setFrom(explode(',', $recipient->EmailFrom));
$email->setTo(explode(',', $recipient->EmailAddress));
$email->setSubject($recipient->EmailSubject);
if ($recipient->EmailReplyTo) {
$email->setReplyTo(explode(',', $recipient->EmailReplyTo));
}
// check to see if they are a dynamic reply to. eg based on a email field a user selected
if ($recipient->SendEmailFromField()) {
$submittedFormField = $submittedFields->find('Name', $recipient->SendEmailFromField()->Name);
@ -340,7 +331,15 @@ JS
if ($submittedFormField && is_string($submittedFormField->Value)) {
$email->setReplyTo(explode(',', $submittedFormField->Value));
}
} elseif ($recipient->EmailReplyTo) {
$email->setReplyTo(explode(',', $recipient->EmailReplyTo));
}
// check for a specified from; otherwise fall back to server defaults
if ($recipient->EmailFrom) {
$email->setFrom(explode(',', $recipient->EmailFrom));
}
// check to see if they are a dynamic reciever eg based on a dropdown field a user selected
if ($recipient->SendEmailToField()) {
$submittedFormField = $submittedFields->find('Name', $recipient->SendEmailToField()->Name);
@ -348,6 +347,8 @@ JS
if ($submittedFormField && is_string($submittedFormField->Value)) {
$email->setTo(explode(',', $submittedFormField->Value));
}
} else {
$email->setTo(explode(',', $recipient->EmailAddress));
}
// check to see if there is a dynamic subject
@ -357,6 +358,8 @@ JS
if ($submittedFormField && trim($submittedFormField->Value)) {
$email->setSubject($submittedFormField->Value);
}
} else {
$email->setSubject($recipient->EmailSubject);
}
$this->extend('updateEmail', $email, $recipient, $emailData);

View File

@ -174,6 +174,7 @@ class UserForm extends Form
->Fields()
->filter('Required', true)
->column('Name');
$requiredNames = array_merge($requiredNames, $this->getEmailRecipientRequiredFields());
$required = new RequiredFields($requiredNames);
$this->extend('updateRequiredFields', $required);
$required->setForm($this);
@ -203,4 +204,30 @@ class UserForm extends Form
{
return $this->config()->get('button_text');
}
/**
* Push fields into the RequiredFields array if they are used by any Email recipients.
* Ignore if there is a backup i.e. the plain string field is set
*
* @return array required fields names
*/
protected function getEmailRecipientRequiredFields()
{
$requiredFields = [];
$recipientFieldsMap = [
'EmailAddress' => 'SendEmailToField',
'EmailSubject' => 'SendEmailSubjectField',
'EmailReplyTo' => 'SendEmailFromField'
];
foreach ($this->getController()->data()->EmailRecipients() as $recipient) {
foreach ($recipientFieldsMap as $textField => $dynamicFormField) {
if (empty($recipient->$textField) && $recipient->getComponent($dynamicFormField)) {
$requiredFields[] = $recipient->getComponent($dynamicFormField)->Name;
}
}
}
return $requiredFields;
}
}

View File

@ -613,6 +613,11 @@ class EmailRecipient extends DataObject
}
}
}
// if there is no from address and no fallback, you'll have errors if this isn't defined
if (!$this->EmailFrom && empty(Email::getSendAllEmailsFrom()) && empty(Email::config()->get('admin)_email'))) {
$result->addError(_t(__CLASS__.".EMAILFROMREQUIRED", '"Email From" address is required'));
}
return $result;
}
}

View File

@ -191,6 +191,7 @@ en:
EMAILCONTENTTAB: 'Email Content'
EMAILDETAILSTAB: 'Email Details'
EMAILFROMINVALID: '"Email From" is not valid'
EMAILFROMREQUIRED: '"Email From" address is required'
EMAILREPLYTOINVALID: '"Email Reply To" is not valid'
PLURALNAME: 'User Defined Form Email Recipients'
SINGULARNAME: 'User Defined Form Email Recipient'