Completed
Push — master ( bbc157...6987bd )
by
unknown
02:12
created

FileFormFactory::getFormFieldDetailsTab()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 2
eloc 16
nc 2
nop 2
1
<?php
2
3
namespace SilverStripe\AssetAdmin\Forms;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\Control\Controller;
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\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, $context = [])
22
    {
23
        // Add extra tab
24
        $tabs = TabSet::create(
25
            'Editor',
26
            $this->getFormFieldDetailsTab($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 $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...
53
    {
54
        // Add new tab for usage
55
        return Tab::create(
56
            'Usage',
57
            DatetimeField::create("Created", _t('AssetTableField.CREATED', 'First uploaded'))
58
                ->setReadonly(true),
59
            DatetimeField::create("LastEdited", _t('AssetTableField.LASTEDIT', 'Last changed'))
60
                ->setReadonly(true)
61
        );
62
    }
63
64
    protected function getFormFieldDetailsTab($record, $context = [])
65
    {
66
        // Update details tab
67
        $tab = Tab::create(
68
            'Details',
69
            TextField::create("Title", File::singleton()->fieldLabel('Title')),
70
            TextField::create('Name', File::singleton()->fieldLabel('Filename')),
71
            ReadonlyField::create("Path", _t('AssetTableField.PATH', 'Path'), $this->getPath($record))
72
        );
73
74
        if ($this->getFormType($context) !== static::TYPE_ADMIN) {
75
            $tab->push(LiteralField::create(
76
                'EditLink',
77
                sprintf(
78
                    '<a href="%s" class="%s" target="_blank"><i class="%s" />%s</a>',
79
                    $record->CMSEditLink(),
80
                    'btn btn-secondary-outline font-icon-edit editor__edit-link',
81
                    '',
82
                    _t('AssetAdmin.EditLink', 'Edit original file')
83
                )
84
            ));
85
        }
86
        return $tab;
87
    }
88
89
    /**
90
     * Create tab for file attributes
91
     *
92
     * @param File $record
93
     * @param array $context
94
     * @return Tab
95
     */
96
    protected function getFormFieldAttributesTab($record, $context = [])
97
    {
98
        return Tab::create(
99
            'Placement',
100
            LiteralField::create(
101
                'AttributesDescription',
102
                '<p>'. _t(
103
                    'AssetAdmin.AttributesDescription',
104
                    'These changes will only affect this particular placement of the file.'
105
                ) .'</p>'
106
            ),
107
            TextField::create('Caption', _t('AssetAdmin.Caption', 'Caption'))
108
        );
109
    }
110
111
    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...
112
    {
113
        return Tab::create(
114
            'History',
115
            HistoryListField::create('HistoryList')
116
                ->setRecord($record)
117
        );
118
    }
119
120
    protected function getFormFields(Controller $controller, $name, $context = [])
121
    {
122
        $record = $context['Record'];
123
124
        // Add status flag before extensions are triggered
125
        $this->beforeExtending('updateFormFields', function (FieldList $fields) use ($record) {
126
            // @todo move specs to a component/class, so it can update specs when a File is replaced
127
            $fields->insertAfter(
128
                'TitleHeader',
129
                LiteralField::create('FileSpecs', $this->getSpecsMarkup($record))
130
            );
131
            $fields->push(HiddenField::create('FileFilename'));
132
            $fields->push(HiddenField::create('FileHash'));
133
            $fields->push(HiddenField::create('FileVariant'));
134
        });
135
136
        return parent::getFormFields($controller, $name, $context);
137
    }
138
139
    /**
140
     * Get publish action
141
     *
142
     * @param File $record
143
     * @return FormAction
144
     */
145
    protected function getPublishAction($record)
146
    {
147
        if (!$record || !$record->canPublish()) {
148
            return null;
149
        }
150
151
        // Build action
152
        $publishText = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.PUBLISH_BUTTON', 'Publish');
153
        return FormAction::create('publish', $publishText)
154
            ->setIcon('rocket')
155
            ->setSchemaData(['data' => ['buttonStyle' => 'primary']]);
156
    }
157
158
    protected function getFormActions(Controller $controller, $name, $context = [])
159
    {
160
        $record = $context['Record'];
161
162
        if ($this->getFormType($context) !== static::TYPE_ADMIN) {
163
            $actions = new FieldList(array_filter([
164
                $this->getInsertAction($record),
165
            ]));
166
        } else {
167
            // Build top level bar
168
            $actions = new FieldList(array_filter([
169
                FieldGroup::create(array_filter([
170
                    $this->getSaveAction($record),
171
                    $this->getPublishAction($record)
172
                ]))
173
                    ->setName('Actions')
174
                    ->addExtraClass('btn-group'),
175
                $this->getPopoverMenu($record),
176
            ]));
177
        }
178
179
        // Update
180
        $this->invokeWithExtensions('updateFormActions', $actions, $controller, $name, $context);
181
        return $actions;
182
    }
183
184
    /**
185
     * get HTML for status icon
186
     *
187
     * @param File $record
188
     * @return null|string
189
     */
190
    protected function getSpecsMarkup($record)
191
    {
192
        if (!$record || !$record->exists()) {
193
            return null;
194
        }
195
        return sprintf(
196
            '<div class="editor__specs">%s %s</div>',
197
            $record->getSize(),
198
            $this->getStatusFlagMarkup($record)
199
        );
200
    }
201
202
    /**
203
     * Get published status flag
204
     *
205
     * @param File $record
206
     * @return null|string
207
     */
208
    protected function getStatusFlagMarkup($record)
209
    {
210
        if ($record && ($statusTitle = $record->getStatusTitle())) {
211
            return "<span class=\"editor__status-flag\">{$statusTitle}</span>";
212
        }
213
        return null;
214
    }
215
216
    /**
217
     * Get user-visible "Path" for this record
218
     *
219
     * @param File $record
220
     * @return string
221
     */
222
    protected function getPath($record)
223
    {
224
        if ($record && $record->isInDB()) {
225
            if ($record->ParentID) {
226
                return $record->Parent()->getFilename();
227
            } else {
228
                return '/';
229
            }
230
        }
231
        return null;
232
    }
233
234
    /**
235
     * Get action for adding to campaign
236
     *
237
     * @param File $record
238
     * @return FormAction|null
239
     */
240
    protected function getAddToCampaignAction($record)
241
    {
242
        if ($record && $record->canPublish()) {
243
            return FormAction::create(
244
                'addtocampaign',
245
                _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.ADDTOCAMPAIGN', 'Add to campaign')
246
            );
247
        }
248
        return null;
249
    }
250
251
    /**
252
     * Get action for publishing
253
     *
254
     * @param File $record
255
     * @return FormAction
256
     */
257
    protected function getUnpublishAction($record)
258
    {
259
        // Check if record is unpublishable
260
        if (!$record || !$record->isPublished() || !$record->canUnpublish()) {
261
            return null;
262
        }
263
264
        // Build action
265
        $unpublishText = _t(
266
            'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.UNPUBLISH_BUTTON',
267
            'Unpublish'
268
        );
269
        return FormAction::create('unpublish', $unpublishText)
270
            ->setIcon('cancel-circled');
271
    }
272
273
    /**
274
     * Build popup menu
275
     *
276
     * @param File $record
277
     * @return PopoverField
278
     */
279
    protected function getPopoverMenu($record)
280
    {
281
        // Build popover actions
282
        $popoverActions = array_filter([
283
            $this->getAddToCampaignAction($record),
284
            $this->getUnpublishAction($record),
285
            $this->getDeleteAction($record)
286
        ]);
287
        if ($popoverActions) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $popoverActions of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
288
            return PopoverField::create($popoverActions)
289
                ->setPlacement('top')
290
                ->setButtonTooltip(_t(
291
                    'SilverStripe\\AssetAdmin\\Forms\\FileFormFactory.OTHER_ACTIONS',
292
                    'Other actions'
293
                ));
294
        }
295
        return null;
296
    }
297
298
    /**
299
     * @param File $record
300
     * @return FormAction
301
     */
302
    protected function getInsertAction($record)
303
    {
304
        if ($record && $record->isInDB() && $record->canEdit()) {
305
            return FormAction::create('insert', _t('CMSMain.INSERT', 'Insert file'))
306
                ->setSchemaData(['data' => ['buttonStyle' => 'primary']]);
307
        }
308
        return null;
309
    }
310
}
311