Completed
Pull Request — master (#647)
by Robbie
02:09
created

EditableMultipleOptionField::getCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 51
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 9.4109
c 0
b 0
f 0
cc 1
eloc 34
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\UserForms\Model\EditableFormField\EditableOption;
16
use SilverStripe\Versioned\Versioned;
17
use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton;
18
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
19
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
20
use Symbiote\GridFieldExtensions\GridFieldTitleHeader;
21
22
/**
23
 * Base class for multiple option fields such as {@link EditableDropdownField}
24
 * and radio sets.
25
 *
26
 * Implemented as a class but should be viewed as abstract, you should
27
 * instantiate a subclass such as {@link EditableDropdownField}
28
 *
29
 * @see EditableCheckboxGroupField
30
 * @see EditableDropdownField
31
 *
32
 * @package userforms
33
 */
34
35
class EditableMultipleOptionField extends EditableFormField
36
{
37
    /**
38
     * Define this field as abstract (not inherited)
39
     *
40
     * @config
41
     * @var bool
42
     */
43
    private static $abstract = true;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $abstract is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
44
45
    private static $has_many = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $has_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
46
        'Options' => EditableOption::class,
47
    ];
48
49
    private static $owns = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $owns is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
50
        'Options',
51
    ];
52
53
    private static $cascade_deletes = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $cascade_deletes is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
54
        'Options',
55
    ];
56
57
    private static $table_name = 'EditableMultipleOptionField';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $table_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
58
59
    /**
60
     * @return FieldList
61
     */
62
    public function getCMSFields()
63
    {
64
        $this->beforeUpdateCMSFields(function ($fields) {
65
            $editableColumns = new GridFieldEditableColumns();
66
            $editableColumns->setDisplayFields([
67
                'Title' => [
68
                    'title' => _t(__CLASS__.'.TITLE', 'Title'),
69
                    'callback' => function ($record, $column, $grid) {
0 ignored issues
show
Unused Code introduced by
The parameter $grid is not used and could be removed.

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

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

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

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

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

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

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
102
                $optionsConfig
103
            );
104
105
            $fields->insertAfter(Tab::create('Options', _t(__CLASS__.'.OPTIONSTAB', 'Options')), 'Main');
106
            $fields->addFieldToTab('Root.Options', $optionsGrid);
107
        });
108
109
        $fields = parent::getCMSFields();
110
111
        return $fields;
112
    }
113
114
    /**
115
     * Duplicate a pages content. We need to make sure all the fields attached
116
     * to that page go with it
117
     *
118
     * {@inheritDoc}
119
     */
120
    public function duplicate($doWrite = true, $manyMany = 'many_many')
121
    {
122
        $clonedNode = parent::duplicate($doWrite, $manyMany);
123
124 View Code Duplication
        foreach ($this->Options() as $field) {
0 ignored issues
show
Bug introduced by
The method Options() does not exist on SilverStripe\UserForms\M...ableMultipleOptionField. Did you maybe mean getHasAddableOptions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
Duplication introduced by
This code seems to be duplicated across 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...
125
            $newField = $field->duplicate(false);
126
            $newField->ParentID = $clonedNode->ID;
127
            $newField->Version = 0;
128
            $newField->write();
129
        }
130
131
        return $clonedNode;
132
    }
133
134
    /**
135
     * Return whether or not this field has addable options such as a
136
     * {@link EditableDropdownField} or {@link EditableRadioField}
137
     *
138
     * @return bool
139
     */
140
    public function getHasAddableOptions()
141
    {
142
        return true;
143
    }
144
145
    /**
146
     * Gets map of field options suitable for use in a form
147
     *
148
     * @return array
149
     */
150
    protected function getOptionsMap()
151
    {
152
        $optionSet = $this->Options();
0 ignored issues
show
Bug introduced by
The method Options() does not exist on SilverStripe\UserForms\M...ableMultipleOptionField. Did you maybe mean getHasAddableOptions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
153
        $optionMap = $optionSet->map('Value', 'Title');
154
        if ($optionMap instanceof Map) {
155
            return $optionMap->toArray();
156
        }
157
        return $optionMap;
158
    }
159
160
    /**
161
     * Returns all default options
162
     *
163
     * @return SS_List
164
     */
165
    protected function getDefaultOptions()
166
    {
167
        return $this->Options()->filter('Default', 1);
0 ignored issues
show
Bug introduced by
The method Options() does not exist on SilverStripe\UserForms\M...ableMultipleOptionField. Did you maybe mean getHasAddableOptions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
168
    }
169
}
170