1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Declares a condition that determines whether an email can be sent to a given recipient |
6
|
|
|
* |
7
|
|
|
* @method UserDefinedForm_EmailRecipient Parent() |
8
|
|
|
*/ |
9
|
|
|
class UserDefinedForm_EmailRecipientCondition extends DataObject { |
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* List of options |
13
|
|
|
* |
14
|
|
|
* @config |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private static $condition_options = array( |
|
|
|
|
18
|
|
|
"IsBlank" => "Is blank", |
19
|
|
|
"IsNotBlank" => "Is not blank", |
20
|
|
|
"Equals" => "Equals", |
21
|
|
|
"NotEquals" => "Doesn't equal" |
22
|
|
|
); |
23
|
|
|
|
24
|
|
|
private static $db = array( |
|
|
|
|
25
|
|
|
'ConditionOption' => 'Enum("IsBlank,IsNotBlank,Equals,NotEquals")', |
26
|
|
|
'ConditionValue' => 'Varchar' |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
private static $has_one = array( |
|
|
|
|
30
|
|
|
'Parent' => 'UserDefinedForm_EmailRecipient', |
31
|
|
|
'ConditionField' => 'EditableFormField' |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Determine if this rule matches the given condition |
36
|
|
|
* |
37
|
|
|
* @param array $data |
38
|
|
|
* @param Form $form |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
|
|
public function matches($data, $form) { |
|
|
|
|
42
|
|
|
$fieldName = $this->ConditionField()->Name; |
|
|
|
|
43
|
|
|
$fieldValue = isset($data[$fieldName]) ? $data[$fieldName] : null; |
44
|
|
|
switch($this->ConditionOption) { |
|
|
|
|
45
|
|
|
case 'IsBlank': |
46
|
|
|
return empty($fieldValue); |
47
|
|
|
case 'IsNotBlank': |
48
|
|
|
return !empty($fieldValue); |
49
|
|
|
default: |
50
|
|
|
$matches = is_array($fieldValue) |
51
|
|
|
? in_array($this->ConditionValue, $fieldValue) |
|
|
|
|
52
|
|
|
: $this->ConditionValue === (string) $fieldValue; |
|
|
|
|
53
|
|
|
return ($this->ConditionOption === 'Equals') === (bool)$matches; |
|
|
|
|
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Return whether a user can create an object of this type |
59
|
|
|
* |
60
|
|
|
* @param Member $member |
61
|
|
|
* @param array $context Virtual parameter to allow context to be passed in to check |
|
|
|
|
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function canCreate($member = null) { |
|
|
|
|
65
|
|
|
// Check parent page |
66
|
|
|
$parent = $this->getCanCreateContext(func_get_args()); |
67
|
|
|
if($parent) { |
68
|
|
|
return $parent->canEdit($member); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Fall back to secure admin permissions |
72
|
|
|
return parent::canCreate($member); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Helper method to check the parent for this object |
77
|
|
|
* |
78
|
|
|
* @param array $args List of arguments passed to canCreate |
79
|
|
|
* @return SiteTree Parent page instance |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
protected function getCanCreateContext($args) { |
|
|
|
|
82
|
|
|
// Inspect second parameter to canCreate for a 'Parent' context |
83
|
|
|
if(isset($args[1]['Parent'])) { |
84
|
|
|
return $args[1]['Parent']; |
85
|
|
|
} |
86
|
|
|
// Hack in currently edited page if context is missing |
87
|
|
|
if(Controller::has_curr() && Controller::curr() instanceof CMSMain) { |
88
|
|
|
return Controller::curr()->currentPage(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// No page being edited |
92
|
|
|
return null; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param Member |
97
|
|
|
* |
98
|
|
|
* @return boolean |
99
|
|
|
*/ |
100
|
|
|
public function canView($member = null) { |
101
|
|
|
return $this->Parent()->canView($member); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param Member |
106
|
|
|
* |
107
|
|
|
* @return boolean |
108
|
|
|
*/ |
109
|
|
|
public function canEdit($member = null) { |
110
|
|
|
return $this->Parent()->canEdit($member); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param Member |
115
|
|
|
* |
116
|
|
|
* @return boolean |
117
|
|
|
*/ |
118
|
|
|
public function canDelete($member = null) { |
119
|
|
|
return $this->canEdit($member); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.