[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/HTML/Helpers/ -> ListHelper.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\Database\DatabaseQuery;
  16  use Joomla\String\StringHelper;
  17  
  18  // phpcs:disable PSR1.Files.SideEffects
  19  \defined('JPATH_PLATFORM') or die;
  20  // phpcs:enable PSR1.Files.SideEffects
  21  
  22  /**
  23   * Utility class for creating different select lists
  24   *
  25   * @since  1.5
  26   */
  27  abstract class ListHelper
  28  {
  29      /**
  30       * Build the select list to choose an image
  31       *
  32       * @param   string  $name        The name of the field
  33       * @param   string  $active      The selected item
  34       * @param   string  $javascript  Alternative javascript
  35       * @param   string  $directory   Directory the images are stored in
  36       * @param   string  $extensions  Allowed extensions
  37       *
  38       * @return  array  Image names
  39       *
  40       * @since   1.5
  41       */
  42      public static function images($name, $active = null, $javascript = null, $directory = null, $extensions = 'bmp|gif|jpg|png')
  43      {
  44          if (!$directory) {
  45              $directory = '/images/';
  46          }
  47  
  48          if (!$javascript) {
  49              $javascript = "onchange=\"if (document.forms.adminForm." . $name
  50                  . ".options[selectedIndex].value!='') {document.imagelib.src='..$directory' + document.forms.adminForm." . $name
  51                  . ".options[selectedIndex].value} else {document.imagelib.src='media/system/images/blank.png'}\"";
  52          }
  53  
  54          $imageFiles = new \DirectoryIterator(JPATH_SITE . '/' . $directory);
  55          $images = array(HTMLHelper::_('select.option', '', Text::_('JOPTION_SELECT_IMAGE')));
  56  
  57          foreach ($imageFiles as $file) {
  58              $fileName = $file->getFilename();
  59  
  60              if (!$file->isFile()) {
  61                  continue;
  62              }
  63  
  64              if (preg_match('#(' . $extensions . ')$#', $fileName)) {
  65                  $images[] = HTMLHelper::_('select.option', $fileName);
  66              }
  67          }
  68  
  69          $images = HTMLHelper::_(
  70              'select.genericlist',
  71              $images,
  72              $name,
  73              array(
  74                  'list.attr' => 'size="1" ' . $javascript,
  75                  'list.select' => $active,
  76              )
  77          );
  78  
  79          return $images;
  80      }
  81  
  82      /**
  83       * Returns an array of options
  84       *
  85       * @param   DatabaseQuery|string   $query  SQL with 'ordering' AS value and 'name field' AS text
  86       * @param   integer                $chop   The length of the truncated headline
  87       *
  88       * @return  array  An array of objects formatted for JHtml list processing
  89       *
  90       * @since   1.5
  91       */
  92      public static function genericordering($query, $chop = 30)
  93      {
  94          $db = Factory::getDbo();
  95          $options = array();
  96          $db->setQuery($query);
  97  
  98          $items = $db->loadObjectList();
  99  
 100          if (empty($items)) {
 101              $options[] = HTMLHelper::_('select.option', 1, Text::_('JLIB_FORM_FIELD_PARAM_INTEGER_FIRST_LABEL'));
 102  
 103              return $options;
 104          }
 105  
 106          $options[] = HTMLHelper::_('select.option', 0, ' - ' . Text::_('JLIB_FORM_FIELD_PARAM_INTEGER_FIRST_LABEL') . ' - ');
 107  
 108          for ($i = 0, $n = count($items); $i < $n; $i++) {
 109              $items[$i]->text = Text::_($items[$i]->text);
 110  
 111              if (StringHelper::strlen($items[$i]->text) > $chop) {
 112                  $text = StringHelper::substr($items[$i]->text, 0, $chop) . '...';
 113              } else {
 114                  $text = $items[$i]->text;
 115              }
 116  
 117              $options[] = HTMLHelper::_('select.option', $items[$i]->value, $text);
 118          }
 119  
 120          $options[] = HTMLHelper::_('select.option', $items[$i - 1]->value + 1, ' - ' . Text::_('JLIB_FORM_FIELD_PARAM_INTEGER_LAST_LABEL') . ' - ');
 121  
 122          return $options;
 123      }
 124  
 125      /**
 126       * Build the select list for Ordering derived from a query
 127       *
 128       * @param   integer  $name      The scalar value
 129       * @param   string   $query     The query
 130       * @param   string   $attribs   HTML tag attributes
 131       * @param   string   $selected  The selected item
 132       * @param   integer  $neworder  1 if new and first, -1 if new and last, 0  or null if existing item
 133       * @param   string   $id        ID attribute for the resulting <select> element
 134       *
 135       * @return  string   HTML markup for the select list
 136       *
 137       * @since   1.6
 138       */
 139      public static function ordering($name, $query, $attribs = null, $selected = null, $neworder = null, ?string $id = null)
 140      {
 141          if (empty($attribs)) {
 142              $attribs = 'size="1"';
 143          }
 144  
 145          if (empty($neworder)) {
 146              $orders = HTMLHelper::_('list.genericordering', $query);
 147              $html   = HTMLHelper::_(
 148                  'select.genericlist',
 149                  $orders,
 150                  $name,
 151                  ['list.attr' => $attribs, 'list.select' => (int) $selected, 'id' => $id ?? false]
 152              );
 153          } else {
 154              if ($neworder > 0) {
 155                  $text = Text::_('JGLOBAL_NEWITEMSLAST_DESC');
 156              } elseif ($neworder <= 0) {
 157                  $text = Text::_('JGLOBAL_NEWITEMSFIRST_DESC');
 158              }
 159  
 160              $html = '<input type="hidden" name="' . $name . '" value="' . (int) $selected . '"><span class="readonly">' . $text . '</span>';
 161          }
 162  
 163          return $html;
 164      }
 165  
 166      /**
 167       * Select list of active users
 168       *
 169       * @param   string   $name        The name of the field
 170       * @param   string   $active      The active user
 171       * @param   integer  $nouser      If set include an option to select no user
 172       * @param   string   $javascript  Custom javascript
 173       * @param   string   $order       Specify a field to order by
 174       *
 175       * @return  string   The HTML for a list of users list of users
 176       *
 177       * @since   1.5
 178       */
 179      public static function users($name, $active, $nouser = 0, $javascript = null, $order = 'name')
 180      {
 181          $db = Factory::getDbo();
 182          $query = $db->getQuery(true)
 183              ->select(
 184                  [
 185                      $db->quoteName('u.id', 'value'),
 186                      $db->quoteName('u.name', 'text'),
 187                  ]
 188              )
 189              ->from($db->quoteName('#__users', 'u'))
 190              ->join('LEFT', $db->quoteName('#__user_usergroup_map', 'm'), $db->quoteName('m.user_id') . ' = ' . $db->quoteName('u.id'))
 191              ->where($db->quoteName('u.block') . ' = 0')
 192              ->order($order)
 193              ->group($db->quoteName('u.id'));
 194          $db->setQuery($query);
 195  
 196          if ($nouser) {
 197              $users[] = HTMLHelper::_('select.option', '0', Text::_('JOPTION_NO_USER'));
 198              $users = array_merge($users, $db->loadObjectList());
 199          } else {
 200              $users = $db->loadObjectList();
 201          }
 202  
 203          $users = HTMLHelper::_(
 204              'select.genericlist',
 205              $users,
 206              $name,
 207              array(
 208                  'list.attr' => 'size="1" ' . $javascript,
 209                  'list.select' => $active,
 210              )
 211          );
 212  
 213          return $users;
 214      }
 215  
 216      /**
 217       * Select list of positions - generally used for location of images
 218       *
 219       * @param   string   $name        Name of the field
 220       * @param   string   $active      The active value
 221       * @param   string   $javascript  Alternative javascript
 222       * @param   boolean  $none        Null if not assigned
 223       * @param   boolean  $center      Null if not assigned
 224       * @param   boolean  $left        Null if not assigned
 225       * @param   boolean  $right       Null if not assigned
 226       * @param   boolean  $id          Null if not assigned
 227       *
 228       * @return  array  The positions
 229       *
 230       * @since   1.5
 231       */
 232      public static function positions(
 233          $name,
 234          $active = null,
 235          $javascript = null,
 236          $none = true,
 237          $center = true,
 238          $left = true,
 239          $right = true,
 240          $id = false
 241      ) {
 242          $pos = array();
 243  
 244          if ($none) {
 245              $pos[''] = Text::_('JNONE');
 246          }
 247  
 248          if ($center) {
 249              $pos['center'] = Text::_('JGLOBAL_CENTER');
 250          }
 251  
 252          if ($left) {
 253              $pos['left'] = Text::_('JGLOBAL_LEFT');
 254          }
 255  
 256          if ($right) {
 257              $pos['right'] = Text::_('JGLOBAL_RIGHT');
 258          }
 259  
 260          $positions = HTMLHelper::_(
 261              'select.genericlist',
 262              $pos,
 263              $name,
 264              array(
 265                  'id' => $id,
 266                  'list.attr' => 'size="1"' . $javascript,
 267                  'list.select' => $active,
 268                  'option.key' => null,
 269              )
 270          );
 271  
 272          return $positions;
 273      }
 274  }


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