Completed
Pull Request — master (#454)
by Michael
33:16
created

EditableMultipleOptionField::getCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 48
rs 9.125
cc 1
eloc 33
nc 1
nop 0
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
	 * Define this field as abstract (not inherited)
20
	 *
21
	 * @config
22
	 * @var bool
23
	 */
24
	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...
25
26
	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...
27
		"Options" => "EditableOption"
28
	);
29
30
	/**
31
	 * @return FieldList
32
	 */
33
	public function getCMSFields() {
34
		$fields = parent::getCMSFields();
35
36
		$editableColumns = new GridFieldEditableColumns();
37
		$editableColumns->setDisplayFields(array(
38
			'Title' => array(
39
				'title' => _t('EditableMultipleOptionField.TITLE', 'Title'),
40
				'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...
41
					return TextField::create($column);
42
				}
43
            ),
44
            'Value' => array(
45
                'title' => _t('EditableMultipleOptionField.VALUE', 'Value'),
46
                '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...
47
                    return TextField::create($column);
48
                }
49
            ),
50
			'Default' => array(
51
				'title' => _t('EditableMultipleOptionField.DEFAULT', 'Selected by default?'),
52
				'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...
53
					return CheckboxField::create($column);
54
				}
55
			)
56
		));
57
58
		$optionsConfig = GridFieldConfig::create()
59
			->addComponents(
60
				new GridFieldToolbarHeader(),
61
				new GridFieldTitleHeader(),
62
				new GridFieldOrderableRows('Sort'),
63
				$editableColumns,
64
				new GridFieldButtonRow(),
65
				new GridFieldAddNewInlineButton(),
66
				new GridFieldDeleteAction()
67
			);
68
69
		$optionsGrid = GridField::create(
70
			'Options',
71
			_t('EditableFormField.CUSTOMOPTIONS', 'Options'),
72
			$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...
73
			$optionsConfig
74
		);
75
76
		$fields->insertAfter(new Tab('Options', _t('EditableMultipleOptionField.OPTIONSTAB','Options')), 'Main');
0 ignored issues
show
Documentation introduced by
'Main' is of type string, but the function expects a object<FormField>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77
		$fields->addFieldToTab('Root.Options', $optionsGrid);
78
79
		return $fields;
80
	}
81
82
	/**
83
	 * Publishing Versioning support.
84
	 *
85
	 * When publishing it needs to handle copying across / publishing
86
	 * each of the individual field options
87
	 *
88
	 * @return void
89
	 */
90
	public function doPublish($fromStage, $toStage, $createNewVersion = false) {
91
		$live = Versioned::get_by_stage("EditableOption", "Live", "\"EditableOption\".\"ParentID\" = $this->ID");
92
93
		if($live) {
94
			foreach($live as $option) {
95
				$option->delete();
96
			}
97
		}
98
99
		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...
100
			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...
101
				$option->publish($fromStage, $toStage, $createNewVersion);
102
			}
103
		}
104
105
		parent::doPublish($fromStage, $toStage, $createNewVersion);
106
	}
107
108
	/**
109
	 * Unpublishing Versioning support
110
	 *
111
	 * When unpublishing the field it has to remove all options attached
112
	 *
113
	 * @return void
114
	 */
115
	public function doDeleteFromStage($stage) {
116
		// Remove options
117
		$options = Versioned::get_by_stage('EditableOption', $stage)
118
			->filter('ParentID', $this->ID);
119
		foreach($options as $option) {
120
			$option->deleteFromStage($stage);
121
		}
122
123
		parent::doDeleteFromStage($stage);
124
	}
125
126
	/**
127
	 * Deletes all the options attached to this field before deleting the
128
	 * field. Keeps stray options from floating around
129
	 *
130
	 * @return void
131
	 */
132
	public function delete() {
133
		$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...
134
135
		if($options) {
136
			foreach($options as $option) {
137
				$option->delete();
138
			}
139
		}
140
141
		parent::delete();
142
	}
143
144
	/**
145
	 * Duplicate a pages content. We need to make sure all the fields attached
146
	 * to that page go with it
147
	 *
148
	 * @return DataObject
149
	 */
150
	public function duplicate($doWrite = true) {
151
		$clonedNode = parent::duplicate();
152
153 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...
154
			$newField = $field->duplicate(false);
155
			$newField->ParentID = $clonedNode->ID;
156
			$newField->Version = 0;
157
			$newField->write();
158
		}
159
160
		return $clonedNode;
161
	}
162
163
	/**
164
	 * Return whether or not this field has addable options such as a
165
	 * {@link EditableDropdownField} or {@link EditableRadioField}
166
	 *
167
	 * @return bool
168
	 */
169
	public function getHasAddableOptions() {
170
		return true;
171
	}
172
173
	/**
174
	 * Gets map of field options suitable for use in a form
175
	 *
176
	 * @return array
177
	 */
178
	protected function getOptionsMap() {
179
		$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...
180
		$optionMap = $optionSet->map('Value', 'Title');
181
		if($optionMap instanceof SS_Map) {
182
			return $optionMap->toArray();
183
		}
184
		return $optionMap;
185
	}
186
187
	/**
188
	 * Returns all default options
189
	 *
190
	 * @return SS_List
191
	 */
192
	protected function getDefaultOptions() {
193
		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...
194
	}
195
}
196