Source for file checkboxes.php

Documentation is available at checkboxes.php

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

Documentation generated on Tue, 19 Nov 2013 14:55:42 +0100 by phpDocumentor 1.4.3