Completed
Pull Request — master (#307)
by Damian
12:34
created

FileFormFactory::getFormFieldTabs()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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