EditableMultipleOptionField   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 139
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getHasAddableOptions() 0 3 1
A getDefaultOptions() 0 3 1
A duplicate() 0 18 4
A getCMSFields() 0 50 1
A getOptionsMap() 0 8 2
1
<?php
2
3
namespace SilverStripe\UserForms\Model\EditableFormField;
4
5
use SilverStripe\Forms\CheckboxField;
6
use SilverStripe\Forms\TextField;
7
use SilverStripe\Forms\GridField\GridField;
8
use SilverStripe\Forms\GridField\GridFieldButtonRow;
9
use SilverStripe\Forms\GridField\GridFieldConfig;
10
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
11
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
12
use SilverStripe\Forms\Tab;
13
use SilverStripe\ORM\Map;
14
use SilverStripe\UserForms\Model\EditableFormField;
15
use SilverStripe\Versioned\Versioned;
16
use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton;
17
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
18
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
19
use Symbiote\GridFieldExtensions\GridFieldTitleHeader;
20
21
/**
22
 * Base class for multiple option fields such as {@link EditableDropdownField}
23
 * and radio sets.
24
 *
25
 * Implemented as a class but should be viewed as abstract, you should
26
 * instantiate a subclass such as {@link EditableDropdownField}
27
 *
28
 * @see EditableCheckboxGroupField
29
 * @see EditableDropdownField
30
 *
31
 * @package userforms
32
 */
33
34
class EditableMultipleOptionField extends EditableFormField
35
{
36
    /**
37
     * Define this field as abstract (not inherited)
38
     *
39
     * @config
40
     * @var bool
41
     */
42
    private static $abstract = true;
0 ignored issues
show
introduced by
The private property $abstract is not used, and could be removed.
Loading history...
43
44
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
45
        'Options' => EditableOption::class,
46
    ];
47
48
    private static $owns = [
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
49
        'Options',
50
    ];
51
52
    private static $cascade_deletes = [
0 ignored issues
show
introduced by
The private property $cascade_deletes is not used, and could be removed.
Loading history...
53
        'Options',
54
    ];
55
56
    private static $table_name = 'EditableMultipleOptionField';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
57
58
    /**
59
     * @return \SilverStripe\Forms\FieldList
60
     */
61
    public function getCMSFields()
62
    {
63
        $this->beforeUpdateCMSFields(function ($fields) {
64
            $editableColumns = new GridFieldEditableColumns();
65
            $editableColumns->setDisplayFields([
66
                'Title' => [
67
                    'title' => _t(__CLASS__.'.TITLE', 'Title'),
68
                    'callback' => function ($record, $column, $grid) {
0 ignored issues
show
Unused Code introduced by
The parameter $grid is not used and could be removed. ( Ignorable by Annotation )

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

68
                    'callback' => function ($record, $column, /** @scrutinizer ignore-unused */ $grid) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69
                        return TextField::create($column);
70
                    }
71
                ],
72
                'Value' => [
73
                    'title' => _t(__CLASS__.'.VALUE', 'Value'),
74
                    'callback' => function ($record, $column, $grid) {
0 ignored issues
show
Unused Code introduced by
The parameter $grid is not used and could be removed. ( Ignorable by Annotation )

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

74
                    'callback' => function ($record, $column, /** @scrutinizer ignore-unused */ $grid) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
                        return TextField::create($column);
76
                    }
77
                ],
78
                'Default' => [
79
                    'title' => _t(__CLASS__.'.DEFAULT', 'Selected by default?'),
80
                    'callback' => function ($record, $column, $grid) {
0 ignored issues
show
Unused Code introduced by
The parameter $grid is not used and could be removed. ( Ignorable by Annotation )

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

80
                    'callback' => function ($record, $column, /** @scrutinizer ignore-unused */ $grid) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
                        return CheckboxField::create($column);
82
                    }
83
                ]
84
            ]);
85
86
            $optionsConfig = GridFieldConfig::create()
87
                ->addComponents(
88
                    new GridFieldToolbarHeader(),
89
                    new GridFieldTitleHeader(),
90
                    new GridFieldOrderableRows('Sort'),
91
                    $editableColumns,
92
                    new GridFieldButtonRow(),
93
                    new GridFieldAddNewInlineButton(),
94
                    new GridFieldDeleteAction()
95
                );
96
97
            $optionsGrid = GridField::create(
98
                'Options',
99
                _t('SilverStripe\\UserForms\\Model\\EditableFormField.CUSTOMOPTIONS', 'Options'),
100
                $this->Options(),
0 ignored issues
show
Bug introduced by
The method Options() does not exist on SilverStripe\UserForms\M...ableMultipleOptionField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

100
                $this->/** @scrutinizer ignore-call */ 
101
                       Options(),
Loading history...
101
                $optionsConfig
102
            );
103
104
            $fields->insertAfter(Tab::create('Options', _t(__CLASS__.'.OPTIONSTAB', 'Options')), 'Main');
105
            $fields->addFieldToTab('Root.Options', $optionsGrid);
106
        });
107
108
        $fields = parent::getCMSFields();
109
110
        return $fields;
111
    }
112
113
    /**
114
     * Duplicate a pages content. We need to make sure all the fields attached
115
     * to that page go with it
116
     *
117
     * {@inheritDoc}
118
     */
119
    public function duplicate($doWrite = true, $manyMany = 'many_many')
120
    {
121
        // Versioned 1.0 has a bug where [] will result in _all_ relations being duplicated
122
        if ($manyMany === 'many_many' && !$this->manyMany()) {
123
            $manyMany = null;
124
        }
125
126
        $clonedNode = parent::duplicate(true, $manyMany);
127
128
        foreach ($this->Options() as $field) {
129
            /** @var EditableOption $newField */
130
            $newField = $field->duplicate(false);
131
            $newField->ParentID = $clonedNode->ID;
0 ignored issues
show
Bug Best Practice introduced by
The property ParentID does not exist on SilverStripe\UserForms\M...ormField\EditableOption. Since you implemented __set, consider adding a @property annotation.
Loading history...
132
            $newField->Version = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property Version does not exist on SilverStripe\UserForms\M...ormField\EditableOption. Since you implemented __set, consider adding a @property annotation.
Loading history...
133
            $newField->write();
134
        }
135
136
        return $clonedNode;
137
    }
138
139
    /**
140
     * Return whether or not this field has addable options such as a
141
     * {@link EditableDropdown} or {@link EditableRadioField}
142
     *
143
     * @return bool
144
     */
145
    public function getHasAddableOptions()
146
    {
147
        return true;
148
    }
149
150
    /**
151
     * Gets map of field options suitable for use in a form
152
     *
153
     * @return array
154
     */
155
    protected function getOptionsMap()
156
    {
157
        $optionSet = $this->Options();
158
        $optionMap = $optionSet->map('Value', 'Title');
159
        if ($optionMap instanceof Map) {
160
            return $optionMap->toArray();
161
        }
162
        return $optionMap;
163
    }
164
165
    /**
166
     * Returns all default options
167
     *
168
     * @return \SilverStripe\ORM\SS_List
169
     */
170
    protected function getDefaultOptions()
171
    {
172
        return $this->Options()->filter('Default', 1);
173
    }
174
}
175