Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
checkbox.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  * Single check box field.
15  * This is a boolean field with null for false and the specified option for true
16  *
17  * @package Joomla.Platform
18  * @subpackage Form
19  * @link http://www.w3.org/TR/html-markup/input.checkbox.html#input.checkbox
20  * @see JFormFieldCheckboxes
21  * @since 11.1
22  */
24 {
25  /**
26  * The form field type.
27  *
28  * @var string
29  * @since 11.1
30  */
31  protected $type = 'Checkbox';
32 
33  /**
34  * The checked state of checkbox field.
35  *
36  * @var boolean
37  * @since 3.2
38  */
39  protected $checked = false;
40 
41  /**
42  * Method to get certain otherwise inaccessible properties from the form field object.
43  *
44  * @param string $name The property name for which to the the value.
45  *
46  * @return mixed The property value or null.
47  *
48  * @since 3.2
49  */
50  public function __get($name)
51  {
52  switch ($name)
53  {
54  case 'checked':
55  return $this->$name;
56  }
57 
58  return parent::__get($name);
59  }
60 
61  /**
62  * Method to set certain otherwise inaccessible properties of the form field object.
63  *
64  * @param string $name The property name for which to the the value.
65  * @param mixed $value The value of the property.
66  *
67  * @return void
68  *
69  * @since 3.2
70  */
71  public function __set($name, $value)
72  {
73  switch ($name)
74  {
75  case 'checked':
76  $value = (string) $value;
77  $this->$name = ($value == 'true' || $value == $name || $value == '1');
78  break;
79 
80  default:
81  parent::__set($name, $value);
82  }
83  }
84 
85  /**
86  * Method to attach a JForm object to the field.
87  *
88  * @param SimpleXMLElement $element The SimpleXMLElement object representing the <field /> tag for the form field object.
89  * @param mixed $value The form field value to validate.
90  * @param string $group The field name group control value. This acts as as an array container for the field.
91  * For example if the field has name="foo" and the group value is set to "bar" then the
92  * full field name would end up being "bar[foo]".
93  *
94  * @return boolean True on success.
95  *
96  * @see JFormField::setup()
97  * @since 3.2
98  */
99  public function setup(SimpleXMLElement $element, $value, $group = null)
100  {
101  $return = parent::setup($element, $value, $group);
102 
103  if ($return)
104  {
105  $checked = (string) $this->element['checked'];
106  $this->checked = ($checked == 'true' || $checked == 'checked' || $checked == '1');
107 
108  empty($this->value) || $this->checked ? null : $this->checked = true;
109  }
110 
111  return $return;
112  }
113 
114  /**
115  * Method to get the field input markup.
116  * The checked element sets the field to selected.
117  *
118  * @return string The field input markup.
119  *
120  * @since 11.1
121  */
122  protected function getInput()
123  {
124  // Initialize some field attributes.
125  $class = !empty($this->class) ? ' class="' . $this->class . '"' : '';
126  $disabled = $this->disabled ? ' disabled' : '';
127  $value = !empty($this->default) ? $this->default : '1';
128  $required = $this->required ? ' required aria-required="true"' : '';
129  $autofocus = $this->autofocus ? ' autofocus' : '';
130  $checked = $this->checked || !empty($this->value) ? ' checked' : '';
131 
132  // Initialize JavaScript field attributes.
133  $onclick = !empty($this->onclick) ? ' onclick="' . $this->onclick . '"' : '';
134  $onchange = !empty($this->onchange) ? ' onchange="' . $this->onchange . '"' : '';
135 
136  // Including fallback code for HTML5 non supported browsers.
137  JHtml::_('jquery.framework');
138  JHtml::_('script', 'system/html5fallback.js', false, true);
139 
140  return '<input type="checkbox" name="' . $this->name . '" id="' . $this->id . '" value="'
141  . htmlspecialchars($value, ENT_COMPAT, 'UTF-8') . '"' . $class . $checked . $disabled . $onclick . $onchange
142  . $required . $autofocus . ' />';
143  }
144 }