[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2009 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\ParameterType;
  16  use Joomla\Utilities\ArrayHelper;
  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 categories
  24   *
  25   * @since  1.5
  26   */
  27  abstract class Category
  28  {
  29      /**
  30       * Cached array of the category items.
  31       *
  32       * @var    array
  33       * @since  1.5
  34       */
  35      protected static $items = array();
  36  
  37      /**
  38       * Returns an array of categories for the given extension.
  39       *
  40       * @param   string  $extension  The extension option e.g. com_something.
  41       * @param   array   $config     An array of configuration options. By default, only
  42       *                              published and unpublished categories are returned.
  43       *
  44       * @return  array
  45       *
  46       * @since   1.5
  47       */
  48      public static function options($extension, $config = array('filter.published' => array(0, 1)))
  49      {
  50          $hash = md5($extension . '.' . serialize($config));
  51  
  52          if (!isset(static::$items[$hash])) {
  53              $config = (array) $config;
  54              $db     = Factory::getDbo();
  55              $user   = Factory::getUser();
  56              $groups = $user->getAuthorisedViewLevels();
  57  
  58              $query = $db->getQuery(true)
  59                  ->select(
  60                      [
  61                          $db->quoteName('a.id'),
  62                          $db->quoteName('a.title'),
  63                          $db->quoteName('a.level'),
  64                          $db->quoteName('a.language'),
  65                      ]
  66                  )
  67                  ->from($db->quoteName('#__categories', 'a'))
  68                  ->where($db->quoteName('a.parent_id') . ' > 0');
  69  
  70              // Filter on extension.
  71              $query->where($db->quoteName('a.extension') . ' = :extension')
  72                  ->bind(':extension', $extension);
  73  
  74              // Filter on user access level
  75              $query->whereIn($db->quoteName('a.access'), $groups);
  76  
  77              // Filter on the published state
  78              if (isset($config['filter.published'])) {
  79                  if (is_numeric($config['filter.published'])) {
  80                      $query->where($db->quoteName('a.published') . ' = :published')
  81                          ->bind(':published', $config['filter.published'], ParameterType::INTEGER);
  82                  } elseif (is_array($config['filter.published'])) {
  83                      $config['filter.published'] = ArrayHelper::toInteger($config['filter.published']);
  84                      $query->whereIn($db->quoteName('a.published'), $config['filter.published']);
  85                  }
  86              }
  87  
  88              // Filter on the language
  89              if (isset($config['filter.language'])) {
  90                  if (is_string($config['filter.language'])) {
  91                      $query->where($db->quoteName('a.language') . ' = :language')
  92                          ->bind(':language', $config['filter.language']);
  93                  } elseif (is_array($config['filter.language'])) {
  94                      $query->whereIn($db->quoteName('a.language'), $config['filter.language'], ParameterType::STRING);
  95                  }
  96              }
  97  
  98              // Filter on the access
  99              if (isset($config['filter.access'])) {
 100                  if (is_numeric($config['filter.access'])) {
 101                      $query->where($db->quoteName('a.access') . ' = :access')
 102                          ->bind(':access', $config['filter_access'], ParameterType::INTEGER);
 103                  } elseif (is_array($config['filter.access'])) {
 104                      $config['filter.access'] = ArrayHelper::toInteger($config['filter.access']);
 105                      $query->whereIn($db->quoteName('a.access'), $config['filter.access']);
 106                  }
 107              }
 108  
 109              $query->order($db->quoteName('a.lft'));
 110  
 111              $db->setQuery($query);
 112              $items = $db->loadObjectList();
 113  
 114              // Assemble the list options.
 115              static::$items[$hash] = array();
 116  
 117              foreach ($items as &$item) {
 118                  $repeat = ($item->level - 1 >= 0) ? $item->level - 1 : 0;
 119                  $item->title = str_repeat('- ', $repeat) . $item->title;
 120  
 121                  if ($item->language !== '*') {
 122                      $item->title .= ' (' . $item->language . ')';
 123                  }
 124  
 125                  static::$items[$hash][] = HTMLHelper::_('select.option', $item->id, $item->title);
 126              }
 127          }
 128  
 129          return static::$items[$hash];
 130      }
 131  
 132      /**
 133       * Returns an array of categories for the given extension.
 134       *
 135       * @param   string  $extension  The extension option.
 136       * @param   array   $config     An array of configuration options. By default, only published and unpublished categories are returned.
 137       *
 138       * @return  array   Categories for the extension
 139       *
 140       * @since   1.6
 141       */
 142      public static function categories($extension, $config = array('filter.published' => array(0, 1)))
 143      {
 144          $hash = md5($extension . '.' . serialize($config));
 145  
 146          if (!isset(static::$items[$hash])) {
 147              $config = (array) $config;
 148              $user = Factory::getUser();
 149              $db = Factory::getDbo();
 150              $query = $db->getQuery(true)
 151                  ->select(
 152                      [
 153                          $db->quoteName('a.id'),
 154                          $db->quoteName('a.title'),
 155                          $db->quoteName('a.level'),
 156                          $db->quoteName('a.parent_id'),
 157                          $db->quoteName('a.language'),
 158                      ]
 159                  )
 160                  ->from($db->quoteName('#__categories', 'a'))
 161                  ->where($db->quoteName('a.parent_id') . ' > 0');
 162  
 163              // Filter on extension.
 164              $query->where($db->quoteName('extension') . ' = :extension')
 165                  ->bind(':extension', $extension);
 166  
 167              // Filter on user level.
 168              $groups = $user->getAuthorisedViewLevels();
 169              $query->whereIn($db->quoteName('a.access'), $groups);
 170  
 171              // Filter on the published state
 172              if (isset($config['filter.published'])) {
 173                  if (is_numeric($config['filter.published'])) {
 174                      $query->where($db->quoteName('a.published') . ' = :published')
 175                          ->bind(':published', $config['filter.published'], ParameterType::INTEGER);
 176                  } elseif (is_array($config['filter.published'])) {
 177                      $config['filter.published'] = ArrayHelper::toInteger($config['filter.published']);
 178                      $query->whereIn($db->quoteName('a.published'), $config['filter.published']);
 179                  }
 180              }
 181  
 182              $query->order($db->quoteName('a.lft'));
 183  
 184              $db->setQuery($query);
 185              $items = $db->loadObjectList();
 186  
 187              // Assemble the list options.
 188              static::$items[$hash] = array();
 189  
 190              foreach ($items as &$item) {
 191                  $repeat = ($item->level - 1 >= 0) ? $item->level - 1 : 0;
 192                  $item->title = str_repeat('- ', $repeat) . $item->title;
 193  
 194                  if ($item->language !== '*') {
 195                      $item->title .= ' (' . $item->language . ')';
 196                  }
 197  
 198                  static::$items[$hash][] = HTMLHelper::_('select.option', $item->id, $item->title);
 199              }
 200  
 201              // Special "Add to root" option:
 202              static::$items[$hash][] = HTMLHelper::_('select.option', '1', Text::_('JLIB_HTML_ADD_TO_ROOT'));
 203          }
 204  
 205          return static::$items[$hash];
 206      }
 207  }


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