Completed
Pull Request — master (#306)
by Will
01:37
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\Core\Convert;
8
use SilverStripe\Forms\DatetimeField;
9
use SilverStripe\Forms\FieldList;
10
use SilverStripe\Forms\FormAction;
11
use SilverStripe\Forms\HTMLReadonlyField;
12
use SilverStripe\Forms\LiteralField;
13
use SilverStripe\Forms\PopoverField;
14
use SilverStripe\Forms\ReadonlyField;
15
use SilverStripe\Forms\Tab;
16
use SilverStripe\Forms\TabSet;
17
use SilverStripe\Forms\TextField;
18
19
class FileFormFactory extends AssetFormFactory
20
{
21
    protected function getFormFieldTabs($record)
22
    {
23
        // Add extra tab
24
        $tabs = TabSet::create(
25
            'Editor',
26
            $this->getFormFieldDetailsTab($record),
27
            $this->getFormFieldUsageTab($record),
28
            $this->getFormFieldHistoryTab($record)
29
        );
30
        return $tabs;
31
    }
32
33
    /**
34
     * Build "Usage" tab
35
     *
36
     * @param File $record
37
     * @return Tab
38
     */
39
    protected function getFormFieldUsageTab($record)
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...
40
    {
41
        // Add new tab for usage
42
        return Tab::create(
43
            'Usage',
44
            DatetimeField::create("Created", _t('AssetTableField.CREATED', 'First uploaded'))
45
                ->setReadonly(true),
46
            DatetimeField::create("LastEdited", _t('AssetTableField.LASTEDIT', 'Last changed'))
47
                ->setReadonly(true)
48
        );
49
    }
50
51
    protected function getFormFieldDetailsTab($record)
52
    {
53
        // Update details tab
54
        return Tab::create(
55
            'Details',
56
            TextField::create("Title", File::singleton()->fieldLabel('Title')),
57
            TextField::create('Name', File::singleton()->fieldLabel('Filename')),
58
            ReadonlyField::create("Path", _t('AssetTableField.PATH', 'Path'), $this->getPath($record))
59
        );
60
    }
61
62
    protected function getFormFieldHistoryTab($record)
63
    {
64
        return Tab::create(
65
            'History',
66
            LiteralField::create('HistoryList','')
67
             ->setSchemaComponent('HistoryList')
68
             ->setSchemaData(array(
69
                'data' => array(
70
                    'fileId' => $record->ID,
71
                    'latestVersionId' => $record->Version
72
                )
73
            ))
74
        );
75
    }
76
77
    protected function getFormFields(Controller $controller, $name, $context = [])
78
    {
79
        $record = $context['Record'];
80
81
        // Add status flag before extensions are triggered
82
        $this->beforeExtending('updateFormFields', function (FieldList $fields) use ($record) {
83
            $fields->insertAfter(
84
                'TitleHeader',
85
                LiteralField::create('FileSpecs', $this->getSpecsMarkup($record))
86
            );
87
        });
88
89
        return parent::getFormFields($controller, $name, $context);
90
    }
91
92
    /**
93
     * Get publish action
94
     *
95
     * @param File $record
96
     * @return FormAction
97
     */
98
    protected function getPublishAction($record)
99
    {
100
        if (!$record || !$record->canPublish()) {
101
            return null;
102
        }
103
104
        // Build action
105
        $publishText = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.PUBLISH_BUTTON', 'Publish');
106
        return FormAction::create('publish', $publishText)
107
            ->setIcon('rocket')
108
            ->setSchemaData(['data' => ['buttonStyle' => 'primary']]);
109
    }
110
111
    protected function getFormActions(Controller $controller, $name, $context = [])
112
    {
113
        $record = $context['Record'];
114
115
        // Build top level bar
116
        $actions = new FieldList(array_filter([
117
            $this->getSaveAction($record),
118
            $this->getPublishAction($record),
119
            $this->getPopoverMenu($record)
120
        ]));
121
122
        // Update
123
        $this->invokeWithExtensions('updateFormActions', $actions, $controller, $name, $context);
124
        return $actions;
125
    }
126
127
    /**
128
     * Get raw HTML for image markup
129
     *
130
     * @param File $file
131
     * @return string
132
     */
133
    protected function getIconMarkup($file)
134
    {
135
        $markup = parent::getIconMarkup($file);
136
        if (!$markup) {
137
            return null;
138
        }
139
        if (!$file->exists()) {
140
            return sprintf('<div class="%s">%s</div>',
141
                'editor__file-preview-message--file-missing',
142
                _t('AssetAdmin.FILE_MISSING', 'File cannot be found')
143
            );
144
        }
145
        $link = $file->Link();
146
        $linkedImage = sprintf(
147
            '<a class="%s" href="%s" target="_blank">%s</a>',
148
            'editor__file-preview-link',
149
            $link,
150
            $markup
151
        );
152
        return $linkedImage;
153
    }
154
155
    /**
156
     * get HTML for status icon
157
     *
158
     * @param File $record
159
     * @return null|string
160
     */
161
    protected function getSpecsMarkup($record)
162
    {
163
        if (!$record || !$record->exists()) {
164
            return null;
165
        }
166
        /**
167
         * Can remove .label and .label-info when Bootstrap has been updated to BS4 Beta
168
         * .label is being replaced with .tag
169
         */
170
        return sprintf(
171
            '<div class="editor__specs">
172
                <span class="label label-info tag tag-info">v.%s</span> %s %s, %s %s
173
            </div>',
174
            $record->Version,
175
            ($record->WasPublished) ? _t('File.PUBLISHED', 'Published') : _t('File.SAVED', 'Saved'),
176
            $record->dbObject('LastEdited')->Ago(),
177
            $record->getSize(),
178
            $this->getStatusFlagMarkup($record)
179
        );
180
    }
181
182
    /**
183
     * Get published status flag
184
     *
185
     * @param File $record
186
     * @return null|string
187
     */
188
    protected function getStatusFlagMarkup($record)
189
    {
190
        if ($record && ($statusTitle = $record->getStatusTitle())) {
191
            return "<span class=\"editor__status-flag\">{$statusTitle}</span>";
192
        }
193
        return null;
194
    }
195
196
    /**
197
     * Get user-visible "Path" for this record
198
     *
199
     * @param File $record
200
     * @return string
201
     */
202
    protected function getPath($record)
203
    {
204
        if ($record && $record->isInDB()) {
205
            if ($record->ParentID) {
206
                return $record->Parent()->getFilename();
207
            } else {
208
                return '/';
209
            }
210
        }
211
        return null;
212
    }
213
214
    /**
215
     * Get action for adding to campaign
216
     *
217
     * @param File $record
218
     * @return FormAction|null
219
     */
220
    protected function getAddToCampaignAction($record)
221
    {
222
        if ($record && $record->canPublish()) {
223
            return FormAction::create(
224
                'addtocampaign',
225
                _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.ADDTOCAMPAIGN', 'Add to campaign')
226
            );
227
        }
228
        return null;
229
    }
230
231
    /**
232
     * Get action for publishing
233
     *
234
     * @param File $record
235
     * @return FormAction
236
     */
237
    protected function getUnpublishAction($record)
238
    {
239
        // Check if record is unpublishable
240
        if (!$record || !$record->isPublished() || !$record->canUnpublish()) {
241
            return null;
242
        }
243
244
        // Build action
245
        $unpublishText = _t(
246
            'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.UNPUBLISH_BUTTON',
247
            'Unpublish'
248
        );
249
        return FormAction::create('unpublish', $unpublishText)
250
            ->setIcon('cancel-circled');
251
    }
252
253
    /**
254
     * Build popup menu
255
     *
256
     * @param File $record
257
     * @return PopoverField
258
     */
259
    protected function getPopoverMenu($record)
260
    {
261
        // Build popover actions
262
        $popoverActions = array_filter([
263
            $this->getAddToCampaignAction($record),
264
            $this->getUnpublishAction($record),
265
            $this->getDeleteAction($record)
266
        ]);
267
        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...
268
            return PopoverField::create($popoverActions)
269
                ->setPlacement('top');
270
        }
271
        return null;
272
    }
273
}
274