[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2013 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\Component\ComponentHelper;
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\HTML\HTMLHelper;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\Uri\Uri;
  17  use Joomla\Database\ParameterType;
  18  use Joomla\Utilities\ArrayHelper;
  19  
  20  // phpcs:disable PSR1.Files.SideEffects
  21  \defined('JPATH_PLATFORM') or die;
  22  // phpcs:enable PSR1.Files.SideEffects
  23  
  24  /**
  25   * Utility class for tags
  26   *
  27   * @since  3.1
  28   */
  29  abstract class Tag
  30  {
  31      /**
  32       * Cached array of the tag items.
  33       *
  34       * @var    array
  35       * @since  3.1
  36       */
  37      protected static $items = array();
  38  
  39      /**
  40       * Returns an array of tags.
  41       *
  42       * @param   array  $config  An array of configuration options. By default, only
  43       *                          published and unpublished categories are returned.
  44       *
  45       * @return  array
  46       *
  47       * @since   3.1
  48       */
  49      public static function options($config = array('filter.published' => array(0, 1)))
  50      {
  51          $hash = md5(serialize($config));
  52  
  53          if (!isset(static::$items[$hash])) {
  54              $config = (array) $config;
  55              $db = Factory::getDbo();
  56              $query = $db->getQuery(true)
  57                  ->select(
  58                      [
  59                          $db->quoteName('a.id'),
  60                          $db->quoteName('a.title'),
  61                          $db->quoteName('a.level'),
  62                      ]
  63                  )
  64                  ->from($db->quoteName('#__tags', 'a'))
  65                  ->where($db->quoteName('a.parent_id') . ' > 0');
  66  
  67              // Filter on the published state
  68              if (isset($config['filter.published'])) {
  69                  if (is_numeric($config['filter.published'])) {
  70                      $query->where('a.published = :published')
  71                          ->bind(':published', $config['filter.published'], ParameterType::INTEGER);
  72                  } elseif (is_array($config['filter.published'])) {
  73                      $config['filter.published'] = ArrayHelper::toInteger($config['filter.published']);
  74                      $query->whereIn($db->quoteName('a.published'), $config['filter.published']);
  75                  }
  76              }
  77  
  78              // Filter on the language
  79              if (isset($config['filter.language'])) {
  80                  if (is_string($config['filter.language'])) {
  81                      $query->where($db->quoteName('a.language') . ' = :language')
  82                          ->bind(':language', $config['filter.language']);
  83                  } elseif (is_array($config['filter.language'])) {
  84                      $query->whereIn($db->quoteName('a.language'), $config['filter.language'], ParameterType::STRING);
  85                  }
  86              }
  87  
  88              $query->order($db->quoteName('a.lft'));
  89  
  90              $db->setQuery($query);
  91              $items = $db->loadObjectList();
  92  
  93              // Assemble the list options.
  94              static::$items[$hash] = array();
  95  
  96              foreach ($items as &$item) {
  97                  $repeat = ($item->level - 1 >= 0) ? $item->level - 1 : 0;
  98                  $item->title = str_repeat('- ', $repeat) . $item->title;
  99                  static::$items[$hash][] = HTMLHelper::_('select.option', $item->id, $item->title);
 100              }
 101          }
 102  
 103          return static::$items[$hash];
 104      }
 105  
 106      /**
 107       * Returns an array of tags.
 108       *
 109       * @param   array  $config  An array of configuration options. By default, only published and unpublished tags are returned.
 110       *
 111       * @return  array  Tag data
 112       *
 113       * @since   3.1
 114       */
 115      public static function tags($config = array('filter.published' => array(0, 1)))
 116      {
 117          $hash = md5(serialize($config));
 118          $config = (array) $config;
 119          $db = Factory::getDbo();
 120          $query = $db->getQuery(true)
 121              ->select(
 122                  [
 123                      $db->quoteName('a.id'),
 124                      $db->quoteName('a.title'),
 125                      $db->quoteName('a.level'),
 126                      $db->quoteName('a.parent_id'),
 127                  ]
 128              )
 129              ->from($db->quoteName('#__tags', 'a'))
 130              ->where($db->quoteName('a.parent_id') . ' > 0');
 131  
 132          // Filter on the published state
 133          if (isset($config['filter.published'])) {
 134              if (is_numeric($config['filter.published'])) {
 135                  $query->where($db->quoteName('a.published') . ' = :published')
 136                      ->bind(':published', $config['filter.published'], ParameterType::INTEGER);
 137              } elseif (is_array($config['filter.published'])) {
 138                  $config['filter.published'] = ArrayHelper::toInteger($config['filter.published']);
 139                  $query->whereIn($db->quoteName('a.published'), $config['filter.published']);
 140              }
 141          }
 142  
 143          $query->order($db->quoteName('a.lft'));
 144  
 145          $db->setQuery($query);
 146          $items = $db->loadObjectList();
 147  
 148          // Assemble the list options.
 149          static::$items[$hash] = array();
 150  
 151          foreach ($items as &$item) {
 152              $repeat = ($item->level - 1 >= 0) ? $item->level - 1 : 0;
 153              $item->title = str_repeat('- ', $repeat) . $item->title;
 154              static::$items[$hash][] = HTMLHelper::_('select.option', $item->id, $item->title);
 155          }
 156  
 157          return static::$items[$hash];
 158      }
 159  
 160      /**
 161       * This is just a proxy for the formbehavior.ajaxchosen method
 162       *
 163       * @param   string   $selector     DOM id of the tag field
 164       * @param   boolean  $allowCustom  Flag to allow custom values
 165       *
 166       * @return  void
 167       *
 168       * @since   3.1
 169       *
 170       * @deprecated  5.0  Without replacement
 171       */
 172      public static function ajaxfield($selector = '#jform_tags', $allowCustom = true)
 173      {
 174          // Get the component parameters
 175          $params = ComponentHelper::getParams('com_tags');
 176          $minTermLength = (int) $params->get('min_term_length', 3);
 177  
 178          Text::script('JGLOBAL_KEEP_TYPING');
 179          Text::script('JGLOBAL_LOOKING_FOR');
 180  
 181          // Include scripts
 182          HTMLHelper::_('behavior.core');
 183          HTMLHelper::_('jquery.framework');
 184          HTMLHelper::_('formbehavior.chosen');
 185          HTMLHelper::_('script', 'legacy/ajax-chosen.min.js', array('version' => 'auto', 'relative' => true));
 186  
 187          Factory::getDocument()->addScriptOptions(
 188              'ajax-chosen',
 189              array(
 190                  'url'            => Uri::root() . 'index.php?option=com_tags&task=tags.searchAjax',
 191                  'debug'          => JDEBUG,
 192                  'selector'       => $selector,
 193                  'type'           => 'GET',
 194                  'dataType'       => 'json',
 195                  'jsonTermKey'    => 'like',
 196                  'afterTypeDelay' => 500,
 197                  'minTermLength'  => $minTermLength
 198              )
 199          );
 200  
 201          // Allow custom values ?
 202          if ($allowCustom) {
 203              HTMLHelper::_('script', 'system/fields/tag.min.js', array('version' => 'auto', 'relative' => true));
 204              Factory::getDocument()->addScriptOptions(
 205                  'field-tag-custom',
 206                  array(
 207                      'minTermLength' => $minTermLength,
 208                      'selector'      => $selector,
 209                      'allowCustom'   => Factory::getUser()->authorise('core.create', 'com_tags') ? $allowCustom : false,
 210                  )
 211              );
 212          }
 213      }
 214  }


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