Completed
Pull Request — master (#465)
by Damian
02:27
created

FileFormFactory::getInsertAction()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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