1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Base Class for EditableOption Fields such as the ones used in |
5
|
|
|
* dropdown fields and in radio check box groups |
6
|
|
|
* |
7
|
|
|
* @method EditableMultipleOptionField Parent() |
8
|
|
|
* @package userforms |
9
|
|
|
*/ |
10
|
|
|
class EditableOption extends DataObject |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
private static $default_sort = "Sort"; |
|
|
|
|
14
|
|
|
|
15
|
|
|
private static $db = array( |
|
|
|
|
16
|
|
|
"Name" => "Varchar(255)", |
17
|
|
|
"Title" => "Varchar(255)", |
18
|
|
|
"Default" => "Boolean", |
19
|
|
|
"Sort" => "Int", |
20
|
|
|
"Value" => "Varchar(255)", |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
private static $has_one = array( |
|
|
|
|
24
|
|
|
"Parent" => "EditableMultipleOptionField", |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
private static $extensions = array( |
|
|
|
|
28
|
|
|
"Versioned('Stage', 'Live')" |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
private static $summary_fields = array( |
|
|
|
|
32
|
|
|
'Title', |
33
|
|
|
'Default' |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
protected static $allow_empty_values = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns whether to allow empty values or not. |
40
|
|
|
* |
41
|
|
|
* @return boolean |
42
|
|
|
*/ |
43
|
2 |
|
public static function allow_empty_values() |
44
|
|
|
{ |
45
|
2 |
|
return (bool) self::$allow_empty_values; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Set whether to allow empty values. |
50
|
|
|
* |
51
|
|
|
* @param boolean $allow |
52
|
|
|
*/ |
53
|
|
|
public static function set_allow_empty_values($allow) |
54
|
|
|
{ |
55
|
|
|
self::$allow_empty_values = (bool) $allow; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param Member $member |
60
|
|
|
* |
61
|
|
|
* @return boolean |
62
|
|
|
*/ |
63
|
|
|
public function canEdit($member = null) |
64
|
|
|
{ |
65
|
|
|
return $this->Parent()->canEdit($member); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param Member $member |
70
|
|
|
* |
71
|
|
|
* @return boolean |
72
|
|
|
*/ |
73
|
|
|
public function canDelete($member = null) |
74
|
|
|
{ |
75
|
|
|
return $this->canEdit($member); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @deprecated 5.0 Use "$Title.XML" in templates instead |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function getEscapedTitle() |
83
|
|
|
{ |
84
|
|
|
return Convert::raw2att($this->Title); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param Member $member |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function canView($member = null) |
92
|
|
|
{ |
93
|
|
|
return $this->Parent()->canView($member); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Return whether a user can create an object of this type |
98
|
|
|
* |
99
|
|
|
* @param Member $member |
100
|
|
|
* @param array $context Virtual parameter to allow context to be passed in to check |
|
|
|
|
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
public function canCreate($member = null) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
// Check parent page |
106
|
|
|
$parent = $this->getCanCreateContext(func_get_args()); |
107
|
|
|
if($parent) { |
108
|
|
|
return $parent->canEdit($member); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// Fall back to secure admin permissions |
112
|
|
|
return parent::canCreate($member); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Helper method to check the parent for this object |
117
|
|
|
* |
118
|
|
|
* @param array $args List of arguments passed to canCreate |
119
|
|
|
* @return DataObject Some parent dataobject to inherit permissions from |
120
|
|
|
*/ |
121
|
|
View Code Duplication |
protected function getCanCreateContext($args) |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
// Inspect second parameter to canCreate for a 'Parent' context |
124
|
|
|
if(isset($args[1]['Parent'])) { |
125
|
|
|
return $args[1]['Parent']; |
126
|
|
|
} |
127
|
|
|
// Hack in currently edited page if context is missing |
128
|
|
|
if(Controller::has_curr() && Controller::curr() instanceof CMSMain) { |
129
|
|
|
return Controller::curr()->currentPage(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// No page being edited |
133
|
|
|
return null; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param Member $member |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
|
|
public function canPublish($member = null) |
141
|
|
|
{ |
142
|
|
|
return $this->canEdit($member); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param Member $member |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
|
|
public function canUnpublish($member = null) |
150
|
|
|
{ |
151
|
|
|
return $this->canDelete($member); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Fetches a value for $this->Value. If empty values are not allowed, |
156
|
|
|
* then this will return the title in the case of an empty value. |
157
|
|
|
* |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
17 |
|
public function getValue() |
161
|
|
|
{ |
162
|
17 |
|
$value = $this->getField('Value'); |
163
|
17 |
|
if (empty($value) && !self::allow_empty_values()) { |
164
|
1 |
|
return $this->Title; |
|
|
|
|
165
|
|
|
} |
166
|
17 |
|
return $value; |
167
|
|
|
} |
168
|
|
|
|
169
|
39 |
|
protected function onBeforeWrite() |
170
|
|
|
{ |
171
|
39 |
|
if (!$this->Sort) { |
|
|
|
|
172
|
39 |
|
$this->Sort = EditableOption::get()->max('Sort') + 1; |
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
39 |
|
parent::onBeforeWrite(); |
176
|
39 |
|
} |
177
|
|
|
} |
178
|
|
|
|
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.