Completed
Push — master ( 708319...e0d939 )
by Daniel
22:58
created

EditableMultipleOptionField::getOptionsMap()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
crap 2.0185
1
<?php
2
3
/**
4
 * Base class for multiple option fields such as {@link EditableDropdownField}
5
 * and radio sets.
6
 *
7
 * Implemented as a class but should be viewed as abstract, you should
8
 * instantiate a subclass such as {@link EditableDropdownField}
9
 *
10
 * @see EditableCheckboxGroupField
11
 * @see EditableDropdownField
12
 *
13
 * @package userforms
14
 */
15
16
class EditableMultipleOptionField extends EditableFormField
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
{
18
19
    /**
20
     * Define this field as abstract (not inherited)
21
     *
22
     * @config
23
     * @var bool
24
     */
25
    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...
26
27
    private static $has_many = array(
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...
28
        "Options" => "EditableOption"
29
    );
30
31
    /**
32
     * @return FieldList
33
     */
34 3
    public function getCMSFields()
35
    {
36
        $this->beforeUpdateCMSFields(function($fields) {
37
            $editableColumns = new GridFieldEditableColumns();
38
            $editableColumns->setDisplayFields(array(
39
                'Title' => array(
40
                    'title' => _t('EditableMultipleOptionField.TITLE', 'Title'),
41
                    '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...
42
                        return TextField::create($column);
43
                    }
44
                ),
45
                'Value' => array(
46
                    'title' => _t('EditableMultipleOptionField.VALUE', 'Value'),
47
                    '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...
48
                        return TextField::create($column);
49
                    }
50
                ),
51
                'Default' => array(
52
                    'title' => _t('EditableMultipleOptionField.DEFAULT', 'Selected by default?'),
53
                    '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...
54
                        return CheckboxField::create($column);
55
                    }
56
                )
57
            ));
58
59
            $optionsConfig = GridFieldConfig::create()
60
                ->addComponents(
61
                    new GridFieldToolbarHeader(),
62
                    new GridFieldTitleHeader(),
63
                    new GridFieldOrderableRows('Sort'),
64
                    $editableColumns,
65
                    new GridFieldButtonRow(),
66 3
                    new GridFieldAddNewInlineButton(),
67
                    new GridFieldDeleteAction()
68
                );
69
70
            $optionsGrid = GridField::create(
71
                'Options',
72
                _t('EditableFormField.CUSTOMOPTIONS', 'Options'),
73
                $this->Options(),
0 ignored issues
show
Bug introduced by
The method Options() does not exist on EditableMultipleOptionField. 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...
74
                $optionsConfig
75
            );
76
77
            $fields->insertAfter(new Tab('Options', _t('EditableMultipleOptionField.OPTIONSTAB', 'Options')), 'Main');
78
            $fields->addFieldToTab('Root.Options', $optionsGrid);
79
        });
80
81
        $fields = parent::getCMSFields();
82
83
        return $fields;
84
    }
85
86
    /**
87
     * Publishing Versioning support.
88
     *
89
     * When publishing it needs to handle copying across / publishing
90
     * each of the individual field options
91
     *
92
     * @return void
93
     */
94 1
    public function doPublish($fromStage, $toStage, $createNewVersion = false)
95
    {
96 1
        $live = Versioned::get_by_stage("EditableOption", "Live", "\"EditableOption\".\"ParentID\" = $this->ID");
97
98 1
        if ($live) {
99 1
            foreach ($live as $option) {
100 1
                $option->delete();
101 1
            }
102 1
        }
103
104 1
        if ($this->Options()) {
0 ignored issues
show
Bug introduced by
The method Options() does not exist on EditableMultipleOptionField. 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...
105 1
            foreach ($this->Options() as $option) {
0 ignored issues
show
Bug introduced by
The method Options() does not exist on EditableMultipleOptionField. 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...
106 1
                $option->publish($fromStage, $toStage, $createNewVersion);
107 1
            }
108 1
        }
109
110 1
        parent::doPublish($fromStage, $toStage, $createNewVersion);
111 1
    }
112
113
    /**
114
     * Unpublishing Versioning support
115
     *
116
     * When unpublishing the field it has to remove all options attached
117
     *
118
     * @return void
119
     */
120
    public function doDeleteFromStage($stage)
121
    {
122
        // Remove options
123
        $options = Versioned::get_by_stage('EditableOption', $stage)
124
            ->filter('ParentID', $this->ID);
125
        foreach ($options as $option) {
126
            $option->deleteFromStage($stage);
127
        }
128
129
        parent::doDeleteFromStage($stage);
130
    }
131
132
    /**
133
     * Deletes all the options attached to this field before deleting the
134
     * field. Keeps stray options from floating around
135
     *
136
     * @return void
137
     */
138
    public function delete()
139
    {
140
        $options = $this->Options();
0 ignored issues
show
Bug introduced by
The method Options() does not exist on EditableMultipleOptionField. 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...
141
142
        if ($options) {
143
            foreach ($options as $option) {
144
                $option->delete();
145
            }
146
        }
147
148
        parent::delete();
149
    }
150
151
    /**
152
     * Duplicate a pages content. We need to make sure all the fields attached
153
     * to that page go with it
154
     *
155
     * @return DataObject
156
     */
157 1
    public function duplicate($doWrite = true)
158
    {
159 1
        $clonedNode = parent::duplicate();
160
161 1 View Code Duplication
        foreach ($this->Options() as $field) {
0 ignored issues
show
Bug introduced by
The method Options() does not exist on EditableMultipleOptionField. 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...
162 1
            $newField = $field->duplicate(false);
163 1
            $newField->ParentID = $clonedNode->ID;
164 1
            $newField->Version = 0;
165 1
            $newField->write();
166 1
        }
167
168 1
        return $clonedNode;
169
    }
170
171
    /**
172
     * Return whether or not this field has addable options such as a
173
     * {@link EditableDropdownField} or {@link EditableRadioField}
174
     *
175
     * @return bool
176
     */
177
    public function getHasAddableOptions()
178
    {
179
        return true;
180
    }
181
182
    /**
183
     * Gets map of field options suitable for use in a form
184
     *
185
     * @return array
186
     */
187 3
    protected function getOptionsMap()
188
    {
189 3
        $optionSet = $this->Options();
0 ignored issues
show
Bug introduced by
The method Options() does not exist on EditableMultipleOptionField. 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...
190 3
        $optionMap = $optionSet->map('Value', 'Title');
191 3
        if ($optionMap instanceof SS_Map) {
192 3
            return $optionMap->toArray();
193
        }
194
        return $optionMap;
195
    }
196
197
    /**
198
     * Returns all default options
199
     *
200
     * @return SS_List
201
     */
202 3
    protected function getDefaultOptions()
203
    {
204 3
        return $this->Options()->filter('Default', 1);
0 ignored issues
show
Bug introduced by
The method Options() does not exist on EditableMultipleOptionField. 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...
205
    }
206
}
207