Completed
Push — 3.5 ( 1a9180...1bec8a )
by Daniel
24s
created

GroupedDropdownField::Field()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 21
nc 10
nop 1
dl 0
loc 28
rs 4.8196
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Grouped dropdown, using <optgroup> tags.
4
 *
5
 * $source parameter (from DropdownField) must be a two dimensional array.
6
 * The first level of the array is used for the <optgroup>, and the second
7
 * level are the <options> for each group.
8
 *
9
 * Returns a <select> tag containing all the appropriate <option> tags, with
10
 * <optgroup> tags around the <option> tags as required.
11
 *
12
 * <b>Usage</b>
13
 *
14
 * <code>
15
 * new GroupedDropdownField(
16
 *    $name = "dropdown",
17
 *    $title = "Simple Grouped Dropdown",
18
 *    $source = array(
19
 *       "numbers" => array(
20
 *       		"1" => "1",
21
 *       		"2" => "2",
22
 *       		"3" => "3",
23
 *       		"4" => "4"
24
 *    		),
25
 *       "letters" => array(
26
 *       		"1" => "A",
27
 *       		"2" => "B",
28
 *       		"3" => "C",
29
 *       		"4" => "D",
30
 *       		"5" => "E",
31
 *       		"6" => "F"
32
 *    		)
33
 *    )
34
 * )
35
 * </code>
36
 *
37
 * <b>Disabling individual items</b>
38
 *
39
 * <code>
40
 * $groupedDrDownField->setDisabledItems(
41
 *    array(
42
 *       "numbers" => array(
43
 *       		"1" => "1",
44
 *       		"3" => "3"
45
 *    		),
46
 *       "letters" => array(
47
 *       		"3" => "C"
48
 *    		)
49
 *    )
50
 * )
51
 * </code>
52
 *
53
 * @package forms
54
 * @subpackage fields-basic
55
 */
56
class GroupedDropdownField extends DropdownField {
57
58
	public function Field($properties = array()) {
59
		$options = '';
60
		foreach($this->getSource() as $value => $title) {
61
			if(is_array($title)) {
62
				$options .= "<optgroup label=\"$value\">";
63
				foreach($title as $value2 => $title2) {
64
					$disabled = '';
65
					if( array_key_exists($value, $this->disabledItems)
66
							&& is_array($this->disabledItems[$value])
67
							&& in_array($value2, $this->disabledItems[$value]) ){
68
						$disabled = 'disabled="disabled"';
69
					}
70
					$selected = $value2 == $this->value ? " selected=\"selected\"" : "";
71
					$options .= "<option$selected value=\"$value2\" $disabled>$title2</option>";
72
				}
73
				$options .= "</optgroup>";
74
			} else { // Fall back to the standard dropdown field
75
				$disabled = '';
76
				if( in_array($value, $this->disabledItems) ){
77
					$disabled = 'disabled="disabled"';
78
				}
79
				$selected = $value == $this->value ? " selected=\"selected\"" : "";
80
				$options .= "<option$selected value=\"$value\" $disabled>$title</option>";
81
			}
82
		}
83
84
		return FormField::create_tag('select', $this->getAttributes(), $options);
85
	}
86
87
	public function Type() {
88
		return 'groupeddropdown dropdown';
89
	}
90
91
	/**
92
	 * Validate this field
93
	 *
94
	 * @param Validator $validator
95
	 * @return bool
96
	 */
97
	public function validate($validator) {
98
		$valid = false;
99
		$source = $this->getSourceAsArray();
100
		$disabled = $this->getDisabledItems();
101
102
		if ($this->value) {
103
			foreach ($source as $value => $title) {
104
				if (is_array($title) && array_key_exists($this->value, $title)) {
105
					// Check that the set value is not in the list of disabled items
106
					if (!isset($disabled[$value]) || !in_array($this->value, $disabled[$value])) {
107
						$valid = true;
108
					}
109
				// Check that the value matches and is not disabled
110
				} elseif($this->value == $value && !in_array($this->value, $disabled)) {
111
					$valid = true;
112
				}
113
			}
114
		} elseif ($this->getHasEmptyDefault()) {
115
			$valid = true;
116
		}
117
118
		if (!$valid) {
119
			$validator->validationError(
120
				$this->name,
121
				_t(
122
					'DropdownField.SOURCE_VALIDATION',
123
					"Please select a value within the list provided. {value} is not a valid option",
124
					array('value' => $this->value)
125
				),
126
				"validation"
127
			);
128
			return false;
129
		}
130
131
		return true;
132
	}
133
134
}
135