Completed
Push — master ( 802e60...e1e82b )
by Damian
10s
created

FileFormFactory::getIconMarkup()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
rs 9.2
cc 3
eloc 16
nc 3
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A FileFormFactory::getSpecsMarkup() 0 11 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\HiddenField;
11
use SilverStripe\Forms\LiteralField;
12
use SilverStripe\Forms\PopoverField;
13
use SilverStripe\Forms\ReadonlyField;
14
use SilverStripe\Forms\Tab;
15
use SilverStripe\Forms\TabSet;
16
use SilverStripe\Forms\TextField;
17
18
class FileFormFactory extends AssetFormFactory
19
{
20
    protected function getFormFieldTabs($record, $context = [])
21
    {
22
        // Add extra tab
23
        $tabs = TabSet::create(
24
            'Editor',
25
            $this->getFormFieldDetailsTab($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 $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...
52
    {
53
        // Add new tab for usage
54
        return Tab::create(
55
            'Usage',
56
            DatetimeField::create("Created", _t('AssetTableField.CREATED', 'First uploaded'))
57
                ->setReadonly(true),
58
            DatetimeField::create("LastEdited", _t('AssetTableField.LASTEDIT', 'Last changed'))
59
                ->setReadonly(true)
60
        );
61
    }
62
63
    protected function getFormFieldDetailsTab($record, $context = [])
64
    {
65
        // Update details tab
66
        $tab = Tab::create(
67
            'Details',
68
            TextField::create("Title", File::singleton()->fieldLabel('Title')),
69
            TextField::create('Name', File::singleton()->fieldLabel('Filename')),
70
            ReadonlyField::create("Path", _t('AssetTableField.PATH', 'Path'), $this->getPath($record))
71
        );
72
73
        if ($this->getFormType($context) !== static::TYPE_ADMIN) {
74
            $tab->push(LiteralField::create(
75
                'EditLink',
76
                sprintf(
77
                    '<a href="%s" class="%s" target="_blank"><i class="%s" />%s</a>',
78
                    $record->CMSEditLink(),
79
                    'btn btn-secondary-outline font-icon-edit editor__edit-link',
80
                    '',
81
                    _t('AssetAdmin.EditLink', 'Edit original file')
82
                )
83
            ));
84
        }
85
        return $tab;
86
    }
87
88
    /**
89
     * Create tab for file attributes
90
     *
91
     * @param File $record
92
     * @param array $context
93
     * @return Tab
94
     */
95
    protected function getFormFieldAttributesTab($record, $context = [])
96
    {
97
        return Tab::create(
98
            'Placement',
99
            LiteralField::create(
100
                'AttributesDescription',
101
                '<p>'. _t(
102
                    'AssetAdmin.AttributesDescription',
103
                    'These changes will only affect this particular placement of the file.'
104
                ) .'</p>'
105
            ),
106
            TextField::create('Caption', _t('AssetAdmin.Caption', 'Caption'))
107
        );
108
    }
109
110
    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...
111
    {
112
        return Tab::create(
113
            'History',
114
            HistoryListField::create('HistoryList')
115
                ->setRecord($record)
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
            // @todo move specs to a component/class, so it can update specs when a File is replaced
126
            $fields->insertAfter(
127
                'TitleHeader',
128
                LiteralField::create('FileSpecs', $this->getSpecsMarkup($record))
129
            );
130
            $fields->push(HiddenField::create('FileFilename'));
131
            $fields->push(HiddenField::create('FileHash'));
132
            $fields->push(HiddenField::create('FileVariant'));
133
        });
134
135
        return parent::getFormFields($controller, $name, $context);
136
    }
137
138
    /**
139
     * Get publish action
140
     *
141
     * @param File $record
142
     * @return FormAction
143
     */
144
    protected function getPublishAction($record)
145
    {
146
        if (!$record || !$record->canPublish()) {
147
            return null;
148
        }
149
150
        // Build action
151
        $publishText = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.PUBLISH_BUTTON', 'Publish');
152
        return FormAction::create('publish', $publishText)
153
            ->setIcon('rocket')
154
            ->setSchemaData(['data' => ['buttonStyle' => 'primary']]);
155
    }
156
157
    protected function getFormActions(Controller $controller, $name, $context = [])
158
    {
159
        $record = $context['Record'];
160
161
        if ($this->getFormType($context) !== static::TYPE_ADMIN) {
162
            $actions = new FieldList(array_filter([
163
                $this->getInsertAction($record),
164
            ]));
165
        } else {
166
            // Build top level bar
167
            $actions = new FieldList(array_filter([
168
                $this->getSaveAction($record),
169
                $this->getPublishAction($record),
170
                $this->getPopoverMenu($record),
171
            ]));
172
        }
173
174
        // Update
175
        $this->invokeWithExtensions('updateFormActions', $actions, $controller, $name, $context);
176
        return $actions;
177
    }
178
179
    /**
180
     * get HTML for status icon
181
     *
182
     * @param File $record
183
     * @return null|string
184
     */
185
    protected function getSpecsMarkup($record)
186
    {
187
        if (!$record || !$record->exists()) {
188
            return null;
189
        }
190
        return sprintf(
191
            '<div class="editor__specs">%s %s</div>',
192
            $record->getSize(),
193
            $this->getStatusFlagMarkup($record)
194
        );
195
    }
196
197
    /**
198
     * Get published status flag
199
     *
200
     * @param File $record
201
     * @return null|string
202
     */
203
    protected function getStatusFlagMarkup($record)
204
    {
205
        if ($record && ($statusTitle = $record->getStatusTitle())) {
206
            return "<span class=\"editor__status-flag\">{$statusTitle}</span>";
207
        }
208
        return null;
209
    }
210
211
    /**
212
     * Get user-visible "Path" for this record
213
     *
214
     * @param File $record
215
     * @return string
216
     */
217
    protected function getPath($record)
218
    {
219
        if ($record && $record->isInDB()) {
220
            if ($record->ParentID) {
221
                return $record->Parent()->getFilename();
222
            } else {
223
                return '/';
224
            }
225
        }
226
        return null;
227
    }
228
229
    /**
230
     * Get action for adding to campaign
231
     *
232
     * @param File $record
233
     * @return FormAction|null
234
     */
235
    protected function getAddToCampaignAction($record)
236
    {
237
        if ($record && $record->canPublish()) {
238
            return FormAction::create(
239
                'addtocampaign',
240
                _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.ADDTOCAMPAIGN', 'Add to campaign')
241
            );
242
        }
243
        return null;
244
    }
245
246
    /**
247
     * Get action for publishing
248
     *
249
     * @param File $record
250
     * @return FormAction
251
     */
252
    protected function getUnpublishAction($record)
253
    {
254
        // Check if record is unpublishable
255
        if (!$record || !$record->isPublished() || !$record->canUnpublish()) {
256
            return null;
257
        }
258
259
        // Build action
260
        $unpublishText = _t(
261
            'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.UNPUBLISH_BUTTON',
262
            'Unpublish'
263
        );
264
        return FormAction::create('unpublish', $unpublishText)
265
            ->setIcon('cancel-circled');
266
    }
267
268
    /**
269
     * Build popup menu
270
     *
271
     * @param File $record
272
     * @return PopoverField
273
     */
274
    protected function getPopoverMenu($record)
275
    {
276
        // Build popover actions
277
        $popoverActions = array_filter([
278
            $this->getAddToCampaignAction($record),
279
            $this->getUnpublishAction($record),
280
            $this->getDeleteAction($record)
281
        ]);
282
        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...
283
            return PopoverField::create($popoverActions)
284
                ->setPlacement('top')
285
                ->setButtonTooltip(_t(
286
                    'SilverStripe\\AssetAdmin\\Forms\\FileFormFactory.OTHER_ACTIONS',
287
                    'Other actions'
288
                ));
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