1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* A custom rule for showing / hiding an EditableFormField |
5
|
|
|
* based the value of another EditableFormField. |
6
|
|
|
* |
7
|
|
|
* @method EditableFormField Parent() |
8
|
|
|
* @package userforms |
9
|
|
|
* |
10
|
|
|
* @property string Display |
11
|
|
|
* @property string ConditionOption |
12
|
|
|
* @property string FieldValue |
13
|
|
|
*/ |
14
|
|
|
class EditableCustomRule extends DataObject |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
private static $condition_options = array( |
|
|
|
|
18
|
|
|
"IsBlank" => "Is blank", |
19
|
|
|
"IsNotBlank" => "Is not blank", |
20
|
|
|
"HasValue" => "Equals", |
21
|
|
|
"ValueNot" => "Doesn't equal", |
22
|
|
|
"ValueLessThan" => "Less than", |
23
|
|
|
"ValueLessThanEqual" => "Less than or equal", |
24
|
|
|
"ValueGreaterThan" => "Greater than", |
25
|
|
|
"ValueGreaterThanEqual" => "Greater than or equal" |
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
private static $db = array( |
|
|
|
|
29
|
|
|
'Display' => 'Enum("Show,Hide")', |
30
|
|
|
'ConditionOption' => 'Enum("IsBlank,IsNotBlank,HasValue,ValueNot,ValueLessThan,ValueLessThanEqual,ValueGreaterThan,ValueGreaterThanEqual")', |
31
|
|
|
'FieldValue' => 'Varchar(255)' |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
private static $has_one = array( |
|
|
|
|
35
|
|
|
'Parent' => 'EditableFormField', |
36
|
|
|
'ConditionField' => 'EditableFormField' |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Built in extensions required |
41
|
|
|
* |
42
|
|
|
* @config |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
private static $extensions = array( |
|
|
|
|
46
|
|
|
"Versioned('Stage', 'Live')" |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Publish this custom rule to the live site |
51
|
|
|
* |
52
|
|
|
* Wrapper for the {@link Versioned} publish function |
53
|
|
|
*/ |
54
|
1 |
|
public function doPublish($fromStage, $toStage, $createNewVersion = false) |
55
|
|
|
{ |
56
|
1 |
|
$this->publish($fromStage, $toStage, $createNewVersion); |
|
|
|
|
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Delete this custom rule from a given stage |
61
|
|
|
* |
62
|
|
|
* Wrapper for the {@link Versioned} deleteFromStage function |
63
|
|
|
*/ |
64
|
|
|
public function doDeleteFromStage($stage) |
65
|
|
|
{ |
66
|
|
|
$this->deleteFromStage($stage); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param Member $member |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function canDelete($member = null) |
75
|
|
|
{ |
76
|
|
|
return $this->canEdit($member); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param Member $member |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
public function canEdit($member = null) |
84
|
|
|
{ |
85
|
|
|
return $this->Parent()->canEdit($member); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param Member $member |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
public function canView($member = null) |
93
|
|
|
{ |
94
|
|
|
return $this->Parent()->canView($member); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Return whether a user can create an object of this type |
99
|
|
|
* |
100
|
|
|
* @param Member $member |
101
|
|
|
* @param array $context Virtual parameter to allow context to be passed in to check |
|
|
|
|
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public function canCreate($member = null) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
// Check parent page |
107
|
|
|
$parent = $this->getCanCreateContext(func_get_args()); |
108
|
|
|
if ($parent) { |
109
|
|
|
return $parent->canEdit($member); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Fall back to secure admin permissions |
113
|
|
|
return parent::canCreate($member); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Helper method to check the parent for this object |
118
|
|
|
* |
119
|
|
|
* @param array $args List of arguments passed to canCreate |
120
|
|
|
* @return DataObject Some parent dataobject to inherit permissions from |
121
|
|
|
*/ |
122
|
|
View Code Duplication |
protected function getCanCreateContext($args) |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
// Inspect second parameter to canCreate for a 'Parent' context |
125
|
|
|
if (isset($args[1]['Parent'])) { |
126
|
|
|
return $args[1]['Parent']; |
127
|
|
|
} |
128
|
|
|
// Hack in currently edited page if context is missing |
129
|
|
|
if (Controller::has_curr() && Controller::curr() instanceof CMSMain) { |
130
|
|
|
return Controller::curr()->currentPage(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// No page being edited |
134
|
|
|
return null; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param Member $member |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
public function canPublish($member = null) |
142
|
|
|
{ |
143
|
|
|
return $this->canEdit($member); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param Member $member |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function canUnpublish($member = null) |
151
|
|
|
{ |
152
|
|
|
return $this->canDelete($member); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Substitutes configured rule logic with it's JS equivalents and returns them as array elements |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
2 |
|
public function buildExpression() |
160
|
|
|
{ |
161
|
|
|
/** @var EditableFormField $formFieldWatch */ |
162
|
2 |
|
$formFieldWatch = $this->ConditionField(); |
|
|
|
|
163
|
|
|
//Encapsulated the action to the object |
164
|
2 |
|
$action = $formFieldWatch->getJsEventHandler(); |
165
|
|
|
|
166
|
|
|
// is this field a special option field |
167
|
2 |
|
$checkboxField = $formFieldWatch->isCheckBoxField(); |
168
|
2 |
|
$radioField = $formFieldWatch->isRadioField(); |
169
|
2 |
|
$target = sprintf('$("%s")', $formFieldWatch->getSelectorFieldOnly()); |
170
|
2 |
|
$fieldValue = Convert::raw2js($this->FieldValue); |
171
|
|
|
|
172
|
|
|
$conditionOptions = array( |
173
|
2 |
|
'ValueLessThan' => '<', |
174
|
|
|
'ValueLessThanEqual' => '<=', |
175
|
|
|
'ValueGreaterThan' => '>', |
176
|
|
|
'ValueGreaterThanEqual' => '>=' |
177
|
|
|
); |
178
|
|
|
// and what should we evaluate |
179
|
2 |
|
switch ($this->ConditionOption) { |
180
|
2 |
|
case 'IsNotBlank': |
181
|
2 |
|
case 'IsBlank': |
182
|
|
|
$expression = ($checkboxField || $radioField) ? "!{$target}.is(\":checked\")" : "{$target}.val() == ''"; |
183
|
|
|
if ($this->ConditionOption == 'IsNotBlank') { |
184
|
|
|
//Negate |
185
|
|
|
$expression = "!({$expression})"; |
186
|
|
|
} |
187
|
|
|
break; |
188
|
2 |
|
case 'HasValue': |
189
|
1 |
|
case 'ValueNot': |
190
|
2 |
|
if ($checkboxField) { |
191
|
|
|
if ($formFieldWatch->isCheckBoxGroupField()) { |
192
|
|
|
$expression = sprintf( |
193
|
|
|
"$.inArray('%s', %s.filter(':checked').map(function(){ return $(this).val();}).get()) > -1", |
194
|
|
|
$fieldValue, |
195
|
|
|
$target |
196
|
|
|
); |
197
|
|
|
} else { |
198
|
|
|
$expression = "{$target}.prop('checked')"; |
199
|
|
|
} |
200
|
2 |
|
} elseif ($radioField) { |
201
|
|
|
// We cannot simply get the value of the radio group, we need to find the checked option first. |
202
|
|
|
$expression = sprintf( |
203
|
|
|
'%s.closest(".field, .control-group").find("input:checked").val() == "%s"', |
204
|
|
|
$target, |
205
|
|
|
$fieldValue |
206
|
|
|
); |
207
|
|
|
} else { |
208
|
2 |
|
$expression = sprintf('%s.val() == "%s"', $target, $fieldValue); |
209
|
|
|
} |
210
|
|
|
|
211
|
2 |
|
if ($this->ConditionOption == 'ValueNot') { |
212
|
|
|
//Negate |
213
|
|
|
$expression = "!({$expression})"; |
214
|
|
|
} |
215
|
2 |
|
break; |
216
|
1 |
|
case 'ValueLessThan': |
217
|
1 |
|
case 'ValueLessThanEqual': |
218
|
1 |
|
case 'ValueGreaterThan': |
219
|
|
|
case 'ValueGreaterThanEqual': |
220
|
1 |
|
$expression = sprintf( |
221
|
1 |
|
'%s.val() %s parseFloat("%s")', |
222
|
1 |
|
$target, |
223
|
1 |
|
$conditionOptions[$this->ConditionOption], |
224
|
1 |
|
$fieldValue |
225
|
|
|
); |
226
|
1 |
|
break; |
227
|
|
|
default: |
228
|
|
|
throw new LogicException("Unhandled rule {$this->ConditionOption}"); |
229
|
|
|
break; |
|
|
|
|
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
$result = array( |
233
|
2 |
|
'operation' => $expression, |
234
|
2 |
|
'event' => $action, |
235
|
|
|
); |
236
|
|
|
|
237
|
2 |
|
return $result; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Returns the opposite visibility function for the value of the initial visibility field, e.g. show/hide. This |
242
|
|
|
* will toggle the "hide" class either way, which is handled by CSS. |
243
|
|
|
* |
244
|
|
|
* @param string $text |
245
|
|
|
* @return string |
246
|
|
|
*/ |
247
|
2 |
|
public function toggleDisplayText($text) |
248
|
|
|
{ |
249
|
2 |
|
return (strtolower($text) === 'hide') ? 'removeClass("hide")' : 'addClass("hide")'; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
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.