|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Base class for multiple option fields such as {@link EditableDropdownField} |
|
5
|
|
|
* and radio sets. |
|
6
|
|
|
* |
|
7
|
|
|
* Implemented as a class but should be viewed as abstract, you should |
|
8
|
|
|
* instantiate a subclass such as {@link EditableDropdownField} |
|
9
|
|
|
* |
|
10
|
|
|
* @see EditableCheckboxGroupField |
|
11
|
|
|
* @see EditableDropdownField |
|
12
|
|
|
* |
|
13
|
|
|
* @package userforms |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
class EditableMultipleOptionField extends EditableFormField { |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Define this field as abstract (not inherited) |
|
20
|
|
|
* |
|
21
|
|
|
* @config |
|
22
|
|
|
* @var bool |
|
23
|
|
|
*/ |
|
24
|
|
|
private static $abstract = true; |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
private static $has_many = array( |
|
|
|
|
|
|
27
|
|
|
"Options" => "EditableOption" |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return FieldList |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getCMSFields() { |
|
34
|
|
|
$fields = parent::getCMSFields(); |
|
35
|
|
|
|
|
36
|
|
|
$editableColumns = new GridFieldEditableColumns(); |
|
37
|
|
|
$editableColumns->setDisplayFields(array( |
|
38
|
|
|
'Title' => array( |
|
39
|
|
|
'title' => _t('EditableMultipleOptionField.TITLE', 'Title'), |
|
40
|
|
|
'callback' => function($record, $column, $grid) { |
|
|
|
|
|
|
41
|
|
|
return TextField::create($column); |
|
42
|
|
|
} |
|
43
|
|
|
), |
|
44
|
|
|
'Value' => array( |
|
45
|
|
|
'title' => _t('EditableMultipleOptionField.VALUE', 'Value'), |
|
46
|
|
|
'callback' => function($record, $column, $grid) { |
|
|
|
|
|
|
47
|
|
|
return TextField::create($column); |
|
48
|
|
|
} |
|
49
|
|
|
), |
|
50
|
|
|
'Default' => array( |
|
51
|
|
|
'title' => _t('EditableMultipleOptionField.DEFAULT', 'Selected by default?'), |
|
52
|
|
|
'callback' => function($record, $column, $grid) { |
|
|
|
|
|
|
53
|
|
|
return CheckboxField::create($column); |
|
54
|
|
|
} |
|
55
|
|
|
) |
|
56
|
|
|
)); |
|
57
|
|
|
|
|
58
|
|
|
$optionsConfig = GridFieldConfig::create() |
|
59
|
|
|
->addComponents( |
|
60
|
|
|
new GridFieldToolbarHeader(), |
|
61
|
|
|
new GridFieldTitleHeader(), |
|
62
|
|
|
new GridFieldOrderableRows('Sort'), |
|
63
|
|
|
$editableColumns, |
|
64
|
|
|
new GridFieldButtonRow(), |
|
65
|
|
|
new GridFieldAddNewInlineButton(), |
|
66
|
|
|
new GridFieldDeleteAction() |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
$optionsGrid = GridField::create( |
|
70
|
|
|
'Options', |
|
71
|
|
|
_t('EditableFormField.CUSTOMOPTIONS', 'Options'), |
|
72
|
|
|
$this->Options(), |
|
|
|
|
|
|
73
|
|
|
$optionsConfig |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
$fields->insertAfter(new Tab('Options', _t('EditableMultipleOptionField.OPTIONSTAB','Options')), 'Main'); |
|
|
|
|
|
|
77
|
|
|
$fields->addFieldToTab('Root.Options', $optionsGrid); |
|
78
|
|
|
|
|
79
|
|
|
return $fields; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Publishing Versioning support. |
|
84
|
|
|
* |
|
85
|
|
|
* When publishing it needs to handle copying across / publishing |
|
86
|
|
|
* each of the individual field options |
|
87
|
|
|
* |
|
88
|
|
|
* @return void |
|
89
|
|
|
*/ |
|
90
|
|
|
public function doPublish($fromStage, $toStage, $createNewVersion = false) { |
|
91
|
|
|
$live = Versioned::get_by_stage("EditableOption", "Live", "\"EditableOption\".\"ParentID\" = $this->ID"); |
|
92
|
|
|
|
|
93
|
|
|
if($live) { |
|
94
|
|
|
foreach($live as $option) { |
|
95
|
|
|
$option->delete(); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if($this->Options()) { |
|
|
|
|
|
|
100
|
|
|
foreach($this->Options() as $option) { |
|
|
|
|
|
|
101
|
|
|
$option->publish($fromStage, $toStage, $createNewVersion); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
parent::doPublish($fromStage, $toStage, $createNewVersion); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Unpublishing Versioning support |
|
110
|
|
|
* |
|
111
|
|
|
* When unpublishing the field it has to remove all options attached |
|
112
|
|
|
* |
|
113
|
|
|
* @return void |
|
114
|
|
|
*/ |
|
115
|
|
|
public function doDeleteFromStage($stage) { |
|
116
|
|
|
// Remove options |
|
117
|
|
|
$options = Versioned::get_by_stage('EditableOption', $stage) |
|
118
|
|
|
->filter('ParentID', $this->ID); |
|
119
|
|
|
foreach($options as $option) { |
|
120
|
|
|
$option->deleteFromStage($stage); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
parent::doDeleteFromStage($stage); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Deletes all the options attached to this field before deleting the |
|
128
|
|
|
* field. Keeps stray options from floating around |
|
129
|
|
|
* |
|
130
|
|
|
* @return void |
|
131
|
|
|
*/ |
|
132
|
|
|
public function delete() { |
|
133
|
|
|
$options = $this->Options(); |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
if($options) { |
|
136
|
|
|
foreach($options as $option) { |
|
137
|
|
|
$option->delete(); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
parent::delete(); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Duplicate a pages content. We need to make sure all the fields attached |
|
146
|
|
|
* to that page go with it |
|
147
|
|
|
* |
|
148
|
|
|
* @return DataObject |
|
149
|
|
|
*/ |
|
150
|
|
|
public function duplicate($doWrite = true) { |
|
151
|
|
|
$clonedNode = parent::duplicate(); |
|
152
|
|
|
|
|
153
|
|
View Code Duplication |
foreach($this->Options() as $field) { |
|
|
|
|
|
|
154
|
|
|
$newField = $field->duplicate(false); |
|
155
|
|
|
$newField->ParentID = $clonedNode->ID; |
|
156
|
|
|
$newField->Version = 0; |
|
157
|
|
|
$newField->write(); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return $clonedNode; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Return whether or not this field has addable options such as a |
|
165
|
|
|
* {@link EditableDropdownField} or {@link EditableRadioField} |
|
166
|
|
|
* |
|
167
|
|
|
* @return bool |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getHasAddableOptions() { |
|
170
|
|
|
return true; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Gets map of field options suitable for use in a form |
|
175
|
|
|
* |
|
176
|
|
|
* @return array |
|
177
|
|
|
*/ |
|
178
|
|
|
protected function getOptionsMap() { |
|
179
|
|
|
$optionSet = $this->Options(); |
|
|
|
|
|
|
180
|
|
|
$optionMap = $optionSet->map('Value', 'Title'); |
|
181
|
|
|
if($optionMap instanceof SS_Map) { |
|
182
|
|
|
return $optionMap->toArray(); |
|
183
|
|
|
} |
|
184
|
|
|
return $optionMap; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Returns all default options |
|
189
|
|
|
* |
|
190
|
|
|
* @return SS_List |
|
191
|
|
|
*/ |
|
192
|
|
|
protected function getDefaultOptions() { |
|
193
|
|
|
return $this->Options()->filter('Default', 1); |
|
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
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.