Joomla! PHP Cross Reference Web Portals

Source: /libraries/joomla/form/fields/groupedlist.php - 179 lines - 4833 bytes - Summary - Text - Print

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

title

Description

title

Description

title

Description

title

title

Body