Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
text.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 one line text field.
15  *
16  * @package Joomla.Platform
17  * @subpackage Form
18  * @link http://www.w3.org/TR/html-markup/input.text.html#input.text
19  * @since 11.1
20  */
22 {
23  /**
24  * The form field type.
25  *
26  * @var string
27  *
28  * @since 11.1
29  */
30  protected $type = 'Text';
31 
32  /**
33  * The allowable maxlength of the field.
34  *
35  * @var integer
36  * @since 3.2
37  */
38  protected $maxLength;
39 
40  /**
41  * The mode of input associated with the field.
42  *
43  * @var mixed
44  * @since 3.2
45  */
46  protected $inputmode;
47 
48  /**
49  * The name of the form field direction (ltr or rtl).
50  *
51  * @var string
52  * @since 3.2
53  */
54  protected $dirname;
55 
56  /**
57  * Method to get certain otherwise inaccessible properties from the form field object.
58  *
59  * @param string $name The property name for which to the the value.
60  *
61  * @return mixed The property value or null.
62  *
63  * @since 3.2
64  */
65  public function __get($name)
66  {
67  switch ($name)
68  {
69  case 'maxLength':
70  case 'dirname':
71  case 'inputmode':
72  return $this->$name;
73  }
74 
75  return parent::__get($name);
76  }
77 
78  /**
79  * Method to set certain otherwise inaccessible properties of the form field object.
80  *
81  * @param string $name The property name for which to the the value.
82  * @param mixed $value The value of the property.
83  *
84  * @return void
85  *
86  * @since 3.2
87  */
88  public function __set($name, $value)
89  {
90  switch ($name)
91  {
92  case 'maxLength':
93  $this->maxLength = (int) $value;
94  break;
95 
96  case 'dirname':
97  $value = (string) $value;
98  $value = ($value == $name || $value == 'true' || $value == '1');
99 
100  case 'inputmode':
101  $this->name = (string) $value;
102  break;
103 
104  default:
105  parent::__set($name, $value);
106  }
107  }
108 
109  /**
110  * Method to attach a JForm object to the field.
111  *
112  * @param SimpleXMLElement $element The SimpleXMLElement object representing the <field /> tag for the form field object.
113  * @param mixed $value The form field value to validate.
114  * @param string $group The field name group control value. This acts as as an array container for the field.
115  * For example if the field has name="foo" and the group value is set to "bar" then the
116  * full field name would end up being "bar[foo]".
117  *
118  * @return boolean True on success.
119  *
120  * @see JFormField::setup()
121  * @since 3.2
122  */
123  public function setup(SimpleXMLElement $element, $value, $group = null)
124  {
125  $result = parent::setup($element, $value, $group);
126 
127  if ($result == true)
128  {
129  $inputmode = (string) $this->element['inputmode'];
130  $dirname = (string) $this->element['dirname'];
131 
132  $this->inputmode = '';
133  $inputmode = preg_replace('/\s+/', ' ', trim($inputmode));
134  $inputmode = explode(' ', $inputmode);
135 
136  if (!empty($inputmode))
137  {
138  $defaultInputmode = in_array('default', $inputmode) ? JText::_("JLIB_FORM_INPUTMODE") . ' ' : '';
139 
140  foreach (array_keys($inputmode, 'default') as $key)
141  {
142  unset($inputmode[$key]);
143  }
144 
145  $this->inputmode = $defaultInputmode . implode(" ", $inputmode);
146  }
147 
148  // Set the dirname.
149  $dirname = ((string) $dirname == 'dirname' || $dirname == 'true' || $dirname == '1');
150  $this->dirname = $dirname ? $this->getName($this->fieldname . '_dir') : false;
151 
152  $this->maxLength = (int) $this->element['maxlength'];
153  }
154 
155  return $result;
156  }
157 
158  /**
159  * Method to get the field input markup.
160  *
161  * @return string The field input markup.
162  *
163  * @since 11.1
164  */
165  protected function getInput()
166  {
167  // Translate placeholder text
168  $hint = $this->translateHint ? JText::_($this->hint) : $this->hint;
169 
170  // Initialize some field attributes.
171  $size = !empty($this->size) ? ' size="' . $this->size . '"' : '';
172  $maxLength = !empty($this->maxLength) ? ' maxlength="' . $this->maxLength . '"' : '';
173  $class = !empty($this->class) ? ' class="' . $this->class . '"' : '';
174  $readonly = $this->readonly ? ' readonly' : '';
175  $disabled = $this->disabled ? ' disabled' : '';
176  $required = $this->required ? ' required aria-required="true"' : '';
177  $hint = $hint ? ' placeholder="' . $hint . '"' : '';
178  $autocomplete = !$this->autocomplete ? ' autocomplete="off"' : ' autocomplete="' . $this->autocomplete . '"';
179  $autocomplete = $autocomplete == ' autocomplete="on"' ? '' : $autocomplete;
180  $autofocus = $this->autofocus ? ' autofocus' : '';
181  $spellcheck = $this->spellcheck ? '' : ' spellcheck="false"';
182  $pattern = !empty($this->pattern) ? ' pattern="' . $this->pattern . '"' : '';
183  $inputmode = !empty($this->inputmode) ? ' inputmode="' . $this->inputmode . '"' : '';
184  $dirname = !empty($this->dirname) ? ' dirname="' . $this->dirname . '"' : '';
185  $list = '';
186 
187  // Initialize JavaScript field attributes.
188  $onchange = !empty($this->onchange) ? ' onchange="' . $this->onchange . '"' : '';
189 
190  // Including fallback code for HTML5 non supported browsers.
191  JHtml::_('jquery.framework');
192  JHtml::_('script', 'system/html5fallback.js', false, true);
193 
194  // Get the field suggestions.
195  $options = (array) $this->getSuggestions();
196 
197  if (!empty($options))
198  {
199  $html[] = JHtml::_('select.suggestionlist', $options, 'value', 'text', $this->id . '_datalist"');
200  $list = ' list="' . $this->id . '_datalist"';
201  }
202 
203  $html[] = '<input type="text" name="' . $this->name . '" id="' . $this->id . '"' . $dirname . ' value="'
204  . htmlspecialchars($this->value, ENT_COMPAT, 'UTF-8') . '"' . $class . $size . $disabled . $readonly . $list
205  . $hint . $onchange . $maxLength . $required . $autocomplete . $autofocus . $spellcheck . $inputmode . $pattern . ' />';
206 
207  return implode($html);
208  }
209 
210  /**
211  * Method to get the field suggestions.
212  *
213  * @return array The field option objects.
214  *
215  * @since 11.1
216  */
217  protected function getSuggestions()
218  {
219  $options = array();
220 
221  foreach ($this->element->children() as $option)
222  {
223  // Only add <option /> elements.
224  if ($option->getName() != 'option')
225  {
226  continue;
227  }
228 
229  // Create a new option object based on the <option /> element.
230  $options[] = JHtml::_(
231  'select.option', (string) $option['value'],
232  JText::alt(trim((string) $option), preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname)), 'value', 'text'
233  );
234  }
235 
236  reset($options);
237 
238  return $options;
239  }
240 }