Completed
Push — master ( 7dd226...34aa18 )
by
unknown
10s
created

FileFormFactory::getFormFieldTabs()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 16
nc 3
nop 2
1
<?php
2
3
namespace SilverStripe\AssetAdmin\Forms;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\Control\RequestHandler;
7
use SilverStripe\Forms\FieldGroup;
8
use SilverStripe\Forms\DatetimeField;
9
use SilverStripe\Forms\FieldList;
10
use SilverStripe\Forms\FormAction;
11
use SilverStripe\Forms\HiddenField;
12
use SilverStripe\Forms\LiteralField;
13
use SilverStripe\Forms\ReadonlyField;
14
use SilverStripe\Forms\Tab;
15
use SilverStripe\Forms\TabSet;
16
use SilverStripe\Forms\TextField;
17
18
class FileFormFactory extends AssetFormFactory
19
{
20
    protected function getFormFieldTabs($record, $context = [])
21
    {
22
        // Add extra tab
23
        $tabs = TabSet::create(
24
            'Editor',
25
            $this->getFormFieldDetailsTab($record, $context),
26
            $this->getFormFieldSecurityTab($record, $context),
27
            $this->getFormFieldUsageTab($record, $context),
28
            $this->getFormFieldHistoryTab($record, $context)
29
        );
30
31
        // All non-admin forms are typically readonly
32
        switch ($this->getFormType($context)) {
33
            case static::TYPE_INSERT:
34
                $tabs->setReadonly(true);
35
                $tabs->unshift($this->getFormFieldAttributesTab($record, $context));
36
                break;
37
            case static::TYPE_SELECT:
38
                $tabs->setReadonly(true);
39
                break;
40
        }
41
42
        return $tabs;
43
    }
44
45
    /**
46
     * Build "Usage" tab
47
     *
48
     * @param File $record
49
     * @param array $context
50
     * @return Tab
51
     */
52
    protected function getFormFieldUsageTab($record, $context = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $record is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
53
    {
54
        // Add new tab for usage
55
        return Tab::create(
56
            'Usage',
57
            DatetimeField::create(
58
                "Created",
59
                _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.CREATED', 'First uploaded')
60
            )
61
                ->setReadonly(true),
62
            DatetimeField::create(
63
                "LastEdited",
64
                _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.LASTEDIT', 'Last changed')
65
            )
66
                ->setReadonly(true)
67
        );
68
    }
69
70
    protected function getFormFieldDetailsTab($record, $context = [])
71
    {
72
        // Update details tab
73
        $tab = Tab::create(
74
            'Details',
75
            TextField::create("Title", File::singleton()->fieldLabel('Title')),
76
            TextField::create('Name', File::singleton()->fieldLabel('Filename')),
77
            ReadonlyField::create(
78
                "Path",
79
                _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.PATH', 'Path'),
80
                $this->getPath($record)
81
            )
82
        );
83
84
        if ($this->getFormType($context) !== static::TYPE_ADMIN) {
85
            $tab->push(LiteralField::create(
86
                'EditLink',
87
                sprintf(
88
                    '<a href="%s" class="%s" target="_blank"><i class="%s" />%s</a>',
89
                    $record->CMSEditLink(),
90
                    'btn btn-secondary-outline font-icon-edit editor__edit-link',
91
                    '',
92
                    _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.EditLink', 'Edit original file')
93
                )
94
            ));
95
        }
96
        return $tab;
97
    }
98
99
    /**
100
     * Create tab for file attributes
101
     *
102
     * @param File $record
103
     * @param array $context
104
     * @return Tab
105
     */
106
    protected function getFormFieldAttributesTab($record, $context = [])
107
    {
108
        return Tab::create(
109
            'Placement',
110
            LiteralField::create(
111
                'AttributesDescription',
112
                '<p>'. _t(
113
                    'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.AttributesDescription',
114
                    'These changes will only affect this particular placement of the file.'
115
                ) .'</p>'
116
            ),
117
            TextField::create('Caption', _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.Caption', 'Caption'))
118
        );
119
    }
120
121
    protected function getFormFieldHistoryTab($record, $context = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
122
    {
123
        return Tab::create(
124
            'History',
125
            HistoryListField::create('HistoryList')
126
                ->setRecord($record)
127
        );
128
    }
129
    
130
    protected function getFormFields(RequestHandler $controller = null, $formName, $context = [])
131
    {
132
        /** @var File $record */
133
        $record = $context['Record'];
134
135
        // Add status flag before extensions are triggered
136
        $this->beforeExtending('updateFormFields', function (FieldList $fields) use ($record) {
137
            // @todo move specs to a component/class, so it can update specs when a File is replaced
138
            $fields->insertAfter(
139
                'TitleHeader',
140
                LiteralField::create('FileSpecs', $this->getSpecsMarkup($record))
141
            );
142
            $fields->push(HiddenField::create('FileFilename'));
143
            $fields->push(HiddenField::create('FileHash'));
144
            $fields->push(HiddenField::create('FileVariant'));
145
        });
146
147
        return parent::getFormFields($controller, $formName, $context);
148
    }
149
150
    /**
151
     * Get publish action
152
     *
153
     * @param File $record
154
     * @return FormAction
155
     */
156
    protected function getPublishAction($record)
157
    {
158
        if (!$record || !$record->canPublish()) {
159
            return null;
160
        }
161
162
        // Build action
163
        $publishText = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.PUBLISH_BUTTON', 'Publish');
164
        /** @var FormAction $action */
165
        $action = FormAction::create('publish', $publishText)
166
            ->setIcon('rocket')
167
            ->setSchemaData(['data' => ['buttonStyle' => 'primary']]);
168
169
        return $action;
170
    }
171
    
172
    protected function getFormActions(RequestHandler $controller = null, $formName, $context = [])
173
    {
174
        $record = $context['Record'];
175
176
        if ($this->getFormType($context) !== static::TYPE_ADMIN) {
177
            $actionItems = array_filter([
178
                $this->getInsertAction($record),
179
            ]);
180
        } else {
181
            $actionItems = array_filter([
182
                $this->getSaveAction($record),
183
                $this->getPublishAction($record)
184
            ]);
185
        }
186
187
        // Group all actions
188
        if (count($actionItems) > 1) {
189
            $actionItems = [
190
                FieldGroup::create($actionItems)
191
                    ->setName('Actions')
192
                    ->addExtraClass('btn-group')
193
            ];
194
        }
195
196
        // Add popover
197
        $popover = $this->getPopoverMenu($record);
198
        if ($popover) {
199
            $actionItems[] = $popover;
200
        }
201
202
        // Build
203
        $actions = new FieldList($actionItems);
204
205
        // Update
206
        $this->invokeWithExtensions('updateFormActions', $actions, $controller, $formName, $context);
207
        return $actions;
208
    }
209
210
    /**
211
     * get HTML for status icon
212
     *
213
     * @param File $record
214
     * @return null|string
215
     */
216
    protected function getSpecsMarkup($record)
217
    {
218
        if (!$record || !$record->exists()) {
219
            return null;
220
        }
221
        return sprintf(
222
            '<div class="editor__specs">%s %s</div>',
223
            $record->getSize(),
224
            $this->getStatusFlagMarkup($record)
225
        );
226
    }
227
228
    /**
229
     * Get published status flag
230
     *
231
     * @param File $record
232
     * @return null|string
233
     */
234
    protected function getStatusFlagMarkup($record)
235
    {
236
        if ($record && ($statusTitle = $record->getStatusTitle())) {
237
            return "<span class=\"editor__status-flag\">{$statusTitle}</span>";
238
        }
239
        return null;
240
    }
241
242
    /**
243
     * Get user-visible "Path" for this record
244
     *
245
     * @param File $record
246
     * @return string
247
     */
248
    protected function getPath($record)
249
    {
250
        if ($record && $record->isInDB()) {
251
            if ($record->ParentID) {
252
                return $record->Parent()->getFilename();
253
            } else {
254
                return '/';
255
            }
256
        }
257
        return null;
258
    }
259
260
    /**
261
     * Get action for publishing
262
     *
263
     * @param File $record
264
     * @return FormAction
265
     */
266
    protected function getUnpublishAction($record)
267
    {
268
        // Check if record is unpublishable
269
        if (!$record || !$record->isPublished() || !$record->canUnpublish()) {
270
            return null;
271
        }
272
273
        // Build action
274
        $unpublishText = _t(
275
            'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.UNPUBLISH_BUTTON',
276
            'Unpublish'
277
        );
278
        return FormAction::create('unpublish', $unpublishText)
279
            ->setIcon('cancel-circled');
280
    }
281
282
    /**
283
     * Get actions that go into the Popover menu
284
     *
285
     * @param $record
286
     * @return array
287
     */
288
    protected function getPopoverActions($record)
289
    {
290
        $this->beforeExtending('updatePopoverActions', function (&$actions, $record) {
291
            // add the unpublish action to the start of the array
292
            array_unshift($actions, $this->getUnpublishAction($record));
293
        });
294
295
        return parent::getPopoverActions($record);
296
    }
297
298
    /**
299
     * @param File $record
300
     * @return FormAction
301
     */
302
    protected function getInsertAction($record)
303
    {
304
        $action = null;
305
        if ($record && $record->isInDB() && $record->canEdit()) {
306
            /** @var FormAction $action */
307
            $action = FormAction::create('insert', _t(__CLASS__.'.INSERT_FILE', 'Insert file'))
308
                ->setSchemaData(['data' => ['buttonStyle' => 'primary']]);
309
        }
310
        return $action;
311
    }
312
}
313