mirror of
https://github.com/silverstripe/silverstripe-userforms.git
synced 2024-10-22 17:05:42 +02:00
Adding extra filters (GreaterThan, LessThan, etc.) to Email Custom Rules (#590)
This commit is contained in:
parent
e31af08c7e
commit
6fd952c737
@ -275,6 +275,7 @@ SQL;
|
|||||||
|
|
||||||
// Filter by rules
|
// Filter by rules
|
||||||
$recipients = $recipients->filterByCallback(function ($recipient) use ($data, $form) {
|
$recipients = $recipients->filterByCallback(function ($recipient) use ($data, $form) {
|
||||||
|
/** @var UserDefinedForm_EmailRecipient $recipient */
|
||||||
return $recipient->canSend($data, $form);
|
return $recipient->canSend($data, $form);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -157,7 +157,7 @@ class UserDefinedForm_EmailRecipient extends DataObject
|
|||||||
$validEmailToFields->merge($multiOptionFields);
|
$validEmailToFields->merge($multiOptionFields);
|
||||||
} else {
|
} else {
|
||||||
// To address cannot be unbound, so restrict to pre-defined lists
|
// To address cannot be unbound, so restrict to pre-defined lists
|
||||||
$validEmailToFields = $multiOptionFields;
|
$validEmailToFields = $multiOptionFields;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build fieldlist
|
// Build fieldlist
|
||||||
@ -383,7 +383,8 @@ class UserDefinedForm_EmailRecipient extends DataObject
|
|||||||
// Check all rules
|
// Check all rules
|
||||||
$isAnd = $this->CustomRulesCondition === 'And';
|
$isAnd = $this->CustomRulesCondition === 'And';
|
||||||
foreach ($customRules as $customRule) {
|
foreach ($customRules as $customRule) {
|
||||||
$matches = $customRule->matches($data, $form);
|
/** @var UserDefinedForm_EmailRecipientCondition $customRule */
|
||||||
|
$matches = $customRule->matches($data);
|
||||||
if ($isAnd && !$matches) {
|
if ($isAnd && !$matches) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,11 @@
|
|||||||
* Declares a condition that determines whether an email can be sent to a given recipient
|
* Declares a condition that determines whether an email can be sent to a given recipient
|
||||||
*
|
*
|
||||||
* @method UserDefinedForm_EmailRecipient Parent()
|
* @method UserDefinedForm_EmailRecipient Parent()
|
||||||
|
*
|
||||||
|
* @property Enum ConditionOption
|
||||||
|
* @property Varchar ConditionValue
|
||||||
|
*
|
||||||
|
* @method EditableFormField ConditionField
|
||||||
*/
|
*/
|
||||||
class UserDefinedForm_EmailRecipientCondition extends DataObject
|
class UserDefinedForm_EmailRecipientCondition extends DataObject
|
||||||
{
|
{
|
||||||
@ -19,11 +24,15 @@ class UserDefinedForm_EmailRecipientCondition extends DataObject
|
|||||||
"IsBlank" => "Is blank",
|
"IsBlank" => "Is blank",
|
||||||
"IsNotBlank" => "Is not blank",
|
"IsNotBlank" => "Is not blank",
|
||||||
"Equals" => "Equals",
|
"Equals" => "Equals",
|
||||||
"NotEquals" => "Doesn't equal"
|
"NotEquals" => "Doesn't equal",
|
||||||
|
"ValueLessThan" => "Less than",
|
||||||
|
"ValueLessThanEqual" => "Less than or equal",
|
||||||
|
"ValueGreaterThan" => "Greater than",
|
||||||
|
"ValueGreaterThanEqual" => "Greater than or equal"
|
||||||
);
|
);
|
||||||
|
|
||||||
private static $db = array(
|
private static $db = array(
|
||||||
'ConditionOption' => 'Enum("IsBlank,IsNotBlank,Equals,NotEquals")',
|
'ConditionOption' => 'Enum("IsBlank,IsNotBlank,Equals,NotEquals,ValueLessThan,ValueLessThanEqual,ValueGreaterThan,ValueGreaterThanEqual")',
|
||||||
'ConditionValue' => 'Varchar'
|
'ConditionValue' => 'Varchar'
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -33,27 +42,55 @@ class UserDefinedForm_EmailRecipientCondition extends DataObject
|
|||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
* Determine if this rule matches the given condition
|
* Determine if this rule matches the given condition
|
||||||
*
|
*
|
||||||
* @param array $data
|
* @param $data
|
||||||
* @param Form $form
|
*
|
||||||
* @return bool
|
* @return bool|null
|
||||||
|
* @throws LogicException
|
||||||
*/
|
*/
|
||||||
public function matches($data, $form)
|
public function matches($data)
|
||||||
{
|
{
|
||||||
$fieldName = $this->ConditionField()->Name;
|
$fieldName = $this->ConditionField()->Name;
|
||||||
$fieldValue = isset($data[$fieldName]) ? $data[$fieldName] : null;
|
$fieldValue = isset($data[$fieldName]) ? $data[$fieldName] : null;
|
||||||
|
$conditionValue = $this->ConditionValue;
|
||||||
|
$result = null;
|
||||||
switch ($this->ConditionOption) {
|
switch ($this->ConditionOption) {
|
||||||
case 'IsBlank':
|
case 'IsBlank':
|
||||||
return empty($fieldValue);
|
$result = empty($fieldValue);
|
||||||
|
break;
|
||||||
case 'IsNotBlank':
|
case 'IsNotBlank':
|
||||||
return !empty($fieldValue);
|
$result = !empty($fieldValue);
|
||||||
|
break;
|
||||||
|
case 'ValueLessThan':
|
||||||
|
$result = ($fieldValue < $conditionValue);
|
||||||
|
break;
|
||||||
|
case 'ValueLessThanEqual':
|
||||||
|
$result = ($fieldValue <= $conditionValue);
|
||||||
|
break;
|
||||||
|
case 'ValueGreaterThan':
|
||||||
|
$result = ($fieldValue > $conditionValue);
|
||||||
|
break;
|
||||||
|
case 'ValueGreaterThanEqual':
|
||||||
|
$result = ($fieldValue >= $conditionValue);
|
||||||
|
break;
|
||||||
|
case 'NotEquals':
|
||||||
|
case 'Equals':
|
||||||
|
$result = is_array($fieldValue)
|
||||||
|
? in_array($conditionValue, $fieldValue)
|
||||||
|
: $fieldValue == $conditionValue;
|
||||||
|
|
||||||
|
if ($this->ConditionOption == 'NotEquals') {
|
||||||
|
$result = !($result);
|
||||||
|
}
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
$matches = is_array($fieldValue)
|
throw new LogicException("Unhandled rule {$this->ConditionOption}");
|
||||||
? in_array($this->ConditionValue, $fieldValue)
|
break;
|
||||||
: $this->ConditionValue === (string) $fieldValue;
|
|
||||||
return ($this->ConditionOption === 'Equals') === (bool)$matches;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -347,6 +347,7 @@ class UserDefinedFormTest extends FunctionalTest
|
|||||||
|
|
||||||
public function testEmailRecipientFilters()
|
public function testEmailRecipientFilters()
|
||||||
{
|
{
|
||||||
|
/** @var UserDefinedForm $form */
|
||||||
$form = $this->objFromFixture('UserDefinedForm', 'filtered-form-page');
|
$form = $this->objFromFixture('UserDefinedForm', 'filtered-form-page');
|
||||||
|
|
||||||
// Check unfiltered recipients
|
// Check unfiltered recipients
|
||||||
@ -404,7 +405,6 @@ class UserDefinedFormTest extends FunctionalTest
|
|||||||
$result2
|
$result2
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
$result3 = $form->FilteredEmailRecipients(
|
$result3 = $form->FilteredEmailRecipients(
|
||||||
array(
|
array(
|
||||||
'your-name' => 'Should be blank but is not',
|
'your-name' => 'Should be blank but is not',
|
||||||
|
64
tests/UserDefinedForm_EmailRecipientConditionTest.php
Normal file
64
tests/UserDefinedForm_EmailRecipientConditionTest.php
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class EditableCustomRulesTest
|
||||||
|
*/
|
||||||
|
class UserDefinedForm_EmailRecipientConditionTest extends SapphireTest
|
||||||
|
{
|
||||||
|
protected static $fixture_file = 'userforms/tests/UserDefinedForm_EmailRecipientConditionTest.yml';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Various matching tests
|
||||||
|
*/
|
||||||
|
public function testMatches()
|
||||||
|
{
|
||||||
|
$fixtureClass = 'UserDefinedForm_EmailRecipientCondition';
|
||||||
|
//Test Blank
|
||||||
|
/** @var UserDefinedForm_EmailRecipientCondition $blankObj */
|
||||||
|
$blankObj = $this->objFromFixture($fixtureClass, 'blankTest');
|
||||||
|
$this->assertTrue($blankObj->matches(array('Name' => null)));
|
||||||
|
$this->assertFalse($blankObj->matches(array('Name' => 'Jane')));
|
||||||
|
|
||||||
|
//Test IsNotBlank
|
||||||
|
/** @var UserDefinedForm_EmailRecipientCondition $blankObj */
|
||||||
|
$blankObj = $this->objFromFixture($fixtureClass, 'isNotBlankTest');
|
||||||
|
$this->assertTrue($blankObj->matches(array('Name' => 'Jane')));
|
||||||
|
$this->assertFalse($blankObj->matches(array('Name' => null)));
|
||||||
|
|
||||||
|
//Test ValueLessthan
|
||||||
|
/** @var UserDefinedForm_EmailRecipientCondition $blankObj */
|
||||||
|
$blankObj = $this->objFromFixture($fixtureClass, 'valueLessThanTest');
|
||||||
|
$this->assertTrue($blankObj->matches(array('Age' => 17)));
|
||||||
|
$this->assertFalse($blankObj->matches(array('Age' => 19)));
|
||||||
|
|
||||||
|
//Test ValueLessThanEquals
|
||||||
|
/** @var UserDefinedForm_EmailRecipientCondition $blankObj */
|
||||||
|
$blankObj = $this->objFromFixture($fixtureClass, 'valueLessThanEqualTest');
|
||||||
|
$this->assertTrue($blankObj->matches(array('Age' => 18)));
|
||||||
|
$this->assertFalse($blankObj->matches(array('Age' => 19)));
|
||||||
|
|
||||||
|
//Test ValueGreaterThan
|
||||||
|
/** @var UserDefinedForm_EmailRecipientCondition $blankObj */
|
||||||
|
$blankObj = $this->objFromFixture($fixtureClass, 'valueGreaterThanTest');
|
||||||
|
$this->assertTrue($blankObj->matches(array('Age' => 19)));
|
||||||
|
$this->assertFalse($blankObj->matches(array('Age' => 17)));
|
||||||
|
|
||||||
|
//Test ValueGreaterThanEquals
|
||||||
|
/** @var UserDefinedForm_EmailRecipientCondition $blankObj */
|
||||||
|
$blankObj = $this->objFromFixture($fixtureClass, 'valueGreaterThanEqualTest');
|
||||||
|
$this->assertTrue($blankObj->matches(array('Age' => 18)));
|
||||||
|
$this->assertFalse($blankObj->matches(array('Age' => 17)));
|
||||||
|
|
||||||
|
//Test Equals
|
||||||
|
/** @var UserDefinedForm_EmailRecipientCondition $blankObj */
|
||||||
|
$blankObj = $this->objFromFixture($fixtureClass, 'equalsTest');
|
||||||
|
$this->assertTrue($blankObj->matches(array('Age' => 18)));
|
||||||
|
$this->assertFalse($blankObj->matches(array('Age' => 17)));
|
||||||
|
|
||||||
|
//Test NotEquals
|
||||||
|
/** @var UserDefinedForm_EmailRecipientCondition $blankObj */
|
||||||
|
$blankObj = $this->objFromFixture($fixtureClass, 'notEqualsTest');
|
||||||
|
$this->assertTrue($blankObj->matches(array('Age' => 17)));
|
||||||
|
$this->assertFalse($blankObj->matches(array('Age' => 18)));
|
||||||
|
}
|
||||||
|
}
|
39
tests/UserDefinedForm_EmailRecipientConditionTest.yml
Normal file
39
tests/UserDefinedForm_EmailRecipientConditionTest.yml
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
EditableTextField:
|
||||||
|
nameField:
|
||||||
|
Name: Name
|
||||||
|
EditableNumericField:
|
||||||
|
ageField:
|
||||||
|
Name: Age
|
||||||
|
UserDefinedForm_EmailRecipientCondition:
|
||||||
|
blankTest:
|
||||||
|
ConditionOption: IsBlank
|
||||||
|
ConditionValue: null
|
||||||
|
ConditionField: =>EditableTextField.nameField
|
||||||
|
isNotBlankTest:
|
||||||
|
ConditionOption: IsNotBlank
|
||||||
|
ConditionValue: null
|
||||||
|
ConditionField: =>EditableTextField.nameField
|
||||||
|
equalsTest:
|
||||||
|
ConditionOption: Equals
|
||||||
|
ConditionValue: 18
|
||||||
|
ConditionField: =>EditableNumericField.ageField
|
||||||
|
notEqualsTest:
|
||||||
|
ConditionOption: NotEquals
|
||||||
|
ConditionValue: 18
|
||||||
|
ConditionField: =>EditableNumericField.ageField
|
||||||
|
valueLessThanTest:
|
||||||
|
ConditionOption: ValueLessThan
|
||||||
|
ConditionValue: 18
|
||||||
|
ConditionField: =>EditableNumericField.ageField
|
||||||
|
valueLessThanEqualTest:
|
||||||
|
ConditionOption: ValueLessThanEqual
|
||||||
|
ConditionValue: 18
|
||||||
|
ConditionField: =>EditableNumericField.ageField
|
||||||
|
valueGreaterThanTest:
|
||||||
|
ConditionOption: ValueGreaterThan
|
||||||
|
ConditionValue: 18
|
||||||
|
ConditionField: =>EditableNumericField.ageField
|
||||||
|
valueGreaterThanEqualTest:
|
||||||
|
ConditionOption: ValueGreaterThanEqual
|
||||||
|
ConditionValue: 18
|
||||||
|
ConditionField: =>EditableNumericField.ageField
|
Loading…
Reference in New Issue
Block a user