1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\AssetAdmin\Forms; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Assets\File; |
6
|
|
|
use SilverStripe\Control\Controller; |
7
|
|
|
use SilverStripe\Core\Convert; |
8
|
|
|
use SilverStripe\Forms\DatetimeField; |
9
|
|
|
use SilverStripe\Forms\FieldList; |
10
|
|
|
use SilverStripe\Forms\FormAction; |
11
|
|
|
use SilverStripe\Forms\HTMLReadonlyField; |
12
|
|
|
use SilverStripe\Forms\LiteralField; |
13
|
|
|
use SilverStripe\Forms\PopoverField; |
14
|
|
|
use SilverStripe\Forms\ReadonlyField; |
15
|
|
|
use SilverStripe\Forms\Tab; |
16
|
|
|
use SilverStripe\Forms\TabSet; |
17
|
|
|
use SilverStripe\Forms\TextField; |
18
|
|
|
|
19
|
|
|
class FileFormFactory extends AssetFormFactory |
20
|
|
|
{ |
21
|
|
|
protected function getFormFieldTabs($record) |
22
|
|
|
{ |
23
|
|
|
// Add extra tab |
24
|
|
|
$tabs = TabSet::create( |
25
|
|
|
'Editor', |
26
|
|
|
$this->getFormFieldDetailsTab($record), |
27
|
|
|
$this->getFormFieldUsageTab($record) |
28
|
|
|
); |
29
|
|
|
return $tabs; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Build "Usage" tab |
34
|
|
|
* |
35
|
|
|
* @param File $record |
36
|
|
|
* @return Tab |
37
|
|
|
*/ |
38
|
|
|
protected function getFormFieldUsageTab($record) |
39
|
|
|
{ |
40
|
|
|
// Add new tab for usage |
41
|
|
|
return Tab::create( |
42
|
|
|
'Usage', |
43
|
|
|
DatetimeField::create("Created", _t('AssetTableField.CREATED', 'First uploaded')) |
44
|
|
|
->setReadonly(true), |
45
|
|
|
DatetimeField::create("LastEdited", _t('AssetTableField.LASTEDIT', 'Last changed')) |
46
|
|
|
->setReadonly(true) |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function getFormFieldDetailsTab($record) |
51
|
|
|
{ |
52
|
|
|
// Update details tab |
53
|
|
|
return Tab::create( |
54
|
|
|
'Details', |
55
|
|
|
TextField::create("Title", File::singleton()->fieldLabel('Title')), |
56
|
|
|
TextField::create('Name', File::singleton()->fieldLabel('Filename')), |
57
|
|
|
ReadonlyField::create("Path", _t('AssetTableField.PATH', 'Path'), $this->getPath($record)) |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function getFormFields(Controller $controller, $name, $context = []) |
62
|
|
|
{ |
63
|
|
|
$record = $context['Record']; |
64
|
|
|
|
65
|
|
|
// Add status flag before extensions are triggered |
66
|
|
|
$this->beforeExtending('updateFormFields', function (FieldList $fields) use ($record) { |
67
|
|
|
$fields->insertAfter( |
68
|
|
|
'TitleHeader', |
69
|
|
|
LiteralField::create('FileSpecs', $this->getSpecsMarkup($record)) |
70
|
|
|
); |
71
|
|
|
}); |
72
|
|
|
|
73
|
|
|
return parent::getFormFields($controller, $name, $context); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get publish action |
78
|
|
|
* |
79
|
|
|
* @param File $record |
80
|
|
|
* @return FormAction |
81
|
|
|
*/ |
82
|
|
|
protected function getPublishAction($record) |
83
|
|
|
{ |
84
|
|
|
if (!$record || !$record->canPublish()) { |
85
|
|
|
return null; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Build action |
89
|
|
|
$publishText = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.PUBLISH_BUTTON', 'Publish'); |
90
|
|
|
return FormAction::create('publish', $publishText) |
91
|
|
|
->setIcon('rocket') |
92
|
|
|
->setSchemaData(['data' => ['buttonStyle' => 'primary']]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function getFormActions(Controller $controller, $name, $context = []) |
96
|
|
|
{ |
97
|
|
|
$record = $context['Record']; |
98
|
|
|
|
99
|
|
|
// Build top level bar |
100
|
|
|
$actions = new FieldList(array_filter([ |
101
|
|
|
$this->getSaveAction($record), |
102
|
|
|
$this->getPublishAction($record), |
103
|
|
|
$this->getPopoverMenu($record) |
104
|
|
|
])); |
105
|
|
|
|
106
|
|
|
// Update |
107
|
|
|
$this->invokeWithExtensions('updateFormActions', $actions, $controller, $name, $context); |
108
|
|
|
return $actions; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get raw HTML for image markup |
113
|
|
|
* |
114
|
|
|
* @param File $file |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
protected function getIconMarkup($file) |
118
|
|
|
{ |
119
|
|
|
$markup = parent::getIconMarkup($file); |
120
|
|
|
if (!$markup) { |
121
|
|
|
return null; |
122
|
|
|
} |
123
|
|
|
if (!$file->exists()) { |
124
|
|
|
return sprintf('<div class="%s">%s</div>', |
125
|
|
|
'editor__file-preview-message--file-missing', |
126
|
|
|
_t('AssetAdmin.FILE_MISSING', 'File cannot be found') |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
$link = $file->Link(); |
130
|
|
|
$linkedImage = sprintf( |
131
|
|
|
'<a class="%s" href="%s" target="_blank">%s</a>', |
132
|
|
|
'editor__file-preview-link', |
133
|
|
|
$link, |
134
|
|
|
$markup |
135
|
|
|
); |
136
|
|
|
return $linkedImage; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* get HTML for status icon |
141
|
|
|
* |
142
|
|
|
* @param File $record |
143
|
|
|
* @return null|string |
144
|
|
|
*/ |
145
|
|
|
protected function getSpecsMarkup($record) |
146
|
|
|
{ |
147
|
|
|
if (!$record || !$record->exists()) { |
148
|
|
|
return null; |
149
|
|
|
} |
150
|
|
|
return sprintf( |
151
|
|
|
'<div class="editor__specs">%s %s</div>', |
152
|
|
|
$record->getSize(), |
153
|
|
|
$this->getStatusFlagMarkup($record) |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get published status flag |
159
|
|
|
* |
160
|
|
|
* @param File $record |
161
|
|
|
* @return null|string |
162
|
|
|
*/ |
163
|
|
|
protected function getStatusFlagMarkup($record) |
164
|
|
|
{ |
165
|
|
|
if ($record && ($statusTitle = $record->getStatusTitle())) { |
166
|
|
|
return "<span class=\"editor__status-flag\">{$statusTitle}</span>"; |
167
|
|
|
} |
168
|
|
|
return null; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get user-visible "Path" for this record |
173
|
|
|
* |
174
|
|
|
* @param File $record |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
protected function getPath($record) |
178
|
|
|
{ |
179
|
|
|
if ($record && $record->isInDB()) { |
180
|
|
|
if ($record->ParentID) { |
181
|
|
|
return $record->Parent()->getFilename(); |
182
|
|
|
} else { |
183
|
|
|
return '/'; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
return null; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Get action for adding to campaign |
191
|
|
|
* |
192
|
|
|
* @param File $record |
193
|
|
|
* @return FormAction|null |
194
|
|
|
*/ |
195
|
|
|
protected function getAddToCampaignAction($record) |
196
|
|
|
{ |
197
|
|
|
if ($record && $record->canPublish()) { |
198
|
|
|
return FormAction::create( |
199
|
|
|
'addtocampaign', |
200
|
|
|
_t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.ADDTOCAMPAIGN', 'Add to campaign') |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
return null; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get action for publishing |
208
|
|
|
* |
209
|
|
|
* @param File $record |
210
|
|
|
* @return FormAction |
211
|
|
|
*/ |
212
|
|
|
protected function getUnpublishAction($record) |
213
|
|
|
{ |
214
|
|
|
// Check if record is unpublishable |
215
|
|
|
if (!$record || !$record->isPublished() || !$record->canUnpublish()) { |
216
|
|
|
return null; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
// Build action |
220
|
|
|
$unpublishText = _t( |
221
|
|
|
'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.UNPUBLISH_BUTTON', |
222
|
|
|
'Unpublish' |
223
|
|
|
); |
224
|
|
|
return FormAction::create('unpublish', $unpublishText) |
225
|
|
|
->setIcon('cancel-circled'); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Build popup menu |
230
|
|
|
* |
231
|
|
|
* @param File $record |
232
|
|
|
* @return PopoverField |
233
|
|
|
*/ |
234
|
|
|
protected function getPopoverMenu($record) |
235
|
|
|
{ |
236
|
|
|
// Build popover actions |
237
|
|
|
$popoverActions = array_filter([ |
238
|
|
|
$this->getAddToCampaignAction($record), |
239
|
|
|
$this->getUnpublishAction($record), |
240
|
|
|
$this->getDeleteAction($record) |
241
|
|
|
]); |
242
|
|
|
if ($popoverActions) { |
|
|
|
|
243
|
|
|
return PopoverField::create($popoverActions) |
244
|
|
|
->setPlacement('top'); |
245
|
|
|
} |
246
|
|
|
return null; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.