Select::options()   F
last analyzed

Complexity

Conditions 38
Paths > 20000

Size

Total Lines 174

Duplication

Lines 21
Ratio 12.07 %

Importance

Changes 0
Metric Value
cc 38
nc 932232
nop 5
dl 21
loc 174
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Part of the Joomla Framework Form Package
4
 *
5
 * @copyright  Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
6
 * @license    GNU General Public License version 2 or later; see LICENSE
7
 */
8
9
namespace Joomla\Form\Html;
10
11
use Joomla\Language\Text;
12
use Joomla\Utilities\ArrayHelper;
13
14
/**
15
 * Utility class for creating HTML select lists
16
 *
17
 * @since       1.0
18
 * @deprecated  The joomla/form package is deprecated
19
 */
20
abstract class Select
21
{
22
	/**
23
	 * Option values related to the generation of HTML output. Recognized
24
	 * options are:
25
	 *     fmtDepth, integer. The current indent depth.
26
	 *     fmtEol, string. The end of line string, default is linefeed.
27
	 *     fmtIndent, string. The string to use for indentation, default is
28
	 *     tab.
29
	 *
30
	 * @var    array
31
	 * @since  1.0
32
	 */
33
	protected static $formatOptions = array('format.depth' => 0, 'format.eol' => "\n", 'format.indent' => "\t");
34
35
	/**
36
	 * Default values for options. Organized by option group.
37
	 *
38
	 * @var     array
39
	 * @since   1.0
40
	 */
41
	static protected $optionDefaults = array(
42
		'option' => array('option.attr' => null, 'option.disable' => 'disable', 'option.id' => null, 'option.key' => 'value',
43
			'option.key.toHtml' => true, 'option.label' => null, 'option.label.toHtml' => true, 'option.text' => 'text',
44
			'option.text.toHtml' => true));
45
46
	/**
47
	 * Generates a yes/no radio list.
48
	 *
49
	 * @param   string  $name      The value of the HTML name attribute
50
	 * @param   array   $attribs   Additional HTML attributes for the <select> tag
51
	 * @param   string  $selected  The key that is selected
52
	 * @param   string  $yes       Language key for Yes
53
	 * @param   string  $no        Language key for no
54
	 * @param   string  $id        The id for the field
55
	 *
56
	 * @return  string  HTML for the radio list
57
	 *
58
	 * @see     \Joomla\Form\Field\Radio
59
	 * @since   1.0
60
	 */
61
	public static function booleanlist($name, $attribs = null, $selected = null, $yes = 'JYES', $no = 'JNO', $id = false)
62
	{
63
		$arr = array(self::option('0', Text::_($no)), self::option('1', Text::_($yes)));
0 ignored issues
show
Deprecated Code introduced by
The method Joomla\Language\Text::_() has been deprecated with message: 2.0 Will be replaced with a `translate` method.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
64
65
		return self::radiolist($arr, $name, $attribs, 'value', 'text', (int) $selected, $id);
0 ignored issues
show
Bug introduced by
It seems like $attribs defined by parameter $attribs on line 61 can also be of type array; however, Joomla\Form\Html\Select::radiolist() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $id defined by parameter $id on line 61 can also be of type string; however, Joomla\Form\Html\Select::radiolist() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
66
	}
67
68
	/**
69
	 * Generates an HTML selection list.
70
	 *
71
	 * @param   array    $data       An array of objects, arrays, or scalars.
72
	 * @param   string   $name       The value of the HTML name attribute.
73
	 * @param   mixed    $attribs    Additional HTML attributes for the <select> tag. This
74
	 *                               can be an array of attributes, or an array of options. Treated as options
75
	 *                               if it is the last argument passed. Valid options are:
76
	 *                               Format options, see {@see Select::$formatOptions}.
77
	 *                               Selection options, see {@see Select::options()}.
78
	 *                               list.attr, string|array: Additional attributes for the select
79
	 *                               element.
80
	 *                               id, string: Value to use as the select element id attribute.
81
	 *                               Defaults to the same as the name.
82
	 *                               list.select, string|array: Identifies one or more option elements
83
	 *                               to be selected, based on the option key values.
84
	 * @param   string   $optKey     The name of the object variable for the option value. If
85
	 *                               set to null, the index of the value array is used.
86
	 * @param   string   $optText    The name of the object variable for the option text.
87
	 * @param   mixed    $selected   The key that is selected (accepts an array or a string).
88
	 * @param   mixed    $idtag      Value of the field id or null by default
89
	 * @param   boolean  $translate  True to translate
90
	 *
91
	 * @return  string  HTML for the select list.
92
	 *
93
	 * @since   1.0
94
	 */
95
	public static function genericlist($data, $name, $attribs = null, $optKey = 'value', $optText = 'text', $selected = null, $idtag = false,
96
		$translate = false)
97
	{
98
		// Set default options
99
		$options = array_merge(self::$formatOptions, array('format.depth' => 0, 'id' => false));
100
101
		if (is_array($attribs) && func_num_args() == 3)
102
		{
103
			// Assume we have an options array
104
			$options = array_merge($options, $attribs);
105
		}
106
		else
107
		{
108
			// Get options from the parameters
109
			$options['id'] = $idtag;
110
			$options['list.attr'] = $attribs;
111
			$options['list.translate'] = $translate;
112
			$options['option.key'] = $optKey;
113
			$options['option.text'] = $optText;
114
			$options['list.select'] = $selected;
115
		}
116
117
		$attribs = '';
118
119 View Code Duplication
		if (isset($options['list.attr']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
		{
121
			if (is_array($options['list.attr']))
122
			{
123
				$attribs = ArrayHelper::toString($options['list.attr']);
124
			}
125
			else
126
			{
127
				$attribs = $options['list.attr'];
128
			}
129
130
			if ($attribs != '')
131
			{
132
				$attribs = ' ' . $attribs;
133
			}
134
		}
135
136
		$id = $options['id'] !== false ? $options['id'] : $name;
137
		$id = str_replace(array('[', ']'), '', $id);
138
139
		$baseIndent = str_repeat($options['format.indent'], $options['format.depth']++);
140
		$html = $baseIndent . '<select' . ($id !== '' ? ' id="' . $id . '"' : '') . ' name="' . $name . '"' . $attribs . '>' . $options['format.eol']
141
			. self::options($data, $options) . $baseIndent . '</select>' . $options['format.eol'];
142
143
		return $html;
144
	}
145
146
	/**
147
	 * Generates a grouped HTML selection list from nested arrays.
148
	 *
149
	 * @param   array   $data     An array of groups, each of which is an array of options.
150
	 * @param   string  $name     The value of the HTML name attribute
151
	 * @param   array   $options  Options, an array of key/value pairs. Valid options are:
152
	 *                            Format options, {@see Select::$formatOptions}.
153
	 *                            Selection options. See {@see Select::options()}.
154
	 *                            group.id: The property in each group to use as the group id
155
	 *                            attribute. Defaults to none.
156
	 *                            group.label: The property in each group to use as the group
157
	 *                            label. Defaults to "text". If set to null, the data array index key is
158
	 *                            used.
159
	 *                            group.items: The property in each group to use as the array of
160
	 *                            items in the group. Defaults to "items". If set to null, group.id and
161
	 *                            group. label are forced to null and the data element is assumed to be a
162
	 *                            list of selections.
163
	 *                            id: Value to use as the select element id attribute. Defaults to
164
	 *                            the same as the name.
165
	 *                            list.attr: Attributes for the select element. Can be a string or
166
	 *                            an array of key/value pairs. Defaults to none.
167
	 *                            list.select: either the value of one selected option or an array
168
	 *                            of selected options. Default: none.
169
	 *                            list.translate: Boolean. If set, text and labels are translated via
170
	 *                            Text::_().
171
	 *
172
	 * @return  string  HTML for the select list
173
	 *
174
	 * @since   1.0
175
	 * @throws  \RuntimeException If a group has contents that cannot be processed.
176
	 */
177
	public static function groupedlist($data, $name, $options = array())
178
	{
179
		// Set default options and overwrite with anything passed in
180
		$options = array_merge(
181
			self::$formatOptions,
182
			array('format.depth' => 0, 'group.items' => 'items', 'group.label' => 'text', 'group.label.toHtml' => true, 'id' => false),
183
			$options
184
		);
185
186
		// Apply option rules
187
		if ($options['group.items'] === null)
188
		{
189
			$options['group.label'] = null;
190
		}
191
192
		$attribs = '';
193
194 View Code Duplication
		if (isset($options['list.attr']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
195
		{
196
			if (is_array($options['list.attr']))
197
			{
198
				$attribs = ArrayHelper::toString($options['list.attr']);
199
			}
200
			else
201
			{
202
				$attribs = $options['list.attr'];
203
			}
204
205
			if ($attribs != '')
206
			{
207
				$attribs = ' ' . $attribs;
208
			}
209
		}
210
211
		$id = $options['id'] !== false ? $options['id'] : $name;
212
		$id = str_replace(array('[', ']'), '', $id);
213
214
		// Disable groups in the options.
215
		$options['groups'] = false;
216
217
		$baseIndent = str_repeat($options['format.indent'], $options['format.depth']++);
218
		$html = $baseIndent . '<select' . ($id !== '' ? ' id="' . $id . '"' : '') . ' name="' . $name . '"' . $attribs . '>' . $options['format.eol'];
219
		$groupIndent = str_repeat($options['format.indent'], $options['format.depth']++);
220
221
		foreach ($data as $dataKey => $group)
222
		{
223
			$label = $dataKey;
224
			$id = '';
225
			$noGroup = is_int($dataKey);
226
227
			if ($options['group.items'] == null)
228
			{
229
				// Sub-list is an associative array
230
				$subList = $group;
231
			}
232
			elseif (is_array($group))
233
			{
234
				// Sub-list is in an element of an array.
235
				$subList = $group[$options['group.items']];
236
237 View Code Duplication
				if (isset($group[$options['group.label']]))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
238
				{
239
					$label = $group[$options['group.label']];
240
					$noGroup = false;
241
				}
242
243 View Code Duplication
				if (isset($options['group.id']) && isset($group[$options['group.id']]))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
244
				{
245
					$id = $group[$options['group.id']];
246
					$noGroup = false;
247
				}
248
			}
249
			elseif (is_object($group))
250
			{
251
				// Sub-list is in a property of an object
252
				$subList = $group->$options['group.items'];
253
254 View Code Duplication
				if (isset($group->$options['group.label']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
255
				{
256
					$label = $group->$options['group.label'];
257
					$noGroup = false;
258
				}
259
260 View Code Duplication
				if (isset($options['group.id']) && isset($group->$options['group.id']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
261
				{
262
					$id = $group->$options['group.id'];
263
					$noGroup = false;
264
				}
265
			}
266
			else
267
			{
268
				throw new \RuntimeException('Invalid group contents.', 1);
269
			}
270
271
			if ($noGroup)
272
			{
273
				$html .= self::options($subList, $options);
274
			}
275
			else
276
			{
277
				$html .= $groupIndent . '<optgroup' . (empty($id) ? '' : ' id="' . $id . '"') . ' label="'
278
					. ($options['group.label.toHtml'] ? htmlspecialchars($label, ENT_COMPAT, 'UTF-8') : $label) . '">' . $options['format.eol']
279
					. self::options($subList, $options) . $groupIndent . '</optgroup>' . $options['format.eol'];
280
			}
281
		}
282
283
		$html .= $baseIndent . '</select>' . $options['format.eol'];
284
285
		return $html;
286
	}
287
288
	/**
289
	 * Generates a selection list of integers.
290
	 *
291
	 * @param   integer  $start     The start integer
292
	 * @param   integer  $end       The end integer
293
	 * @param   integer  $inc       The increment
294
	 * @param   string   $name      The value of the HTML name attribute
295
	 * @param   mixed    $attribs   Additional HTML attributes for the <select> tag, an array of
296
	 *                              attributes, or an array of options. Treated as options if it is the last
297
	 *                              argument passed.
298
	 * @param   mixed    $selected  The key that is selected
299
	 * @param   string   $format    The printf format to be applied to the number
300
	 *
301
	 * @return  string   HTML for the select list
302
	 *
303
	 * @since   1.0
304
	 */
305
	public static function integerlist($start, $end, $inc, $name, $attribs = null, $selected = null, $format = '')
306
	{
307
		// Set default options
308
		$options = array_merge(self::$formatOptions, array('format.depth' => 0, 'option.format' => '', 'id' => null));
309
310 View Code Duplication
		if (is_array($attribs) && func_num_args() == 5)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
311
		{
312
			// Assume we have an options array
313
			$options = array_merge($options, $attribs);
314
315
			// Extract the format and remove it from downstream options
316
			$format = $options['option.format'];
317
			unset($options['option.format']);
318
		}
319
		else
320
		{
321
			// Get options from the parameters
322
			$options['list.attr'] = $attribs;
323
			$options['list.select'] = $selected;
324
		}
325
326
		$start = (int) $start;
327
		$end   = (int) $end;
328
		$inc   = (int) $inc;
329
330
		$data = array();
331
332
		for ($i = $start; $i <= $end; $i += $inc)
333
		{
334
			$data[$i] = $format ? sprintf($format, $i) : $i;
335
		}
336
337
		// Tell genericlist() to use array keys
338
		$options['option.key'] = null;
339
340
		return self::genericlist($data, $name, $options);
341
	}
342
343
	/**
344
	 * Create an object that represents an option in an option list.
345
	 *
346
	 * @param   string   $value    The value of the option
347
	 * @param   string   $text     The text for the option
348
	 * @param   mixed    $optKey   If a string, the returned object property name for
349
	 *                             the value. If an array, options. Valid options are:
350
	 *                             attr: String|array. Additional attributes for this option.
351
	 *                             Defaults to none.
352
	 *                             disable: Boolean. If set, this option is disabled.
353
	 *                             label: String. The value for the option label.
354
	 *                             option.attr: The property in each option array to use for
355
	 *                             additional selection attributes. Defaults to none.
356
	 *                             option.disable: The property that will hold the disabled state.
357
	 *                             Defaults to "disable".
358
	 *                             option.key: The property that will hold the selection value.
359
	 *                             Defaults to "value".
360
	 *                             option.label: The property in each option array to use as the
361
	 *                             selection label attribute. If a "label" option is provided, defaults to
362
	 *                             "label", if no label is given, defaults to null (none).
363
	 *                             option.text: The property that will hold the the displayed text.
364
	 *                             Defaults to "text". If set to null, the option array is assumed to be a
365
	 *                             list of displayable scalars.
366
	 * @param   string   $optText  The property that will hold the the displayed text. This
367
	 *                             parameter is ignored if an options array is passed.
368
	 * @param   boolean  $disable  Not used.
369
	 *
370
	 * @return  object
371
	 *
372
	 * @since   1.0
373
	 */
374
	public static function option($value, $text = '', $optKey = 'value', $optText = 'text', $disable = false)
375
	{
376
		$options = array('attr' => null, 'disable' => false, 'option.attr' => null, 'option.disable' => 'disable', 'option.key' => 'value',
377
			'option.label' => null, 'option.text' => 'text');
378
379
		if (is_array($optKey))
380
		{
381
			// Merge in caller's options
382
			$options = array_merge($options, $optKey);
383
		}
384
		else
385
		{
386
			// Get options from the parameters
387
			$options['option.key'] = $optKey;
388
			$options['option.text'] = $optText;
389
			$options['disable'] = $disable;
390
		}
391
392
		$obj = new \stdClass;
393
		$obj->{$options['option.key']} = $value;
394
		$obj->{$options['option.text']} = trim($text) ? $text : $value;
395
396
		/*
397
		 * If a label is provided, save it. If no label is provided and there is
398
		 * a label name, initialise to an empty string.
399
		 */
400
		$hasProperty = $options['option.label'] !== null;
401
402
		if (isset($options['label']))
403
		{
404
			$labelProperty = $hasProperty ? $options['option.label'] : 'label';
405
			$obj->$labelProperty = $options['label'];
406
		}
407
		elseif ($hasProperty)
408
		{
409
			$obj->{$options['option.label']} = '';
410
		}
411
412
		// Set attributes only if there is a property and a value
413
		if ($options['attr'] !== null)
414
		{
415
			$obj->{$options['option.attr']} = $options['attr'];
416
		}
417
418
		// Set disable only if it has a property and a value
419
		if ($options['disable'] !== null)
420
		{
421
			$obj->{$options['option.disable']} = $options['disable'];
422
		}
423
424
		return $obj;
425
	}
426
427
	/**
428
	 * Generates the option tags for an HTML select list (with no select tag
429
	 * surrounding the options).
430
	 *
431
	 * @param   array    $arr        An array of objects, arrays, or values.
432
	 * @param   mixed    $optKey     If a string, this is the name of the object variable for
433
	 *                               the option value. If null, the index of the array of objects is used. If
434
	 *                               an array, this is a set of options, as key/value pairs. Valid options are:
435
	 *                               -Format options, {@see Select::$formatOptions}.
436
	 *                               -groups: Boolean. If set, looks for keys with the value
437
	 *                                "&lt;optgroup>" and synthesizes groups from them. Deprecated. Defaults
438
	 *                                true for backwards compatibility.
439
	 *                               -list.select: either the value of one selected option or an array
440
	 *                                of selected options. Default: none.
441
	 *                               -list.translate: Boolean. If set, text and labels are translated via
442
	 *                                Text::_(). Default is false.
443
	 *                               -option.id: The property in each option array to use as the
444
	 *                                selection id attribute. Defaults to none.
445
	 *                               -option.key: The property in each option array to use as the
446
	 *                                selection value. Defaults to "value". If set to null, the index of the
447
	 *                                option array is used.
448
	 *                               -option.label: The property in each option array to use as the
449
	 *                                selection label attribute. Defaults to null (none).
450
	 *                               -option.text: The property in each option array to use as the
451
	 *                               displayed text. Defaults to "text". If set to null, the option array is
452
	 *                               assumed to be a list of displayable scalars.
453
	 *                               -option.attr: The property in each option array to use for
454
	 *                                additional selection attributes. Defaults to none.
455
	 *                               -option.disable: The property that will hold the disabled state.
456
	 *                                Defaults to "disable".
457
	 *                               -option.key: The property that will hold the selection value.
458
	 *                                Defaults to "value".
459
	 *                               -option.text: The property that will hold the the displayed text.
460
	 *                               Defaults to "text". If set to null, the option array is assumed to be a
461
	 *                               list of displayable scalars.
462
	 * @param   string   $optText    The name of the object variable for the option text.
463
	 * @param   mixed    $selected   The key that is selected (accepts an array or a string)
464
	 * @param   boolean  $translate  Translate the option values.
465
	 *
466
	 * @return  string  HTML for the select list
467
	 *
468
	 * @since   1.0
469
	 */
470
	public static function options($arr, $optKey = 'value', $optText = 'text', $selected = null, $translate = false)
471
	{
472
		$options = array_merge(
473
			self::$formatOptions,
474
			self::$optionDefaults['option'],
475
			array('format.depth' => 0, 'groups' => true, 'list.select' => null, 'list.translate' => false)
476
		);
477
478 View Code Duplication
		if (is_array($optKey))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
479
		{
480
			// Set default options and overwrite with anything passed in
481
			$options = array_merge($options, $optKey);
482
		}
483
		else
484
		{
485
			// Get options from the parameters
486
			$options['option.key'] = $optKey;
487
			$options['option.text'] = $optText;
488
			$options['list.select'] = $selected;
489
			$options['list.translate'] = $translate;
490
		}
491
492
		$html = '';
493
		$baseIndent = str_repeat($options['format.indent'], $options['format.depth']);
494
495
		foreach ($arr as $elementKey => &$element)
496
		{
497
			$attr = '';
498
			$extra = '';
499
			$label = '';
500
			$id = '';
501
502
			if (is_array($element))
503
			{
504
				$key = $options['option.key'] === null ? $elementKey : $element[$options['option.key']];
505
				$text = $element[$options['option.text']];
506
507
				if (isset($element[$options['option.attr']]))
508
				{
509
					$attr = $element[$options['option.attr']];
510
				}
511
512
				if (isset($element[$options['option.id']]))
513
				{
514
					$id = $element[$options['option.id']];
515
				}
516
517
				if (isset($element[$options['option.label']]))
518
				{
519
					$label = $element[$options['option.label']];
520
				}
521
522 View Code Duplication
				if (isset($element[$options['option.disable']]) && $element[$options['option.disable']])
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
523
				{
524
					$extra .= ' disabled="disabled"';
525
				}
526
			}
527
			elseif (is_object($element))
528
			{
529
				$key = $options['option.key'] === null ? $elementKey : $element->{$options['option.key']};
530
				$text = $element->{$options['option.text']};
531
532
				if (isset($element->{$options['option.attr']}))
533
				{
534
					$attr = $element->{$options['option.attr']};
535
				}
536
537
				if (isset($element->{$options['option.id']}))
538
				{
539
					$id = $element->{$options['option.id']};
540
				}
541
542
				if (isset($element->{$options['option.label']}))
543
				{
544
					$label = $element->{$options['option.label']};
545
				}
546
547 View Code Duplication
				if (isset($element->{$options['option.disable']}) && $element->{$options['option.disable']})
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
548
				{
549
					$extra .= ' disabled="disabled"';
550
				}
551
			}
552
			else
553
			{
554
				// This is a simple associative array
555
				$key = $elementKey;
556
				$text = $element;
557
			}
558
559
			/*
560
			 * The use of options that contain optgroup HTML elements was
561
			 * somewhat hacked for J1.5. J1.6 introduces the grouplist() method
562
			 * to handle this better. The old solution is retained through the
563
			 * "groups" option, which defaults true in J1.6, but should be
564
			 * deprecated at some point in the future.
565
			 */
566
567
			$key = (string) $key;
568
569
			if ($options['groups'] && $key == '<OPTGROUP>')
570
			{
571
				$html .= $baseIndent . '<optgroup label="' . ($options['list.translate'] ? Text::_($text) : $text) . '">' . $options['format.eol'];
0 ignored issues
show
Deprecated Code introduced by
The method Joomla\Language\Text::_() has been deprecated with message: 2.0 Will be replaced with a `translate` method.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
572
				$baseIndent = str_repeat($options['format.indent'], ++$options['format.depth']);
573
			}
574
			elseif ($options['groups'] && $key == '</OPTGROUP>')
575
			{
576
				$baseIndent = str_repeat($options['format.indent'], --$options['format.depth']);
577
				$html .= $baseIndent . '</optgroup>' . $options['format.eol'];
578
			}
579
			else
580
			{
581
				// If no string after hyphen - take hyphen out
582
				$splitText = explode(' - ', $text, 2);
583
				$text = $splitText[0];
584
585
				if (isset($splitText[1]))
586
				{
587
					$text .= ' - ' . $splitText[1];
588
				}
589
590
				if ($options['list.translate'] && !empty($label))
591
				{
592
					$label = Text::_($label);
0 ignored issues
show
Deprecated Code introduced by
The method Joomla\Language\Text::_() has been deprecated with message: 2.0 Will be replaced with a `translate` method.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
593
				}
594
595
				if ($options['option.label.toHtml'])
596
				{
597
					$label = htmlentities($label);
598
				}
599
600
				if (is_array($attr))
601
				{
602
					$attr = ArrayHelper::toString($attr);
603
				}
604
				else
605
				{
606
					$attr = trim($attr);
607
				}
608
609
				$extra = ($id ? ' id="' . $id . '"' : '') . ($label ? ' label="' . $label . '"' : '') . ($attr ? ' ' . $attr : '') . $extra;
610
611
				if (is_array($options['list.select']))
612
				{
613
					foreach ($options['list.select'] as $val)
614
					{
615
						$key2 = is_object($val) ? $val->$options['option.key'] : $val;
616
617
						if ($key == $key2)
618
						{
619
							$extra .= ' selected="selected"';
620
							break;
621
						}
622
					}
623
				}
624
				elseif ((string) $key == (string) $options['list.select'])
625
				{
626
					$extra .= ' selected="selected"';
627
				}
628
629
				if ($options['list.translate'])
630
				{
631
					$text = Text::_($text);
0 ignored issues
show
Deprecated Code introduced by
The method Joomla\Language\Text::_() has been deprecated with message: 2.0 Will be replaced with a `translate` method.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
632
				}
633
634
				// Generate the option, encoding as required
635
				$html .= $baseIndent . '<option value="' . ($options['option.key.toHtml'] ? htmlspecialchars($key, ENT_COMPAT, 'UTF-8') : $key) . '"'
636
					. $extra . '>';
637
				$html .= $options['option.text.toHtml'] ? htmlentities(html_entity_decode($text, ENT_COMPAT, 'UTF-8'), ENT_COMPAT, 'UTF-8') : $text;
638
				$html .= '</option>' . $options['format.eol'];
639
			}
640
		}
641
642
		return $html;
643
	}
644
645
	/**
646
	 * Generates an HTML radio list.
647
	 *
648
	 * @param   array    $data       An array of objects
649
	 * @param   string   $name       The value of the HTML name attribute
650
	 * @param   string   $attribs    Additional HTML attributes for the <select> tag
651
	 * @param   mixed    $optKey     The key that is selected
652
	 * @param   string   $optText    The name of the object variable for the option value
653
	 * @param   string   $selected   The name of the object variable for the option text
654
	 * @param   boolean  $idtag      Value of the field id or null by default
655
	 * @param   boolean  $translate  True if options will be translated
656
	 *
657
	 * @return  string  HTML for the select list
658
	 *
659
	 * @since   1.0
660
	 */
661
	public static function radiolist($data, $name, $attribs = null, $optKey = 'value', $optText = 'text', $selected = null, $idtag = false,
662
		$translate = false)
663
	{
664
		reset($data);
665
		$html = '';
666
667
		if (is_array($attribs))
668
		{
669
			$attribs = ArrayHelper::toString($attribs);
670
		}
671
672
		$id_text = $idtag ? $idtag : $name;
673
674
		foreach ($data as $obj)
675
		{
676
			$k = $obj->$optKey;
677
			$t = $translate ? Text::_($obj->$optText) : $obj->$optText;
0 ignored issues
show
Deprecated Code introduced by
The method Joomla\Language\Text::_() has been deprecated with message: 2.0 Will be replaced with a `translate` method.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
678
			$id = (isset($obj->id) ? $obj->id : null);
679
680
			$extra = '';
681
			$extra .= $id ? ' id="' . $obj->id . '"' : '';
682
683
			if (is_array($selected))
684
			{
685
				foreach ($selected as $val)
686
				{
687
					$k2 = is_object($val) ? $val->$optKey : $val;
688
689
					if ($k == $k2)
690
					{
691
						$extra .= ' selected="selected"';
692
						break;
693
					}
694
				}
695
			}
696
			else
697
			{
698
				$extra .= ((string) $k == (string) $selected ? ' checked="checked"' : '');
699
			}
700
701
			$html .= "\n\t" . '<input type="radio" name="' . $name . '"' . ' id="' . $id_text . $k . '" value="' . $k . '"' . ' ' . $extra . ' '
702
				. $attribs . '/>' . "\n\t" . '<label for="' . $id_text . $k . '"' . ' id="' . $id_text . $k . '-lbl" class="radiobtn">' . $t
703
				. '</label>';
704
		}
705
706
		$html .= "\n";
707
708
		return $html;
709
	}
710
}
711