1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Form
5 *
6 * @copyright Copyright (C) 2005 - 2017 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 * @link http://www.w3.org/TR/html-markup/input.text.html#input.text
17 * @since 11.1
18 */
19 class JFormFieldText extends JFormField
20 {
21 /**
22 * The form field type.
23 *
24 * @var string
25 * @since 11.1
26 */
27 protected $type = 'Text';
28
29 /**
30 * The allowable maxlength of the field.
31 *
32 * @var integer
33 * @since 3.2
34 */
35 protected $maxLength;
36
37 /**
38 * The mode of input associated with the field.
39 *
40 * @var mixed
41 * @since 3.2
42 */
43 protected $inputmode;
44
45 /**
46 * The name of the form field direction (ltr or rtl).
47 *
48 * @var string
49 * @since 3.2
50 */
51 protected $dirname;
52
53 /**
54 * Name of the layout being used to render the field
55 *
56 * @var string
57 * @since 3.7
58 */
59 protected $layout = 'joomla.form.field.text';
60
61 /**
62 * Method to get certain otherwise inaccessible properties from the form field object.
63 *
64 * @param string $name The property name for which to the the value.
65 *
66 * @return mixed The property value or null.
67 *
68 * @since 3.2
69 */
70 public function __get($name)
71 {
72 switch ($name)
73 {
74 case 'maxLength':
75 case 'dirname':
76 case 'inputmode':
77 return $this->$name;
78 }
79
80 return parent::__get($name);
81 }
82
83 /**
84 * Method to set certain otherwise inaccessible properties of the form field object.
85 *
86 * @param string $name The property name for which to the the value.
87 * @param mixed $value The value of the property.
88 *
89 * @return void
90 *
91 * @since 3.2
92 */
93 public function __set($name, $value)
94 {
95 switch ($name)
96 {
97 case 'maxLength':
98 $this->maxLength = (int) $value;
99 break;
100
101 case 'dirname':
102 $value = (string) $value;
103 $this->dirname = ($value == $name || $value == 'true' || $value == '1');
104 break;
105
106 case 'inputmode':
107 $this->inputmode = (string) $value;
108 break;
109
110 default:
111 parent::__set($name, $value);
112 }
113 }
114
115 /**
116 * Method to attach a JForm object to the field.
117 *
118 * @param SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
119 * @param mixed $value The form field value to validate.
120 * @param string $group The field name group control value. This acts as an array container for the field.
121 * For example if the field has name="foo" and the group value is set to "bar" then the
122 * full field name would end up being "bar[foo]".
123 *
124 * @return boolean True on success.
125 *
126 * @see JFormField::setup()
127 * @since 3.2
128 */
129 public function setup(SimpleXMLElement $element, $value, $group = null)
130 {
131 $result = parent::setup($element, $value, $group);
132
133 if ($result == true)
134 {
135 $inputmode = (string) $this->element['inputmode'];
136 $dirname = (string) $this->element['dirname'];
137
138 $this->inputmode = '';
139 $inputmode = preg_replace('/\s+/', ' ', trim($inputmode));
140 $inputmode = explode(' ', $inputmode);
141
142 if (!empty($inputmode))
143 {
144 $defaultInputmode = in_array('default', $inputmode) ? JText::_('JLIB_FORM_INPUTMODE') . ' ' : '';
145
146 foreach (array_keys($inputmode, 'default') as $key)
147 {
148 unset($inputmode[$key]);
149 }
150
151 $this->inputmode = $defaultInputmode . implode(' ', $inputmode);
152 }
153
154 // Set the dirname.
155 $dirname = ((string) $dirname == 'dirname' || $dirname == 'true' || $dirname == '1');
156 $this->dirname = $dirname ? $this->getName($this->fieldname . '_dir') : false;
157
158 $this->maxLength = (int) $this->element['maxlength'];
159 }
160
161 return $result;
162 }
163
164 /**
165 * Method to get the field input markup.
166 *
167 * @return string The field input markup.
168 *
169 * @since 11.1
170 */
171 protected function getInput()
172 {
173 if ($this->element['useglobal'])
174 {
175 $component = JFactory::getApplication()->input->getCmd('option');
176
177 // Get correct component for menu items
178 if ($component == 'com_menus')
179 {
180 $link = $this->form->getData()->get('link');
181 $uri = new JUri($link);
182 $component = $uri->getVar('option', 'com_menus');
183 }
184
185 $params = JComponentHelper::getParams($component);
186 $value = $params->get($this->fieldname);
187
188 // Try with global configuration
189 if (is_null($value))
190 {
191 $value = JFactory::getConfig()->get($this->fieldname);
192 }
193
194 // Try with menu configuration
195 if (is_null($value) && JFactory::getApplication()->input->getCmd('option') == 'com_menus')
196 {
197 $value = JComponentHelper::getParams('com_menus')->get($this->fieldname);
198 }
199
200 if (!is_null($value))
201 {
202 $value = (string) $value;
203
204 $this->hint = JText::sprintf('JGLOBAL_USE_GLOBAL_VALUE', $value);
205 }
206 }
207
208 return $this->getRenderer($this->layout)->render($this->getLayoutData());
209 }
210
211 /**
212 * Method to get the field options.
213 *
214 * @return array The field option objects.
215 *
216 * @since 3.4
217 */
218 protected function getOptions()
219 {
220 $options = array();
221
222 foreach ($this->element->children() as $option)
223 {
224 // Only add <option /> elements.
225 if ($option->getName() != 'option')
226 {
227 continue;
228 }
229
230 // Create a new option object based on the <option /> element.
231 $options[] = JHtml::_(
232 'select.option', (string) $option['value'],
233 JText::alt(trim((string) $option), preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname)), 'value', 'text'
234 );
235 }
236
237 return $options;
238 }
239
240 /**
241 * Method to get the field suggestions.
242 *
243 * @return array The field option objects.
244 *
245 * @since 3.2
246 * @deprecated 4.0 Use getOptions instead
247 */
248 protected function getSuggestions()
249 {
250 return $this->getOptions();
251 }
252
253 /**
254 * Method to get the data to be passed to the layout for rendering.
255 *
256 * @return array
257 *
258 * @since 3.7
259 */
260 protected function getLayoutData()
261 {
262 $data = parent::getLayoutData();
263
264 // Initialize some field attributes.
265 $maxLength = !empty($this->maxLength) ? ' maxlength="' . $this->maxLength . '"' : '';
266 $inputmode = !empty($this->inputmode) ? ' inputmode="' . $this->inputmode . '"' : '';
267 $dirname = !empty($this->dirname) ? ' dirname="' . $this->dirname . '"' : '';
268
269 /* Get the field options for the datalist.
270 Note: getSuggestions() is deprecated and will be changed to getOptions() with 4.0. */
271 $options = (array) $this->getSuggestions();
272
273 $extraData = array(
274 'maxLength' => $maxLength,
275 'pattern' => $this->pattern,
276 'inputmode' => $inputmode,
277 'dirname' => $dirname,
278 'options' => $options,
279 );
280
281 return array_merge($data, $extraData);
282 }
283 }
284