Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 177 lines (153 sloc) 4.164 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
<?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;
}
}
Something went wrong with that request. Please try again.