Passed
Pull Request — master (#8)
by Robbie
03:25
created

DataObjectVersionFormFactory::getForm()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 7
nop 3
dl 0
loc 29
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\VersionedAdmin\Forms;
4
5
use InvalidArgumentException;
6
use SilverStripe\Control\RequestHandler;
7
use SilverStripe\Core\Config\Configurable;
8
use SilverStripe\Core\Extensible;
9
use SilverStripe\Core\Injector\Injectable;
10
use SilverStripe\Forms\CompositeField;
11
use SilverStripe\Forms\FieldList;
12
use SilverStripe\Forms\Form;
13
use SilverStripe\Forms\FormFactory;
14
use SilverStripe\Forms\FormField;
15
use SilverStripe\Forms\Tab;
16
use SilverStripe\Forms\TabSet;
17
use SilverStripe\ORM\DataObject;
18
19
class DataObjectVersionFormFactory implements FormFactory
20
{
21
    use Configurable;
22
    use Extensible;
23
    use Injectable;
24
25
    /**
26
     * View a version of the record, readonly: Default.
27
     *
28
     * @var string
29
     */
30
    const TYPE_HISTORY = 'history';
31
32
    /**
33
     * Define context types that will automatically be converted to readonly forms
34
     *
35
     * @config
36
     * @var string[]
37
     */
38
    private static $readonly_types = [
0 ignored issues
show
introduced by
The private property $readonly_types is not used, and could be removed.
Loading history...
39
        self::TYPE_HISTORY,
40
    ];
41
42
    public function getForm(RequestHandler $controller = null, $name = FormFactory::DEFAULT_NAME, $context = [])
43
    {
44
        // Validate context
45
        foreach ($this->getRequiredContext() as $required) {
46
            if (!isset($context[$required])) {
47
                throw new InvalidArgumentException("Missing required context $required");
48
            }
49
        }
50
51
        $fields = $this->getFormFields($controller, $name, $context);
52
        $actions = $this->getFormActions($controller, $name, $context);
53
        $form = Form::create($controller, $name, $fields, $actions);
0 ignored issues
show
Bug introduced by
It seems like $controller can also be of type SilverStripe\Control\RequestHandler; however, parameter $args of SilverStripe\View\ViewableData::create() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        $form = Form::create(/** @scrutinizer ignore-type */ $controller, $name, $fields, $actions);
Loading history...
54
55
        $this->invokeWithExtensions('updateForm', $form, $controller, $name, $context);
56
57
        // Populate form from record
58
        if (isset($context['Record'])) {
59
            /** @var DataObject $record */
60
            $record = $context['Record'];
61
            $form->loadDataFrom($record);
62
63
            // Mark as readonly for some types
64
            if ($this->isReadonlyFormType($context)) {
65
                $form->makeReadonly();
66
            }
67
        }
68
69
        $form->addExtraClass('form--fill-height form--no-dividers');
70
        return $form;
71
    }
72
73
    /**
74
     * Get form type from 'type' context
75
     *
76
     * @param array $context
77
     * @return string
78
     */
79
    public function getFormType(array $context)
80
    {
81
        return empty($context['Type']) ? static::TYPE_HISTORY : $context['Type'];
82
    }
83
84
    /**
85
     * Get whether the current form type should be treated as readonly
86
     *
87
     * @param array $context
88
     * @return bool
89
     */
90
    public function isReadonlyFormType(array $context)
91
    {
92
        return in_array($this->getFormType($context), $this->config()->get('readonly_types'));
93
    }
94
95
    protected function getFormFields(RequestHandler $controller = null, $name, $context = [])
96
    {
97
        $record = $context['Record'];
98
        /** @var FieldList $fields */
99
        $fields = $record->getCMSFields();
100
101
        $this->removeHistoryViewerFields($fields);
102
        $this->removeSelectedRightTitles($fields);
103
104
        $this->invokeWithExtensions('updateFormFields', $fields, $controller, $name, $context);
105
106
        return $fields;
107
    }
108
109
    /**
110
     * Do not return {@link HistoryViewerField} instances in the form - remove them if they are found
111
     *
112
     * @param FieldList $fields
113
     */
114
    protected function removeHistoryViewerFields(FieldList $fields)
115
    {
116
        // Remove HistoryViewerFields
117
        $fields->recursiveWalk(function (FormField $field) {
118
            if ($field instanceof HistoryViewerField) {
119
                $field->getContainerFieldList()->remove($field);
120
            }
121
        });
122
123
        // Cleanup empty tabs after removing HistoryViewerFields
124
        $fields->recursiveWalk(function (FormField $field) {
125
            if ($field instanceof Tab && !$field->Fields()->count()) {
126
                $field->getContainerFieldList()->remove($field);
127
            }
128
        });
129
    }
130
131
    /**
132
     * Remove right titles from selected form fields by default
133
     *
134
     * @param FieldList $fields
135
     */
136
    protected function removeSelectedRightTitles(FieldList $fields)
137
    {
138
        $noRightTitle = ['MetaDescription', 'ExtraMeta'];
139
140
        foreach ($noRightTitle as $fieldName) {
141
            if ($field = $fields->dataFieldByName($fieldName)) {
142
                $field->setRightTitle('');
143
            }
144
        }
145
    }
146
147
    protected function getFormActions(RequestHandler $controller = null, $formName, $context = [])
148
    {
149
        $actions = FieldList::create();
150
        $this->invokeWithExtensions('updateFormActions', $actions, $controller, $formName, $context);
151
        return $actions;
152
    }
153
154
    public function getRequiredContext()
155
    {
156
        return ['Record'];
157
    }
158
}
159