|
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
|
|
|
{ |
|
84
|
|
|
$validator = new RequiredFields('Name'); |
|
85
|
|
|
|
|
86
|
|
|
return $validator; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get form type from 'type' context |
|
91
|
|
|
* |
|
92
|
|
|
* @param array $context |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function getFormType($context) |
|
96
|
|
|
{ |
|
97
|
|
|
return empty($context['Type']) ? static::TYPE_ADMIN : $context['Type']; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Gets the main tabs for the file edit form |
|
102
|
|
|
* |
|
103
|
|
|
* @param File $record |
|
104
|
|
|
* @return TabSet |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function getFormFieldTabs($record, $context = []) |
|
107
|
|
|
{ |
|
108
|
|
|
$tabs = TabSet::create('Editor', $this->getFormFieldDetailsTab($record)); |
|
109
|
|
|
|
|
110
|
|
|
return $tabs; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param File $record |
|
115
|
|
|
* @return FormAction |
|
116
|
|
|
*/ |
|
117
|
|
View Code Duplication |
protected function getSaveAction($record) |
|
|
|
|
|
|
118
|
|
|
{ |
|
119
|
|
|
if ($record && $record->isInDB() && $record->canEdit()) { |
|
120
|
|
|
return FormAction::create('save', _t('CMSMain.SAVE', 'Save')) |
|
121
|
|
|
->setIcon('save'); |
|
122
|
|
|
} |
|
123
|
|
|
return null; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Get delete action, if this record is deletable |
|
128
|
|
|
* |
|
129
|
|
|
* @param File $record |
|
130
|
|
|
* @return FormAction |
|
131
|
|
|
*/ |
|
132
|
|
View Code Duplication |
protected function getDeleteAction($record) |
|
|
|
|
|
|
133
|
|
|
{ |
|
134
|
|
|
// Delete action |
|
135
|
|
|
if ($record && $record->isInDB() && $record->canDelete()) { |
|
136
|
|
|
$deleteText = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.DELETE_BUTTON', 'Delete'); |
|
137
|
|
|
return FormAction::create('delete', $deleteText) |
|
138
|
|
|
->setIcon('trash-bin'); |
|
139
|
|
|
} |
|
140
|
|
|
return null; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param Controller $controller |
|
145
|
|
|
* @param $formName |
|
146
|
|
|
* @param array $context |
|
147
|
|
|
* @return FieldList |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function getFormActions(Controller $controller, $formName, $context = []) |
|
150
|
|
|
{ |
|
151
|
|
|
$record = isset($context['Record']) ? $context['Record'] : null; |
|
152
|
|
|
|
|
153
|
|
|
$actions = new FieldList(); |
|
154
|
|
|
if ($saveAction = $this->getSaveAction($record)) { |
|
155
|
|
|
$actions->push($saveAction); |
|
156
|
|
|
} |
|
157
|
|
|
$menu = $this->getPopoverMenu($record); |
|
158
|
|
|
if ($menu && $menu->FieldList()->count()) { |
|
159
|
|
|
$actions->push($menu); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$this->invokeWithExtensions('updateFormActions', $actions, $controller, $formName, $context); |
|
163
|
|
|
return $actions; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Get fields for this form |
|
168
|
|
|
* |
|
169
|
|
|
* @param Controller $controller |
|
170
|
|
|
* @param string $formName |
|
171
|
|
|
* @param array $context |
|
172
|
|
|
* @return FieldList |
|
173
|
|
|
*/ |
|
174
|
|
|
protected function getFormFields(Controller $controller, $formName, $context = []) |
|
175
|
|
|
{ |
|
176
|
|
|
$record = isset($context['Record']) ? $context['Record'] : null; |
|
177
|
|
|
|
|
178
|
|
|
// Build standard fields for all folders / files |
|
179
|
|
|
/** @var File $record */ |
|
180
|
|
|
$fields = new FieldList( |
|
181
|
|
|
HeaderField::create('TitleHeader', $record ? $record->Title : null, 1) |
|
182
|
|
|
->addExtraClass('editor__heading'), |
|
183
|
|
|
$this->getFormFieldTabs($record, $context) |
|
184
|
|
|
); |
|
185
|
|
|
if ($record) { |
|
186
|
|
|
$fields->push(HiddenField::create('ID', $record->ID)); |
|
187
|
|
|
$fields->insertAfter( |
|
188
|
|
|
'TitleHeader', |
|
189
|
|
|
PreviewImageField::create('PreviewImage') |
|
190
|
|
|
->setRecordID($record->ID) |
|
191
|
|
|
->addExtraClass('editor__file-preview') |
|
192
|
|
|
); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
$this->invokeWithExtensions('updateFormFields', $fields, $controller, $formName, $context); |
|
196
|
|
|
return $fields; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Build popup menu |
|
201
|
|
|
* |
|
202
|
|
|
* @param File $record |
|
203
|
|
|
* @return PopoverField |
|
204
|
|
|
*/ |
|
205
|
|
|
protected function getPopoverMenu($record) |
|
206
|
|
|
{ |
|
207
|
|
|
// Build popover actions |
|
208
|
|
|
$popoverActions = $this->getPopoverActions($record); |
|
209
|
|
|
if ($popoverActions) { |
|
|
|
|
|
|
210
|
|
|
return PopoverField::create($popoverActions) |
|
211
|
|
|
->setPlacement('top') |
|
212
|
|
|
->setButtonTooltip(_t( |
|
213
|
|
|
'SilverStripe\\AssetAdmin\\Forms\\FileFormFactory.OTHER_ACTIONS', |
|
214
|
|
|
'Other actions' |
|
215
|
|
|
)); |
|
216
|
|
|
} |
|
217
|
|
|
return null; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Get actions that go into the Popover menu |
|
222
|
|
|
* |
|
223
|
|
|
* @param $record |
|
224
|
|
|
* @return array |
|
225
|
|
|
*/ |
|
226
|
|
|
protected function getPopoverActions($record) |
|
227
|
|
|
{ |
|
228
|
|
|
return array_filter([ |
|
229
|
|
|
$this->getDeleteAction($record) |
|
230
|
|
|
]); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Build "details" formfield tab |
|
235
|
|
|
* |
|
236
|
|
|
* @param File $record |
|
237
|
|
|
* @param array $context |
|
238
|
|
|
* @return Tab |
|
239
|
|
|
*/ |
|
240
|
|
|
protected function getFormFieldDetailsTab($record, $context = []) |
|
241
|
|
|
{ |
|
242
|
|
|
return Tab::create( |
|
243
|
|
|
'Details', |
|
244
|
|
|
TextField::create('Name', File::singleton()->fieldLabel('Filename')) |
|
245
|
|
|
); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
public function getRequiredContext() |
|
249
|
|
|
{ |
|
250
|
|
|
return ['Record']; |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.