Completed
Push — master ( cb6000...0f63fa )
by Sam
02:23
created

AssetFormFactory::getFormFields()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 2
nop 3
1
<?php
2
3
namespace SilverStripe\AssetAdmin\Forms;
4
5
use InvalidArgumentException;
6
use SilverStripe\Assets\File;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\Core\Convert;
9
use SilverStripe\Core\Extensible;
10
use SilverStripe\Core\Injector\Injectable;
11
use SilverStripe\Forms\FieldList;
12
use SilverStripe\Forms\Form;
13
use SilverStripe\Forms\FormAction;
14
use SilverStripe\Forms\FormFactory;
15
use SilverStripe\Forms\HeaderField;
16
use SilverStripe\Forms\HiddenField;
17
use SilverStripe\Forms\LiteralField;
18
use SilverStripe\Forms\Tab;
19
use SilverStripe\Forms\TabSet;
20
use SilverStripe\Forms\TextField;
21
22
abstract class AssetFormFactory implements FormFactory
23
{
24
    use Extensible;
25
    use Injectable;
26
27
	public function __construct() {
28
		$this->constructExtensions();
29
	}
30
31
	public function getForm(Controller $controller, $name = FormFactory::DEFAULT_NAME, $context = [])
32
	{
33
		// Validate context
34
		foreach($this->getRequiredContext() as $required) {
35
			if (!isset($context[$required])) {
36
				throw new InvalidArgumentException("Missing required context $required");
37
			}
38
		}
39
40
		$fields = $this->getFormFields($controller, $name, $context);
41
		$actions = $this->getFormActions($controller, $name, $context);
42
		$form = Form::create($controller, $name, $fields, $actions);
43
44
		// Extend form
45
		$this->invokeWithExtensions('updateForm', $form, $controller, $name, $context);
46
47
		// Populate form from record
48
		$form->loadDataFrom($context['Record']);
49
50
		return $form;
51
	}
52
53
    /**
54
     * Get raw HTML for image markup
55
     *
56
     * @param File $file
57
     * @return string
58
     */
59
    protected function getIconMarkup($file)
60
    {
61
        if (!$file) {
62
            return null;
63
        }
64
        $previewLink = Convert::raw2att($file->PreviewLink());
65
        return "<img src=\"{$previewLink}\" class=\"editor__thumbnail\" />";
66
    }
67
68
    /**
69
     * Gets the main tabs for the file edit form
70
     *
71
     * @param File $record
72
     * @return TabSet
73
     */
74
    protected function getFormFieldTabs($record)
75
    {
76
        $tabs = TabSet::create('Editor', $this->getFormFieldDetailsTab($record));
77
        return $tabs;
78
    }
79
80
    /**
81
     * @param File $record
82
     * @return FormAction
83
     */
84 View Code Duplication
    protected function getSaveAction($record)
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...
85
    {
86
        if ($record && $record->isInDB() && $record->canEdit()) {
87
            return FormAction::create('save', _t('CMSMain.SAVE', 'Save'))
88
                ->setIcon('save');
89
        }
90
        return null;
91
    }
92
93
    /**
94
     * Get delete action, if this record is deletable
95
     *
96
     * @param File $record
97
     * @return FormAction
98
     */
99 View Code Duplication
    protected function getDeleteAction($record)
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...
100
    {
101
        // Delete action
102
        if ($record && $record->isInDB() && $record->canDelete()) {
103
            $deleteText = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.DELETE_BUTTON', 'Delete');
104
            return FormAction::create('delete', $deleteText)
105
                ->setIcon('trash-bin');
106
        }
107
        return null;
108
    }
109
110
	protected function getFormActions(Controller $controller, $name, $context = [])
111
    {
112
        $record = $context['Record'];
113
114
        $actions = new FieldList();
115
        if ($saveAction = $this->getSaveAction($record)) {
116
            $actions->push($saveAction);
117
        }
118
119
        $this->invokeWithExtensions('updateFormActions', $actions, $controller, $name, $context);
120
        return $actions;
121
    }
122
123
    protected function getFormFields(Controller $controller, $name, $context = [])
124
    {
125
        $record = $context['Record'];
126
127
        // Build standard fields for all folders / files
128
        /** @var File $record */
129
        $fields = new FieldList(
130
            HeaderField::create('TitleHeader', $record ? $record->Title : null, 1)
131
                ->addExtraClass('editor__heading'),
132
            LiteralField::create("IconFull", $this->getIconMarkup($record))
133
                ->addExtraClass('editor__file-preview'),
134
            $this->getFormFieldTabs($record)
135
        );
136
        if ($record) {
137
            $fields->push(HiddenField::create('ID', $record->ID));
138
        }
139
140
        $this->invokeWithExtensions('updateFormFields', $fields, $controller, $name, $context);
141
        return $fields;
142
    }
143
144
    /**
145
     * Build "details" formfield tab
146
     *
147
     * @param File $record
148
     * @return Tab
149
     */
150
    protected function getFormFieldDetailsTab($record)
151
    {
152
        return Tab::create(
153
            'Details',
154
            TextField::create('Name', File::singleton()->fieldLabel('Filename'))
155
        );
156
    }
157
158
    public function getRequiredContext()
159
    {
160
        return ['Record'];
161
    }
162
}
163