PageRenderTime 28ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/rokcommon/RokCommon/HTML/SelectList.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 719 lines | 412 code | 47 blank | 260 comment | 63 complexity | ca2714841bf8415a63f2d009dc017c0d MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage HTML
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('ROKCOMMON') or die;
  10. /**
  11. * Utility class for creating HTML select lists
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage HTML
  15. * @since 11.1
  16. */
  17. abstract class RokCommon_HTML_SelectList
  18. {
  19. public static $formatOptions = array('format.depth' => 0, 'format.eol' => "\n", 'format.indent' => "\t");
  20. /**
  21. * Set format related options.
  22. *
  23. * Updates the formatOptions array with all valid values in the passed
  24. * array. See {@see self::$formatOptions} for details.
  25. *
  26. * @param array $options Option key/value pairs.
  27. *
  28. * @return void
  29. *
  30. * @since 11.1
  31. */
  32. public static function setFormatOptions($options)
  33. {
  34. foreach ($options as $key => $val)
  35. {
  36. if (isset(self::$formatOptions[$key]))
  37. {
  38. self::$formatOptions[$key] = $val;
  39. }
  40. }
  41. }
  42. /**
  43. * Default values for options. Organized by option group.
  44. *
  45. * @var array
  46. * @since 11.1
  47. */
  48. static protected $_optionDefaults = array(
  49. 'option' => array('option.attr' => null, 'option.disable' => 'disable', 'option.id' => null, 'option.key' => 'value',
  50. 'option.key.toHtml' => true, 'option.label' => null, 'option.label.toHtml' => true, 'option.text' => 'text',
  51. 'option.text.toHtml' => true));
  52. /**
  53. * Generates a yes/no radio list.
  54. *
  55. * @param string $name The value of the HTML name attribute
  56. * @param array $attribs Additional HTML attributes for the <select> tag
  57. * @param string $selected The key that is selected
  58. * @param string $yes Language key for Yes
  59. * @param string $no Language key for no
  60. * @param string $id The id for the field
  61. *
  62. * @return string HTML for the radio list
  63. *
  64. * @since 11.1
  65. * @see JFormFieldRadio
  66. */
  67. public static function booleanlist($name, $attribs = null, $selected = null, $yes = 'JYES', $no = 'JNO', $id = false)
  68. {
  69. $arr = array(self::option( '0', rc__($no)), self::option( '1', rc__($yes)));
  70. return self::radiolist( $arr, $name, $attribs, 'value', 'text', (int) $selected, $id);
  71. }
  72. /**
  73. * Generates an HTML selection list.
  74. *
  75. * @param array $data An array of objects, arrays, or scalars.
  76. * @param string $name The value of the HTML name attribute.
  77. * @param mixed $attribs Additional HTML attributes for the <select> tag. This
  78. * can be an array of attributes, or an array of options. Treated as options
  79. * if it is the last argument passed. Valid options are:
  80. * Format options, see {@see self::$formatOptions}.
  81. * Selection options, see {@see RokCommon_HTML_SelectList::options()}.
  82. * list.attr, string|array: Additional attributes for the select
  83. * element.
  84. * id, string: Value to use as the select element id attribute.
  85. * Defaults to the same as the name.
  86. * list.select, string|array: Identifies one or more option elements
  87. * to be selected, based on the option key values.
  88. * @param string $optKey The name of the object variable for the option value. If
  89. * set to null, the index of the value array is used.
  90. * @param string $optText The name of the object variable for the option text.
  91. * @param mixed $selected The key that is selected (accepts an array or a string).
  92. * @param mixed $idtag Value of the field id or null by default
  93. * @param boolean $translate True to translate
  94. *
  95. * @return string HTML for the select list.
  96. *
  97. * @since 11.1
  98. */
  99. public static function genericlist($data, $name, $attribs = null, $optKey = 'value', $optText = 'text', $selected = null, $idtag = false,
  100. $translate = false)
  101. {
  102. // Set default options
  103. $options = array_merge(self::$formatOptions, array('format.depth' => 0, 'id' => false));
  104. if (is_array($attribs) && func_num_args() == 3)
  105. {
  106. // Assume we have an options array
  107. $options = array_merge($options, $attribs);
  108. }
  109. else
  110. {
  111. // Get options from the parameters
  112. $options['id'] = $idtag;
  113. $options['list.attr'] = $attribs;
  114. $options['list.translate'] = $translate;
  115. $options['option.key'] = $optKey;
  116. $options['option.text'] = $optText;
  117. $options['list.select'] = $selected;
  118. }
  119. $attribs = '';
  120. if (isset($options['list.attr']))
  121. {
  122. if (is_array($options['list.attr']))
  123. {
  124. $attribs = RokCommon_Utils_ArrayHelper::toString($options['list.attr']);
  125. }
  126. else
  127. {
  128. $attribs = $options['list.attr'];
  129. }
  130. if ($attribs != '')
  131. {
  132. $attribs = ' ' . $attribs;
  133. }
  134. }
  135. $id = $options['id'] !== false ? $options['id'] : $name;
  136. $id = str_replace(array('[', ']'), '', $id);
  137. $baseIndent = str_repeat($options['format.indent'], $options['format.depth']++);
  138. $html = $baseIndent . '<select' . ($id !== '' ? ' id="' . $id . '"' : '') . ' name="' . $name . '"' . $attribs . '>' . $options['format.eol']
  139. . self::options($data, $options) . $baseIndent . '</select>' . $options['format.eol'];
  140. return $html;
  141. }
  142. /**
  143. * Generates a grouped HTML selection list from nested arrays.
  144. *
  145. * @param array $data An array of groups, each of which is an array of options.
  146. * @param string $name The value of the HTML name attribute
  147. * @param array $options Options, an array of key/value pairs. Valid options are:
  148. * Format options, {@see self::$formatOptions}.
  149. * Selection options. See {@see RokCommon_HTML_SelectList::options()}.
  150. * group.id: The property in each group to use as the group id
  151. * attribute. Defaults to none.
  152. * group.label: The property in each group to use as the group
  153. * label. Defaults to "text". If set to null, the data array index key is
  154. * used.
  155. * group.items: The property in each group to use as the array of
  156. * items in the group. Defaults to "items". If set to null, group.id and
  157. * group. label are forced to null and the data element is assumed to be a
  158. * list of selections.
  159. * id: Value to use as the select element id attribute. Defaults to
  160. * the same as the name.
  161. * list.attr: Attributes for the select element. Can be a string or
  162. * an array of key/value pairs. Defaults to none.
  163. * list.select: either the value of one selected option or an array
  164. * of selected options. Default: none.
  165. * list.translate: Boolean. If set, text and labels are translated via
  166. * rc__().
  167. *
  168. * @return string HTML for the select list
  169. *
  170. * @since 11.1
  171. *
  172. * @throws JException If a group has unprocessable contents.
  173. */
  174. public static function groupedlist($data, $name, $options = array())
  175. {
  176. // Set default options and overwrite with anything passed in
  177. $options = array_merge(
  178. self::$formatOptions,
  179. array('format.depth' => 0, 'group.items' => 'items', 'group.label' => 'text', 'group.label.toHtml' => true, 'id' => false),
  180. $options
  181. );
  182. // Apply option rules
  183. if ($options['group.items'] === null)
  184. {
  185. $options['group.label'] = null;
  186. }
  187. $attribs = '';
  188. if (isset($options['list.attr']))
  189. {
  190. if (is_array($options['list.attr']))
  191. {
  192. $attribs = RokCommon_Utils_ArrayHelper::toString($options['list.attr']);
  193. }
  194. else
  195. {
  196. $attribs = $options['list.attr'];
  197. }
  198. if ($attribs != '')
  199. {
  200. $attribs = ' ' . $attribs;
  201. }
  202. }
  203. $id = $options['id'] !== false ? $options['id'] : $name;
  204. $id = str_replace(array('[', ']'), '', $id);
  205. // Disable groups in the options.
  206. $options['groups'] = false;
  207. $baseIndent = str_repeat($options['format.indent'], $options['format.depth']++);
  208. $html = $baseIndent . '<select' . ($id !== '' ? ' id="' . $id . '"' : '') . ' name="' . $name . '"' . $attribs . '>' . $options['format.eol'];
  209. $groupIndent = str_repeat($options['format.indent'], $options['format.depth']++);
  210. foreach ($data as $dataKey => $group)
  211. {
  212. $label = $dataKey;
  213. $id = '';
  214. $noGroup = is_int($dataKey);
  215. if ($options['group.items'] == null)
  216. {
  217. // Sub-list is an associative array
  218. $subList = $group;
  219. }
  220. elseif (is_array($group))
  221. {
  222. // Sub-list is in an element of an array.
  223. $subList = $group[$options['group.items']];
  224. if (isset($group[$options['group.label']]))
  225. {
  226. $label = $group[$options['group.label']];
  227. $noGroup = false;
  228. }
  229. if (isset($options['group.id']) && isset($group[$options['group.id']]))
  230. {
  231. $id = $group[$options['group.id']];
  232. $noGroup = false;
  233. }
  234. }
  235. elseif (is_object($group))
  236. {
  237. // Sub-list is in a property of an object
  238. $subList = $group->$options['group.items'];
  239. if (isset($group->$options['group.label']))
  240. {
  241. $label = $group->$options['group.label'];
  242. $noGroup = false;
  243. }
  244. if (isset($options['group.id']) && isset($group->$options['group.id']))
  245. {
  246. $id = $group->$options['group.id'];
  247. $noGroup = false;
  248. }
  249. }
  250. else
  251. {
  252. throw new JException('Invalid group contents.', 1, E_WARNING);
  253. }
  254. if ($noGroup)
  255. {
  256. $html .= self::options($subList, $options);
  257. }
  258. else
  259. {
  260. $html .= $groupIndent . '<optgroup' . (empty($id) ? '' : ' id="' . $id . '"') . ' label="'
  261. . ($options['group.label.toHtml'] ? htmlspecialchars($label, ENT_COMPAT, 'UTF-8') : $label) . '">' . $options['format.eol']
  262. . self::options($subList, $options) . $groupIndent . '</optgroup>' . $options['format.eol'];
  263. }
  264. }
  265. $html .= $baseIndent . '</select>' . $options['format.eol'];
  266. return $html;
  267. }
  268. /**
  269. * Generates a selection list of integers.
  270. *
  271. * @param integer $start The start integer
  272. * @param integer $end The end integer
  273. * @param integer $inc The increment
  274. * @param string $name The value of the HTML name attribute
  275. * @param mixed $attribs Additional HTML attributes for the <select> tag, an array of
  276. * attributes, or an array of options. Treated as options if it is the last
  277. * argument passed.
  278. * @param mixed $selected The key that is selected
  279. * @param string $format The printf format to be applied to the number
  280. *
  281. * @return string HTML for the select list
  282. *
  283. * @since 11.1
  284. */
  285. public static function integerlist($start, $end, $inc, $name, $attribs = null, $selected = null, $format = '')
  286. {
  287. // Set default options
  288. $options = array_merge(self::$formatOptions, array('format.depth' => 0, 'option.format' => '', 'id' => null));
  289. if (is_array($attribs) && func_num_args() == 5)
  290. {
  291. // Assume we have an options array
  292. $options = array_merge($options, $attribs);
  293. // Extract the format and remove it from downstream options
  294. $format = $options['option.format'];
  295. unset($options['option.format']);
  296. }
  297. else
  298. {
  299. // Get options from the parameters
  300. $options['list.attr'] = $attribs;
  301. $options['list.select'] = $selected;
  302. }
  303. $start = intval($start);
  304. $end = intval($end);
  305. $inc = intval($inc);
  306. $data = array();
  307. for ($i = $start; $i <= $end; $i += $inc)
  308. {
  309. $data[$i] = $format ? sprintf($format, $i) : $i;
  310. }
  311. // Tell genericlist() to use array keys
  312. $options['option.key'] = null;
  313. return self::genericlist( $data, $name, $options);
  314. }
  315. /**
  316. * Create a placeholder for an option group.
  317. *
  318. * @param string $text The text for the option
  319. * @param string $optKey The returned object property name for the value
  320. * @param string $optText The returned object property name for the text
  321. *
  322. * @return object
  323. *
  324. * @deprecated 12.1 Use RokCommon_HTML_SelectList::groupedList()
  325. * @see RokCommon_HTML_SelectList::groupedList()
  326. * @since 11.1
  327. */
  328. public static function optgroup($text, $optKey = 'value', $optText = 'text')
  329. {
  330. // Set initial state
  331. static $state = 'open';
  332. // Toggle between open and close states:
  333. switch ($state)
  334. {
  335. case 'open':
  336. $obj = new stdClass;
  337. $obj->$optKey = '<OPTGROUP>';
  338. $obj->$optText = $text;
  339. $state = 'close';
  340. break;
  341. case 'close':
  342. $obj = new stdClass;
  343. $obj->$optKey = '</OPTGROUP>';
  344. $obj->$optText = $text;
  345. $state = 'open';
  346. break;
  347. }
  348. return $obj;
  349. }
  350. /**
  351. * Create an object that represents an option in an option list.
  352. *
  353. * @param string $value The value of the option
  354. * @param string $text The text for the option
  355. * @param mixed $optKey If a string, the returned object property name for
  356. * the value. If an array, options. Valid options are:
  357. * attr: String|array. Additional attributes for this option.
  358. * Defaults to none.
  359. * disable: Boolean. If set, this option is disabled.
  360. * label: String. The value for the option label.
  361. * option.attr: The property in each option array to use for
  362. * additional selection attributes. Defaults to none.
  363. * option.disable: The property that will hold the disabled state.
  364. * Defaults to "disable".
  365. * option.key: The property that will hold the selection value.
  366. * Defaults to "value".
  367. * option.label: The property in each option array to use as the
  368. * selection label attribute. If a "label" option is provided, defaults to
  369. * "label", if no label is given, defaults to null (none).
  370. * option.text: The property that will hold the the displayed text.
  371. * Defaults to "text". If set to null, the option array is assumed to be a
  372. * list of displayable scalars.
  373. * @param string $optText The property that will hold the the displayed text. This
  374. * parameter is ignored if an options array is passed.
  375. * @param boolean $disable Not used.
  376. *
  377. * @return object
  378. *
  379. * @since 11.1
  380. */
  381. public static function option($value, $text = '', $optKey = 'value', $optText = 'text', $disable = false)
  382. {
  383. $options = array('attr' => null, 'disable' => false, 'option.attr' => null, 'option.disable' => 'disable', 'option.key' => 'value',
  384. 'option.label' => null, 'option.text' => 'text');
  385. if (is_array($optKey))
  386. {
  387. // Merge in caller's options
  388. $options = array_merge($options, $optKey);
  389. }
  390. else
  391. {
  392. // Get options from the parameters
  393. $options['option.key'] = $optKey;
  394. $options['option.text'] = $optText;
  395. $options['disable'] = $disable;
  396. }
  397. $obj = new stdClass();
  398. $obj->$options['option.key'] = $value;
  399. $obj->$options['option.text'] = trim($text) ? $text : $value;
  400. /*
  401. * If a label is provided, save it. If no label is provided and there is
  402. * a label name, initialise to an empty string.
  403. */
  404. $hasProperty = $options['option.label'] !== null;
  405. if (isset($options['label']))
  406. {
  407. $labelProperty = $hasProperty ? $options['option.label'] : 'label';
  408. $obj->$labelProperty = $options['label'];
  409. }
  410. elseif ($hasProperty)
  411. {
  412. $obj->$options['option.label'] = '';
  413. }
  414. // Set attributes only if there is a property and a value
  415. if ($options['attr'] !== null)
  416. {
  417. $obj->$options['option.attr'] = $options['attr'];
  418. }
  419. // Set disable only if it has a property and a value
  420. if ($options['disable'] !== null)
  421. {
  422. $obj->$options['option.disable'] = $options['disable'];
  423. }
  424. return $obj;
  425. }
  426. /**
  427. * Generates the option tags for an HTML select list (with no select tag
  428. * surrounding the options).
  429. *
  430. * @param array $arr An array of objects, arrays, or values.
  431. * @param mixed $optKey If a string, this is the name of the object variable for
  432. * the option value. If null, the index of the array of objects is used. If
  433. * an array, this is a set of options, as key/value pairs. Valid options are:
  434. * -Format options, {@see self::$formatOptions}.
  435. * -groups: Boolean. If set, looks for keys with the value
  436. * "&lt;optgroup>" and synthesizes groups from them. Deprecated. Defaults
  437. * true for backwards compatibility.
  438. * -list.select: either the value of one selected option or an array
  439. * of selected options. Default: none.
  440. * -list.translate: Boolean. If set, text and labels are translated via
  441. * rc__(). Default is false.
  442. * -option.id: The property in each option array to use as the
  443. * selection id attribute. Defaults to none.
  444. * -option.key: The property in each option array to use as the
  445. * selection value. Defaults to "value". If set to null, the index of the
  446. * option array is used.
  447. * -option.label: The property in each option array to use as the
  448. * selection label attribute. Defaults to null (none).
  449. * -option.text: The property in each option array to use as the
  450. * displayed text. Defaults to "text". If set to null, the option array is
  451. * assumed to be a list of displayable scalars.
  452. * -option.attr: The property in each option array to use for
  453. * additional selection attributes. Defaults to none.
  454. * -option.disable: The property that will hold the disabled state.
  455. * Defaults to "disable".
  456. * -option.key: The property that will hold the selection value.
  457. * Defaults to "value".
  458. * -option.text: The property that will hold the the displayed text.
  459. * Defaults to "text". If set to null, the option array is assumed to be a
  460. * list of displayable scalars.
  461. * @param string $optText The name of the object variable for the option text.
  462. * @param mixed $selected The key that is selected (accepts an array or a string)
  463. * @param boolean $translate Translate the option values.
  464. *
  465. * @return string HTML for the select list
  466. *
  467. * @since 11.1
  468. */
  469. public static function options($arr, $optKey = 'value', $optText = 'text', $selected = null, $translate = false)
  470. {
  471. $options = array_merge(
  472. self::$formatOptions,
  473. self::$_optionDefaults['option'],
  474. array('format.depth' => 0, 'groups' => true, 'list.select' => null, 'list.translate' => false)
  475. );
  476. if (is_array($optKey))
  477. {
  478. // Set default options and overwrite with anything passed in
  479. $options = array_merge($options, $optKey);
  480. }
  481. else
  482. {
  483. // Get options from the parameters
  484. $options['option.key'] = $optKey;
  485. $options['option.text'] = $optText;
  486. $options['list.select'] = $selected;
  487. $options['list.translate'] = $translate;
  488. }
  489. $html = '';
  490. $baseIndent = str_repeat($options['format.indent'], $options['format.depth']);
  491. foreach ($arr as $elementKey => &$element)
  492. {
  493. $attr = '';
  494. $extra = '';
  495. $label = '';
  496. $id = '';
  497. if (is_array($element))
  498. {
  499. $key = $options['option.key'] === null ? $elementKey : $element[$options['option.key']];
  500. $text = $element[$options['option.text']];
  501. if (isset($element[$options['option.attr']]))
  502. {
  503. $attr = $element[$options['option.attr']];
  504. }
  505. if (isset($element[$options['option.id']]))
  506. {
  507. $id = $element[$options['option.id']];
  508. }
  509. if (isset($element[$options['option.label']]))
  510. {
  511. $label = $element[$options['option.label']];
  512. }
  513. if (isset($element[$options['option.disable']]) && $element[$options['option.disable']])
  514. {
  515. $extra .= ' disabled="disabled"';
  516. }
  517. }
  518. elseif (is_object($element))
  519. {
  520. $key = $options['option.key'] === null ? $elementKey : $element->$options['option.key'];
  521. $text = $element->$options['option.text'];
  522. if (isset($element->$options['option.attr']))
  523. {
  524. $attr = $element->$options['option.attr'];
  525. }
  526. if (isset($element->$options['option.id']))
  527. {
  528. $id = $element->$options['option.id'];
  529. }
  530. if (isset($element->$options['option.label']))
  531. {
  532. $label = $element->$options['option.label'];
  533. }
  534. if (isset($element->$options['option.disable']) && $element->$options['option.disable'])
  535. {
  536. $extra .= ' disabled="disabled"';
  537. }
  538. }
  539. else
  540. {
  541. // This is a simple associative array
  542. $key = $elementKey;
  543. $text = $element;
  544. }
  545. // The use of options that contain optgroup HTML elements was
  546. // somewhat hacked for J1.5. J1.6 introduces the grouplist() method
  547. // to handle this better. The old solution is retained through the
  548. // "groups" option, which defaults true in J1.6, but should be
  549. // deprecated at some point in the future.
  550. $key = (string) $key;
  551. if ($options['groups'] && $key == '<OPTGROUP>')
  552. {
  553. $html .= $baseIndent . '<optgroup label="' . ($options['list.translate'] ? rc__($text) : $text) . '">' . $options['format.eol'];
  554. $baseIndent = str_repeat($options['format.indent'], ++$options['format.depth']);
  555. }
  556. elseif ($options['groups'] && $key == '</OPTGROUP>')
  557. {
  558. $baseIndent = str_repeat($options['format.indent'], --$options['format.depth']);
  559. $html .= $baseIndent . '</optgroup>' . $options['format.eol'];
  560. }
  561. else
  562. {
  563. // if no string after hyphen - take hyphen out
  564. $splitText = explode(' - ', $text, 2);
  565. $text = $splitText[0];
  566. if (isset($splitText[1]))
  567. {
  568. $text .= ' - ' . $splitText[1];
  569. }
  570. if ($options['list.translate'] && !empty($label))
  571. {
  572. $label = rc__($label);
  573. }
  574. if ($options['option.label.toHtml'])
  575. {
  576. $label = htmlentities($label);
  577. }
  578. if (is_array($attr))
  579. {
  580. $attr = RokCommon_Utils_ArrayHelper::toString($attr);
  581. }
  582. else
  583. {
  584. $attr = trim($attr);
  585. }
  586. $extra = ($id ? ' id="' . $id . '"' : '') . ($label ? ' label="' . $label . '"' : '') . ($attr ? ' ' . $attr : '') . $extra;
  587. if (is_array($options['list.select']))
  588. {
  589. foreach ($options['list.select'] as $val)
  590. {
  591. $key2 = is_object($val) ? $val->$options['option.key'] : $val;
  592. if ($key == $key2)
  593. {
  594. $extra .= ' selected="selected"';
  595. break;
  596. }
  597. }
  598. }
  599. elseif ((string) $key == (string) $options['list.select'])
  600. {
  601. $extra .= ' selected="selected"';
  602. }
  603. if ($options['list.translate'])
  604. {
  605. $text = rc__($text);
  606. }
  607. // Generate the option, encoding as required
  608. $html .= $baseIndent . '<option value="' . ($options['option.key.toHtml'] ? htmlspecialchars($key, ENT_COMPAT, 'UTF-8') : $key) . '"'
  609. . $extra . '>';
  610. $html .= $options['option.text.toHtml'] ? htmlentities(html_entity_decode($text, ENT_COMPAT, 'UTF-8'), ENT_COMPAT, 'UTF-8') : $text;
  611. $html .= '</option>' . $options['format.eol'];
  612. }
  613. }
  614. return $html;
  615. }
  616. /**
  617. * Generates an HTML radio list.
  618. *
  619. * @param array $data An array of objects
  620. * @param string $name The value of the HTML name attribute
  621. * @param string $attribs Additional HTML attributes for the <select> tag
  622. * @param mixed $optKey The key that is selected
  623. * @param string $optText The name of the object variable for the option value
  624. * @param string $selected The name of the object variable for the option text
  625. * @param boolean $idtag Value of the field id or null by default
  626. * @param boolean $translate True if options will be translated
  627. *
  628. * @return string HTML for the select list
  629. *
  630. * @since 11.1
  631. */
  632. public static function radiolist($data, $name, $attribs = null, $optKey = 'value', $optText = 'text', $selected = null, $idtag = false,
  633. $translate = false)
  634. {
  635. reset($data);
  636. $html = '';
  637. if (is_array($attribs))
  638. {
  639. $attribs = RokCommon_Utils_ArrayHelper::toString($attribs);
  640. }
  641. $id_text = $idtag ? $idtag : $name;
  642. foreach ($data as $obj)
  643. {
  644. $k = $obj->$optKey;
  645. $t = $translate ? rc__($obj->$optText) : $obj->$optText;
  646. $id = (isset($obj->id) ? $obj->id : null);
  647. $extra = '';
  648. $extra .= $id ? ' id="' . $obj->id . '"' : '';
  649. if (is_array($selected))
  650. {
  651. foreach ($selected as $val)
  652. {
  653. $k2 = is_object($val) ? $val->$optKey : $val;
  654. if ($k == $k2)
  655. {
  656. $extra .= ' selected="selected"';
  657. break;
  658. }
  659. }
  660. }
  661. else
  662. {
  663. $extra .= ((string) $k == (string) $selected ? ' checked="checked"' : '');
  664. }
  665. $html .= "\n\t" . '<input type="radio" name="' . $name . '"' . ' id="' . $id_text . $k . '" value="' . $k . '"' . ' ' . $extra . ' '
  666. . $attribs . '/>' . "\n\t" . '<label for="' . $id_text . $k . '"' . ' id="' . $id_text . $k . '-lbl" class="radiobtn">' . $t
  667. . '</label>';
  668. }
  669. $html .= "\n";
  670. return $html;
  671. }
  672. }