Completed
Push — master ( 23423a...b8da20 )
by Damian
14s
created

FileFormFactory::getFormFieldLinkOptionsTab()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
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\CheckboxField;
8
use SilverStripe\Forms\FieldGroup;
9
use SilverStripe\Forms\DatetimeField;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\Forms\FormAction;
12
use SilverStripe\Forms\HiddenField;
13
use SilverStripe\Forms\LiteralField;
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_MEDIA:
34
                $tabs->setReadonly(true);
35
                $tabs->unshift($this->getFormFieldAttributesTab($record, $context));
36
                break;
37
            case static::TYPE_INSERT_LINK:
38
                $tabs->setReadonly(true);
39
                $tabs->unshift($this->getFormFieldLinkOptionsTab($record, $context));
40
                break;
41
            case static::TYPE_SELECT:
42
                $tabs->setReadonly(true);
43
                break;
44
        }
45
46
        return $tabs;
47
    }
48
49
    /**
50
     * Build "Usage" tab
51
     *
52
     * @param File $record
53
     * @param array $context
54
     * @return Tab
55
     */
56
    protected function getFormFieldUsageTab($record, $context = [])
0 ignored issues
show
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...
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...
57
    {
58
        // Add new tab for usage
59
        return Tab::create(
60
            'Usage',
61
            DatetimeField::create(
62
                "Created",
63
                _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.CREATED', 'First uploaded')
64
            )
65
                ->setReadonly(true),
66
            DatetimeField::create(
67
                "LastEdited",
68
                _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.LASTEDIT', 'Last changed')
69
            )
70
                ->setReadonly(true)
71
        );
72
    }
73
74
    protected function getFormFieldDetailsTab($record, $context = [])
75
    {
76
        // Update details tab
77
        $tab = parent::getFormFieldDetailsTab($record, $context);
78
79
        $tab->insertBefore('Name', TextField::create("Title", File::singleton()->fieldLabel('Title')));
80
        
81
        if ($this->getFormType($context) !== static::TYPE_ADMIN) {
82
            $tab->push(LiteralField::create(
83
                'EditLink',
84
                sprintf(
85
                    '<a href="%s" class="%s" target="_blank"><i class="%s" />%s</a>',
86
                    $record->CMSEditLink(),
87
                    'btn btn-secondary-outline font-icon-edit editor__edit-link',
88
                    '',
89
                    _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.EditLink', 'Edit original file')
90
                )
91
            ));
92
        }
93
        return $tab;
94
    }
95
96
    protected function getFormFieldLinkOptionsTab($record, $context = [])
0 ignored issues
show
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...
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...
97
    {
98
        return Tab::create(
99
            'LinkOptions',
100
            _t(__CLASS__ .'.LINKOPTIONS', 'Link options'),
101
            TextField::create(
102
                'Description',
103
                _t(__CLASS__.'.LINKDESCR', 'Link description')
104
            ),
105
            CheckboxField::create(
106
                'TargetBlank',
107
                _t(__CLASS__.'.LINKOPENNEWWIN', 'Open in new window/tab')
108
            )
109
        );
110
    }
111
    
112
    /**
113
     * Create tab for file attributes
114
     *
115
     * @param File $record
116
     * @param array $context
117
     * @return Tab
118
     */
119
    protected function getFormFieldAttributesTab($record, $context = [])
