forked from joomla/joomla-platform
/
groupedlist.php
181 lines (156 loc) · 4.77 KB
/
groupedlist.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/**
* @package Joomla.Platform
* @subpackage Form
*
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
defined('JPATH_PLATFORM') or die;
/**
* Form Field class for the Joomla Platform.
* Provides a grouped list select field.
*
* @package Joomla.Platform
* @subpackage Form
* @since 11.1
*/
class JFormFieldGroupedList extends JFormField
{
/**
* The form field type.
*
* @var string
* @since 11.1
*/
protected $type = 'GroupedList';
/**
* Method to get the field option groups.
*
* @return array The field option objects as a nested array in groups.
*
* @since 11.1
* @throws UnexpectedValueException
*/
protected function getGroups()
{
// Initialize variables.
$groups = array();
$label = 0;
foreach ($this->element->children() as $element)
{
switch ($element->getName())
{
// The element is an <option />
case 'option':
// Initialize the group if necessary.
if (!isset($groups[$label]))
{
$groups[$label] = array();
}
// Create a new option object based on the <option /> element.
$tmp = JHtml::_(
'select.option', ($element['value']) ? (string) $element['value'] : trim((string) $element),
JText::alt(trim((string) $element), preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname)), 'value', 'text',
((string) $element['disabled'] == 'true')
);
// Set some option attributes.
$tmp->class = (string) $element['class'];
// Set some JavaScript option attributes.
$tmp->onclick = (string) $element['onclick'];
// Add the option.
$groups[$label][] = $tmp;
break;
// The element is a <group />
case 'group':
// Get the group label.
if ($groupLabel = (string) $element['label'])
{
$label = JText::_($groupLabel);
}
// Initialize the group if necessary.
if (!isset($groups[$label]))
{
$groups[$label] = array();
}
// Iterate through the children and build an array of options.
foreach ($element->children() as $option)
{
// Only add <option /> elements.
if ($option->getName() != 'option')
{
continue;
}
// Create a new option object based on the <option /> element.
$tmp = JHtml::_(
'select.option', ($option['value']) ? (string) $option['value'] : JText::_(trim((string) $option)),
JText::_(trim((string) $option)), 'value', 'text', ((string) $option['disabled'] == 'true')
);
// Set some option attributes.
$tmp->class = (string) $option['class'];
// Set some JavaScript option attributes.
$tmp->onclick = (string) $option['onclick'];
// Add the option.
$groups[$label][] = $tmp;
}
if ($groupLabel)
{
$label = count($groups);
}
break;
// Unknown element type.
default:
throw new UnexpectedValueException(sprintf('Unsupported element %s in JFormFieldGroupedList', $element->getName()), 500);
}
}
reset($groups);
return $groups;
}
/**
* Method to get the field input markup fora grouped list.
* Multiselect is enabled by using the multiple attribute.
*
* @return string The field input markup.
*
* @since 11.1
*/
protected function getInput()
{
// Initialize variables.
$html = array();
$attr = '';
// Initialize some field attributes.
$attr .= $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';
$attr .= ((string) $this->element['disabled'] == 'true') ? ' disabled="disabled"' : '';
$attr .= $this->element['size'] ? ' size="' . (int) $this->element['size'] . '"' : '';
$attr .= $this->multiple ? ' multiple="multiple"' : '';
// Initialize JavaScript field attributes.
$attr .= $this->element['onchange'] ? ' onchange="' . (string) $this->element['onchange'] . '"' : '';
// Get the field groups.
$groups = (array) $this->getGroups();
// Create a read-only list (no name) with a hidden input to store the value.
if ((string) $this->element['readonly'] == 'true')
{
$html[] = JHtml::_(
'select.groupedlist', $groups, null,
array(
'list.attr' => $attr, 'id' => $this->id, 'list.select' => $this->value, 'group.items' => null, 'option.key.toHtml' => false,
'option.text.toHtml' => false
)
);
$html[] = '<input type="hidden" name="' . $this->name . '" value="' . $this->value . '"/>';
}
// Create a regular list.
else
{
$html[] = JHtml::_(
'select.groupedlist', $groups, $this->name,
array(
'list.attr' => $attr, 'id' => $this->id, 'list.select' => $this->value, 'group.items' => null, 'option.key.toHtml' => false,
'option.text.toHtml' => false
)
);
}
return implode($html);
}
}