Field_GroupedList::getInput()   B
last analyzed

Complexity

Conditions 7
Paths 64

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 64
nop 0
dl 0
loc 45
rs 8.2666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Part of the Joomla Framework Form Package
4
 *
5
 * @copyright  Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
6
 * @license    GNU General Public License version 2 or later; see LICENSE
7
 */
8
9
namespace Joomla\Form;
10
11
use Joomla\Form\Html\Select as HtmlSelect;
12
use Joomla\Language\Text;
13
use UnexpectedValueException;
14
15
/**
16
 * Form Field class for the Joomla Framework.
17
 * Provides a grouped list select field.
18
 *
19
 * @since       1.0
20
 * @deprecated  The joomla/form package is deprecated
21
 */
22
class Field_GroupedList extends Field
0 ignored issues
show
Deprecated Code introduced by
The class Joomla\Form\Field has been deprecated with message: The joomla/form package is deprecated

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
23
{
24
	/**
25
	 * The form field type.
26
	 *
27
	 * @var    string
28
	 * @since  1.0
29
	 */
30
	protected $type = 'GroupedList';
31
32
	/**
33
	 * Method to get the field option groups.
34
	 *
35
	 * @return  array  The field option objects as a nested array in groups.
36
	 *
37
	 * @since   1.0
38
	 * @throws  UnexpectedValueException
39
	 */
40
	protected function getGroups()
41
	{
42
		$groups = array();
43
		$label = 0;
44
45
		foreach ($this->element->children() as $element)
46
		{
47
			switch ($element->getName())
48
			{
49
				// The element is an <option />
50
				case 'option':
51
					// Initialize the group if necessary.
52
					if (!isset($groups[$label]))
53
					{
54
						$groups[$label] = array();
55
					}
56
57
					// Create a new option object based on the <option /> element.
58
					$tmp = HtmlSelect::option(
59
						($element['value']) ? (string) $element['value'] : trim((string) $element),
60
						Text::alt(trim((string) $element), preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname)), 'value', 'text',
61
						((string) $element['disabled'] == 'true')
62
					);
63
64
					// Set some option attributes.
65
					$tmp->class = (string) $element['class'];
66
67
					// Set some JavaScript option attributes.
68
					$tmp->onclick = (string) $element['onclick'];
69
70
					// Add the option.
71
					$groups[$label][] = $tmp;
72
					break;
73
74
				// The element is a <group />
75
				case 'group':
76
					// Get the group label.
77
					if ($groupLabel = (string) $element['label'])
78
					{
79
						$label = Text::_($groupLabel);
0 ignored issues
show
Deprecated Code introduced by
The method Joomla\Language\Text::_() has been deprecated with message: 2.0 Will be replaced with a `translate` method.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
80
					}
81
82
					// Initialize the group if necessary.
83
					if (!isset($groups[$label]))
84
					{
85
						$groups[$label] = array();
86
					}
87
88
					// Iterate through the children and build an array of options.
89
					foreach ($element->children() as $option)
90
					{
91
						// Only add <option /> elements.
92
						if ($option->getName() != 'option')
93
						{
94
							continue;
95
						}
96
97
						// Create a new option object based on the <option /> element.
98
						$tmp = HtmlSelect::option(
99
							($option['value']) ? (string) $option['value'] : Text::_(trim((string) $option)),
0 ignored issues
show
Deprecated Code introduced by
The method Joomla\Language\Text::_() has been deprecated with message: 2.0 Will be replaced with a `translate` method.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
100
							Text::_(trim((string) $option)), 'value', 'text', ((string) $option['disabled'] == 'true')
0 ignored issues
show
Deprecated Code introduced by
The method Joomla\Language\Text::_() has been deprecated with message: 2.0 Will be replaced with a `translate` method.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
101
						);
102
103
						// Set some option attributes.
104
						$tmp->class = (string) $option['class'];
105
106
						// Set some JavaScript option attributes.
107
						$tmp->onclick = (string) $option['onclick'];
108
109
						// Add the option.
110
						$groups[$label][] = $tmp;
111
					}
112
113
					if ($groupLabel)
114
					{
115
						$label = count($groups);
116
					}
117
					break;
118
119
				// Unknown element type.
120
				default:
121
					throw new UnexpectedValueException(sprintf('Unsupported element %s in JFormFieldGroupedList', $element->getName()), 500);
122
			}
123
		}
124
125
		reset($groups);
126
127
		return $groups;
128
	}
129
130
	/**
131
	 * Method to get the field input markup fora grouped list.
132
	 * Multiselect is enabled by using the multiple attribute.
133
	 *
134
	 * @return  string  The field input markup.
135
	 *
136
	 * @since   1.0
137
	 */
138
	protected function getInput()
139
	{
140
		$html = array();
141
		$attr = '';
142
143
		// Initialize some field attributes.
144
		$attr .= $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';
145
		$attr .= ((string) $this->element['disabled'] == 'true') ? ' disabled="disabled"' : '';
146
		$attr .= $this->element['size'] ? ' size="' . (int) $this->element['size'] . '"' : '';
147
		$attr .= $this->multiple ? ' multiple="multiple"' : '';
148
149
		// Initialize JavaScript field attributes.
150
		$attr .= $this->element['onchange'] ? ' onchange="' . (string) $this->element['onchange'] . '"' : '';
151
152
		// Get the field groups.
153
		$groups = (array) $this->getGroups();
154
155
		// Create a read-only list (no name) with a hidden input to store the value.
156
		if ((string) $this->element['readonly'] == 'true')
157
		{
158
			$html[] = HtmlSelect::groupedlist(
159
				$groups,
160
				null,
161
				array(
162
					'list.attr' => $attr, 'id' => $this->id, 'list.select' => $this->value, 'group.items' => null, 'option.key.toHtml' => false,
163
					'option.text.toHtml' => false
164
				)
165
			);
166
			$html[] = '<input type="hidden" name="' . $this->name . '" value="' . $this->value . '"/>';
167
		}
168
		else
169
		// Create a regular list.
170
		{
171
			$html[] = HtmlSelect::groupedlist(
172
				$groups,
173
				$this->name,
174
				array(
175
					'list.attr' => $attr, 'id' => $this->id, 'list.select' => $this->value, 'group.items' => null, 'option.key.toHtml' => false,
176
					'option.text.toHtml' => false
177
				)
178
			);
179
		}
180
181
		return implode($html);
182
	}
183
}
184