Completed
Pull Request — master (#458)
by Roman
02:25
created

FileFormFactory   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 267
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 12

Importance

Changes 0
Metric Value
wmc 33
c 0
b 0
f 0
lcom 2
cbo 12
dl 0
loc 267
rs 9.3999

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getSpecsMarkup() 0 11 3
A getStatusFlagMarkup() 0 7 3
A getPath() 0 11 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
B getFormActions() 0 25 2
A getFormFields() 0 19 1
A getUnpublishAction() 0 15 4
A getPopoverActions() 0 9 1
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\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->getFormFieldUsageTab($record, $context),
27
            $this->getFormFieldHistoryTab($record, $context)
28
        );
29
30
        // All non-admin forms are typically readonly
31
        switch ($this->getFormType($context)) {
32
            case static::TYPE_INSERT:
33
                $tabs->setReadonly(true);
34
                $tabs->unshift($this->getFormFieldAttributesTab($record, $context));
35
                break;
36
            case static::TYPE_SELECT:
37
                $tabs->setReadonly(true);
38
                break;
39
        }
40
41
        return $tabs;
42
    }
43
44
    /**
45
     * Build "Usage" tab
46
     *
47
     * @param File $record
48
     * @param array $context
49
     * @return Tab
50
     */
51
    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...
52
    {
53
        // Add new tab for usage
54
        return Tab::create(
55
            'Usage',
56
            DatetimeField::create("Created", _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.CREATED', 'First uploaded'))
57
                ->setReadonly(true),
58
            DatetimeField::create("LastEdited", _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.LASTEDIT', 'Last changed'))
59
                ->setReadonly(true)
60
        );
61
    }
62
63
    protected function getFormFieldDetailsTab($record, $context = [])
64
    {
65
        // Update details tab
66
        $tab = Tab::create(
67
            'Details',
68
            TextField::create("Title", File::singleton()->fieldLabel('Title')),
69
            TextField::create('Name', File::singleton()->fieldLabel('Filename')),
70
            ReadonlyField::create("Path", _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.PATH', 'Path'), $this->getPath($record))
71
        );
72
73
        if ($this->getFormType($context) !== static::TYPE_ADMIN) {
74
            $tab->push(LiteralField::create(
75
                'EditLink',
76
                sprintf(
77
                    '<a href="%s" class="%s" target="_blank"><i class="%s" />%s</a>',
78
                    $record->CMSEditLink(),
79
                    'btn btn-secondary-outline font-icon-edit editor__edit-link',
80
                    '',
81
                    _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.EditLink', 'Edit original file')
82
                )
83
            ));
84
        }
85
        return $tab;
86
    }
87
88
    /**
89
     * Create tab for file attributes
90
     *
91
     * @param File $record
92
     * @param array $context
93
     * @return Tab
94
     */
95
    protected function getFormFieldAttributesTab($record, $context = [])
96
    {
97
        return Tab::create(
98
            'Placement',
99
            LiteralField::create(
100
                'AttributesDescription',
101
                '<p>'. _t(
102
                    'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.AttributesDescription',
103
                    'These changes will only affect this particular placement of the file.'
104
                ) .'</p>'
105
            ),
106
            TextField::create('Caption', _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.Caption', 'Caption'))
107
        );
108
    }
109
110
    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...
111
    {
112
        return Tab::create(
113
            'History',
114
            HistoryListField::create('HistoryList')
115
                ->setRecord($record)
116
        );
117
    }
118
119
    protected function getFormFields(Controller $controller, $name, $context = [])
120
    {
121
        /** @var File $record */
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, $formName, $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, $formName, $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 publishing
236
     *
237
     * @param File $record
238
     * @return FormAction
239
     */
240
    protected function getUnpublishAction($record)
241
    {
242
        // Check if record is unpublishable
243
        if (!$record || !$record->isPublished() || !$record->canUnpublish()) {
244
            return null;
245
        }
246
247
        // Build action
248
        $unpublishText = _t(
249
            'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.UNPUBLISH_BUTTON',
250
            'Unpublish'
251
        );
252
        return FormAction::create('unpublish', $unpublishText)
253
            ->setIcon('cancel-circled');
254
    }
255
256
    /**
257
     * Get actions that go into the Popover menu
258
     *
259
     * @param $record
260
     * @return array
261
     */
262
    protected function getPopoverActions($record)
263
    {
264
        $this->beforeExtending('updatePopoverActions', function (&$actions, $record) {
265
            // add the unpublish action to the start of the array
266
            array_unshift($actions, $this->getUnpublishAction($record));
267
        });
268
269
        return parent::getPopoverActions($record);
270
    }
271
272
    /**
273
     * @param File $record
274
     * @return FormAction
275
     */
276
    protected function getInsertAction($record)
277
    {
278
        if ($record && $record->isInDB() && $record->canEdit()) {
279
            return FormAction::create('insert', _t('SilverStripe\\CMS\\Controllers\\CMSMain.INSERT', 'Insert file'))
280
                ->setSchemaData(['data' => ['buttonStyle' => 'primary']]);
281
        }
282
        return null;
283
    }
284
}
285