PageRenderTime 23ms CodeModel.GetById 13ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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