Completed
Pull Request — master (#386)
by
unknown
36:03
created

EditableMultipleOptionField::getCMSFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 41
rs 8.8571
cc 1
eloc 28
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
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
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
			'Default' => array(
45
				'title' => _t('EditableMultipleOptionField.DEFAULT', 'Selected by default?'),
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 CheckboxField::create($column);
48
				}
49
			)
50
		));
51
52
		$optionsConfig = GridFieldConfig::create()
53
			->addComponents(
54
				new GridFieldToolbarHeader(),
55
				new GridFieldTitleHeader(),
56
				$editableColumns,
57
				new GridFieldButtonRow(),
58
				new GridFieldAddNewInlineButton(),
59
				new GridFieldDeleteAction()
60
			);
61
62
		$optionsGrid = GridField::create(
63
			'Options',
64
			_t('EditableFormField.CUSTOMOPTIONS', 'Options'),
65
			$this->Options(),
66
			$optionsConfig
67
		);
68
69
		$fields->insertAfter(new Tab('Options', _t('EditableMultipleOptionField.OPTIONSTAB','Options')), 'Main');
70
		$fields->addFieldToTab('Root.Options', $optionsGrid);
71
72
		return $fields;
73
	}
74
75
	/**
76
	 * Publishing Versioning support.
77
	 *
78
	 * When publishing it needs to handle copying across / publishing
79
	 * each of the individual field options
80
	 *
81
	 * @return void
82
	 */
83
	public function doPublish($fromStage, $toStage, $createNewVersion = false) {
84
		$live = Versioned::get_by_stage("EditableOption", "Live", "\"EditableOption\".\"ParentID\" = $this->ID");
85
86
		if($live) {
87
			foreach($live as $option) {
88
				$option->delete();
89
			}
90
		}
91
92
		if($this->Options()) {
93
			foreach($this->Options() as $option) {
94
				$option->publish($fromStage, $toStage, $createNewVersion);
95
			}
96
		}
97
98
		parent::doPublish($fromStage, $toStage, $createNewVersion);
99
	}
100
101
	/**
102
	 * Unpublishing Versioning support
103
	 *
104
	 * When unpublishing the field it has to remove all options attached
105
	 *
106
	 * @return void
107
	 */
108
	public function doDeleteFromStage($stage) {
109
		// Remove options
110
		$options = Versioned::get_by_stage('EditableOption', $stage)
111
			->filter('ParentID', $this->ID);
112
		foreach($options as $option) {
113
			$option->deleteFromStage($stage);
114
		}
115
116
		parent::doDeleteFromStage($stage);
117
	}
118
119
	/**
120
	 * Deletes all the options attached to this field before deleting the
121
	 * field. Keeps stray options from floating around
122
	 *
123
	 * @return void
124
	 */
125
	public function delete() {
126
		$options = $this->Options();
127
128
		if($options) {
129
			foreach($options as $option) {
130
				$option->delete();
131
			}
132
		}
133
134
		parent::delete();
135
	}
136
137
	/**
138
	 * Duplicate a pages content. We need to make sure all the fields attached
139
	 * to that page go with it
140
	 *
141
	 * @return DataObject
142
	 */
143
	public function duplicate($doWrite = true) {
144
		$clonedNode = parent::duplicate();
145
146 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...
147
			$newField = $field->duplicate(false);
148
			$newField->ParentID = $clonedNode->ID;
149
			$newField->Version = 0;
150
			$newField->write();
151
		}
152
153
		return $clonedNode;
154
	}
155
156
	/**
157
	 * Return whether or not this field has addable options such as a
158
	 * {@link EditableDropdownField} or {@link EditableRadioField}
159
	 *
160
	 * @return bool
161
	 */
162
	public function getHasAddableOptions() {
163
		return true;
164
	}
165
166
	/**
167
	 * Gets map of field options suitable for use in a form
168
	 *
169
	 * @return array
170
	 */
171
	protected function getOptionsMap() {
172
		$optionSet = $this->Options();
173
		$optionMap = $optionSet->map('EscapedTitle', 'Title');
174
		if($optionMap instanceof SS_Map) {
175
			return $optionMap->toArray();
176
		}
177
		return $optionMap;
178
	}
179
180
	/**
181
	 * Returns all default options
182
	 *
183
	 * @return SS_List
184
	 */
185
	protected function getDefaultOptions() {
186
		return $this->Options()->filter('Default', 1);
187
	}
188
}
189