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