Completed
Push — master ( 916d56...6e2c74 )
by Damian
02:26
created

FileFormFactory::getFormFieldDetailsTab()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 2
eloc 13
nc 2
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\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\Tab;
14
use SilverStripe\Forms\TabSet;
15
use SilverStripe\Forms\TextField;
16
17
class FileFormFactory extends AssetFormFactory
18
{
19
    protected function getFormFieldTabs($record, $context = [])
20
    {
21
        // Add extra tab
22
        $tabs = TabSet::create(
23
            'Editor',
24
            $this->getFormFieldDetailsTab($record, $context),
25
            $this->getFormFieldSecurityTab($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 $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...
52
    {
53
        // Add new tab for usage
54
        return Tab::create(
55
            'Usage',
56
            DatetimeField::create(
57
                "Created",
58
                _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.CREATED', 'First uploaded')
59
            )
60
                ->setReadonly(true),
61
            DatetimeField::create(
62
                "LastEdited",
63
                _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.LASTEDIT', 'Last changed')
64
            )
65
                ->setReadonly(true)
66
        );
67
    }
68
69
    protected function getFormFieldDetailsTab($record, $context = [])
70
    {
71
        // Update details tab
72
        $tab = parent::getFormFieldDetailsTab($record, $context);
73
74
        $tab->insertBefore('Name', TextField::create("Title", File::singleton()->fieldLabel('Title')));
75
        
76
        if ($this->getFormType($context) !== static::TYPE_ADMIN) {
77
            $tab->push(LiteralField::create(
78
                'EditLink',
79
                sprintf(
80
                    '<a href="%s" class="%s" target="_blank"><i class="%s" />%s</a>',
81
                    $record->CMSEditLink(),
82
                    'btn btn-secondary-outline font-icon-edit editor__edit-link',
83
                    '',
84
                    _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.EditLink', 'Edit original file')
85
                )
86
            ));
87
        }
88
        return $tab;
89
    }
90
91
    /**
92
     * Create tab for file attributes
93
     *
94
     * @param File $record
95
     * @param array $context
96
     * @return Tab
97
     */
98
    protected function getFormFieldAttributesTab($record, $context = [])
99
    {
100
        return Tab::create(
101
            'Placement',
102
            LiteralField::create(
103
                'AttributesDescription',
104
                '<p>'. _t(
105
                    'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.AttributesDescription',
106
                    'These changes will only affect this particular placement of the file.'
107
                ) .'</p>'
108
            ),
109
            TextField::create('Caption', _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.Caption', 'Caption'))
110
        );
111
    }
112
113
    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...
114
    {
115
        return Tab::create(
116
            'History',
117
            HistoryListField::create('HistoryList')
118
                ->setRecord($record)
119
        );
120
    }
121
122
    protected function getFormFields(RequestHandler $controller = null, $formName, $context = [])
123
    {
124
        /** @var File $record */
125
        $record = $context['Record'];
126
127
        // Add status flag before extensions are triggered
128
        $this->beforeExtending('updateFormFields', function (FieldList $fields) use ($record) {
129
            // @todo move specs to a component/class, so it can update specs when a File is replaced
130
            $fields->insertAfter(
131
                'TitleHeader',
132
                LiteralField::create('FileSpecs', $this->getSpecsMarkup($record))
133
            );
134
            $fields->push(HiddenField::create('FileFilename'));
135
            $fields->push(HiddenField::create('FileHash'));
136
            $fields->push(HiddenField::create('FileVariant'));
137
        });
138
139
        return parent::getFormFields($controller, $formName, $context);
140
    }
141
142
    /**
143
     * Get publish action
144
     *
145
     * @param File $record
146
     * @return FormAction
147
     */
148
    protected function getPublishAction($record)
149
    {
150
        if (!$record || !$record->canPublish()) {
151
            return null;
152
        }
153
154
        // Build action
155
        $publishText = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.PUBLISH_BUTTON', 'Publish');
156
        /** @var FormAction $action */
157
        $action = FormAction::create('publish', $publishText)
158
            ->setIcon('rocket')
159
            ->setSchemaData(['data' => ['buttonStyle' => 'primary']]);
160
161
        return $action;
162
    }
163
164
    protected function getFormActions(RequestHandler $controller = null, $formName, $context = [])
165
    {
166
        $record = $context['Record'];
167
168
        if ($this->getFormType($context) !== static::TYPE_ADMIN) {
169
            $actionItems = array_filter([
170
                $this->getInsertAction($record),
171
            ]);
172
        } else {
173
            $actionItems = array_filter([
174
                $this->getSaveAction($record),
175
                $this->getPublishAction($record)
176
            ]);
177
        }
178
179
        // Group all actions
180
        if (count($actionItems) > 1) {
181
            $actionItems = [
182
                FieldGroup::create($actionItems)
183
                    ->setName('Actions')
184
                    ->addExtraClass('btn-group')
185
            ];
186
        }
187
188
        // Add popover
189
        $popover = $this->getPopoverMenu($record);
190
        if ($popover) {
191
            $actionItems[] = $popover;
192
        }
193
194
        // Build
195
        $actions = new FieldList($actionItems);
196
197
        // Update
198
        $this->invokeWithExtensions('updateFormActions', $actions, $controller, $formName, $context);
199
        return $actions;
200
    }
201
202
    /**
203
     * get HTML for status icon
204
     *
205
     * @param File $record
206
     * @return null|string
207
     */
208
    protected function getSpecsMarkup($record)
209
    {
210
        if (!$record || !$record->exists()) {
211
            return null;
212
        }
213
        return sprintf(
214
            '<div class="editor__specs">%s %s</div>',
215
            $record->getSize(),
216
            $this->getStatusFlagMarkup($record)
217
        );
218
    }
219
220
    /**
221
     * Get published status flag
222
     *
223
     * @param File $record
224
     * @return null|string
225
     */
226
    protected function getStatusFlagMarkup($record)
227
    {
228
        if ($record && ($statusTitle = $record->getStatusTitle())) {
229
            return "<span class=\"editor__status-flag\">{$statusTitle}</span>";
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
        $action = null;
279
        if ($record && $record->isInDB() && $record->canEdit()) {
280
            /** @var FormAction $action */
281
            $action = FormAction::create('insert', _t(__CLASS__.'.INSERT_FILE', 'Insert file'))
282
                ->setSchemaData(['data' => ['buttonStyle' => 'primary']]);
283
        }
284
        return $action;
285
    }
286
}
287