Completed
Push — master ( cb6000...0f63fa )
by Sam
02:23
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
22
    /**
23
     * Get markdown for clickable link
24
     *
25
     * @param File $record
26
     * @return string HTML markdown for link
27
     */
28
    protected function getClickableLinkMarkdown($record)
29
    {
30
        if (!$record || !$record->isInDB()) {
31
            return null;
32
        }
33
        $link = $record->Link();
34
        $clickableLink = sprintf(
35
            '<i class="%1$s"></i><a href="%2$s" target="_blank">%2$s</a>',
36
            'font-icon-link btn--icon-large form-control-static__icon',
37
            Convert::raw2xml($link)
38
        );
39
        return $clickableLink;
40
    }
41
42
    protected function getFormFieldTabs($record)
43
    {
44
        // Add extra tab
45
        $tabs = TabSet::create(
46
            'Editor',
47
            $this->getFormFieldDetailsTab($record),
48
            $this->getFormFieldUsageTab($record)
49
        );
50
        return $tabs;
51
    }
52
53
    /**
54
     * Build "Usage" tab
55
     *
56
     * @param File $record
57
     * @return Tab
58
     */
59
    protected function getFormFieldUsageTab($record)
60
    {
61
        // Add new tab for usage
62
        return Tab::create(
63
            'Usage',
64
            DatetimeField::create("Created", _t('AssetTableField.CREATED', 'First uploaded'))
65
                ->setReadonly(true),
66
            DatetimeField::create("LastEdited", _t('AssetTableField.LASTEDIT', 'Last changed'))
67
                ->setReadonly(true)
68
        );
69
    }
70
71
    protected function getFormFieldDetailsTab($record)
72
    {
73
        // Update details tab
74
        return Tab::create(
75
            'Details',
76
            TextField::create("Title", File::singleton()->fieldLabel('Title')),
77
            TextField::create('Name', File::singleton()->fieldLabel('Filename')),
78
            ReadonlyField::create("Path", _t('AssetTableField.PATH', 'Path'), $this->getPath($record)),
79
            HTMLReadonlyField::create(
80
                'ClickableURL',
81
                _t('AssetTableField.URL', 'URL'),
82
                $this->getClickableLinkMarkdown($record)
83
            )
84
        );
85
    }
86
87
    protected function getFormFields(Controller $controller, $name, $context = [])
88
    {
89
        $record = $context['Record'];
90
91
        // Add status flag before extensions are triggered
92
        $this->beforeExtending('updateFormFields', function (FieldList $fields) use ($record) {
93
            $fields->insertAfter(
94
                'TitleHeader',
95
                LiteralField::create('FileSpecs', $this->getSpecsMarkup($record))
96
            );
97
        });
98
99
        return parent::getFormFields($controller, $name, $context);
100
    }
101
102
    /**
103
     * Get publish action
104
     *
105
     * @param File $record
106
     * @return FormAction
107
     */
108
    protected function getPublishAction($record)
109
    {
110
        if (!$record || !$record->canPublish()) {
111
            return null;
112
        }
113
114
        // Build action
115
        $publishText = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.PUBLISH_BUTTON', 'Publish');
116
        return FormAction::create('publish', $publishText)
117
            ->setIcon('rocket')
118
            ->setSchemaData(['data' => ['buttonStyle' => 'primary']]);
119
    }
120
121
    protected function getFormActions(Controller $controller, $name, $context = [])
122
    {
123
        $record = $context['Record'];
124
125
        // Build top level bar
126
        $actions = new FieldList(array_filter([
127
            $this->getSaveAction($record),
128
            $this->getPublishAction($record),
129
            $this->getPopoverMenu($record)
130
        ]));
131
132
        // Update
133
        $this->invokeWithExtensions('updateFormActions', $actions, $controller, $name, $context);
134
        return $actions;
135
    }
136
137
    /**
138
     * get HTML for status icon
139
     *
140
     * @param File $record
141
     * @return null|string
142
     */
143
    protected function getSpecsMarkup($record)
144
    {
145
        if (!$record || !$record->exists()) {
146
            return null;
147
        }
148
        return sprintf(
149
            '<div class="editor__specs">%s %s</div>',
150
            $record->getSize(),
151
            $this->getStatusFlagMarkup($record)
152
        );
153
    }
154
155
    /**
156
     * Get published status flag
157
     *
158
     * @param File $record
159
     * @return null|string
160
     */
161
    protected function getStatusFlagMarkup($record)
162
    {
163
        if ($record && ($statusTitle = $record->getStatusTitle())) {
164
            return "<span class=\"editor__status-flag\">{$statusTitle}</span>";
165
        }
166
        return null;
167
    }
168
169
    /**
170
     * Get user-visible "Path" for this record
171
     *
172
     * @param File $record
173
     * @return string
174
     */
175
    protected function getPath($record)
176
    {
177
        if ($record && $record->isInDB()) {
178
            if ($record->ParentID) {
179
                return $record->Parent()->getFilename();
180
            } else {
181
                return '/';
182
            }
183
        }
184
        return null;
185
    }
186
187
    /**
188
     * Get action for adding to campaign
189
     *
190
     * @param File $record
191
     * @return FormAction|null
192
     */
193
    protected function getAddToCampaignAction($record)
194
    {
195
        if ($record && $record->canPublish()) {
196
            return FormAction::create(
197
                'addtocampaign',
198
                _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.ADDTOCAMPAIGN', 'Add to campaign')
199
            );
200
        }
201
        return null;
202
    }
203
204
    /**
205
     * Get action for publishing
206
     *
207
     * @param File $record
208
     * @return FormAction
209
     */
210
    protected function getUnpublishAction($record)
211
    {
212
        // Check if record is unpublishable
213
        if (!$record || !$record->isPublished() || !$record->canUnpublish()) {
214
            return null;
215
        }
216
217
        // Build action
218
        $unpublishText = _t(
219
            'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.UNPUBLISH_BUTTON',
220
            'Unpublish'
221
        );
222
        return FormAction::create('unpublish', $unpublishText)
223
            ->setIcon('cancel-circled');
224
    }
225
226
    /**
227
     * Build popup menu
228
     *
229
     * @param File $record
230
     * @return PopoverField
231
     */
232
    protected function getPopoverMenu($record)
233
    {
234
        // Build popover actions
235
        $popoverActions = array_filter([
236
            $this->getAddToCampaignAction($record),
237
            $this->getUnpublishAction($record),
238
            $this->getDeleteAction($record)
239
        ]);
240
        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...
241
            return PopoverField::create($popoverActions)
242
                ->setPlacement('top');
243
        }
244
        return null;
245
    }
246
}
247