Completed
Pull Request — master (#306)
by Will
01:42
created

FileFormFactory::getPopoverMenu()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
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
        return sprintf(
167
            '<div class="editor__specs">%s %s</div>',
168
            $record->getSize(),
169
            $this->getStatusFlagMarkup($record)
170
        );
171
    }
172
173
    /**
174
     * Get published status flag
175
     *
176
     * @param File $record
177
     * @return null|string
178
     */
179
    protected function getStatusFlagMarkup($record)
180
    {
181
        if ($record && ($statusTitle = $record->getStatusTitle())) {
182
            return "<span class=\"editor__status-flag\">{$statusTitle}</span>";
183
        }
184
        return null;
185
    }
186
187
    /**
188
     * Get user-visible "Path" for this record
189
     *
190
     * @param File $record
191
     * @return string
192
     */
193
    protected function getPath($record)
194
    {
195
        if ($record && $record->isInDB()) {
196
            if ($record->ParentID) {
197
                return $record->Parent()->getFilename();
198
            } else {
199
                return '/';
200
            }
201
        }
202
        return null;
203
    }
204
205
    /**
206
     * Get action for adding to campaign
207
     *
208
     * @param File $record
209
     * @return FormAction|null
210
     */
211
    protected function getAddToCampaignAction($record)
212
    {
213
        if ($record && $record->canPublish()) {
214
            return FormAction::create(
215
                'addtocampaign',
216
                _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.ADDTOCAMPAIGN', 'Add to campaign')
217
            );
218
        }
219
        return null;
220
    }
221
222
    /**
223
     * Get action for publishing
224
     *
225
     * @param File $record
226
     * @return FormAction
227
     */
228
    protected function getUnpublishAction($record)
229
    {
230
        // Check if record is unpublishable
231
        if (!$record || !$record->isPublished() || !$record->canUnpublish()) {
232
            return null;
233
        }
234
235
        // Build action
236
        $unpublishText = _t(
237
            'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.UNPUBLISH_BUTTON',
238
            'Unpublish'
239
        );
240
        return FormAction::create('unpublish', $unpublishText)
241
            ->setIcon('cancel-circled');
242
    }
243
244
    /**
245
     * Build popup menu
246
     *
247
     * @param File $record
248
     * @return PopoverField
249
     */
250
    protected function getPopoverMenu($record)
251
    {
252
        // Build popover actions
253
        $popoverActions = array_filter([
254
            $this->getAddToCampaignAction($record),
255
            $this->getUnpublishAction($record),
256
            $this->getDeleteAction($record)
257
        ]);
258
        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...
259
            return PopoverField::create($popoverActions)
260
                ->setPlacement('top');
261
        }
262
        return null;
263
    }
264
}
265