1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\AssetAdmin\Forms; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use SilverStripe\Assets\File; |
7
|
|
|
use SilverStripe\Control\Controller; |
8
|
|
|
use SilverStripe\Core\Config\Configurable; |
9
|
|
|
use SilverStripe\Core\Extensible; |
10
|
|
|
use SilverStripe\Core\Injector\Injectable; |
11
|
|
|
use SilverStripe\Forms\FieldList; |
12
|
|
|
use SilverStripe\Forms\Form; |
13
|
|
|
use SilverStripe\Forms\FormAction; |
14
|
|
|
use SilverStripe\Forms\FormFactory; |
15
|
|
|
use SilverStripe\Forms\HeaderField; |
16
|
|
|
use SilverStripe\Forms\HiddenField; |
17
|
|
|
use SilverStripe\Forms\PopoverField; |
18
|
|
|
use SilverStripe\Forms\RequiredFields; |
19
|
|
|
use SilverStripe\Forms\Tab; |
20
|
|
|
use SilverStripe\Forms\TabSet; |
21
|
|
|
use SilverStripe\Forms\TextField; |
22
|
|
|
|
23
|
|
|
abstract class AssetFormFactory implements FormFactory |
24
|
|
|
{ |
25
|
|
|
use Extensible; |
26
|
|
|
use Injectable; |
27
|
|
|
use Configurable; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Insert into HTML content area |
31
|
|
|
*/ |
32
|
|
|
const TYPE_INSERT = 'insert'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Select file by ID only |
36
|
|
|
*/ |
37
|
|
|
const TYPE_SELECT = 'select'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Edit form: Default |
41
|
|
|
*/ |
42
|
|
|
const TYPE_ADMIN = 'admin'; |
43
|
|
|
|
44
|
|
|
public function __construct() |
45
|
|
|
{ |
46
|
|
|
$this->constructExtensions(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getForm(Controller $controller, $name = FormFactory::DEFAULT_NAME, $context = []) |
50
|
|
|
{ |
51
|
|
|
// Validate context |
52
|
|
|
foreach ($this->getRequiredContext() as $required) { |
53
|
|
|
if (!isset($context[$required])) { |
54
|
|
|
throw new InvalidArgumentException("Missing required context $required"); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$fields = $this->getFormFields($controller, $name, $context); |
59
|
|
|
$actions = $this->getFormActions($controller, $name, $context); |
60
|
|
|
$validator = $this->getValidator($controller, $name, $context); |
61
|
|
|
$form = Form::create($controller, $name, $fields, $actions, $validator); |
62
|
|
|
|
63
|
|
|
// Extend form |
64
|
|
|
$this->invokeWithExtensions('updateForm', $form, $controller, $name, $context); |
65
|
|
|
|
66
|
|
|
// Populate form from record |
67
|
|
|
if (isset($context['Record'])) { |
68
|
|
|
$form->loadDataFrom($context['Record']); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $form; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the validator for the form to be built |
76
|
|
|
* |
77
|
|
|
* @param $controller |
78
|
|
|
* @param $formName |
79
|
|
|
* @param $context |
80
|
|
|
* @return RequiredFields |
81
|
|
|
*/ |
82
|
|
|
protected function getValidator(Controller $controller, $formName, $context = []) { |
|
|
|
|
83
|
|
|
$validator = new RequiredFields('Name'); |
84
|
|
|
|
85
|
|
|
return $validator; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get form type from 'type' context |
90
|
|
|
* |
91
|
|
|
* @param array $context |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
protected function getFormType($context) |
95
|
|
|
{ |
96
|
|
|
return empty($context['Type']) ? static::TYPE_ADMIN : $context['Type']; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Gets the main tabs for the file edit form |
101
|
|
|
* |
102
|
|
|
* @param File $record |
103
|
|
|
* @return TabSet |
104
|
|
|
*/ |
105
|
|
|
protected function getFormFieldTabs($record, $context = []) |
106
|
|
|
{ |
107
|
|
|
$tabs = TabSet::create('Editor', $this->getFormFieldDetailsTab($record)); |
108
|
|
|
|
109
|
|
|
return $tabs; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param File $record |
114
|
|
|
* @return FormAction |
115
|
|
|
*/ |
116
|
|
View Code Duplication |
protected function getSaveAction($record) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
if ($record && $record->isInDB() && $record->canEdit()) { |
119
|
|
|
return FormAction::create('save', _t('CMSMain.SAVE', 'Save')) |
120
|
|
|
->setIcon('save'); |
121
|
|
|
} |
122
|
|
|
return null; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get delete action, if this record is deletable |
127
|
|
|
* |
128
|
|
|
* @param File $record |
129
|
|
|
* @return FormAction |
130
|
|
|
*/ |
131
|
|
View Code Duplication |
protected function getDeleteAction($record) |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
// Delete action |
134
|
|
|
if ($record && $record->isInDB() && $record->canDelete()) { |
135
|
|
|
$deleteText = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.DELETE_BUTTON', 'Delete'); |
136
|
|
|
return FormAction::create('delete', $deleteText) |
137
|
|
|
->setIcon('trash-bin'); |
138
|
|
|
} |
139
|
|
|
return null; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param Controller $controller |
144
|
|
|
* @param $formName |
145
|
|
|
* @param array $context |
146
|
|
|
* @return FieldList |
147
|
|
|
*/ |
148
|
|
|
protected function getFormActions(Controller $controller, $formName, $context = []) |
149
|
|
|
{ |
150
|
|
|
$record = isset($context['Record']) ? $context['Record'] : null; |
151
|
|
|
|
152
|
|
|
$actions = new FieldList(); |
153
|
|
|
if ($saveAction = $this->getSaveAction($record)) { |
154
|
|
|
$actions->push($saveAction); |
155
|
|
|
} |
156
|
|
|
$menu = $this->getPopoverMenu($record); |
157
|
|
|
if ($menu && $menu->FieldList()->count()) { |
158
|
|
|
$actions->push($menu); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$this->invokeWithExtensions('updateFormActions', $actions, $controller, $formName, $context); |
162
|
|
|
return $actions; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get fields for this form |
167
|
|
|
* |
168
|
|
|
* @param Controller $controller |
169
|
|
|
* @param string $formName |
170
|
|
|
* @param array $context |
171
|
|
|
* @return FieldList |
172
|
|
|
*/ |
173
|
|
|
protected function getFormFields(Controller $controller, $formName, $context = []) |
174
|
|
|
{ |
175
|
|
|
$record = isset($context['Record']) ? $context['Record'] : null; |
176
|
|
|
|
177
|
|
|
// Build standard fields for all folders / files |
178
|
|
|
/** @var File $record */ |
179
|
|
|
$fields = new FieldList( |
180
|
|
|
HeaderField::create('TitleHeader', $record ? $record->Title : null, 1) |
181
|
|
|
->addExtraClass('editor__heading'), |
182
|
|
|
$this->getFormFieldTabs($record, $context) |
183
|
|
|
); |
184
|
|
|
if ($record) { |
185
|
|
|
$fields->push(HiddenField::create('ID', $record->ID)); |
186
|
|
|
$fields->insertAfter( |
187
|
|
|
'TitleHeader', |
188
|
|
|
PreviewImageField::create('PreviewImage') |
189
|
|
|
->setRecordID($record->ID) |
190
|
|
|
->addExtraClass('editor__file-preview') |
191
|
|
|
); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$this->invokeWithExtensions('updateFormFields', $fields, $controller, $formName, $context); |
195
|
|
|
return $fields; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Build popup menu |
200
|
|
|
* |
201
|
|
|
* @param File $record |
202
|
|
|
* @return PopoverField |
203
|
|
|
*/ |
204
|
|
|
protected function getPopoverMenu($record) |
205
|
|
|
{ |
206
|
|
|
// Build popover actions |
207
|
|
|
$popoverActions = $this->getPopoverActions($record); |
208
|
|
|
if ($popoverActions) { |
|
|
|
|
209
|
|
|
return PopoverField::create($popoverActions) |
210
|
|
|
->setPlacement('top') |
211
|
|
|
->setButtonTooltip(_t( |
212
|
|
|
'SilverStripe\\AssetAdmin\\Forms\\FileFormFactory.OTHER_ACTIONS', |
213
|
|
|
'Other actions' |
214
|
|
|
)); |
215
|
|
|
} |
216
|
|
|
return null; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Get actions that go into the Popover menu |
221
|
|
|
* |
222
|
|
|
* @param $record |
223
|
|
|
* @return array |
224
|
|
|
*/ |
225
|
|
|
protected function getPopoverActions($record) |
226
|
|
|
{ |
227
|
|
|
return array_filter([ |
228
|
|
|
$this->getDeleteAction($record) |
229
|
|
|
]); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Build "details" formfield tab |
234
|
|
|
* |
235
|
|
|
* @param File $record |
236
|
|
|
* @param array $context |
237
|
|
|
* @return Tab |
238
|
|
|
*/ |
239
|
|
|
protected function getFormFieldDetailsTab($record, $context = []) |
240
|
|
|
{ |
241
|
|
|
return Tab::create( |
242
|
|
|
'Details', |
243
|
|
|
TextField::create('Name', File::singleton()->fieldLabel('Filename')) |
244
|
|
|
); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function getRequiredContext() |
248
|
|
|
{ |
249
|
|
|
return ['Record']; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.