Completed
Pull Request — master (#289)
by Ingo
01:56
created

FileFormFactory::getFormFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
1
<?php
2
3
namespace SilverStripe\AssetAdmin\Forms;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Forms\DatetimeField;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\FormAction;
10
use SilverStripe\Forms\LiteralField;
11
use SilverStripe\Forms\PopoverField;
12
use SilverStripe\Forms\ReadonlyField;
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->getFormFieldUsageTab($record, $context),
26
            $this->getFormFieldHistoryTab($record, $context)
27
        );
28
29
        if (isset($context['Type']) && $context['Type'] === 'insert') {
30
            $tabs->setReadonly(true);
31
32
            $tabs->unshift($this->getFormFieldAttributesTab($record, $context));
33
        }
34
35
        return $tabs;
36
    }
37
38
    /**
39
     * Build "Usage" tab
40
     *
41
     * @param File $record
42
     * @param array $context
43
     * @return Tab
44
     */
45
    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...
46
    {
47
        // Add new tab for usage
48
        return Tab::create(
49
            'Usage',
50
            DatetimeField::create("Created", _t('AssetTableField.CREATED', 'First uploaded'))
51
                ->setReadonly(true),
52
            DatetimeField::create("LastEdited", _t('AssetTableField.LASTEDIT', 'Last changed'))
53
                ->setReadonly(true)
54
        );
55
    }
56
57
    protected function getFormFieldDetailsTab($record, $context = [])
58
    {
59
        // Update details tab
60
        $tab = Tab::create(
61
            'Details',
62
            TextField::create("Title", File::singleton()->fieldLabel('Title')),
63
            TextField::create('Name', File::singleton()->fieldLabel('Filename')),
64
            ReadonlyField::create("Path", _t('AssetTableField.PATH', 'Path'), $this->getPath($record))
65
        );
66
67
        if (isset($context['Type']) && $context['Type'] === 'insert') {
68
            $tab->push(LiteralField::create(
69
                'EditLink',
70
                sprintf(
71
                    '<a href="%s" class="%s" target="_blank"><i class="%s" />%s</a>',
72
                    $record->CMSEditLink(),
73
                    'btn btn-secondary-outline font-icon-edit editor__edit-link',
74
                    '',
75
                    _t('AssetAdmin.EditLink', 'Edit original file')
76
                )
77
            ));
78
        }
79
        return $tab;
80
    }
81
82
    /**
83
     * Create tab for file attributes
84
     *
85
     * @param File $record
86
     * @param array $context
87
     * @return Tab
88
     */
