Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
checkboxes.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  * Displays options as a list of check boxes.
15  * Multiselect may be forced to be true.
16  *
17  * @package Joomla.Platform
18  * @subpackage Form
19  * @see JFormFieldCheckbox
20  * @since 11.1
21  */
23 {
24  /**
25  * The form field type.
26  *
27  * @var string
28  * @since 11.1
29  */
30  protected $type = 'Checkboxes';
31 
32  /**
33  * Flag to tell the field to always be in multiple values mode.
34  *
35  * @var boolean
36  * @since 11.1
37  */
38  protected $forceMultiple = true;
39 
40  /**
41  * The comma seprated list of checked checkboxes value.
42  *
43  * @var mixed
44  * @since 3.2
45  */
47 
48  /**
49  * Method to get certain otherwise inaccessible properties from the form field object.
50  *
51  * @param string $name The property name for which to the the value.
52  *
53  * @return mixed The property value or null.
54  *
55  * @since 3.2
56  */
57  public function __get($name)
58  {
59  switch ($name)
60  {
61  case 'forceMultiple':
62  case 'checkedOptions':
63  return $this->$name;
64  }
65 
66  return parent::__get($name);
67  }
68 
69  /**
70  * Method to set certain otherwise inaccessible properties of the form field object.
71  *
72  * @param string $name The property name for which to the the value.
73  * @param mixed $value The value of the property.
74  *
75  * @return void
76  *
77  * @since 3.2
78  */
79  public function __set($name, $value)
80  {
81  switch ($name)
82  {
83  case 'checkedOptions':
84  $this->checkedOptions = (string) $value;
85  break;
86 
87  default:
88  parent::__set($name, $value);
89  }
90  }
91 
92  /**
93  * Method to attach a JForm object to the field.
94  *
95  * @param SimpleXMLElement $element The SimpleXMLElement object representing the <field /> tag for the form field object.
96  * @param mixed $value The form field value to validate.
97  * @param string $group The field name group control value. This acts as as an array container for the field.
98  * For example if the field has name="foo" and the group value is set to "bar" then the
99  * full field name would end up being "bar[foo]".
100  *
101  * @return boolean True on success.
102  *
103  * @see JFormField::setup()
104  * @since 3.2
105  */
106  public function setup(SimpleXMLElement $element, $value, $group = null)
107  {
108  $return = parent::setup($element, $value, $group);
109 
110  if ($return)
111  {
112  $this->checkedOptions = (string) $this->element['checked'];
113  }
114 
115  return $return;
116  }
117 
118  /**
119  * Method to get the field input markup for check boxes.
120  *
121  * @return string The field input markup.
122  *
123  * @since 11.1
124  */
125  protected function getInput()
126  {
127  $html = array();
128 
129  // Initialize some field attributes.
130  $class = !empty($this->class) ? ' class=checkboxes "' . $this->class . '"' : ' class="checkboxes"';
131  $checkedOptions = explode(',', (string) $this->checkedOptions);
132  $required = $this->required ? ' required aria-required="true"' : '';
133  $autofocus = $this->autofocus ? ' autofocus' : '';
134 
135  // Including fallback code for HTML5 non supported browsers.
136  JHtml::_('jquery.framework');
137  JHtml::_('script', 'system/html5fallback.js', false, true);
138 
139  // Start the checkbox field output.
140  $html[] = '<fieldset id="' . $this->id . '"' . $class . $required . $autofocus . '>';
141 
142  // Get the field options.
143  $options = $this->getOptions();
144 
145  // Build the checkbox field output.
146  $html[] = '<ul>';
147 
148  foreach ($options as $i => $option)
149  {
150  // Initialize some option attributes.
151  if (!isset($this->value) || empty($this->value))
152  {
153  $checked = (in_array((string) $option->value, (array) $checkedOptions) ? ' checked' : '');
154  }
155  else
156  {
157  $value = !is_array($this->value) ? explode(',', $this->value) : $this->value;
158  $checked = (in_array((string) $option->value, $value) ? ' checked' : '');
159  }
160 
161  $checked = empty($checked) && $option->checked ? ' checked' : $checked;
162 
163  $class = !empty($option->class) ? ' class="' . $option->class . '"' : '';
164  $disabled = !empty($option->disable) || $this->disabled ? ' disabled' : '';
165 
166  // Initialize some JavaScript option attributes.
167  $onclick = !empty($option->onclick) ? ' onclick="' . $option->onclick . '"' : '';
168  $onchange = !empty($option->onchange) ? ' onchange="' . $option->onchange . '"' : '';
169 
170  $html[] = '<li>';
171  $html[] = '<input type="checkbox" id="' . $this->id . $i . '" name="' . $this->name . '" value="'
172  . htmlspecialchars($option->value, ENT_COMPAT, 'UTF-8') . '"' . $checked . $class . $onclick . $onchange . $disabled . '/>';
173 
174  $html[] = '<label for="' . $this->id . $i . '"' . $class . '>' . JText::_($option->text) . '</label>';
175  $html[] = '</li>';
176  }
177 
178  $html[] = '</ul>';
179 
180  // End the checkbox field output.
181  $html[] = '</fieldset>';
182 
183  return implode($html);
184  }
185 
186  /**
187  * Method to get the field options.
188  *
189  * @return array The field option objects.
190  *
191  * @since 11.1
192  */
193  protected function getOptions()
194  {
195  $options = array();
196 
197  foreach ($this->element->children() as $option)
198  {
199  // Only add <option /> elements.
200  if ($option->getName() != 'option')
201  {
202  continue;
203  }
204 
205  $disabled = (string) $option['disabled'];
206  $disabled = ($disabled == 'true' || $disabled == 'disabled' || $disabled == '1');
207 
208  $checked = (string) $option['checked'];
209  $checked = ($checked == 'true' || $checked == 'checked' || $checked == '1');
210 
211  // Create a new option object based on the <option /> element.
212  $tmp = JHtml::_('select.option', (string) $option['value'], trim((string) $option), 'value', 'text', $disabled);
213 
214  // Set some option attributes.
215  $tmp->class = (string) $option['class'];
216  $tmp->checked = $checked;
217 
218  // Set some JavaScript option attributes.
219  $tmp->onclick = (string) $option['onclick'];
220  $tmp->onchange = (string) $option['onchange'];
221 
222  // Add the option object to the result set.
223  $options[] = $tmp;
224  }
225 
226  reset($options);
227 
228  return $options;
229  }
230 }