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