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