Completed
Pull Request — master (#307)
by
unknown
01:52
created

FileFormFactory::getFormFieldAttributesTab()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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