Completed
Pull Request — master (#518)
by Daniel
35:40
created

EditableMultipleOptionField::getDefaultOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
use SilverStripe\Forms\TextField;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, TextField.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
4
use SilverStripe\Forms\CheckboxField;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, CheckboxField.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
5
use SilverStripe\Forms\GridField\GridFieldConfig;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, GridFieldConfig.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, GridFieldToolbarHeader.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use SilverStripe\Forms\GridField\GridFieldButtonRow;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, GridFieldButtonRow.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, GridFieldDeleteAction.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
use SilverStripe\Forms\GridField\GridField;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, GridField.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use SilverStripe\Forms\Tab;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Tab.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
11
use SilverStripe\ORM\Versioning\Versioned;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Versioned.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

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

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
207 1
            return $optionMap->toArray();
208
        }
209
        return $optionMap;
210
    }
211
212
    /**
213
     * Returns all default options
214
     *
215
     * @return SS_List
216
     */
217
    protected function getDefaultOptions()
218
    {
219
        return $this->Options()->filter('Default', 1);
220
    }
221
}
222