API Custom rules for email recipients

This commit is contained in:
Damian Mooyman 2015-07-13 15:36:20 +12:00
parent 95d7b5f608
commit d17e8a773f
9 changed files with 749 additions and 252 deletions

View File

@ -2,17 +2,17 @@
language: php
php:
- 5.3
- 5.4
env:
- DB=MYSQL CORE_RELEASE=3.1
- DB=MYSQL CORE_RELEASE=3
- DB=MYSQL CORE_RELEASE=3.1
- DB=PGSQL CORE_RELEASE=3.1
matrix:
include:
- php: 5.4
env: DB=MYSQL CORE_RELEASE=master
- php: 5.3
env: DB=MYSQL CORE_RELEASE=3.1
before_script:
- git clone git://github.com/silverstripe-labs/silverstripe-travis-support.git ~/travis-support

View File

@ -83,6 +83,7 @@ class UserDefinedForm extends Page {
// define tabs
$fields->findOrMakeTab('Root.FormContent', _t('UserDefinedForm.FORM', 'Form'));
$fields->findOrMakeTab('Root.FormOptions', _t('UserDefinedForm.CONFIGURATION', 'Configuration'));
$fields->findOrMakeTab('Root.Recipients', _t('UserDefinedForm.RECIPIENTS', 'Recipients'));
$fields->findOrMakeTab('Root.Submissions', _t('UserDefinedForm.SUBMISSIONS', 'Submissions'));
// field editor
@ -99,21 +100,19 @@ class UserDefinedForm extends Page {
$editor->setRows(3);
$label->addExtraClass('left');
// Set the summary fields of UserDefinedForm_EmailRecipient dynamically via config system
Config::inst()->update(
'UserDefinedForm_EmailRecipient',
'summary_fields',
array(
'EmailAddress' => _t('UserDefinedForm.EMAILADDRESS', 'Email'),
'EmailSubject' => _t('UserDefinedForm.EMAILSUBJECT', 'Subject'),
'EmailFrom' => _t('UserDefinedForm.EMAILFROM', 'From'),
)
);
// Define config for email recipients
$emailRecipientsConfig = GridFieldConfig_RecordEditor::create(10);
$emailRecipientsConfig->getComponentByType('GridFieldAddNewButton')
->setButtonName(
_t('UserDefinedForm.ADDEMAILRECIPIENT', 'Add Email Recipient')
);
// who do we email on submission
$emailRecipients = new GridField('EmailRecipients', _t('UserDefinedForm.EMAILRECIPIENTS', 'Email Recipients'), $self->EmailRecipients(), GridFieldConfig_RecordEditor::create(10));
$emailRecipients->getConfig()->getComponentByType('GridFieldAddNewButton')->setButtonName(
_t('UserDefinedForm.ADDEMAILRECIPIENT', 'Add Email Recipient')
$emailRecipients = new GridField(
'EmailRecipients',
_t('UserDefinedForm.EMAILRECIPIENTS', 'Email Recipients'),
$self->EmailRecipients(),
$emailRecipientsConfig
);
$emailRecipients
->getConfig()
@ -121,7 +120,7 @@ class UserDefinedForm extends Page {
->setItemRequestClass('UserDefinedForm_EmailRecipient_ItemRequest');
$fields->addFieldsToTab('Root.FormOptions', $onCompleteFieldSet);
$fields->addFieldToTab('Root.FormOptions', $emailRecipients);
$fields->addFieldToTab('Root.Recipients', $emailRecipients);
$fields->addFieldsToTab('Root.FormOptions', $self->getFormOptions());
@ -322,7 +321,12 @@ SQL;
* @return ArrayList
*/
public function FilteredEmailRecipients($data = null, $form = null) {
$recipients = new ArrayList($this->getComponents('EmailRecipients')->toArray());
$recipients = new ArrayList($this->EmailRecipients()->toArray());
// Filter by rules
$recipients = $recipients->filterByCallback(function($recipient) use ($data, $form) {
return $recipient->canSend($data, $form);
});
$this->extend('updateFilteredEmailRecipients', $recipients, $data, $form);
@ -1169,13 +1173,13 @@ JS
}
/**
* A Form can have multiply members / emails to email the submission
* A Form can have multiply members / emails to email the submission
* to and custom subjects
*
*
* @package userforms
*/
class UserDefinedForm_EmailRecipient extends DataObject {
private static $db = array(
'EmailAddress' => 'Varchar(200)',
'EmailSubject' => 'Varchar(200)',
@ -1185,90 +1189,236 @@ class UserDefinedForm_EmailRecipient extends DataObject {
'EmailBodyHtml' => 'HTMLText',
'EmailTemplate' => 'Varchar',
'SendPlain' => 'Boolean',
'HideFormData' => 'Boolean'
'HideFormData' => 'Boolean',
'CustomRulesCondition' => 'Enum("And,Or")'
);
private static $has_one = array(
'Form' => 'UserDefinedForm',
'SendEmailFromField' => 'EditableFormField',
'SendEmailToField' => 'EditableFormField',
'SendEmailSubjectField' => 'EditableFormField'
);
private static $summary_fields = array();
private static $has_many = array(
'CustomRules' => 'UserDefinedForm_EmailRecipientCondition'
);
private static $summary_fields = array(
'EmailAddress',
'EmailSubject',
'EmailFrom'
);
public function summaryFields() {
$fields = parent::summaryFields();
if(isset($fields['EmailAddress'])) {
$fields['EmailAddress'] = _t('UserDefinedForm.EMAILADDRESS', 'Email');
}
if(isset($fields['EmailSubject'])) {
$fields['EmailSubject'] = _t('UserDefinedForm.EMAILSUBJECT', 'Subject');
}
if(isset($fields['EmailFrom'])) {
$fields['EmailFrom'] = _t('UserDefinedForm.EMAILFROM', 'From');
}
return $fields;
}
/**
* Get instance of UserDefinedForm when editing in getCMSFields
*
* @return UserDefinedFrom
*/
protected function getFormParent() {
$formID = $this->FormID
? $this->FormID
: Session::get('CMSMain.currentPage');
return UserDefinedForm::get()->byID($formID);
}
public function getTitle() {
if($this->EmailAddress) {
return $this->EmailAddress;
}
if($this->EmailSubject) {
return $this->EmailSubject;
}
return parent::getTitle();
}
/**
* Generate a gridfield config for editing filter rules
*
* @return GridFieldConfig
*/
protected function getRulesConfig() {
$formFields = $this->getFormParent()->Fields();
$config = GridFieldConfig::create()
->addComponents(
new GridFieldButtonRow('before'),
new GridFieldToolbarHeader(),
new GridFieldAddNewInlineButton(),
new GridState_Component(),
new GridFieldDeleteAction(),
$columns = new GridFieldEditableColumns()
);
$columns->setDisplayFields(array(
'ConditionFieldID' => function($record, $column, $grid) use ($formFields) {
return DropdownField::create($column, false, $formFields->map('ID', 'Title'));
},
'ConditionOption' => function($record, $column, $grid) {
$options = UserDefinedForm_EmailRecipientCondition::config()->condition_options;
return DropdownField::create($column, false, $options);
},
'ConditionValue' => function($record, $column, $grid) {
return TextField::create($column);
}
));
return $config;
}
/**
* @return FieldList
*/
public function getCMSFields() {
// Determine optional field values
$form = $this->getFormParent();
// predefined choices are also candidates
$multiOptionFields = EditableMultipleOptionField::get()->filter('ParentID', $form->ID);
// if they have email fields then we could send from it
$validEmailFromFields = EditableEmailField::get()->filter('ParentID', $form->ID);
// For the subject, only one-line entry boxes make sense
$validSubjectFields = EditableTextField::get()
->filter('ParentID', $form->ID)
->filterByCallback(function($item, $list) {
return (int)$item->getSetting('Rows') === 1;
});
$validSubjectFields->merge($multiOptionFields);
// To address can only be email fields or multi option fields
$validEmailToFields = new ArrayList($validEmailFromFields->toArray());
$validEmailToFields->merge($multiOptionFields);
// Build fieldlist
$fields = FieldList::create(Tabset::create('Root')->addExtraClass('EmailRecipientForm'));
// Configuration fields
$fields->addFieldsToTab('Root.EmailDetails', array(
// Subject
FieldGroup::create(
TextField::create('EmailSubject', _t('UserDefinedForm.TYPESUBJECT', 'Type subject'))
->setAttribute('style', 'min-width: 400px;'),
DropdownField::create(
'SendEmailSubjectFieldID',
_t('UserDefinedForm.SELECTAFIELDTOSETSUBJECT', '.. or select a field to use as the subject'),
$validSubjectFields->map('ID', 'Title')
)->setEmptyString('')
)
->setTitle(_t('UserDefinedForm.EMAILSUBJECT', 'Email subject')),
// To
FieldGroup::create(
TextField::create('EmailAddress', _t('UserDefinedForm.TYPETO', 'Type to address'))
->setAttribute('style', 'min-width: 400px;'),
DropdownField::create(
'SendEmailToFieldID',
_t('UserDefinedForm.ORSELECTAFIELDTOUSEASTO', '.. or select a field to use as the to address'),
$validEmailToFields->map('ID', 'Title')
)->setEmptyString(' ')
)
->setTitle(_t('UserDefinedForm.SENDEMAILTO','Send email to'))
->setDescription(_t(
'UserDefinedForm.SENDEMAILTO_DESCRIPTION',
'You may enter multiple email addresses as a comma separated list.'
)),
// From
TextField::create('EmailFrom', _t('UserDefinedForm.FROMADDRESS','Send email from'))
->setDescription(_t(
'UserDefinedForm.EmailFromContent',
"The from address allows you to set who the email comes from. On most servers this ".
"will need to be set to an email address on the same domain name as your site. ".
"For example on yoursite.com the from address may need to be something@yoursite.com. ".
"You can however, set any email address you wish as the reply to address."
)),
// Reply-To
FieldGroup::create(
TextField::create('EmailReplyTo', _t('UserDefinedForm.TYPEREPLY', 'Type reply address'))
->setAttribute('style', 'min-width: 400px;'),
DropdownField::create(
'SendEmailFromFieldID',
_t('UserDefinedForm.ORSELECTAFIELDTOUSEASFROM', '.. or select a field to use as reply to address'),
$validEmailFromFields->map('ID', 'Title')
)->setEmptyString(' ')
)
->setTitle(_t('UserDefinedForm.REPLYADDRESS', 'Email for reply to'))
->setDescription(_t(
'UserDefinedForm.REPLYADDRESS_DESCRIPTION',
'The email address which the recipient is able to \'reply\' to.'
))
));
// Only show the preview link if the recipient has been saved.
if (!empty($this->EmailTemplate)) {
$translatableKey = 'UserDefinedForm.EMAILPREVIEWAVAILABLE';
$previewHTML = '<p><a href="admin/pages/edit/EditForm/field/EmailRecipients/item/' . $this->ID . '/preview" target="_blank" class="ss-ui-button">Preview email</a></p>' .
'<em>Note: Unsaved changes will not appear in the preview.</em>';
$preview = sprintf(
'<p><a href="%s" target="_blank" class="ss-ui-button">%s</a></p><em>%s</em>',
"admin/pages/edit/EditForm/field/EmailRecipients/item/{$this->ID}/preview",
_t('UserDefinedForm.PREVIEW_EMAIL', 'Preview email'),
_t('UserDefinedForm.PREVIEW_EMAIL_DESCRIPTION', 'Note: Unsaved changes will not appear in the preview.')
);
} else {
$translatableKey = 'UserDefinedForm.EMAILPREVIEWUNAVAILABLE';
$previewHTML = '<em>You can preview this email once you have saved the Recipient.</em>';
$preview = sprintf(
'<em>%s</em>',
_t(
'UserDefinedForm.PREVIEW_EMAIL_UNAVAILABLE',
'You can preview this email once you have saved the Recipient.'
)
);
}
$fields = new FieldList(
new TextField('EmailSubject', _t('UserDefinedForm.EMAILSUBJECT', 'Email subject')),
new LiteralField('EmailFromContent', '<p>'._t(
'UserDefinedForm.EmailFromContent',
"The from address allows you to set who the email comes from. On most servers this ".
"will need to be set to an email address on the same domain name as your site. ".
"For example on yoursite.com the from address may need to be something@yoursite.com. ".
"You can however, set any email address you wish as the reply to address."
) . "</p>"),
new TextField('EmailFrom', _t('UserDefinedForm.FROMADDRESS','Send email from')),
new TextField('EmailReplyTo', _t('UserDefinedForm.REPLYADDRESS', 'Email for reply to')),
new TextField('EmailAddress', _t('UserDefinedForm.SENDEMAILTO','Send email to')),
// Email templates
$fields->addFieldsToTab('Root.EmailContent', array(
new CheckboxField('HideFormData', _t('UserDefinedForm.HIDEFORMDATA', 'Hide form data from email?')),
new CheckboxField('SendPlain', _t('UserDefinedForm.SENDPLAIN', 'Send email as plain text? (HTML will be stripped)')),
new DropdownField('EmailTemplate', _t('UserDefinedForm.EMAILTEMPLATE', 'Email template'), $this->getEmailTemplateDropdownValues()),
new HTMLEditorField('EmailBodyHtml', _t('UserDefinedForm.EMAILBODYHTML','Body')),
new TextareaField('EmailBody', _t('UserDefinedForm.EMAILBODY','Body')),
new LiteralField('EmailPreview', '<div id="EmailPreview">' . _t($translatableKey, $previewHTML) . '</div>')
new LiteralField('EmailPreview', '<div id="EmailPreview">' . $preview . '</div>')
));
// Custom rules for sending this field
$grid = new GridField(
"CustomRules",
_t('EditableFormField.CUSTOMRULES', 'Custom Rules'),
$this->CustomRules(),
$this->getRulesConfig()
);
$formID = ($this->FormID != 0) ? $this->FormID : Session::get('CMSMain.currentPage');
$dropdowns = array();
// if they have email fields then we could send from it
$validEmailFields = EditableEmailField::get()->filter('ParentID', (int)$formID);
// for the subject, only one-line entry boxes make sense
$validSubjectFields = EditableTextField::get()->filter('ParentID', (int)$formID)->filterByCallback(function($item, $list) { return (int)$item->getSetting('Rows') === 1; });
// predefined choices are also candidates
$multiOptionFields = EditableMultipleOptionField::get()->filter('ParentID', (int)$formID);
$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 = new ArrayList($validEmailFields->toArray());
$validEmailFields->merge($multiOptionFields);
$validSubjectFields->merge($multiOptionFields);
$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');
foreach($dropdowns as $dropdown) {
$dropdown->setHasEmptyDefault(true);
$dropdown->setEmptyString(" ");
}
$grid->setDescription(_t(
'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.'
));
$fields->addFieldsToTab('Root.CustomRules', array(
new DropdownField(
'CustomRulesCondition',
_t('UserDefinedForm.SENDIF', 'Send condition'),
array(
'Or' => 'Any conditions are true',
'And' => 'All conditions are true'
)
),
$grid
));
$this->extend('updateCMSFields', $fields);
return $fields;
}
@ -1289,7 +1439,7 @@ class UserDefinedForm_EmailRecipient extends DataObject {
public function canView($member = null) {
return $this->Form()->canView();
}
/**
* @param Member
*
@ -1298,7 +1448,7 @@ class UserDefinedForm_EmailRecipient extends DataObject {
public function canEdit($member = null) {
return $this->Form()->canEdit();
}
/**
* @param Member
*
@ -1308,6 +1458,36 @@ class UserDefinedForm_EmailRecipient extends DataObject {
return $this->Form()->canDelete();
}
/*
* Determine if this recipient may receive notifications for this submission
*
* @param array $data
* @param Form $form
* @return bool
*/
public function canSend($data, $form) {
// Skip if no rules configured
$customRules = $this->CustomRules();
if(!$customRules->count()) {
return true;
}
// Check all rules
$isAnd = $this->CustomRulesCondition === 'And';
foreach($customRules as $customRule) {
$matches = $customRule->matches($data, $form);
if($isAnd && !$matches) {
return false;
}
if(!$isAnd && $matches) {
return true;
}
}
// Once all rules are checked
return $isAnd;
}
/**
* Make sure the email template saved against the recipient exists on the file system.
*
@ -1382,6 +1562,58 @@ class UserDefinedForm_EmailRecipient_ItemRequest extends GridFieldDetailForm_Ite
}
}
/**
* Declares a condition that determines whether an email can be sent to a given recipient
*/
class UserDefinedForm_EmailRecipientCondition extends DataObject {
/**
* List of options
*
* @config
* @var array
*/
private static $condition_options = array(
"IsBlank" => "Is blank",
"IsNotBlank" => "Is not blank",
"Equals" => "Equals",
"NotEquals" => "Doesn't equal"
);
private static $db = array(
'ConditionOption' => 'Enum("IsBlank,IsNotBlank,Equals,NotEquals")',
'ConditionValue' => 'Varchar'
);
private static $has_one = array(
'Parent' => 'UserDefinedForm_EmailRecipient',
'ConditionField' => 'EditableFormField'
);
/**
* Determine if this rule matches the given condition
*
* @param array $data
* @param Form $form
* @return bool
*/
public function matches($data, $form) {
$fieldName = $this->ConditionField()->Name;
$fieldValue = isset($data[$fieldName]) ? $data[$fieldName] : null;
switch($this->ConditionOption) {
case 'IsBlank':
return empty($fieldValue);
case 'IsNotBlank':
return !empty($fieldValue);
default:
$matches = is_array($fieldValue)
? in_array($this->ConditionValue, $fieldValue)
: $this->ConditionValue === (string)$fieldValue;
return ($this->ConditionOption === 'Equals') === (bool)$matches;
}
}
}
/**
* Email that gets sent to the people listed in the Email Recipients when a
* submission is made.
@ -1407,5 +1639,5 @@ class UserDefinedForm_SubmittedFormEmail extends Email {
*/
public function setReplyTo($email) {
$this->customHeaders['Reply-To'] = $email;
}
}
}

View File

@ -10,7 +10,8 @@
}],
"require": {
"silverstripe/framework": ">=3.1.0",
"silverstripe/cms": ">=3.1.0"
"silverstripe/cms": ">=3.1.0",
"silverstripe-australia/gridfieldextensions": "1.1.0"
},
"suggest": {
"colymba/gridfield-bulk-editing-tools": "Allows for bulk management of form submissions"

View File

@ -51,6 +51,19 @@ li.class-UserDefinedForm > a .jstree-pageicon { background-position: 0 -64px; }
}
*/
/* Email Recipient Form
---------------------------------------- */
.EmailRecipientForm .fieldgroup .fieldgroup-field {
padding-top: 0;
}
.EmailRecipientForm .fieldgroup .fieldgroup-field.last {
padding-bottom: 0;
}
.EmailRecipientForm .ss-ui-button {
margin-bottom: 4px;
}
/* Field Listing

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

View File

@ -4,7 +4,8 @@ In this section:
* Learn how to create and edit forms
* Learn how to add fields to a form
* Learn how to view submissions and reply to them automatically
* Learn how to view submissions
* Learn how to set up automated emails upon form completion
## Before we begin:
@ -29,8 +30,6 @@ You will notice that a new page has been created, with the name of "New UserDefi
Simply click on the new page in the content page to bring it up in the editing pane.
<div class="note" markdown="1">
### Notes:
Don't worry if you create your page in the "wrong" place. Pages can be moved and re-ordered
easily, and we will cover that under "Managing Your Site."
</div>
@ -175,9 +174,19 @@ to determine the size and the number of rows in a text field.
* Use [HTML Block](#html-block), with the appropriate level [Heading](#heading).
## Sending emails
## Viewing form submissions
The UserForms module allows you to email form submissions to multiple people.
To view form submissions navigate to the 'Submissions' tab. You can click any of the listed submissions to view the content of each submission.
![Viewing submissions](_images/viewing-submissions.png)
## Setting up automated emails
It is possible to set up automated emails upon each form submission, to do this navigate to the "Recipients" tab and click "Add Email Recipient".
![Add email recipient](_images/add-email-recipient.png)
You will be prompted with a form where you can fill in the details of the email.
### Using form fields in submission emails
@ -188,3 +197,67 @@ Each form field has a unique merge field located under the field's options.
Simply insert the merge field into the email content, and the field's value will be displayed, when the email is sent.
![Merge field in content](_images/mergefieldcontent.png)
### Email details
#### Email Subject
The subject of the email, you can either type a custom subject here or select a field from the form to use as the email subject.
#### Send email to
This is the recipient's address where the email will be sent.
#### Send email from
This shows where the email was sent from, and will most likely need to be an email address on the same domain as your site. For example If your website is yoursite.com, the email address for this field should be something@yoursite.com.
#### Email for reply to
This will be the address which the email recipient will be able to 'reply' to.
#### Email content
In this field you can add a custom message to add to the email
#### Hide form data from email?
You can check this if you do not wish for the email recipient to see the form submission's data in the email.
#### Send email as plain text?
You can check this if you want to remove all of the HTML from the email, this means the email
will have no custom styling and the recipient will only see the plain text.
If `Send email as plain text?` is unselected, several additional options for HTML editing are displayed.
If sending as HTML, there is the option to preview the HTML that is sent in the editor. Additionally, a HTML
template can be selected to provide a standard formatted email to contain the editable HTML content.
The list of available templates can be controlled by specifying the folder for these template files in yaml config.
:::yaml
UserDefinedForm:
email_template_directory: mysite/templates/useremails/
### Custom Rules
In this section you can determine whether to send the email to the recipient based on the data in the form submission.
#### Send conditions
This decides whether to send the email based on two options
1. *All* conditions are true (Every single custom rule must be met in order to send the email)
2. *Any* conditions are true (At least one of the custom rules must be met in order to send the email)
#### Adding a custom rule
* Click 'Add' to add a custom sending rule.
* Select the field which you want the custom rule to apply to
* Select the condition the field must follow
* enter for the condition (the 'is blank' and 'is not blank' conditions do not require any text)

View File

@ -13,8 +13,8 @@ class UserDefinedFormTest extends FunctionalTest {
$this->markTestSkipped(
'UserDefinedForm::rollback() has not been implemented completely'
);
// @todo
// @todo
$this->logInWithPermission('ADMIN');
$form = $this->objFromFixture('UserDefinedForm', 'basic-form-page');
@ -22,7 +22,7 @@ class UserDefinedFormTest extends FunctionalTest {
$form->write();
$form->doPublish();
$origVersion = $form->Version;
$form->SubmitButtonText = 'Updated Button Text';
$form->write();
$form->doPublish();
@ -32,15 +32,15 @@ class UserDefinedFormTest extends FunctionalTest {
$this->assertEquals($updated->SubmitButtonText, 'Updated Button Text');
$form->doRollbackTo($origVersion);
$orignal = Versioned::get_one_by_stage("UserDefinedForm", "Stage", "\"UserDefinedForm\".\"ID\" = $form->ID");
$this->assertEquals($orignal->SubmitButtonText, 'Button Text');
}
function testGetCMSFields() {
$this->logInWithPermission('ADMIN');
$form = $this->objFromFixture('UserDefinedForm', 'basic-form-page');
$fields = $form->getCMSFields();
$this->assertTrue($fields->dataFieldByName('Fields') !== null);
@ -51,37 +51,37 @@ class UserDefinedFormTest extends FunctionalTest {
function testEmailRecipientPopup() {
$this->logInWithPermission('ADMIN');
$form = $this->objFromFixture('UserDefinedForm', 'basic-form-page');
$popup = new UserDefinedForm_EmailRecipient();
$popup->FormID = $form->ID;
$fields = $popup->getCMSFields();
$this->assertTrue($fields->dataFieldByName('EmailSubject') !== null);
$this->assertTrue($fields->dataFieldByName('EmailFrom') !== null);
$this->assertTrue($fields->dataFieldByName('EmailAddress') !== null);
$this->assertTrue($fields->dataFieldByName('HideFormData') !== null);
$this->assertTrue($fields->dataFieldByName('SendPlain') !== null);
$this->assertTrue($fields->dataFieldByName('EmailBody') !== null);
// add an email field, it should now add a or from X address picker
$email = $this->objFromFixture('EditableEmailField','email-field');
$form->Fields()->add($email);
$popup->FormID = $form->ID;
$popup->write();
$fields = $popup->getCMSFields();
$this->assertThat($fields->fieldByName('SendEmailToFieldID'), $this->isInstanceOf('DropdownField'));
$this->assertThat($fields->dataFieldByName('SendEmailToFieldID'), $this->isInstanceOf('DropdownField'));
// if the front end has checkboxs or dropdown they can select from that can also be used to send things
$dropdown = $this->objFromFixture('EditableDropdown', 'department-dropdown');
$form->Fields()->add($dropdown);
$fields = $popup->getCMSFields();
$this->assertTrue($fields->dataFieldByName('SendEmailToFieldID') !== null);
$popup->delete();
}
@ -138,95 +138,95 @@ class UserDefinedFormTest extends FunctionalTest {
$this->assertTrue($recipient->canEdit());
$this->assertTrue($recipient->canDelete());
}
$member = Member::currentUser();
$member->logOut();
$this->logInWithPermission('SITETREE_VIEW_ALL');
foreach($form->EmailRecipients() as $recipient) {
$this->assertFalse($recipient->canEdit());
$this->assertFalse($recipient->canDelete());
}
}
function testPublishing() {
$this->logInWithPermission('ADMIN');
$form = $this->objFromFixture('UserDefinedForm', 'basic-form-page');
$form->write();
$form->doPublish();
$live = Versioned::get_one_by_stage("UserDefinedForm", "Live", "\"UserDefinedForm_Live\".\"ID\" = $form->ID");
$this->assertNotNull($live);
$this->assertEquals($live->Fields()->Count(), 1);
$dropdown = $this->objFromFixture('EditableDropdown', 'basic-dropdown');
$form->Fields()->add($dropdown);
$stage = Versioned::get_one_by_stage("UserDefinedForm", "Stage", "\"UserDefinedForm\".\"ID\" = $form->ID");
$this->assertEquals($stage->Fields()->Count(), 2);
// should not have published the dropdown
$liveDropdown = Versioned::get_one_by_stage("EditableFormField", "Live", "\"EditableFormField_Live\".\"ID\" = $dropdown->ID");
$this->assertNull($liveDropdown);
// when publishing it should have added it
$form->doPublish();
$live = Versioned::get_one_by_stage("UserDefinedForm", "Live", "\"UserDefinedForm_Live\".\"ID\" = $form->ID");
$this->assertEquals($live->Fields()->Count(), 2);
// edit the title
// edit the title
$text = $form->Fields()->First();
$text->Title = 'Edited title';
$text->write();
$liveText = Versioned::get_one_by_stage("EditableFormField", "Live", "\"EditableFormField_Live\".\"ID\" = $text->ID");
$this->assertFalse($liveText->Title == $text->Title);
$form->doPublish();
$liveText = Versioned::get_one_by_stage("EditableFormField", "Live", "\"EditableFormField_Live\".\"ID\" = $text->ID");
$this->assertTrue($liveText->Title == $text->Title);
}
function testUnpublishing() {
$this->logInWithPermission('ADMIN');
$form = $this->objFromFixture('UserDefinedForm', 'basic-form-page');
$form->write();
$form->doPublish();
// assert that it exists and has a field
$live = Versioned::get_one_by_stage("UserDefinedForm", "Live", "\"UserDefinedForm_Live\".\"ID\" = $form->ID");
$this->assertTrue(isset($live));
$this->assertEquals(DB::query("SELECT COUNT(*) FROM \"EditableFormField_Live\"")->value(), 1);
// unpublish
$form->doUnpublish();
$this->assertNull(Versioned::get_one_by_stage("UserDefinedForm", "Live", "\"UserDefinedForm_Live\".\"ID\" = $form->ID"));
$this->assertEquals(DB::query("SELECT COUNT(*) FROM \"EditableFormField_Live\"")->value(), 0);
$this->assertEquals(DB::query("SELECT COUNT(*) FROM \"EditableFormField_Live\"")->value(), 0);
}
function testDoRevertToLive() {
$this->logInWithPermission('ADMIN');
$form = $this->objFromFixture('UserDefinedForm', 'basic-form-page');
$field = $form->Fields()->First();
$field->Title = 'Title';
$field->write();
$form->doPublish();
$field->Title = 'Edited title';
$field->write();
// check that the published version is not updated
$live = Versioned::get_one_by_stage("EditableFormField", "Live", "\"EditableFormField_Live\".\"ID\" = $field->ID");
$this->assertEquals('Title', $live->Title);
@ -234,21 +234,21 @@ class UserDefinedFormTest extends FunctionalTest {
// revert back to the live data
$form->doRevertToLive();
$form->flushCache();
$check = Versioned::get_one_by_stage("EditableFormField", "Stage", "\"EditableFormField\".\"ID\" = $field->ID");
$this->assertEquals('Title', $check->Title);
}
function testDuplicatingForm() {
$this->logInWithPermission('ADMIN');
$form = $this->objFromFixture('UserDefinedForm', 'basic-form-page');
$duplicate = $form->duplicate();
$this->assertEquals($form->Fields()->Count(), $duplicate->Fields()->Count());
$this->assertEquals($form->EmailRecipients()->Count(), $form->EmailRecipients()->Count());
// can't compare object since the dates/ids change
$this->assertEquals($form->Fields()->First()->Title, $duplicate->Fields()->First()->Title);
}
@ -256,7 +256,7 @@ class UserDefinedFormTest extends FunctionalTest {
function testFormOptions() {
$this->logInWithPermission('ADMIN');
$form = $this->objFromFixture('UserDefinedForm', 'basic-form-page');
$fields = $form->getFormOptions();
$submit = $fields->fieldByName('SubmitButtonText');
$reset = $fields->fieldByName('ShowClearButton');
@ -264,4 +264,97 @@ class UserDefinedFormTest extends FunctionalTest {
$this->assertEquals($submit->Title(), 'Text on submit button:');
$this->assertEquals($reset->Title(), 'Show Clear Form Button');
}
public function testEmailRecipientFilters() {
$form = $this->objFromFixture('UserDefinedForm', 'filtered-form-page');
// Check unfiltered recipients
$result0 = $form
->EmailRecipients()
->sort('EmailAddress')
->column('EmailAddress');
$this->assertEquals(
array(
'filtered1@example.com',
'filtered2@example.com',
'unfiltered@example.com'
),
$result0
);
// check filters based on given data
$result1 = $form->FilteredEmailRecipients(
array(
'your-name' => 'Value',
'address' => '',
'street' => 'Anything',
'city' => 'Matches Not Equals',
'colours' => array('Red') // matches 2
), null
)
->sort('EmailAddress')
->column('EmailAddress');
$this->assertEquals(
array(
'filtered2@example.com',
'unfiltered@example.com'
),
$result1
);
// Check all positive matches
$result2 = $form->FilteredEmailRecipients(
array(
'your-name' => '',
'address' => 'Anything',
'street' => 'Matches Equals',
'city' => 'Anything',
'colours' => array('Red', 'Blue') // matches 2
), null
)
->sort('EmailAddress')
->column('EmailAddress');
$this->assertEquals(
array(
'filtered1@example.com',
'filtered2@example.com',
'unfiltered@example.com'
),
$result2
);
$result3 = $form->FilteredEmailRecipients(
array(
'your-name' => 'Should be blank but is not',
'address' => 'Anything',
'street' => 'Matches Equals',
'city' => 'Anything',
'colours' => array('Blue')
), null
)->column('EmailAddress');
$this->assertEquals(
array(
'unfiltered@example.com'
),
$result3
);
$result4 = $form->FilteredEmailRecipients(
array(
'your-name' => '',
'address' => 'Anything',
'street' => 'Wrong value for this field',
'city' => '',
'colours' => array('Blue', 'Green')
), null
)->column('EmailAddress');
$this->assertEquals(
array(
'unfiltered@example.com'
),
$result4
);
}
}

View File

@ -1,127 +1,212 @@
EditableOption:
option-1:
Name: Option1
Title: Option 1
option-2:
Name: Option2
Title: Option 2
option-1:
Name: Option1
Title: Option 1
option-2:
Name: Option2
Title: Option 2
department-1:
Name: dept1
Title: sales@example.com
department-1:
Name: dept1
Title: sales@example.com
department-2:
Name: dept2
Title: accounts@example.com
department-2:
Name: dept2
Title: accounts@example.com
option-3:
Name: Option3
Title: Option 3
option-3:
Name: Option3
Title: Option 3
option-4:
Name: Option4
Title: Option 4
option-4:
Name: Option4
Title: Option 4
option-5:
Name: Option5
Title: Option 5
option-5:
Name: Option5
Title: Option 5
option-6:
Name: Option6
Title: Option 6
UserDefinedForm_EmailRecipient:
recipient-1:
EmailAddress: test@example.com
EmailSubject: Email Subject
EmailFrom: no-reply@example.com
no-html:
EmailAddress: nohtml@example.com
EmailSubject: Email Subject
EmailFrom: no-reply@example.com
SendPlain: true
no-data:
EmailAddress: nodata@example.com
EmailSubject: Email Subject
EmailFrom: no-reply@example.com
HideFormData: true
option-6:
Name: Option6
Title: Option 6
option-7:
Name: Option7
Title: Red
option-8:
Name: Option8
Title: Blue
option-9:
Name: Option9
Title: Green
EditableTextField:
basic-text:
Name: basic-text-name
Title: Basic Text Field
basic-text:
Name: basic-text-name
Title: Basic Text Field
basic-text-2:
Name: basic-text-name
Title: Basic Text Field
required-text:
Name: required-text-field
Title: Required Text Field
CustomErrorMessage: Custom Error Message
Required: true
basic-text-2:
Name: basic-text-name
Title: Basic Text Field
your-name-field:
Name: your-name
Title: Name
address-field:
Name: address
Title: Address
street-field:
Name: street
Title: Street
city-field:
Name: city
Title: City
required-text:
Name: required-text-field
Title: Required Text Field
CustomErrorMessage: Custom Error Message
Required: true
EditableDropdown:
basic-dropdown:
Name: basic-dropdown
Title: Basic Dropdown Field
Options: =>EditableOption.option-1, =>EditableOption.option-2
department-dropdown:
Name: department
Title: Department
Options: =>EditableOption.department-1, =>EditableOption.department-2
basic-dropdown:
Name: basic-dropdown
Title: Basic Dropdown Field
Options: =>EditableOption.option-1, =>EditableOption.option-2
department-dropdown:
Name: department
Title: Department
Options: =>EditableOption.department-1, =>EditableOption.department-2
EditableCheckbox:
checkbox-1:
Name: checkbox-1
Title: Checkbox 1
checkbox-2:
Name: checkbox-1
Title: Checkbox 1
checkbox-1:
Name: checkbox-1
Title: Checkbox 1
checkbox-2:
Name: checkbox-1
Title: Checkbox 1
EditableCheckboxGroupField:
checkbox-group:
Name: check-box-group
Title: Check box group
Options: =>EditableOption.option-3, =>EditableOption.option-4
checkbox-group:
Name: check-box-group
Title: Check box group
Options: =>EditableOption.option-3, =>EditableOption.option-4
colour-checkbox-group:
Name: colours
Title: 'Select Colours'
Options: =>EditableOption.option-7, =>EditableOption.option-8, =>EditableOption.option-9
EditableEmailField:
email-field:
Name: email-field
Title: Email
email-field:
Name: email-field
Title: Email
EditableRadioField:
radio-field:
Name: radio-option
Title: Radio Option
Options: =>EditableOption.option-5, =>EditableOption.option-6
radio-field:
Name: radio-option
Title: Radio Option
Options: =>EditableOption.option-5, =>EditableOption.option-6
UserDefinedForm_EmailRecipientCondition:
# filtered recipient 1
blank-rule:
ConditionOption: IsBlank
ConditionField: =>EditableTextField.your-name-field
not-blank-rule:
ConditionOption: IsNotBlank
ConditionField: =>EditableTextField.address-field
equals-rule:
ConditionOption: Equals
ConditionField: =>EditableTextField.street-field
ConditionValue: 'Matches Equals'
not-equals-rule:
ConditionOption: NotEquals
ConditionField: =>EditableTextField.city-field
ConditionValue: 'Matches Not Equals'
# filtered recipient 2
group-equals-rule:
ConditionOption: Equals
ConditionField: =>EditableCheckboxGroupField.colour-checkbox-group
ConditionValue: Red
group-not-equals-rule:
ConditionOption: NotEquals
ConditionField: =>EditableCheckboxGroupField.colour-checkbox-group
ConditionValue: Blue
UserDefinedForm_EmailRecipient:
recipient-1:
EmailAddress: test@example.com
EmailSubject: Email Subject
EmailFrom: no-reply@example.com
no-html:
EmailAddress: nohtml@example.com
EmailSubject: Email Subject
EmailFrom: no-reply@example.com
SendPlain: true
no-data:
EmailAddress: nodata@example.com
EmailSubject: Email Subject
EmailFrom: no-reply@example.com
HideFormData: true
unfiltered-recipient-1:
EmailAddress: unfiltered@example.com
EmailSubject: Email Subject
EmailFrom: no-reply@example.com
filtered-recipient-1:
EmailAddress: filtered1@example.com
EmailSubject: Email Subject
EmailFrom: no-reply@example.com
CustomRules: =>UserDefinedForm_EmailRecipientCondition.blank-rule, =>UserDefinedForm_EmailRecipientCondition.not-blank-rule, =>UserDefinedForm_EmailRecipientCondition.equals-rule, =>UserDefinedForm_EmailRecipientCondition.not-equals-rule
CustomRulesCondition: 'And'
filtered-recipient-2:
EmailAddress: filtered2@example.com
EmailSubject: Email Subject
EmailFrom: no-reply@example.com
CustomRules: =>UserDefinedForm_EmailRecipientCondition.group-equals-rule, =>UserDefinedForm_EmailRecipientCondition.group-not-equals-rule
CustomRulesCondition: 'Or'
UserDefinedForm:
basic-form-page:
Title: User Defined Form
Fields: =>EditableTextField.basic-text
EmailRecipients: =>UserDefinedForm_EmailRecipient.recipient-1, =>UserDefinedForm_EmailRecipient.no-html, =>UserDefinedForm_EmailRecipient.no-data
form-with-reset-and-custom-action:
Title: Form with Reset Action
SubmitButtonText: Custom Button
ShowClearButton: true
validation-form:
Title: Validation Form
Fields: =>EditableTextField.required-text
custom-rules-form:
Title: Custom Rules Form
Fields: =>EditableCheckbox.checkbox-2, =>EditableTextField.basic-text-2
empty-form:
Title: Empty Form
basic-form-page:
Title: User Defined Form
Fields: =>EditableTextField.basic-text
EmailRecipients: =>UserDefinedForm_EmailRecipient.recipient-1, =>UserDefinedForm_EmailRecipient.no-html, =>UserDefinedForm_EmailRecipient.no-data
form-with-reset-and-custom-action:
Title: Form with Reset Action
SubmitButtonText: Custom Button
ShowClearButton: true
validation-form:
Title: Validation Form
Fields: =>EditableTextField.required-text
custom-rules-form:
Title: Custom Rules Form
Fields: =>EditableCheckbox.checkbox-2, =>EditableTextField.basic-text-2
empty-form:
Title: Empty Form
filtered-form-page:
Title: 'Page with filtered recipients'
Fields: =>EditableCheckboxGroupField.checkbox-group, =>EditableTextField.your-name-field, =>EditableTextField.street-field, =>EditableTextField.city-field
EmailRecipients: =>UserDefinedForm_EmailRecipient.unfiltered-recipient-1, =>UserDefinedForm_EmailRecipient.filtered-recipient-1, =>UserDefinedForm_EmailRecipient.filtered-recipient-2