Completed
Pull Request — master (#288)
by Damian
02:08
created

FileFormBuilder::getFormActions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\AssetAdmin\Forms;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\Core\Convert;
7
use SilverStripe\Forms\DatetimeField;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\FormAction;
10
use SilverStripe\Forms\HTMLReadonlyField;
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 FileFormBuilder extends AssetFormBuilder
19
{
20
21
    /**
22
     * Get markdown for clickable link
23
     *
24
     * @return string HTML markdown for link
25
     */
26
    protected function getClickableLinkMarkdown()
27
    {
28
        /** @var File $record */
29
        $record = $this->getRecord();
30
        if (!$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()
43
    {
44
        // Add extra tab
45
        $tabs = TabSet::create(
46
            'Editor',
47
            $this->getFormFieldDetailsTab(),
48
            $this->getFormFieldUsageTab()
49
        );
50
        $this->extendAll('updateFormFieldTabs', $tabs);
51
        return $tabs;
52
    }
53
54
    /**
55
     * Build "Usage" tab
56
     *
57
     * @return Tab
58
     */
59
    protected function getFormFieldUsageTab()
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()
72
    {
73
        // Update details tab
74
        return Tab::create(
75
            'Details',
76
            TextField::create("Title", $this->record->fieldLabel('Title')),
77
            TextField::create('Name', $this->getRecord()->fieldLabel('Filename')),
78
            ReadonlyField::create("Path", _t('AssetTableField.PATH', 'Path'), $this->getPath()),
79
            HTMLReadonlyField::create(
80
                'ClickableURL',
81
                _t('AssetTableField.URL', 'URL'),
82
                $this->getClickableLinkMarkdown()
83
            )
84
        );
85
    }
86
87
    public function getFormFields()
88
    {
89
        // Add status flag before extensions are triggered
90
        $this->beforeUpdate('FormFields', function (FieldList $fields) {
91
            $fields->insertAfter(
92
                'TitleHeader',
93
                LiteralField::create('FileSpecs', $this->getSpecsMarkup())
94
            );
95
        });
96
        return parent::getFormFields();
97
    }
98
99
    /**
100
     * Get publish action
101
     *
102
     * @return FormAction
103
     */
104
    protected function getPublishAction()
105
    {
106
        /** @var File $record */
107
        $record = $this->getRecord();
108
        if (!$record->canPublish()) {
109
            return null;
110
        }
111
112
        // Build action
113
        $publishText = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.PUBLISH_BUTTON', 'Publish');
114
        return FormAction::create('publish', $publishText)
115
            ->setIcon('rocket')
116
            ->setSchemaData(['data' => ['buttonStyle' => 'primary']]);
117
    }
118
119
    public function getFormActions()
120
    {
121
        // Build top level bar
122
        $actions = new FieldList(array_filter([
123
            $this->getSaveAction(),
124
            $this->getPublishAction(),
125
            $this->getPopoverMenu()
126
        ]));
127
128
        // Update
129
        $this->extendAll('updateFormActions', $actions);
130
        return $actions;
131
    }
132
133
    /**
134
     * get HTML for status icon
135
     *
136
     * @return string|null
137
     */
138
    protected function getSpecsMarkup()
139
    {
140
        /** @var File $record */
141
        $record = $this->getRecord();
142
        if (!$record->isInDB()) {
143
            return null;
144
        }
145
        return sprintf(
146
            '<div class="editor__specs">%s %s</div>',
147
            $record->getSize(),
148
            $this->getStatusFlagMarkup()
149
        );
150
    }
151
152
    /**
153
     * Get published status flag
154
     *
155
     * @return null|string
156
     */
157
    protected function getStatusFlagMarkup()
158
    {
159
        /** @var File $record */
160
        $record = $this->getRecord();
161
        $statusTitle = $record->getStatusTitle();
162
        if ($statusTitle) {
163
            return "<span class=\"editor__status-flag\">{$statusTitle}</span>";
164
        }
165
        return null;
166
    }
167
168
    /**
169
     * Get user-visible "Path" for this record
170
     *
171
     * @return string
172
     */
173
    protected function getPath()
174
    {
175
        /** @var File $record */
176
        $record = $this->getRecord();
177
        if (!$record->isInDB()) {
178
            return null;
179
        }
180
        $path = $record->ParentID ? $record->Parent()->getFilename() : '/';
181
        return $path;
182
    }
183
184
    /**
185
     * Get action for adding to campaign
186
     *
187
     * @return FormAction
188
     */
189
    protected function getAddToCampaignAction()
190
    {
191
        return FormAction::create(
192
            'addtocampaign',
193
            _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.ADDTOCAMPAIGN', 'Add to campaign')
194
        );
195
    }
196
197
    /**
198
     * Get action for publishing
199
     *
200
     * @return FormAction
201
     */
202 View Code Duplication
    protected function getUnpublishAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
203
    {
204
        // Check if record is unpublishable
205
        /** @var File $record */
206
        $record = $this->getRecord();
207
        if (!$record->isPublished() || !$record->canUnpublish()) {
208
            return null;
209
        }
210
211
        // Build action
212
        $unpublishText = _t(
213
            'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.UNPUBLISH_BUTTON',
214
            'Unpublish'
215
        );
216
        return FormAction::create('unpublish', $unpublishText)
217
            ->setIcon('cancel-circled');
218
    }
219
220
    /**
221
     * Build popup menu
222
     *
223
     * @return PopoverField
224
     */
225
    protected function getPopoverMenu()
226
    {
227
        // Build popover actions
228
        $popoverActions = array_filter([
229
            $this->getAddToCampaignAction(),
230
            $this->getUnpublishAction(),
231
            $this->getDeleteAction()
232
        ]);
233
        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...
234
            return PopoverField::create($popoverActions)
235
                ->setPlacement('top');
236
        }
237
        return null;
238
    }
239
}
240