1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\UserForms\Model\EditableFormField; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\CheckboxField; |
6
|
|
|
use SilverStripe\Forms\TextField; |
7
|
|
|
use SilverStripe\Forms\GridField\GridField; |
8
|
|
|
use SilverStripe\Forms\GridField\GridFieldButtonRow; |
9
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig; |
10
|
|
|
use SilverStripe\Forms\GridField\GridFieldDeleteAction; |
11
|
|
|
use SilverStripe\Forms\GridField\GridFieldToolbarHeader; |
12
|
|
|
use SilverStripe\Forms\Tab; |
13
|
|
|
use SilverStripe\ORM\Map; |
14
|
|
|
use SilverStripe\UserForms\Model\EditableFormField; |
15
|
|
|
use SilverStripe\Versioned\Versioned; |
16
|
|
|
use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton; |
17
|
|
|
use Symbiote\GridFieldExtensions\GridFieldEditableColumns; |
18
|
|
|
use Symbiote\GridFieldExtensions\GridFieldOrderableRows; |
19
|
|
|
use Symbiote\GridFieldExtensions\GridFieldTitleHeader; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Base class for multiple option fields such as {@link EditableDropdownField} |
23
|
|
|
* and radio sets. |
24
|
|
|
* |
25
|
|
|
* Implemented as a class but should be viewed as abstract, you should |
26
|
|
|
* instantiate a subclass such as {@link EditableDropdownField} |
27
|
|
|
* |
28
|
|
|
* @see EditableCheckboxGroupField |
29
|
|
|
* @see EditableDropdownField |
30
|
|
|
* |
31
|
|
|
* @package userforms |
32
|
|
|
*/ |
33
|
|
|
|
34
|
|
|
class EditableMultipleOptionField extends EditableFormField |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* Define this field as abstract (not inherited) |
38
|
|
|
* |
39
|
|
|
* @config |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
private static $abstract = true; |
|
|
|
|
43
|
|
|
|
44
|
|
|
private static $has_many = [ |
|
|
|
|
45
|
|
|
'Options' => EditableOption::class, |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
private static $owns = [ |
|
|
|
|
49
|
|
|
'Options', |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
private static $cascade_deletes = [ |
|
|
|
|
53
|
|
|
'Options', |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
private static $table_name = 'EditableMultipleOptionField'; |
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return \SilverStripe\Forms\FieldList |
60
|
|
|
*/ |
61
|
|
|
public function getCMSFields() |
62
|
|
|
{ |
63
|
|
|
$this->beforeUpdateCMSFields(function ($fields) { |
64
|
|
|
$editableColumns = new GridFieldEditableColumns(); |
65
|
|
|
$editableColumns->setDisplayFields([ |
66
|
|
|
'Title' => [ |
67
|
|
|
'title' => _t(__CLASS__.'.TITLE', 'Title'), |
68
|
|
|
'callback' => function ($record, $column, $grid) { |
|
|
|
|
69
|
|
|
return TextField::create($column); |
70
|
|
|
} |
71
|
|
|
], |
72
|
|
|
'Value' => [ |
73
|
|
|
'title' => _t(__CLASS__.'.VALUE', 'Value'), |
74
|
|
|
'callback' => function ($record, $column, $grid) { |
|
|
|
|
75
|
|
|
return TextField::create($column); |
76
|
|
|
} |
77
|
|
|
], |
78
|
|
|
'Default' => [ |
79
|
|
|
'title' => _t(__CLASS__.'.DEFAULT', 'Selected by default?'), |
80
|
|
|
'callback' => function ($record, $column, $grid) { |
|
|
|
|
81
|
|
|
return CheckboxField::create($column); |
82
|
|
|
} |
83
|
|
|
] |
84
|
|
|
]); |
85
|
|
|
|
86
|
|
|
$optionsConfig = GridFieldConfig::create() |
87
|
|
|
->addComponents( |
88
|
|
|
new GridFieldToolbarHeader(), |
89
|
|
|
new GridFieldTitleHeader(), |
90
|
|
|
new GridFieldOrderableRows('Sort'), |
91
|
|
|
$editableColumns, |
92
|
|
|
new GridFieldButtonRow(), |
93
|
|
|
new GridFieldAddNewInlineButton(), |
94
|
|
|
new GridFieldDeleteAction() |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
$optionsGrid = GridField::create( |
98
|
|
|
'Options', |
99
|
|
|
_t('SilverStripe\\UserForms\\Model\\EditableFormField.CUSTOMOPTIONS', 'Options'), |
100
|
|
|
$this->Options(), |
|
|
|
|
101
|
|
|
$optionsConfig |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
$fields->insertAfter(Tab::create('Options', _t(__CLASS__.'.OPTIONSTAB', 'Options')), 'Main'); |
105
|
|
|
$fields->addFieldToTab('Root.Options', $optionsGrid); |
106
|
|
|
}); |
107
|
|
|
|
108
|
|
|
$fields = parent::getCMSFields(); |
109
|
|
|
|
110
|
|
|
return $fields; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Duplicate a pages content. We need to make sure all the fields attached |
115
|
|
|
* to that page go with it |
116
|
|
|
* |
117
|
|
|
* {@inheritDoc} |
118
|
|
|
*/ |
119
|
|
|
public function duplicate($doWrite = true, $manyMany = 'many_many') |
120
|
|
|
{ |
121
|
|
|
// Versioned 1.0 has a bug where [] will result in _all_ relations being duplicated |
122
|
|
|
if ($manyMany === 'many_many' && !$this->manyMany()) { |
123
|
|
|
$manyMany = null; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$clonedNode = parent::duplicate(true, $manyMany); |
127
|
|
|
|
128
|
|
|
foreach ($this->Options() as $field) { |
129
|
|
|
/** @var EditableOption $newField */ |
130
|
|
|
$newField = $field->duplicate(false); |
131
|
|
|
$newField->ParentID = $clonedNode->ID; |
|
|
|
|
132
|
|
|
$newField->Version = 0; |
|
|
|
|
133
|
|
|
$newField->write(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $clonedNode; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Return whether or not this field has addable options such as a |
141
|
|
|
* {@link EditableDropdown} or {@link EditableRadioField} |
142
|
|
|
* |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
|
|
public function getHasAddableOptions() |
146
|
|
|
{ |
147
|
|
|
return true; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Gets map of field options suitable for use in a form |
152
|
|
|
* |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
protected function getOptionsMap() |
156
|
|
|
{ |
157
|
|
|
$optionSet = $this->Options(); |
158
|
|
|
$optionMap = $optionSet->map('Value', 'Title'); |
159
|
|
|
if ($optionMap instanceof Map) { |
160
|
|
|
return $optionMap->toArray(); |
161
|
|
|
} |
162
|
|
|
return $optionMap; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Returns all default options |
167
|
|
|
* |
168
|
|
|
* @return \SilverStripe\ORM\SS_List |
169
|
|
|
*/ |
170
|
|
|
protected function getDefaultOptions() |
171
|
|
|
{ |
172
|
|
|
return $this->Options()->filter('Default', 1); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|