120
    {
121
        return Tab::create(
122
            'Placement',
123
            LiteralField::create(
124
                'AttributesDescription',
125
                '<p>'. _t(
126
                    'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.AttributesDescription',
127
                    'These changes will only affect this particular placement of the file.'
128
                ) .'</p>'
129
            ),
130
            TextField::create('Caption', _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.Caption', 'Caption'))
131
        );
132
    }
133
134
    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...
135
    {
136
        return Tab::create(
137
            'History',
138
            HistoryListField::create('HistoryList')
139
                ->setRecord($record)
140
        );
141
    }
142
143
    protected function getFormFields(RequestHandler $controller = null, $formName, $context = [])
144
    {
145
        /** @var File $record */
146
        $record = $context['Record'];
147
148
        // Add status flag before extensions are triggered
149
        $this->beforeExtending('updateFormFields', function (FieldList $fields) use ($record) {
150
            // @todo move specs to a component/class, so it can update specs when a File is replaced
151
            $fields->insertAfter(
152
                'TitleHeader',
153
                LiteralField::create('FileSpecs', $this->getSpecsMarkup($record))
154
            );
155
            $fields->push(HiddenField::create('FileFilename'));
156
            $fields->push(HiddenField::create('FileHash'));
157
            $fields->push(HiddenField::create('FileVariant'));
158
        });
159
160
        return parent::getFormFields($controller, $formName, $context);
161
    }
162
163
    /**
164
     * Get publish action
165
     *
166
     * @param File $record
167
     * @return FormAction
168
     */
169
    protected function getPublishAction($record)
170
    {
171
        if (!$record || !$record->canPublish()) {
172
            return null;
173
        }
174
175
        // Build action
176
        $publishText = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.PUBLISH_BUTTON', 'Publish');
177
        /** @var FormAction $action */
178
        $action = FormAction::create('publish', $publishText)
179
            ->setIcon('rocket')
180
            ->setSchemaData(['data' => ['buttonStyle' => 'primary']]);
181
182
        return $action;
183
    }
184
185
    protected function getFormActions(RequestHandler $controller = null, $formName, $context = [])
186
    {
187
        $record = $context['Record'];
188
        $type = $this->getFormType($context);
189
190
        if ($type === static::TYPE_SELECT || $type === static::TYPE_INSERT_MEDIA) {
191
            $actionItems = array_filter([
192
                $this->getInsertAction($record),
193
            ]);
194
        } elseif ($type === static::TYPE_INSERT_LINK) {
195
            $actionItems = array_filter([
196
                $this->getInsertLinkAction($record),
197
            ]);
198
        } else {
199
            $actionItems = array_filter([
200
                $this->getSaveAction($record),
201
                $this->getPublishAction($record)
202
            ]);
203
        }
204
205
        // Group all actions
206
        if (count($actionItems) > 1) {
207
            $actionItems = [
208
                FieldGroup::create($actionItems)
209
                    ->setName('Actions')
210
                    ->addExtraClass('btn-group')
211
            ];
212
        }
213
214
        if ($type === static::TYPE_ADMIN) {
215
            // Add popover
216
            $popover = $this->getPopoverMenu($record);
217
            if ($popover) {
218
                $actionItems[] = $popover;
219
            }
220
        }
221
222
        // Build
223
        $actions = new FieldList($actionItems);
224
225
        // Update
226
        $this->invokeWithExtensions('updateFormActions', $actions, $controller, $formName, $context);
227
        return $actions;
228
    }
229
230
    /**
231
     * get HTML for status icon
232
     *
233
     * @param File $record
234
     * @return null|string
235
     */
236
    protected function getSpecsMarkup($record)
237
    {
238
        if (!$record || !$record->exists()) {
239
            return null;
240
        }
241
        return sprintf(
242
            '<div class="editor__specs">%s %s</div>',
243
            $record->getSize(),
244
            $this->getStatusFlagMarkup($record)
245
        );
246
    }
247
248
    /**
249
     * Get published status flag
250
     *
251
     * @param File $record
252
     * @return null|string
253
     */
254
    protected function getStatusFlagMarkup($record)
255
    {
256
        if ($record && ($statusTitle = $record->getStatusTitle())) {
257
            return "<span class=\"editor__status-flag\">{$statusTitle}</span>";
258
        }
259
        return null;
260
    }
261
262
    /**
263
     * Get action for publishing
264
     *
265
     * @param File $record
266
     * @return FormAction
267
     */
268
    protected function getUnpublishAction($record)
269
    {
270
        // Check if record is unpublishable
271
        if (!$record || !$record->isPublished() || !$record->canUnpublish()) {
272
            return null;
273
        }
274
275
        // Build action
276
        $unpublishText = _t(
277
            'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.UNPUBLISH_BUTTON',
278
            'Unpublish'
279
        );
280
        return FormAction::create('unpublish', $unpublishText)
281
            ->setIcon('cancel-circled');
282
    }
283
284
    /**
285
     * Get actions that go into the Popover menu
286
     *
287
     * @param $record
288
     * @return array
289
     */
290
    protected function getPopoverActions($record)
291
    {
292
        $this->beforeExtending('updatePopoverActions', function (&$actions, $record) {
293
            // add the unpublish action to the start of the array
294
            array_unshift($actions, $this->getUnpublishAction($record));
295
        });
296
297
        return parent::getPopoverActions($record);
298
    }
299
300
    /**
301
     * @param File $record
302
     * @return FormAction
303
     */
304 View Code Duplication
    protected function getInsertAction($record)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
305
    {
306
        $action = null;
307
        if ($record && $record->isInDB() && $record->canEdit()) {
308
            /** @var FormAction $action */
309
            $action = FormAction::create('insert', _t(__CLASS__.'.INSERT_FILE', 'Insert file'))
310
                ->setSchemaData(['data' => ['buttonStyle' => 'primary']]);
311
        }
312
        return $action;
313
    }
314
    
315
    /**
316
     * @param File $record
317
     * @return FormAction
318
     */
319 View Code Duplication
    protected function getInsertLinkAction($record)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
320
    {
321
        $action = null;
322
        if ($record && $record->isInDB() && $record->canEdit()) {
323
            /** @var FormAction $action */
324
            $action = FormAction::create('insert', _t(__CLASS__.'.INSERT_LINK', 'Link to file'))
325
                ->setSchemaData(['data' => ['buttonStyle' => 'primary']]);
326
        }
327
        return $action;
328
    }
329
}
330