Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

178 lines (153 sloc) 4.164 kb
<?php
/**
* @package Joomla.Platform
* @subpackage Form
*
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
defined('JPATH_PLATFORM') or die;
/**
* Form Field class for the Joomla Platform.
* Supports a generic list of options.
*
* @package Joomla.Platform
* @subpackage Form
* @since 11.1
*/
class JFormFieldList extends JFormField
{
/**
* The form field type.
*
* @var string
* @since 11.1
*/
protected $type = 'List';
/**
* Method to get the field input markup for a generic list.
* Use the multiple attribute to enable multiselect.
*
* @return string The field input markup.
*
* @since 11.1
*/
protected function getInput()
{
// Initialize variables.
$html = array();
$attr = '';
// Initialize some field attributes.
$attr .= $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';
// To avoid user's confusion, readonly="true" should imply disabled="true".
if ((string) $this->element['readonly'] == 'true' || (string) $this->element['disabled'] == 'true')
{
$attr .= ' disabled="disabled"';
}
$attr .= $this->element['size'] ? ' size="' . (int) $this->element['size'] . '"' : '';
$attr .= $this->multiple ? ' multiple="multiple"' : '';
// Initialize JavaScript field attributes.
$attr .= $this->element['onchange'] ? ' onchange="' . (string) $this->element['onchange'] . '"' : '';
// Get the field options.
$options = (array) $this->getOptions();
// Get the value(s)
if ($this->multiple)
{
if (empty($this->value))
{
// Filter the option using the preset flag
$filter = array_filter(
$options,
function($option)
{
return $option->preset;
}
);
// If the data exists or there is no preset
if ($this->form->existValue($this->fieldname, $this->group) || empty($filter))
{
$filter = array_filter(
$options,
function($option)
{
return $option->default;
}
);
}
// Extract the values from the filter
$values = array_map(
function ($option)
{
return $option->value;
},
$filter
);
}
elseif (is_array($this->value))
{
$values = $this->value;
}
else
{
$values = array($this->value);
}
}
else
{
$values = array($this->value);
}
// Create a read-only list (no name) with a hidden input to store the value.
if ((string) $this->element['readonly'] == 'true')
{
$html[] = JHtml::_('select.genericlist', $options, '', trim($attr), 'value', 'text', $values, $this->id);
foreach ($values as $value)
{
$html[] = '<input type="hidden" name="' . $this->name . '" value="' . $value . '"/>';
}
}
// Create a regular list.
else
{
$html[] = JHtml::_('select.genericlist', $options, $this->name, trim($attr), 'value', 'text', $values, $this->id);
}
return implode($html);
}
/**
* Method to get the field options.
*
* @return array The field option objects.
*
* @since 11.1
*/
protected function getOptions()
{
// Initialize variables.
$options = array();
foreach ($this->element->children() as $option)
{
// Only add <option /> elements.
if ($option->getName() != 'option')
{
continue;
}
// Create a new option object based on the <option /> element.
$tmp = JHtml::_(
'select.option', (string) $option['value'],
JText::alt(trim((string) $option), preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname)), 'value', 'text',
((string) $option['disabled'] == 'true')
);
// Set some option attributes.
$tmp->class = (string) $option['class'];
// Set some JavaScript option attributes.
$tmp->onclick = (string) $option['onclick'];
// Set the default attribute.
$tmp->default = (string) $option['default'] == 'true';
// Set the preset attribute.
$tmp->preset = (string) $option['preset'] == 'true';
// Add the option object to the result set.
$options[] = $tmp;
}
reset($options);
return $options;
}
}
Jump to Line
Something went wrong with that request. Please try again.