Completed
Pull Request — master (#322)
by Damian
01:59
created

FileFormFactory::getFormFieldDetailsTab()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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