Source for file radio.php

Documentation is available at radio.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.  * Provides radio button inputs
  15.  *
  16.  * @package     Joomla.Platform
  17.  * @subpackage  Form
  18.  * @link        http://www.w3.org/TR/html-markup/command.radio.html#command.radio
  19.  * @since       11.1
  20.  */
  21. class JFormFieldRadio extends JFormField
  22. {
  23.     /**
  24.      * The form field type.
  25.      *
  26.      * @var    string 
  27.      * @since  11.1
  28.      */
  29.     protected $type = 'Radio';
  30.  
  31.     /**
  32.      * Method to get the radio button field input markup.
  33.      *
  34.      * @return  string  The field input markup.
  35.      *
  36.      * @since   11.1
  37.      */
  38.     protected function getInput()
  39.     {
  40.         $html = array();
  41.  
  42.         // Initialize some field attributes.
  43.         $class     = !empty($this->class) ? ' class="radio ' . $this->class . '"' : ' class="radio"';
  44.         $required  = $this->required ? ' required aria-required="true"' : '';
  45.         $autofocus = $this->autofocus ? ' autofocus' : '';
  46.         $disabled  = $this->disabled ? ' disabled' : '';
  47.         $readonly  = $this->readonly;
  48.  
  49.         // Start the radio field output.
  50.         $html[] = '<fieldset id="' . $this->id . '"' . $class . $required . $autofocus . $disabled . ' >';
  51.  
  52.         // Get the field options.
  53.         $options = $this->getOptions();
  54.  
  55.         // Build the radio field output.
  56.         foreach ($options as $i => $option)
  57.         {
  58.             // Initialize some option attributes.
  59.             $checked = ((string) $option->value == (string) $this->value) ? ' checked="checked"' : '';
  60.             $class = !empty($option->class) ? ' class="' . $option->class . '"' : '';
  61.  
  62.             $disabled = !empty($option->disable) || ($readonly && !$checked);
  63.  
  64.             $disabled = $disabled ? ' disabled' : '';
  65.  
  66.             // Initialize some JavaScript option attributes.
  67.             $onclick = !empty($option->onclick) ? ' onclick="' . $option->onclick . '"' : '';
  68.             $onchange = !empty($option->onchange) ? ' onchange="' . $option->onchange . '"' : '';
  69.  
  70.             $html[] = '<input type="radio" id="' . $this->id . $i . '" name="' . $this->name . '" value="'
  71.                 . htmlspecialchars($option->value, ENT_COMPAT, 'UTF-8') . '"' . $checked . $class . $required . $onclick
  72.                 . $onchange . $disabled . ' />';
  73.  
  74.             $html[] = '<label for="' . $this->id . $i . '"' . $class . ' >'
  75.                 . JText::alt($option->text, preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname)) . '</label>';
  76.  
  77.             $required = '';
  78.         }
  79.  
  80.         // End the radio field output.
  81.         $html[] = '</fieldset>';
  82.  
  83.         return implode($html);
  84.     }
  85.  
  86.     /**
  87.      * Method to get the field options for radio buttons.
  88.      *
  89.      * @return  array  The field option objects.
  90.      *
  91.      * @since   11.1
  92.      */
  93.     protected function getOptions()
  94.     {
  95.         $options = array();
  96.  
  97.         foreach ($this->element->children() as $option)
  98.         {
  99.             // Only add <option /> elements.
  100.             if ($option->getName() != 'option')
  101.             {
  102.                 continue;
  103.             }
  104.  
  105.             $disabled = (string) $option['disabled'];
  106.             $disabled = ($disabled == 'true' || $disabled == 'disabled' || $disabled == '1');
  107.  
  108.             // Create a new option object based on the <option /> element.
  109.             $tmp = JHtml::_(
  110.                 'select.option', (string) $option['value'], trim((string) $option), 'value', 'text',
  111.                 $disabled
  112.             );
  113.  
  114.             // Set some option attributes.
  115.             $tmp->class = (string) $option['class'];
  116.  
  117.             // Set some JavaScript option attributes.
  118.             $tmp->onclick = (string) $option['onclick'];
  119.             $tmp->onchange = (string) $option['onchange'];
  120.  
  121.             // Add the option object to the result set.
  122.             $options[] = $tmp;
  123.         }
  124.  
  125.         reset($options);
  126.  
  127.         return $options;
  128.     }
  129. }

Documentation generated on Tue, 19 Nov 2013 15:11:33 +0100 by phpDocumentor 1.4.3