/libraries/joomla/form/fields/list.php

https://gitlab.com/vitaliylukin91/lavka · PHP · 156 lines · 87 code · 23 blank · 46 comment · 39 complexity · b9f9f06dd17d00468310cf2481e0b71f 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. * Supports a generic list of options.
  13. *
  14. * @since 11.1
  15. */
  16. class JFormFieldList extends JFormField
  17. {
  18. /**
  19. * The form field type.
  20. *
  21. * @var string
  22. * @since 11.1
  23. */
  24. protected $type = 'List';
  25. /**
  26. * Method to get the field input markup for a generic list.
  27. * Use the multiple attribute to enable multiselect.
  28. *
  29. * @return string The field input markup.
  30. *
  31. * @since 11.1
  32. */
  33. protected function getInput()
  34. {
  35. $html = array();
  36. $attr = '';
  37. // Initialize some field attributes.
  38. $attr .= !empty($this->class) ? ' class="' . $this->class . '"' : '';
  39. $attr .= !empty($this->size) ? ' size="' . $this->size . '"' : '';
  40. $attr .= $this->multiple ? ' multiple' : '';
  41. $attr .= $this->required ? ' required aria-required="true"' : '';
  42. $attr .= $this->autofocus ? ' autofocus' : '';
  43. // To avoid user's confusion, readonly="true" should imply disabled="true".
  44. if ((string) $this->readonly == '1' || (string) $this->readonly == 'true' || (string) $this->disabled == '1'|| (string) $this->disabled == 'true')
  45. {
  46. $attr .= ' disabled="disabled"';
  47. }
  48. // Initialize JavaScript field attributes.
  49. $attr .= $this->onchange ? ' onchange="' . $this->onchange . '"' : '';
  50. // Get the field options.
  51. $options = (array) $this->getOptions();
  52. // Create a read-only list (no name) with hidden input(s) to store the value(s).
  53. if ((string) $this->readonly == '1' || (string) $this->readonly == 'true')
  54. {
  55. $html[] = JHtml::_('select.genericlist', $options, '', trim($attr), 'value', 'text', $this->value, $this->id);
  56. // E.g. form field type tag sends $this->value as array
  57. if ($this->multiple && is_array($this->value))
  58. {
  59. if (!count($this->value))
  60. {
  61. $this->value[] = '';
  62. }
  63. foreach ($this->value as $value)
  64. {
  65. $html[] = '<input type="hidden" name="' . $this->name . '" value="' . htmlspecialchars($value, ENT_COMPAT, 'UTF-8') . '"/>';
  66. }
  67. }
  68. else
  69. {
  70. $html[] = '<input type="hidden" name="' . $this->name . '" value="' . htmlspecialchars($this->value, ENT_COMPAT, 'UTF-8') . '"/>';
  71. }
  72. }
  73. else
  74. // Create a regular list.
  75. {
  76. $html[] = JHtml::_('select.genericlist', $options, $this->name, trim($attr), 'value', 'text', $this->value, $this->id);
  77. }
  78. return implode($html);
  79. }
  80. /**
  81. * Method to get the field options.
  82. *
  83. * @return array The field option objects.
  84. *
  85. * @since 11.1
  86. */
  87. protected function getOptions()
  88. {
  89. $fieldname = preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname);
  90. $options = array();
  91. foreach ($this->element->xpath('option') as $option)
  92. {
  93. // Filter requirements
  94. if ($requires = explode(',', (string) $option['requires']))
  95. {
  96. // Requires multilanguage
  97. if (in_array('multilanguage', $requires) && !JLanguageMultilang::isEnabled())
  98. {
  99. continue;
  100. }
  101. // Requires associations
  102. if (in_array('associations', $requires) && !JLanguageAssociations::isEnabled())
  103. {
  104. continue;
  105. }
  106. }
  107. $value = (string) $option['value'];
  108. $text = trim((string) $option) ? trim((string) $option) : $value;
  109. $disabled = (string) $option['disabled'];
  110. $disabled = ($disabled == 'true' || $disabled == 'disabled' || $disabled == '1');
  111. $disabled = $disabled || ($this->readonly && $value != $this->value);
  112. $checked = (string) $option['checked'];
  113. $checked = ($checked == 'true' || $checked == 'checked' || $checked == '1');
  114. $selected = (string) $option['selected'];
  115. $selected = ($selected == 'true' || $selected == 'selected' || $selected == '1');
  116. $tmp = array(
  117. 'value' => $value,
  118. 'text' => JText::alt($text, $fieldname),
  119. 'disable' => $disabled,
  120. 'class' => (string) $option['class'],
  121. 'selected' => ($checked || $selected),
  122. 'checked' => ($checked || $selected)
  123. );
  124. // Set some event handler attributes. But really, should be using unobtrusive js.
  125. $tmp['onclick'] = (string) $option['onclick'];
  126. $tmp['onchange'] = (string) $option['onchange'];
  127. // Add the option object to the result set.
  128. $options[] = (object) $tmp;
  129. }
  130. reset($options);
  131. return $options;
  132. }
  133. }