From 5446aa2c1b64d12701329234fa8e80e6b9f1a6c6 Mon Sep 17 00:00:00 2001 From: Jean Spiteri Date: Wed, 2 Jul 2014 11:56:15 +0000 Subject: [PATCH] Implementing the Dynamic Subject feature --- code/model/UserDefinedForm.php | 76 +++++++++++++++++----------------- 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/code/model/UserDefinedForm.php b/code/model/UserDefinedForm.php index 9e0452c..cd18a5e 100755 --- a/code/model/UserDefinedForm.php +++ b/code/model/UserDefinedForm.php @@ -977,8 +977,8 @@ JS $email->populateTemplate($emailData); $email->setFrom($recipient->EmailFrom); $email->setBody($recipient->EmailBody); - $email->setSubject($recipient->EmailSubject); $email->setTo($recipient->EmailAddress); + $email->setSubject($recipient->EmailSubject); if($recipient->EmailReplyTo) { $email->setReplyTo($recipient->EmailReplyTo); @@ -1001,6 +1001,14 @@ JS } } + // check to see if there is a dynamic subject + if($recipient->SendEmailSubjectField()) { + $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailSubjectField()->Name); + if($submittedFormField && is_string($submittedFormField->Value)) { + $email->setSubject($submittedFormField->Value); + } + } + $this->extend('updateEmail', $email, $recipient, $emailData); if($recipient->SendPlain) { @@ -1100,7 +1108,8 @@ class UserDefinedForm_EmailRecipient extends DataObject { private static $has_one = array( 'Form' => 'UserDefinedForm', 'SendEmailFromField' => 'EditableFormField', - 'SendEmailToField' => 'EditableFormField' + 'SendEmailToField' => 'EditableFormField', + 'SendEmailSubjectField' => 'EditableFormField' ); private static $summary_fields = array(); @@ -1129,48 +1138,37 @@ class UserDefinedForm_EmailRecipient extends DataObject { if($this->Form()) { $dropdowns = array(); - - $validEmailFields = DataObject::get("EditableEmailField", "\"ParentID\" = '" . (int)$this->FormID . "'"); - $multiOptionFields = DataObject::get("EditableMultipleOptionField", "\"ParentID\" = '" . (int)$this->FormID . "'"); - // if they have email fields then we could send from it - if($validEmailFields) { - $fields->insertAfter($dropdowns[] = new DropdownField( - 'SendEmailFromFieldID', - _t('UserDefinedForm.ORSELECTAFIELDTOUSEASFROM', '.. or select a field to use as reply to address'), - $validEmailFields->map('ID', 'Title') - ), 'EmailReplyTo'); - } + $validEmailFields = EditableEmailField::get()->filter('ParentID', (int)$this->FormID); + // for the subject, only one-line entry boxes make sense + $validSubjectFields = EditableTextField::get()->filter('ParentID', (int)$this->FormID)->filterByCallback(function($item, $list) { return (int)$item->getSetting('Rows') === 1; }); + // predefined choices are also candidates + $multiOptionFields = EditableMultipleOptionField::get()->filter('ParentID', (int)$this->FormID); - // if they have multiple options - if($multiOptionFields || $validEmailFields) { + $fields->insertAfter($dropdowns[] = new DropdownField( + 'SendEmailFromFieldID', + _t('UserDefinedForm.ORSELECTAFIELDTOUSEASFROM', '.. or select a field to use as reply to address'), + $validEmailFields->map('ID', 'Title') + ), 'EmailReplyTo'); - if($multiOptionFields && $validEmailFields) { - $multiOptionFields = $multiOptionFields->toArray(); - $multiOptionFields = array_merge( - $multiOptionFields, - $validEmailFields->toArray() - ); + $validEmailFields = new ArrayList($validEmailFields->toArray()); + $validEmailFields->merge($multiOptionFields); + $validSubjectFields->merge($multiOptionFields); - $multiOptionFields = ArrayList::create($multiOptionFields); - } - else if(!$multiOptionFields) { - $multiOptionFields = $validEmailFields; - } - - $multiOptionFields = $multiOptionFields->map('ID', 'Title'); - $fields->insertAfter($dropdowns[] = new DropdownField( - 'SendEmailToFieldID', - _t('UserDefinedForm.ORSELECTAFIELDTOUSEASTO', '.. or select a field to use as the to address'), - $multiOptionFields - ), 'EmailAddress'); - } + $fields->insertAfter($dropdowns[] = new DropdownField( + 'SendEmailToFieldID', + _t('UserDefinedForm.ORSELECTAFIELDTOUSEASTO', '.. or select a field to use as the to address'), + $validEmailFields->map('ID', 'Title') + ), 'EmailAddress'); + $fields->insertAfter($dropdowns[] = new DropdownField( + 'SendEmailSubjectFieldID', + _t('UserDefinedForm.SELECTAFIELDTOSETSUBJECT', '.. or select a field to use as the subject'), + $validSubjectFields->map('ID', 'Title') + ), 'EmailSubject'); - if($dropdowns) { - foreach($dropdowns as $dropdown) { - $dropdown->setHasEmptyDefault(true); - $dropdown->setEmptyString(" "); - } + foreach($dropdowns as $dropdown) { + $dropdown->setHasEmptyDefault(true); + $dropdown->setEmptyString(" "); } }