Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
list.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Form
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * Form Field class for the Joomla Platform.
14  * Supports a generic list of options.
15  *
16  * @package Joomla.Platform
17  * @subpackage Form
18  * @since 11.1
19  */
21 {
22  /**
23  * The form field type.
24  *
25  * @var string
26  * @since 11.1
27  */
28  protected $type = 'List';
29 
30  /**
31  * Method to get the field input markup for a generic list.
32  * Use the multiple attribute to enable multiselect.
33  *
34  * @return string The field input markup.
35  *
36  * @since 11.1
37  */
38  protected function getInput()
39  {
40  $html = array();
41  $attr = '';
42 
43  // Initialize some field attributes.
44  $attr .= !empty($this->class) ? ' class="' . $this->class . '"' : '';
45  $attr .= !empty($this->size) ? ' size="' . $this->size . '"' : '';
46  $attr .= $this->multiple ? ' multiple' : '';
47  $attr .= $this->required ? ' required aria-required="true"' : '';
48  $attr .= $this->autofocus ? ' autofocus' : '';
49 
50  // To avoid user's confusion, readonly="true" should imply disabled="true".
51  if ((string) $this->readonly == '1' || (string) $this->readonly == 'true' || (string) $this->disabled == '1'|| (string) $this->disabled == 'true')
52  {
53  $attr .= ' disabled="disabled"';
54  }
55 
56  // Initialize JavaScript field attributes.
57  $attr .= $this->onchange ? ' onchange="' . $this->onchange . '"' : '';
58 
59  // Get the field options.
60  $options = (array) $this->getOptions();
61 
62  // Create a read-only list (no name) with a hidden input to store the value.
63  if ((string) $this->readonly == '1' || (string) $this->readonly == 'true')
64  {
65  $html[] = JHtml::_('select.genericlist', $options, '', trim($attr), 'value', 'text', $this->value, $this->id);
66  $html[] = '<input type="hidden" name="' . $this->name . '" value="' . $this->value . '"/>';
67  }
68  else
69  // Create a regular list.
70  {
71  $html[] = JHtml::_('select.genericlist', $options, $this->name, trim($attr), 'value', 'text', $this->value, $this->id);
72  }
73 
74  return implode($html);
75  }
76 
77  /**
78  * Method to get the field options.
79  *
80  * @return array The field option objects.
81  *
82  * @since 11.1
83  */
84  protected function getOptions()
85  {
86  $options = array();
87 
88  foreach ($this->element->children() as $option)
89  {
90  // Only add <option /> elements.
91  if ($option->getName() != 'option')
92  {
93  continue;
94  }
95 
96  // Filter requirements
97  if ($requires = explode(',', (string) $option['requires']))
98  {
99  // Requires multilanguage
100  if (in_array('multilanguage', $requires) && !JLanguageMultilang::isEnabled())
101  {
102  continue;
103  }
104 
105  // Requires associations
106  if (in_array('associations', $requires) && !JLanguageAssociations::isEnabled())
107  {
108  continue;
109  }
110  }
111 
112  $value = (string) $option['value'];
113 
114  $disabled = (string) $option['disabled'];
115  $disabled = ($disabled == 'true' || $disabled == 'disabled' || $disabled == '1');
116 
117  $disabled = $disabled || ($this->readonly && $value != $this->value);
118 
119  // Create a new option object based on the <option /> element.
120  $tmp = JHtml::_(
121  'select.option', $value,
122  JText::alt(trim((string) $option), preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname)), 'value', 'text',
123  $disabled
124  );
125 
126  // Set some option attributes.
127  $tmp->class = (string) $option['class'];
128 
129  // Set some JavaScript option attributes.
130  $tmp->onclick = (string) $option['onclick'];
131 
132  // Add the option object to the result set.
133  $options[] = $tmp;
134  }
135 
136  reset($options);
137 
138  return $options;
139  }
140 }