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

AssetFormBuilder::getIconMarkup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
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\FieldList;
8
use SilverStripe\Forms\FormAction;
9
use SilverStripe\Forms\HeaderField;
10
use SilverStripe\Forms\HiddenField;
11
use SilverStripe\Forms\LiteralField;
12
use SilverStripe\Forms\Tab;
13
use SilverStripe\Forms\TabSet;
14
use SilverStripe\Forms\DefaultFormBuilder;
15
use SilverStripe\Forms\TextField;
16
17
abstract class AssetFormBuilder extends DefaultFormBuilder
18
{
19
20
    /**
21
     * Get raw HTML for image markup
22
     *
23
     * @return string
24
     */
25
    protected function getIconMarkup()
26
    {
27
        /** @var File $file */
28
        $file = $this->getRecord();
29
        $previewLink = Convert::raw2att($file->PreviewLink());
30
        return "<img src=\"{$previewLink}\" class=\"editor__thumbnail\" />";
31
    }
32
33
    /**
34
     * Gets the main tabs for the file edit form
35
     *
36
     * @return TabSet
37
     */
38
    protected function getFormFieldTabs()
39
    {
40
        $tabs = TabSet::create('Editor', $this->getFormFieldDetailsTab());
41
        $this->extendAll('updateFormFieldTabs', $tabs);
42
        return $tabs;
43
    }
44
45
    /**
46
     * @return FormAction
47
     */
48
    protected function getSaveAction()
49
    {
50
        $record = $this->getRecord();
51
        $editing = $record->isInDB();
52
        $canSave = $editing ? $record->canEdit() : $record->canCreate();
53
        if ($canSave) {
54
            return FormAction::create('save', _t('CMSMain.SAVE', 'Save'))->setIcon('save');
55
        }
56
        return null;
57
    }
58
59
    /**
60
     * Get delete action, if this record is deletable
61
     *
62
     * @return FormAction
63
     */
64 View Code Duplication
    protected function getDeleteAction()
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...
65
    {
66
        // Delete action
67
        $record = $this->getRecord();
68
        if ($record->isInDB() && $record->canDelete()) {
69
            $deleteText = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.DELETE_BUTTON', 'Delete');
70
            return FormAction::create('delete', $deleteText)->setIcon('trash-bin');
71
        }
72
        return null;
73
    }
74
75
    /**
76
     * Build basic actions
77
     *
78
     * @return FieldList
79
     */
80
    public function getFormActions()
81
    {
82
        $actions = new FieldList();
83
        if ($saveAction = $this->getSaveAction()) {
84
            $actions->push($saveAction);
85
        }
86
87
        $this->extendAll('updateFormActions', $actions);
88
        return $actions;
89
    }
90
91
    public function getFormFields()
92
    {
93
        // Build standard fields for all folders / files
94
        /** @var File $record */
95
        $record = $this->getRecord();
96
        $fields = new FieldList(
97
            HeaderField::create('TitleHeader', $record->Title, 1)
98
                ->addExtraClass('editor__heading'),
99
            LiteralField::create("IconFull", $this->getIconMarkup())
100
                ->addExtraClass('editor__file-preview'),
101
            $this->getFormFieldTabs(),
102
            HiddenField::create('ID', $record->ID)
103
        );
104
105
        $this->extendAll('updateFormFields', $fields);
106
        return $fields;
107
    }
108
109
    /**
110
     * Build "details" formfield tab
111
     *
112
     * @return Tab
113
     */
114
    protected function getFormFieldDetailsTab()
115
    {
116
        return Tab::create(
117
            'Details',
118
            TextField::create('Name', $this->getRecord()->fieldLabel('Filename'))
119
        );
120
    }
121
}
122