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