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