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\Convert; |
10
|
|
|
use SilverStripe\Core\Extensible; |
11
|
|
|
use SilverStripe\Core\Injector\Injectable; |
12
|
|
|
use SilverStripe\Forms\FieldList; |
13
|
|
|
use SilverStripe\Forms\Form; |
14
|
|
|
use SilverStripe\Forms\FormAction; |
15
|
|
|
use SilverStripe\Forms\FormFactory; |
16
|
|
|
use SilverStripe\Forms\HeaderField; |
17
|
|
|
use SilverStripe\Forms\HiddenField; |
18
|
|
|
use SilverStripe\Forms\LiteralField; |
19
|
|
|
use SilverStripe\Forms\RequiredFields; |
20
|
|
|
use SilverStripe\Forms\Tab; |
21
|
|
|
use SilverStripe\Forms\TabSet; |
22
|
|
|
use SilverStripe\Forms\TextField; |
23
|
|
|
|
24
|
|
|
abstract class AssetFormFactory implements FormFactory |
25
|
|
|
{ |
26
|
|
|
use Extensible; |
27
|
|
|
use Injectable; |
28
|
|
|
use Configurable; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Insert into HTML content area |
32
|
|
|
*/ |
33
|
|
|
const TYPE_INSERT = 'insert'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Select file by ID only |
37
|
|
|
*/ |
38
|
|
|
const TYPE_SELECT = 'select'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Edit form: Default |
42
|
|
|
*/ |
43
|
|
|
const TYPE_ADMIN = 'admin'; |
44
|
|
|
|
45
|
|
|
public function __construct() |
46
|
|
|
{ |
47
|
|
|
$this->constructExtensions(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getForm(Controller $controller, $name = FormFactory::DEFAULT_NAME, $context = []) |
51
|
|
|
{ |
52
|
|
|
// Validate context |
53
|
|
|
foreach ($this->getRequiredContext() as $required) { |
54
|
|
|
if (!isset($context[$required])) { |
55
|
|
|
throw new InvalidArgumentException("Missing required context $required"); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$fields = $this->getFormFields($controller, $name, $context); |
60
|
|
|
$actions = $this->getFormActions($controller, $name, $context); |
61
|
|
|
$validator = new RequiredFields('Name'); |
62
|
|
|
$form = Form::create($controller, $name, $fields, $actions, $validator); |
63
|
|
|
|
64
|
|
|
// Extend form |
65
|
|
|
$this->invokeWithExtensions('updateForm', $form, $controller, $name, $context); |
66
|
|
|
|
67
|
|
|
// Populate form from record |
68
|
|
|
$form->loadDataFrom($context['Record']); |
69
|
|
|
|
70
|
|
|
return $form; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get form type from 'type' context |
75
|
|
|
* |
76
|
|
|
* @param array $context |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
protected function getFormType($context) |
80
|
|
|
{ |
81
|
|
|
return empty($context['Type']) ? static::TYPE_ADMIN : $context['Type']; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Gets the main tabs for the file edit form |
86
|
|
|
* |
87
|
|
|
* @param File $record |
88
|
|
|
* @return TabSet |
89
|
|
|
*/ |
90
|
|
|
protected function getFormFieldTabs($record, $context = []) |
91
|
|
|
{ |
92
|
|
|
$tabs = TabSet::create('Editor', $this->getFormFieldDetailsTab($record)); |
93
|
|
|
|
94
|
|
|
return $tabs; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param File $record |
99
|
|
|
* @return FormAction |
100
|
|
|
*/ |
101
|
|
View Code Duplication |
protected function getSaveAction($record) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
if ($record && $record->isInDB() && $record->canEdit()) { |
104
|
|
|
return FormAction::create('save', _t('CMSMain.SAVE', 'Save')) |
105
|
|
|
->setIcon('save'); |
106
|
|
|
} |
107
|
|
|
return null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get delete action, if this record is deletable |
112
|
|
|
* |
113
|
|
|
* @param File $record |
114
|
|
|
* @return FormAction |
115
|
|
|
*/ |
116
|
|
View Code Duplication |
protected function getDeleteAction($record) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
// Delete action |
119
|
|
|
if ($record && $record->isInDB() && $record->canDelete()) { |
120
|
|
|
$deleteText = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.DELETE_BUTTON', 'Delete'); |
121
|
|
|
return FormAction::create('delete', $deleteText) |
122
|
|
|
->setIcon('trash-bin'); |
123
|
|
|
} |
124
|
|
|
return null; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
protected function getFormActions(Controller $controller, $name, $context = []) |
128
|
|
|
{ |
129
|
|
|
$record = $context['Record']; |
130
|
|
|
|
131
|
|
|
$actions = new FieldList(); |
132
|
|
|
if ($saveAction = $this->getSaveAction($record)) { |
133
|
|
|
$actions->push($saveAction); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$this->invokeWithExtensions('updateFormActions', $actions, $controller, $name, $context); |
137
|
|
|
return $actions; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
protected function getFormFields(Controller $controller, $name, $context = []) |
141
|
|
|
{ |
142
|
|
|
$record = $context['Record']; |
143
|
|
|
|
144
|
|
|
// Build standard fields for all folders / files |
145
|
|
|
/** @var File $record */ |
146
|
|
|
$fields = new FieldList( |
147
|
|
|
HeaderField::create('TitleHeader', $record ? $record->Title : null, 1) |
148
|
|
|
->addExtraClass('editor__heading'), |
149
|
|
|
PreviewImageField::create('PreviewImage') |
150
|
|
|
->setRecordID($record->ID) |
151
|
|
|
->addExtraClass('editor__file-preview'), |
152
|
|
|
$this->getFormFieldTabs($record, $context) |
153
|
|
|
); |
154
|
|
|
if ($record) { |
155
|
|
|
$fields->push(HiddenField::create('ID', $record->ID)); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$this->invokeWithExtensions('updateFormFields', $fields, $controller, $name, $context); |
159
|
|
|
return $fields; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Build "details" formfield tab |
164
|
|
|
* |
165
|
|
|
* @param File $record |
166
|
|
|
* @param array $context |
167
|
|
|
* @return Tab |
168
|
|
|
*/ |
169
|
|
|
protected function getFormFieldDetailsTab($record, $context = []) |
170
|
|
|
{ |
171
|
|
|
return Tab::create( |
172
|
|
|
'Details', |
173
|
|
|
TextField::create('Name', File::singleton()->fieldLabel('Filename')) |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function getRequiredContext() |
178
|
|
|
{ |
179
|
|
|
return ['Record']; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.