From e2066a55b3efa1115c7fc26dd3fdf61d8bbe31a2 Mon Sep 17 00:00:00 2001 From: Andrew Aitken-Fincham Date: Tue, 20 Feb 2018 14:32:14 +0000 Subject: [PATCH] missing 'not', add alert-warning for unsaved form --- code/Model/Recipient/EmailRecipient.php | 33 +++++++++++++++++++++---- lang/en.yml | 1 + tests/Model/UserDefinedFormTest.php | 2 +- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/code/Model/Recipient/EmailRecipient.php b/code/Model/Recipient/EmailRecipient.php index e6dc4ba..01ae893 100644 --- a/code/Model/Recipient/EmailRecipient.php +++ b/code/Model/Recipient/EmailRecipient.php @@ -31,11 +31,13 @@ use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DB; use SilverStripe\ORM\FieldType\DBField; use SilverStripe\ORM\ValidationResult; +use SilverStripe\Security\Member; use SilverStripe\UserForms\Model\EditableFormField; use SilverStripe\UserForms\Model\EditableFormField\EditableEmailField; use SilverStripe\UserForms\Model\EditableFormField\EditableMultipleOptionField; use SilverStripe\UserForms\Model\EditableFormField\EditableTextField; use SilverStripe\UserForms\Model\UserDefinedForm; +use SilverStripe\UserForms\UserForm; use SilverStripe\View\Requirements; use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton; use Symbiote\GridFieldExtensions\GridFieldEditableColumns; @@ -126,7 +128,7 @@ class EmailRecipient extends DataObject /** * Get instance of UserForm when editing in getCMSFields * - * @return UserDefinedForm + * @return UserDefinedForm|UserForm */ protected function getFormParent() { @@ -197,6 +199,10 @@ class EmailRecipient extends DataObject // Build fieldlist $fields = FieldList::create(Tabset::create('Root')->addExtraClass('EmailRecipientForm')); + if (!$this->getFormParent()) { + $fields->addFieldToTab('Root.EmailDetails', $this->getUnsavedFormLiteralField()); + } + // Configuration fields $fields->addFieldsToTab('Root.EmailDetails', [ $this->getSubjectCMSFields(), @@ -204,6 +210,7 @@ class EmailRecipient extends DataObject $this->getEmailFromCMSFields(), $this->getEmailReplyToCMSFields(), ]); + $fields->fieldByName('Root.EmailDetails')->setTitle(_t(__CLASS__ . '.EMAILDETAILSTAB', 'Email Details')); // Only show the preview link if the recipient has been saved. @@ -227,7 +234,7 @@ class EmailRecipient extends DataObject ); } else { $preview = sprintf( - '%s', + '

%s

', _t( 'SilverStripe\\UserForms\\Model\\UserDefinedForm.PREVIEW_EMAIL_UNAVAILABLE', 'You can preview this email once you have saved the Recipient.' @@ -285,8 +292,10 @@ class EmailRecipient extends DataObject ); $grid->setDescription(_t( 'SilverStripe\\UserForms\\Model\\UserDefinedForm.RulesDescription', - 'Emails will only be sent to the recipient if the custom rules are met. If no rules are defined, this receipient will receive notifications for every submission.' + 'Emails will only be sent to the recipient if the custom rules are met. If no rules are defined, ' + . 'this recipient will receive notifications for every submission.' )); + $fields->addFieldsToTab('Root.CustomRules', [ DropdownField::create( 'CustomRulesCondition', @@ -305,7 +314,7 @@ class EmailRecipient extends DataObject $grid ]); - $fields->fieldByName('Root.CustomRules')->setTitle(_t(__CLASS__ . '.CUSTOMRULESTAB', 'Custom Rules')); + $fields->fieldByName('Root.CustomRules')->setTitle(_t(__CLASS__ . '.CUSTOMRULESTAB', 'Custom Rules')); $this->extend('updateCMSFields', $fields); return $fields; @@ -688,7 +697,7 @@ class EmailRecipient extends DataObject */ protected function getValidEmailToFields() { - if ($this->getFormParent()) { + if (!$this->getFormParent()) { return null; } @@ -703,4 +712,18 @@ class EmailRecipient extends DataObject return $this->getMultiOptionFields(); } } + + protected function getUnsavedFormLiteralField() + { + return LiteralField::create( + 'UnsavedFormMessage', + sprintf( + '

%s

', + _t( + 'SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAIL_RECIPIENT_UNSAVED_FORM', + 'You will be able to select from valid form fields after saving this record.' + ) + ) + ); + } } diff --git a/lang/en.yml b/lang/en.yml index c4f528e..a91fbad 100644 --- a/lang/en.yml +++ b/lang/en.yml @@ -221,6 +221,7 @@ en: EMAILBODYHTML: Body EMAILFROM: From EMAILRECIPIENTS: 'Email Recipients' + EMAIL_RECIPIENT_UNSAVED_FORM: 'You will be able to select from valid form fields after saving this record.' EMAILSUBJECT: 'Email subject' EMAILTEMPLATE: 'Email template' ENABLELIVEVALIDATION: 'Enable live validation' diff --git a/tests/Model/UserDefinedFormTest.php b/tests/Model/UserDefinedFormTest.php index a3cccff..7ba426a 100644 --- a/tests/Model/UserDefinedFormTest.php +++ b/tests/Model/UserDefinedFormTest.php @@ -130,7 +130,7 @@ class UserDefinedFormTest extends FunctionalTest $fields = $popup->getCMSFields(); $this->assertThat($fields->dataFieldByName('SendEmailToFieldID'), $this->isInstanceOf(DropdownField::class)); - // if the front end has checkboxs or dropdown they can select from that can also be used to send things + // if the front end has checkboxes or dropdown they can select from that can also be used to send things $dropdown = $this->objFromFixture(EditableDropdown::class, 'department-dropdown'); $form->Fields()->add($dropdown);