1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\UserForms\Model\Recipient; |
4
|
|
|
|
5
|
|
|
use LogicException; |
6
|
|
|
use SilverStripe\CMS\Controllers\CMSMain; |
7
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
8
|
|
|
use SilverStripe\Control\Controller; |
9
|
|
|
use SilverStripe\ORM\DataObject; |
10
|
|
|
use SilverStripe\Security\Member; |
11
|
|
|
use SilverStripe\UserForms\Model\Recipient\EmailRecipient; |
12
|
|
|
use SilverStripe\UserForms\Model\EditableFormField; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Declares a condition that determines whether an email can be sent to a given recipient |
16
|
|
|
* |
17
|
|
|
* @method EmailRecipient Parent() |
18
|
|
|
* |
19
|
|
|
* @property Enum ConditionOption |
20
|
|
|
* @property Varchar ConditionValue |
21
|
|
|
* |
22
|
|
|
* @method EditableFormField ConditionField |
23
|
|
|
*/ |
24
|
|
|
class EmailRecipientCondition extends DataObject |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* List of options |
28
|
|
|
* |
29
|
|
|
* @config |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private static $condition_options = [ |
|
|
|
|
33
|
|
|
'IsBlank' => 'Is blank', |
34
|
|
|
'IsNotBlank' => 'Is not blank', |
35
|
|
|
'Equals' => 'Equals', |
36
|
|
|
'NotEquals' => "Doesn't equal", |
37
|
|
|
'ValueLessThan' => 'Less than', |
38
|
|
|
'ValueLessThanEqual' => 'Less than or equal', |
39
|
|
|
'ValueGreaterThan' => 'Greater than', |
40
|
|
|
'ValueGreaterThanEqual' => 'Greater than or equal' |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
private static $db = [ |
|
|
|
|
44
|
|
|
'ConditionOption' => 'Enum("IsBlank,IsNotBlank,Equals,NotEquals,ValueLessThan,ValueLessThanEqual,ValueGreaterThan,ValueGreaterThanEqual")', |
45
|
|
|
'ConditionValue' => 'Varchar' |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
private static $has_one = [ |
|
|
|
|
49
|
|
|
'Parent' => EmailRecipient::class, |
50
|
|
|
'ConditionField' => EditableFormField::class |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
private static $table_name = 'UserDefinedForm_EmailRecipientCondition'; |
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* |
57
|
|
|
* Determine if this rule matches the given condition |
58
|
|
|
* |
59
|
|
|
* @param $data |
60
|
|
|
* |
61
|
|
|
* @return bool|null |
62
|
|
|
* @throws LogicException |
63
|
|
|
*/ |
64
|
|
|
public function matches($data) |
65
|
|
|
{ |
66
|
|
|
$fieldName = $this->ConditionField()->Name; |
67
|
|
|
$fieldValue = isset($data[$fieldName]) ? $data[$fieldName] : null; |
68
|
|
|
$conditionValue = $this->ConditionValue; |
69
|
|
|
$result = null; |
70
|
|
|
switch ($this->ConditionOption) { |
71
|
|
|
case 'IsBlank': |
72
|
|
|
$result = empty($fieldValue); |
73
|
|
|
break; |
74
|
|
|
case 'IsNotBlank': |
75
|
|
|
$result = !empty($fieldValue); |
76
|
|
|
break; |
77
|
|
|
case 'ValueLessThan': |
78
|
|
|
$result = ($fieldValue < $conditionValue); |
79
|
|
|
break; |
80
|
|
|
case 'ValueLessThanEqual': |
81
|
|
|
$result = ($fieldValue <= $conditionValue); |
82
|
|
|
break; |
83
|
|
|
case 'ValueGreaterThan': |
84
|
|
|
$result = ($fieldValue > $conditionValue); |
85
|
|
|
break; |
86
|
|
|
case 'ValueGreaterThanEqual': |
87
|
|
|
$result = ($fieldValue >= $conditionValue); |
88
|
|
|
break; |
89
|
|
|
case 'NotEquals': |
90
|
|
|
case 'Equals': |
91
|
|
|
$result = is_array($fieldValue) |
92
|
|
|
? in_array($conditionValue, $fieldValue) |
93
|
|
|
: $fieldValue == $conditionValue; |
94
|
|
|
|
95
|
|
|
if ($this->ConditionOption == 'NotEquals') { |
96
|
|
|
$result = !($result); |
97
|
|
|
} |
98
|
|
|
break; |
99
|
|
|
default: |
100
|
|
|
throw new LogicException("Unhandled rule {$this->ConditionOption}"); |
101
|
|
|
break; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $result; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Return whether a user can create an object of this type |
109
|
|
|
* |
110
|
|
|
* @param Member $member |
111
|
|
|
* @param array $context Virtual parameter to allow context to be passed in to check |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
public function canCreate($member = null, $context = []) |
115
|
|
|
{ |
116
|
|
|
// Check parent page |
117
|
|
|
$parent = $this->getCanCreateContext(func_get_args()); |
118
|
|
|
if ($parent) { |
|
|
|
|
119
|
|
|
return $parent->canEdit($member); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// Fall back to secure admin permissions |
123
|
|
|
return parent::canCreate($member); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Helper method to check the parent for this object |
128
|
|
|
* |
129
|
|
|
* @param array $args List of arguments passed to canCreate |
130
|
|
|
* @return SiteTree Parent page instance |
131
|
|
|
*/ |
132
|
|
|
protected function getCanCreateContext($args) |
133
|
|
|
{ |
134
|
|
|
// Inspect second parameter to canCreate for a 'Parent' context |
135
|
|
|
if (isset($args[1]['Parent'])) { |
136
|
|
|
return $args[1]['Parent']; |
137
|
|
|
} |
138
|
|
|
// Hack in currently edited page if context is missing |
139
|
|
|
if (Controller::has_curr() && Controller::curr() instanceof CMSMain) { |
140
|
|
|
return Controller::curr()->currentPage(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// No page being edited |
144
|
|
|
return null; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function canView($member = null) |
148
|
|
|
{ |
149
|
|
|
return $this->Parent()->canView($member); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function canEdit($member = null) |
153
|
|
|
{ |
154
|
|
|
return $this->Parent()->canEdit($member); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function canDelete($member = null) |
158
|
|
|
{ |
159
|
|
|
return $this->canEdit($member); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|