PageRenderTime 35ms CodeModel.GetById 9ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/form/fields/groupedlist.php

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