89
    protected function getFormFieldAttributesTab($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...
90
    {
91
        return Tab::create(
92
            'Placement',
93
            LiteralField::create(
94
                'AttributesDescription',
95
                '<p>'. _t(
96
                    'AssetAdmin.AttributesDescription',
97
                    'These changes will only affect this particular placement of the file.'
98
                ) .'</p>'
99
            ),
100
            TextField::create('Caption', _t('AssetAdmin.Caption', 'Caption'))
101
        );
102
    }
103
104
    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...
105
    {
106
        return Tab::create(
107
            'History',
108
            LiteralField::create('HistoryList', '')
109
             ->setSchemaComponent('HistoryList')
110
             ->setSchemaData(array(
111
                'data' => array(
112
                    'fileId' => $record->ID,
113
                    'latestVersionId' => $record->Version
114
                )
115
             ))
116
        );
117
    }
118
119
    protected function getFormFields(Controller $controller, $name, $context = [])
120
    {
121
        $record = $context['Record'];
122
123
        // Add status flag before extensions are triggered
124
        $this->beforeExtending('updateFormFields', function (FieldList $fields) use ($record) {
125
            $fields->insertAfter(
126
                'TitleHeader',
127
                LiteralField::create('FileSpecs', $this->getSpecsMarkup($record))
128
            );
129
        });
130
131
        return parent::getFormFields($controller, $name, $context);
132
    }
133
134
    /**
135
     * Get publish action
136
     *
137
     * @param File $record
138
     * @return FormAction
139
     */
140
    protected function getPublishAction($record)
141
    {
142
        if (!$record || !$record->canPublish()) {
143
            return null;
144
        }
145
146
        // Build action
147
        $publishText = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.PUBLISH_BUTTON', 'Publish');
148
        return FormAction::create('publish', $publishText)
149
            ->setIcon('rocket')
150
            ->setSchemaData(['data' => ['buttonStyle' => 'primary']]);
151
    }
152
153
    protected function getFormActions(Controller $controller, $name, $context = [])
154
    {
155
        $record = $context['Record'];
156
157
        if (isset($context['Type']) && $context['Type'] === 'insert') {
158
            $actions = new FieldList(array_filter([
159
                $this->getInsertAction($record),
160
            ]));
161
        } else {
162
            // Build top level bar
163
            $actions = new FieldList(array_filter([
164
                $this->getSaveAction($record),
165
                $this->getPublishAction($record),
166
                $this->getPopoverMenu($record),
167
            ]));
168
        }
169
170
        // Update
171
        $this->invokeWithExtensions('updateFormActions', $actions, $controller, $name, $context);
172
        return $actions;
173
    }
174
175
    /**
176
     * Get raw HTML for image markup
177
     *
178
     * @param File $file
179
     * @return string
180
     */
181
    protected function getIconMarkup($file)
182
    {
183
        $markup = parent::getIconMarkup($file);
184
        if (!$markup) {
185
            return null;
186
        }
187
        if (!$file->exists()) {
188
            return sprintf(
189
                '<div class="%s">%s</div>',
190
                'editor__file-preview-message--file-missing',
191
                _t('AssetAdmin.FILE_MISSING', 'File cannot be found')
192
            );
193
        }
194
        $link = $file->Link();
195
        $linkedImage = sprintf(
196
            '<a class="%s" href="%s" target="_blank">%s</a>',
197
            'editor__file-preview-link',
198
            $link,
199
            $markup
200
        );
201
        return $linkedImage;
202
    }
203
204
    /**
205
     * get HTML for status icon
206
     *
207
     * @param File $record
208
     * @return null|string
209
     */
210
    protected function getSpecsMarkup($record)
211
    {
212
        if (!$record || !$record->exists()) {
213
            return null;
214
        }
215
        return sprintf(
216
            '<div class="editor__specs">%s %s</div>',
217
            $record->getSize(),
218
            $this->getStatusFlagMarkup($record)
219
        );
220
    }
221
222
    /**
223
     * Get published status flag
224
     *
225
     * @param File $record
226
     * @return null|string
227
     */
228
    protected function getStatusFlagMarkup($record)
229
    {
230
        if ($record && ($statusTitle = $record->getStatusTitle())) {
231
            return "<span class=\"editor__status-flag\">{$statusTitle}</span>";
232
        }
233
        return null;
234
    }
235
236
    /**
237
     * Get user-visible "Path" for this record
238
     *
239
     * @param File $record
240
     * @return string
241
     */
242
    protected function getPath($record)
243
    {
244
        if ($record && $record->isInDB()) {
245
            if ($record->ParentID) {
246
                return $record->Parent()->getFilename();
247
            } else {
248
                return '/';
249
            }
250
        }
251
        return null;
252
    }
253
254
    /**
255
     * Get action for adding to campaign
256
     *
257
     * @param File $record
258
     * @return FormAction|null
259
     */
260
    protected function getAddToCampaignAction($record)
261
    {
262
        if ($record && $record->canPublish()) {
263
            return FormAction::create(
264
                'addtocampaign',
265
                _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.ADDTOCAMPAIGN', 'Add to campaign')
266
            );
267
        }
268
        return null;
269
    }
270
271
    /**
272
     * Get action for publishing
273
     *
274
     * @param File $record
275
     * @return FormAction
276
     */
277
    protected function getUnpublishAction($record)
278
    {
279
        // Check if record is unpublishable
280
        if (!$record || !$record->isPublished() || !$record->canUnpublish()) {
281
            return null;
282
        }
283
284
        // Build action
285
        $unpublishText = _t(
286
            'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.UNPUBLISH_BUTTON',
287
            'Unpublish'
288
        );
289
        return FormAction::create('unpublish', $unpublishText)
290
            ->setIcon('cancel-circled');
291
    }
292
293
    /**
294
     * Build popup menu
295
     *
296
     * @param File $record
297
     * @return PopoverField
298
     */
299
    protected function getPopoverMenu($record)
300
    {
301
        // Build popover actions
302
        $popoverActions = array_filter([
303
            $this->getAddToCampaignAction($record),
304
            $this->getUnpublishAction($record),
305
            $this->getDeleteAction($record)
306
        ]);
307
        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...
308
            return PopoverField::create($popoverActions)
309
                ->setPlacement('top');
310
        }
311
        return null;
312
    }
313
314
    /**
315
     * @param File $record
316
     * @return FormAction
317
     */
318
    protected function getInsertAction($record)
319
    {
320
        if ($record && $record->isInDB() && $record->canEdit()) {
321
            return FormAction::create('insert', _t('CMSMain.INSERT', 'Insert file'))
322
                ->setSchemaData(['data' => ['buttonStyle' => 'primary']]);
323
        }
324
        return null;
325
    }
326
}
327