Completed
Pull Request — master (#405)
by
unknown
02:21
created

FileFormFactory   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 291
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 14

Importance

Changes 0
Metric Value
wmc 37
lcom 2
cbo 14
dl 0
loc 291
rs 8.6
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getSpecsMarkup() 0 11 3
A getStatusFlagMarkup() 0 7 3
A getPath() 0 11 4
A getAddToCampaignAction() 0 10 3
A getUnpublishAction() 0 15 4
A getPublishAction() 0 12 3
A getFormFieldTabs() 0 23 3
A getFormFieldUsageTab() 0 11 1
B getFormFieldDetailsTab() 0 24 2
A getFormFieldAttributesTab() 0 14 1
A getFormFieldHistoryTab() 0 8 1
A getFormFields() 0 18 1
B getFormActions() 0 24 2
A getPopoverMenu() 0 18 2
A getInsertAction() 0 8 4
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([
170
                    $this->getSaveAction($record),
171
                    $this->getPublishAction($record)
172
                ])
173
                    ->setSchemaData(['extraClass' => 'btn-group']),
174
                $this->getPopoverMenu($record),
175
            ]));
176
        }
177
178
        // Update
179
        $this->invokeWithExtensions('updateFormActions', $actions, $controller, $name, $context);
180
        return $actions;
181
    }
182
183
    /**
184
     * get HTML for status icon
185
     *
186
     * @param File $record
187
     * @return null|string
188
     */
189
    protected function getSpecsMarkup($record)
190
    {
191
        if (!$record || !$record->exists()) {
192
            return null;
193
        }
194
        return sprintf(
195
            '<div class="editor__specs">%s %s</div>',
196
            $record->getSize(),
197
            $this->getStatusFlagMarkup($record)
198
        );
199
    }
200
201
    /**
202
     * Get published status flag
203
     *
204
     * @param File $record
205
     * @return null|string
206
     */
207
    protected function getStatusFlagMarkup($record)
208
    {
209
        if ($record && ($statusTitle = $record->getStatusTitle())) {
210
            return "<span class=\"editor__status-flag\">{$statusTitle}</span>";
211
        }
212
        return null;
213
    }
214
215
    /**
216
     * Get user-visible "Path" for this record
217
     *
218
     * @param File $record
219
     * @return string
220
     */
221
    protected function getPath($record)
222
    {
223
        if ($record && $record->isInDB()) {
224
            if ($record->ParentID) {
225
                return $record->Parent()->getFilename();
226
            } else {
227
                return '/';
228
            }
229
        }
230
        return null;
231
    }
232
233
    /**
234
     * Get action for adding to campaign
235
     *
236
     * @param File $record
237
     * @return FormAction|null
238
     */
239
    protected function getAddToCampaignAction($record)
240
    {
241
        if ($record && $record->canPublish()) {
242
            return FormAction::create(
243
                'addtocampaign',
244
                _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.ADDTOCAMPAIGN', 'Add to campaign')
245
            );
246
        }
247
        return null;
248
    }
249
250
    /**
251
     * Get action for publishing
252
     *
253
     * @param File $record
254
     * @return FormAction
255
     */
256
    protected function getUnpublishAction($record)
257
    {
258
        // Check if record is unpublishable
259
        if (!$record || !$record->isPublished() || !$record->canUnpublish()) {
260
            return null;
261
        }
262
263
        // Build action
264
        $unpublishText = _t(
265
            'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.UNPUBLISH_BUTTON',
266
            'Unpublish'
267
        );
268
        return FormAction::create('unpublish', $unpublishText)
269
            ->setIcon('cancel-circled');
270
    }
271
272
    /**
273
     * Build popup menu
274
     *
275
     * @param File $record
276
     * @return PopoverField
277
     */
278
    protected function getPopoverMenu($record)
279
    {
280
        // Build popover actions
281
        $popoverActions = array_filter([
282
            $this->getAddToCampaignAction($record),
283
            $this->getUnpublishAction($record),
284
            $this->getDeleteAction($record)
285
        ]);
286
        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...
287
            return PopoverField::create($popoverActions)
288
                ->setPlacement('top')
289
                ->setButtonTooltip(_t(
290
                    'SilverStripe\\AssetAdmin\\Forms\\FileFormFactory.OTHER_ACTIONS',
291
                    'Other actions'
292
                ));
293
        }
294
        return null;
295
    }
296
297
    /**
298
     * @param File $record
299
     * @return FormAction
300
     */
301
    protected function getInsertAction($record)
302
    {
303
        if ($record && $record->isInDB() && $record->canEdit()) {
304
            return FormAction::create('insert', _t('CMSMain.INSERT', 'Insert file'))
305
                ->setSchemaData(['data' => ['buttonStyle' => 'primary']]);
306
        }
307
        return null;
308
    }
309
}
310