Completed
Push — 3.2 ( 9dabf1...378749 )
by Damian
09:42
created

OptionsetField::getDisabledItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Set of radio buttons designed to emulate a dropdown.
4
 *
5
 * This field allows you to ensure that a form element is submitted is not optional and is part of a fixed set of
6
 * data. This field uses the input type of radio. It's a direct subclass of {@link DropdownField},
7
 * so the constructor and arguments are in the same format.
8
 *
9
 * <b>Usage</b>
10
 *
11
 * <code>
12
 * new OptionsetField(
13
 *    $name = "Foobar",
14
 *    $title = "FooBar's optionset",
15
 *    $source = array(
16
 *       "1" => "Option 1",
17
 *       "2" => "Option 2",
18
 *       "3" => "Option 3",
19
 *       "4" => "Option 4",
20
 *       "5" => "Option 5"
21
 *    ),
22
 *    $value = "1"
23
 * );
24
 * </code>
25
 *
26
 * You can use the helper functions on data object set to create the source array. eg:
27
 *
28
 * <code>
29
 * //Database request for the object
30
 * $map = FooBar::get()->map();
31
 *  // returns an SS_Map object containing an array of ID => Title
32
 *
33
 * // Instantiate the OptionsetField
34
 * $FieldList = new FieldList(
35
 *   new OptionsetField(
36
 *    $name = "Foobar",
37
 *    $title = "FooBar's optionset",
38
 *    $source = $map,
39
 *    $value = $map[0]
40
 *   )
41
 * );
42
 *
43
 * // Pass the fields to the form constructor. etc
44
 * </code>
45
 *
46
 * @see CheckboxSetField for multiple selections through checkboxes instead.
47
 * @see DropdownField for a simple <select> field with a single element.
48
 * @see TreeDropdownField for a rich and customizeable UI that can visualize a tree of selectable elements
49
 *
50
 * @package forms
51
 * @subpackage fields-basic
52
 */
53
class OptionsetField extends DropdownField {
54
55
	/**
56
	 * {@inheritdoc}
57
	 */
58
	public function Field($properties = array()) {
59
		$source = $this->getSource();
60
		$odd = 0;
61
		$options = array();
62
63
		if($source) {
64
			foreach($source as $value => $title) {
65
				$itemID = $this->ID() . '_' . preg_replace('/[^a-zA-Z0-9]/', '', $value);
66
				$odd = ($odd + 1) % 2;
67
				$extraClass = $odd ? 'odd' : 'even';
68
				$extraClass .= ' val' . preg_replace('/[^a-zA-Z0-9\-\_]/', '_', $value);
69
70
				$options[] = new ArrayData(array(
71
					'ID' => $itemID,
72
					'Class' => $extraClass,
73
					'Name' => $this->name,
74
					'Value' => $value,
75
					'Title' => $title,
76
					'isChecked' => $value == $this->value,
77
					'isDisabled' => $this->disabled || in_array($value, $this->disabledItems),
78
				));
79
			}
80
		}
81
82
		$properties = array_merge($properties, array(
83
			'Options' => new ArrayList($options)
84
		));
85
86
		return $this->customise($properties)->renderWith(
87
			$this->getTemplates()
88
		);
89
	}
90
91
	/**
92
	 * {@inheritdoc}
93
	 */
94
	public function validate($validator) {
95
		if (!$this->value) {
96
			return true;
97
		}
98
99
		return parent::validate($validator);
100
	}
101
102
	public function ExtraOptions() {
103
		return new ArrayList();
104
	}
105
}
106