Completed
Pull Request — master (#307)
by Damian
01:45
created

FileFormFactory::getFormActions()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 3
eloc 12
nc 2
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
            $details = $this->getFormFieldDetailsTab($record, $context),
25
            $this->getFormFieldUsageTab($record, $context)
26
        );
27
28
        if (isset($context['Type']) && $context['Type'] === 'insert') {
29
            $tabs->setReadonly(true);
30
            $tabs->unshift($this->getFormFieldAttributesTab($record));
31
        }
32
33
        return $tabs;
34
    }
35
36
    /**
37
     * Build "Usage" tab
38
     *
39
     * @param File $record
40
     * @param array $context
41
     * @return Tab
42
     */
43
    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...
44
    {
45
        // Add new tab for usage
46
        return Tab::create(
47
            'Usage',
48
            DatetimeField::create("Created", _t('AssetTableField.CREATED', 'First uploaded'))
49
                ->setReadonly(true),
50
            DatetimeField::create("LastEdited", _t('AssetTableField.LASTEDIT', 'Last changed'))
51
                ->setReadonly(true)
52
        );
53
    }
54
55
    protected function getFormFieldDetailsTab($record, $context = [])
56
    {
57
        // Update details tab
58
        $tab = Tab::create(
59
            'Details',
60
            TextField::create("Title", File::singleton()->fieldLabel('Title')),
61
            TextField::create('Name', File::singleton()->fieldLabel('Filename')),
62
            ReadonlyField::create("Path", _t('AssetTableField.PATH', 'Path'), $this->getPath($record))
63
        );
64
65
        if (isset($context['Type']) && $context['Type'] === 'insert') {
66
            $tab->push(LiteralField::create('EditLink',
67
                sprintf('<a href="%s" class="%s" target="_blank"><i class="%s" />%s</a>',
68
                    $record->CMSEditLink(),
69
                    'btn btn-secondary-outline font-icon-edit editor__edit-link',
70
                    '',
71
                    _t('AssetAdmin.EditLink', 'Edit original file')
72
                )
73
            ));
74
        }
75
        return $tab;
76
    }
77
78
    /**
79
     * Create tab for file attributes
80
     *
81
     * @param File $record
82
     * @param array $context
83
     * @return Tab
84
     */
85
    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...
86
    {
87
        return Tab::create(
88
            'Placement',
89
            LiteralField::create('AttributesDescription',
90
                '<p>'. _t(
91
                    'AssetAdmin.AttributesDescription',
92
                    'These changes will only affect this particular placement of the file.'
93
                ) .'</p>'
94
            ),
95
            TextField::create('Caption', _t('AssetAdmin.Caption', 'Caption'))
96
        );
97
    }
98
99
    protected function getFormFields(Controller $controller, $name, $context = [])
100
    {
101
        $record = $context['Record'];
102
103
        // Add status flag before extensions are triggered
104
        $this->beforeExtending('updateFormFields', function (FieldList $fields) use ($record) {
105
            $fields->insertAfter(
106
                'TitleHeader',
107
                LiteralField::create('FileSpecs', $this->getSpecsMarkup($record))
108
            );
109
        });
110
111
        return parent::getFormFields($controller, $name, $context);
112
    }
113
114
    /**
115
     * Get publish action
116
     *
117
     * @param File $record
118
     * @return FormAction
119
     */
120
    protected function getPublishAction($record)
121
    {
122
        if (!$record || !$record->canPublish()) {
123
            return null;
124
        }
125
126
        // Build action
127
        $publishText = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.PUBLISH_BUTTON', 'Publish');
128
        return FormAction::create('publish', $publishText)
129
            ->setIcon('rocket')
130
            ->setSchemaData(['data' => ['buttonStyle' => 'primary']]);
131
    }
132
133
    protected function getFormActions(Controller $controller, $name, $context = [])
134
    {
135
        $record = $context['Record'];
136
137
        if (isset($context['Type']) && $context['Type'] === 'insert') {
138
            $actions = new FieldList(array_filter([
139
                $this->getInsertAction($record),
140
            ]));
141
        } else {
142
            // Build top level bar
143
            $actions = new FieldList(array_filter([
144
                $this->getSaveAction($record),
145
                $this->getPublishAction($record),
146
                $this->getPopoverMenu($record),
147
            ]));
148
        }
149
150
        // Update
151
        $this->invokeWithExtensions('updateFormActions', $actions, $controller, $name, $context);
152
        return $actions;
153
    }
154
155
    /**
156
     * Get raw HTML for image markup
157
     *
158
     * @param File $file
159
     * @return string
160
     */
161
    protected function getIconMarkup($file)
162
    {
163
        $markup = parent::getIconMarkup($file);
164
        if (!$markup) {
165
            return null;
166
        }
167
        if (!$file->exists()) {
168
            return sprintf('<div class="%s">%s</div>',
169
                'editor__file-preview-message--file-missing',
170
                _t('AssetAdmin.FILE_MISSING', 'File cannot be found')
171
            );
172
        }
173
        $link = $file->Link();
174
        $linkedImage = sprintf(
175
            '<a class="%s" href="%s" target="_blank">%s</a>',
176
            'editor__file-preview-link',
177
            $link,
178
            $markup
179
        );
180
        return $linkedImage;
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
        }
290
        return null;
291
    }
292
293
    /**
294
     * @param File $record
295
     * @return FormAction
296
     */
297
    protected function getInsertAction($record)
298
    {
299
        if ($record && $record->isInDB() && $record->canEdit()) {
300
            return FormAction::create('insert', _t('CMSMain.INSERT', 'Insert file'))
301
                ->setSchemaData(['data' => ['buttonStyle' => 'primary']]);
302
        }
303
        return null;
304
    }
305
306
}
307