Source for file groupedlist.php

Documentation is available at groupedlist.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 a grouped list select field.
  15.  *
  16.  * @package     Joomla.Platform
  17.  * @subpackage  Form
  18.  * @since       11.1
  19.  */
  20. {
  21.     /**
  22.      * The form field type.
  23.      *
  24.      * @var    string 
  25.      * @since  11.1
  26.      */
  27.     protected $type = 'GroupedList';
  28.  
  29.     /**
  30.      * Method to get the field option groups.
  31.      *
  32.      * @return  array  The field option objects as a nested array in groups.
  33.      *
  34.      * @since   11.1
  35.      * @throws  UnexpectedValueException
  36.      */
  37.     protected function getGroups()
  38.     {
  39.         $groups array();
  40.         $label 0;
  41.  
  42.         foreach ($this->element->children(as $element)
  43.         {
  44.             switch ($element->getName())
  45.             {
  46.                 // The element is an <option />
  47.                 case 'option':
  48.                     // Initialize the group if necessary.
  49.                     if (!isset($groups[$label]))
  50.                     {
  51.                         $groups[$labelarray();
  52.                     }
  53.  
  54.                     $disabled = (string) $element['disabled'];
  55.                     $disabled ($disabled == 'true' || $disabled == 'disabled' || $disabled == '1');
  56.  
  57.                     // Create a new option object based on the <option /> element.
  58.                     $tmp JHtml::_(
  59.                         'select.option'($element['value']? (string) $element['value'trim((string) $element),
  60.                         JText::alt(trim((string) $element)preg_replace('/[^a-zA-Z0-9_\-]/''_'$this->fieldname))'value''text',
  61.                         $disabled
  62.                     );
  63.  
  64.                     // Set some option attributes.
  65.                     $tmp->class = (string) $element['class'];
  66.  
  67.                     // Set some JavaScript option attributes.
  68.                     $tmp->onclick = (string) $element['onclick'];
  69.  
  70.                     // Add the option.
  71.                     $groups[$label][$tmp;
  72.                     break;
  73.  
  74.                 // The element is a <group />
  75.                 case 'group':
  76.                     // Get the group label.
  77.                     if ($groupLabel = (string) $element['label'])
  78.                     {
  79.                         $label JText::_($groupLabel);
  80.                     }
  81.  
  82.                     // Initialize the group if necessary.
  83.                     if (!isset($groups[$label]))
  84.                     {
  85.                         $groups[$labelarray();
  86.                     }
  87.  
  88.                     // Iterate through the children and build an array of options.
  89.                     foreach ($element->children(as $option)
  90.                     {
  91.                         // Only add <option /> elements.
  92.                         if ($option->getName(!= 'option')
  93.                         {
  94.                             continue;
  95.                         }
  96.  
  97.                         $disabled = (string) $option['disabled'];
  98.                         $disabled ($disabled == 'true' || $disabled == 'disabled' || $disabled == '1');
  99.  
  100.                         // Create a new option object based on the <option /> element.
  101.                         $tmp JHtml::_(
  102.                             'select.option'($option['value']? (string) $option['value'JText::_(trim((string) $option)),
  103.                             JText::_(trim((string) $option))'value''text'$disabled
  104.                         );
  105.  
  106.                         // Set some option attributes.
  107.                         $tmp->class = (string) $option['class'];
  108.  
  109.                         // Set some JavaScript option attributes.
  110.                         $tmp->onclick = (string) $option['onclick'];
  111.  
  112.                         // Add the option.
  113.                         $groups[$label][$tmp;
  114.                     }
  115.  
  116.                     if ($groupLabel)
  117.                     {
  118.                         $label count($groups);
  119.                     }
  120.                     break;
  121.  
  122.                 // Unknown element type.
  123.                 default:
  124.                     throw new UnexpectedValueException(sprintf('Unsupported element %s in JFormFieldGroupedList'$element->getName())500);
  125.             }
  126.         }
  127.  
  128.         reset($groups);
  129.  
  130.         return $groups;
  131.     }
  132.  
  133.     /**
  134.      * Method to get the field input markup fora grouped list.
  135.      * Multiselect is enabled by using the multiple attribute.
  136.      *
  137.      * @return  string  The field input markup.
  138.      *
  139.      * @since   11.1
  140.      */
  141.     protected function getInput()
  142.     {
  143.         $html array();
  144.         $attr '';
  145.  
  146.         // Initialize some field attributes.
  147.         $attr .= !empty($this->class' class="' $this->class . '"' '';
  148.         $attr .= $this->disabled ? ' disabled' '';
  149.         $attr .= !empty($this->size' size="' $this->size . '"' '';
  150.         $attr .= $this->multiple ? ' multiple' '';
  151.         $attr .= $this->required ? ' required aria-required="true"' '';
  152.         $attr .= $this->autofocus ? ' autofocus' '';
  153.  
  154.         // Initialize JavaScript field attributes.
  155.         $attr .= !empty($this->onchange' onchange="' $this->onchange . '"' '';
  156.  
  157.         // Get the field groups.
  158.         $groups = (array) $this->getGroups();
  159.  
  160.         // Create a read-only list (no name) with a hidden input to store the value.
  161.         if ($this->readonly)
  162.         {
  163.             $html[JHtml::_(
  164.                 'select.groupedlist'$groupsnull,
  165.                 array(
  166.                     'list.attr' => $attr'id' => $this->id'list.select' => $this->value'group.items' => null'option.key.toHtml' => false,
  167.                     'option.text.toHtml' => false
  168.                 )
  169.             );
  170.             $html['<input type="hidden" name="' $this->name . '" value="' $this->value . '"/>';
  171.         }
  172.  
  173.         // Create a regular list.
  174.         else
  175.         {
  176.             $html[JHtml::_(
  177.                 'select.groupedlist'$groups$this->name,
  178.                 array(
  179.                     'list.attr' => $attr'id' => $this->id'list.select' => $this->value'group.items' => null'option.key.toHtml' => false,
  180.                     'option.text.toHtml' => false
  181.                 )
  182.             );
  183.         }
  184.  
  185.         return implode($html);
  186.     }
  187. }

Documentation generated on Tue, 19 Nov 2013 15:04:06 +0100 by phpDocumentor 1.4.3