[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/HTML/Helpers/ -> Select.php (source)

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


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer