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
|
|
|
class EditableCustomRule extends DataObject { |
|
|
|
|
11
|
|
|
|
12
|
|
|
private static $condition_options = array( |
|
|
|
|
13
|
|
|
"IsBlank" => "Is blank", |
14
|
|
|
"IsNotBlank" => "Is not blank", |
15
|
|
|
"HasValue" => "Equals", |
16
|
|
|
"ValueNot" => "Doesn't equal", |
17
|
|
|
"ValueLessThan" => "Less than", |
18
|
|
|
"ValueLessThanEqual" => "Less than or equal", |
19
|
|
|
"ValueGreaterThan" => "Greater than", |
20
|
|
|
"ValueGreaterThanEqual" => "Greater than or equal" |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
private static $db = array( |
|
|
|
|
24
|
|
|
'Display' => 'Enum("Show,Hide")', |
25
|
|
|
'ConditionOption' => 'Enum("IsBlank,IsNotBlank,HasValue,ValueNot,ValueLessThan,ValueLessThanEqual,ValueGreaterThan,ValueGreaterThanEqual")', |
26
|
|
|
'FieldValue' => 'Varchar' |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
private static $has_one = array( |
|
|
|
|
30
|
|
|
'Parent' => 'EditableFormField', |
31
|
|
|
'ConditionField' => 'EditableFormField' |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Built in extensions required |
36
|
|
|
* |
37
|
|
|
* @config |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private static $extensions = array( |
|
|
|
|
41
|
|
|
"Versioned('Stage', 'Live')" |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Publish this custom rule to the live site |
46
|
|
|
* |
47
|
|
|
* Wrapper for the {@link Versioned} publish function |
48
|
|
|
*/ |
49
|
|
|
public function doPublish($fromStage, $toStage, $createNewVersion = false) { |
50
|
|
|
$this->publish($fromStage, $toStage, $createNewVersion); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Delete this custom rule from a given stage |
55
|
|
|
* |
56
|
|
|
* Wrapper for the {@link Versioned} deleteFromStage function |
57
|
|
|
*/ |
58
|
|
|
public function doDeleteFromStage($stage) { |
59
|
|
|
$this->deleteFromStage($stage); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Member $member |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public function canDelete($member = null) { |
68
|
|
|
return $this->canEdit($member); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Member $member |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public function canEdit($member = null) { |
76
|
|
|
return $this->Parent()->canEdit($member); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param Member $member |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
public function canView($member = null) { |
84
|
|
|
return $this->Parent()->canView($member); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Return whether a user can create an object of this type |
89
|
|
|
* |
90
|
|
|
* @param Member $member |
91
|
|
|
* @param array $context Virtual parameter to allow context to be passed in to check |
|
|
|
|
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function canCreate($member = null) { |
|
|
|
|
95
|
|
|
// Check parent page |
96
|
|
|
$parent = $this->getCanCreateContext(func_get_args()); |
97
|
|
|
if($parent) { |
98
|
|
|
return $parent->canEdit($member); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// Fall back to secure admin permissions |
102
|
|
|
return parent::canCreate($member); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Helper method to check the parent for this object |
107
|
|
|
* |
108
|
|
|
* @param array $args List of arguments passed to canCreate |
109
|
|
|
* @return DataObject Some parent dataobject to inherit permissions from |
110
|
|
|
*/ |
111
|
|
View Code Duplication |
protected function getCanCreateContext($args) { |
|
|
|
|
112
|
|
|
// Inspect second parameter to canCreate for a 'Parent' context |
113
|
|
|
if(isset($args[1]['Parent'])) { |
114
|
|
|
return $args[1]['Parent']; |
115
|
|
|
} |
116
|
|
|
// Hack in currently edited page if context is missing |
117
|
|
|
if(Controller::has_curr() && Controller::curr() instanceof CMSMain) { |
118
|
|
|
return Controller::curr()->currentPage(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// No page being edited |
122
|
|
|
return null; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param Member $member |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
|
public function canPublish($member = null) { |
130
|
|
|
return $this->canEdit($member); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param Member $member |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
public function canUnpublish($member = null) { |
138
|
|
|
return $this->canDelete($member); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
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.