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