2015-07-24 04:37:48 +02:00
|
|
|
<?php
|
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
namespace SilverStripe\UserForms\Model\Recipient;
|
|
|
|
|
|
|
|
use LogicException;
|
|
|
|
use SilverStripe\CMS\Controllers\CMSMain;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\UserForms\Model\Recipient\EmailRecipient;
|
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableFormField;
|
2015-07-24 04:37:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Declares a condition that determines whether an email can be sent to a given recipient
|
2015-12-22 23:14:36 +01:00
|
|
|
*
|
2017-08-11 01:33:06 +02:00
|
|
|
* @method EmailRecipient Parent()
|
2017-05-15 04:05:18 +02:00
|
|
|
*
|
|
|
|
* @property Enum ConditionOption
|
|
|
|
* @property Varchar ConditionValue
|
|
|
|
*
|
|
|
|
* @method EditableFormField ConditionField
|
2015-07-24 04:37:48 +02:00
|
|
|
*/
|
2017-08-11 01:33:06 +02:00
|
|
|
class EmailRecipientCondition extends DataObject
|
2016-07-21 07:53:59 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* List of options
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $condition_options = [
|
|
|
|
'IsBlank' => 'Is blank',
|
|
|
|
'IsNotBlank' => 'Is not blank',
|
|
|
|
'Equals' => 'Equals',
|
|
|
|
'NotEquals' => "Doesn't equal",
|
|
|
|
'ValueLessThan' => 'Less than',
|
|
|
|
'ValueLessThanEqual' => 'Less than or equal',
|
|
|
|
'ValueGreaterThan' => 'Greater than',
|
|
|
|
'ValueGreaterThanEqual' => 'Greater than or equal'
|
|
|
|
];
|
2015-07-24 04:37:48 +02:00
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $db = [
|
2017-05-15 04:05:18 +02:00
|
|
|
'ConditionOption' => 'Enum("IsBlank,IsNotBlank,Equals,NotEquals,ValueLessThan,ValueLessThanEqual,ValueGreaterThan,ValueGreaterThanEqual")',
|
2016-07-21 07:53:59 +02:00
|
|
|
'ConditionValue' => 'Varchar'
|
2017-08-11 01:33:06 +02:00
|
|
|
];
|
2015-07-24 04:37:48 +02:00
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $has_one = [
|
|
|
|
'Parent' => EmailRecipient::class,
|
2017-08-09 01:55:09 +02:00
|
|
|
'ConditionField' => EditableFormField::class
|
2017-08-11 01:33:06 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
private static $table_name = 'UserDefinedForm_EmailRecipientCondition';
|
2015-07-24 04:37:48 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
2017-05-15 04:05:18 +02:00
|
|
|
*
|
2016-07-21 07:53:59 +02:00
|
|
|
* Determine if this rule matches the given condition
|
|
|
|
*
|
2017-05-15 04:05:18 +02:00
|
|
|
* @param $data
|
|
|
|
*
|
|
|
|
* @return bool|null
|
|
|
|
* @throws LogicException
|
2016-07-21 07:53:59 +02:00
|
|
|
*/
|
2017-05-15 04:05:18 +02:00
|
|
|
public function matches($data)
|
2016-07-21 07:53:59 +02:00
|
|
|
{
|
|
|
|
$fieldName = $this->ConditionField()->Name;
|
|
|
|
$fieldValue = isset($data[$fieldName]) ? $data[$fieldName] : null;
|
2017-05-15 04:05:18 +02:00
|
|
|
$conditionValue = $this->ConditionValue;
|
|
|
|
$result = null;
|
2016-07-21 07:53:59 +02:00
|
|
|
switch ($this->ConditionOption) {
|
|
|
|
case 'IsBlank':
|
2017-05-15 04:05:18 +02:00
|
|
|
$result = empty($fieldValue);
|
|
|
|
break;
|
2016-07-21 07:53:59 +02:00
|
|
|
case 'IsNotBlank':
|
2017-05-15 04:05:18 +02:00
|
|
|
$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;
|
2016-07-21 07:53:59 +02:00
|
|
|
default:
|
2017-05-15 04:05:18 +02:00
|
|
|
throw new LogicException("Unhandled rule {$this->ConditionOption}");
|
|
|
|
break;
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
2017-05-15 04:05:18 +02:00
|
|
|
|
|
|
|
return $result;
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
2015-12-22 23:14:36 +01:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
|
|
|
* Return whether a user can create an object of this type
|
|
|
|
*
|
2015-12-22 23:14:36 +01:00
|
|
|
* @param Member $member
|
|
|
|
* @param array $context Virtual parameter to allow context to be passed in to check
|
2016-07-21 07:53:59 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canCreate($member = null)
|
|
|
|
{
|
|
|
|
// Check parent page
|
2015-12-22 23:14:36 +01:00
|
|
|
$parent = $this->getCanCreateContext(func_get_args());
|
2016-07-21 07:53:59 +02:00
|
|
|
if ($parent) {
|
2015-12-22 23:14:36 +01:00
|
|
|
return $parent->canEdit($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to secure admin permissions
|
|
|
|
return parent::canCreate($member);
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
2015-12-22 23:14:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to check the parent for this object
|
|
|
|
*
|
|
|
|
* @param array $args List of arguments passed to canCreate
|
|
|
|
* @return SiteTree Parent page instance
|
|
|
|
*/
|
2016-07-21 07:53:59 +02:00
|
|
|
protected function getCanCreateContext($args)
|
|
|
|
{
|
2015-12-22 23:14:36 +01:00
|
|
|
// Inspect second parameter to canCreate for a 'Parent' context
|
2016-07-21 07:53:59 +02:00
|
|
|
if (isset($args[1]['Parent'])) {
|
2015-12-22 23:14:36 +01:00
|
|
|
return $args[1]['Parent'];
|
|
|
|
}
|
|
|
|
// Hack in currently edited page if context is missing
|
2016-07-21 07:53:59 +02:00
|
|
|
if (Controller::has_curr() && Controller::curr() instanceof CMSMain) {
|
2015-12-22 23:14:36 +01:00
|
|
|
return Controller::curr()->currentPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
// No page being edited
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
|
|
|
* @param Member
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canView($member = null)
|
|
|
|
{
|
|
|
|
return $this->Parent()->canView($member);
|
|
|
|
}
|
2015-12-22 23:14:36 +01:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
|
|
|
* @param Member
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canEdit($member = null)
|
|
|
|
{
|
|
|
|
return $this->Parent()->canEdit($member);
|
|
|
|
}
|
2015-12-22 23:14:36 +01:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
|
|
|
* @param Member
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canDelete($member = null)
|
|
|
|
{
|
|
|
|
return $this->canEdit($member);
|
|
|
|
}
|
2015-12-22 23:14:36 +01:00
|
|
|
}
|