[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/HTML/Helpers/ -> SearchTools.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\Factory;
  13  use Joomla\CMS\Layout\LayoutHelper;
  14  use Joomla\Registry\Registry;
  15  
  16  // phpcs:disable PSR1.Files.SideEffects
  17  \defined('JPATH_PLATFORM') or die;
  18  // phpcs:enable PSR1.Files.SideEffects
  19  
  20  /**
  21   * Searchtools elements.
  22   *
  23   * @since  3.2
  24   */
  25  abstract class SearchTools
  26  {
  27      /**
  28       * @var    array  Array containing information for loaded files
  29       * @since  3.2
  30       */
  31      protected static $loaded = array();
  32  
  33      /**
  34       * Load searchtools for a specific form
  35       *
  36       * @param   mixed  $selector  Is debugging mode on? [optional]
  37       * @param   array  $options   Optional array of parameters for search tools
  38       *
  39       * @return  void
  40       *
  41       * @since   3.2
  42       */
  43      public static function form($selector = '.js-stools-form', $options = array())
  44      {
  45          $sig = md5(serialize(array($selector, $options)));
  46  
  47          // Only load once
  48          if (!isset(static::$loaded[__METHOD__][$sig])) {
  49              // Add the form selector to the search tools options
  50              $options['formSelector'] = $selector;
  51  
  52              // Generate options with default values
  53              $options = static::optionsToRegistry($options);
  54  
  55              // Load the script && css files
  56              Factory::getApplication()->getDocument()->getWebAssetManager()
  57                  ->useStyle('searchtools')
  58                  ->useScript('searchtools');
  59  
  60              Factory::getDocument()->addScriptOptions('searchtools', $options);
  61  
  62              static::$loaded[__METHOD__][$sig] = true;
  63          }
  64      }
  65  
  66      /**
  67       * Function to receive & pre-process javascript options
  68       *
  69       * @param   mixed  $options  Associative array/Registry object with options
  70       *
  71       * @return  Registry         Options converted to Registry object
  72       */
  73      private static function optionsToRegistry($options)
  74      {
  75          // Support options array
  76          if (is_array($options)) {
  77              $options = new Registry($options);
  78          }
  79  
  80          if (!($options instanceof Registry)) {
  81              $options = new Registry();
  82          }
  83  
  84          return $options;
  85      }
  86  
  87      /**
  88       * Method to sort a column in a grid
  89       *
  90       * @param   string  $title         The link title
  91       * @param   string  $order         The order field for the column
  92       * @param   string  $direction     The current direction
  93       * @param   mixed   $selected      The selected ordering
  94       * @param   string  $task          An optional task override
  95       * @param   string  $newDirection  An optional direction for the new column
  96       * @param   string  $tip           An optional text shown as tooltip title instead of $title
  97       * @param   string  $icon          Icon to show
  98       * @param   string  $formName      Name of the form to submit
  99       *
 100       * @return  string
 101       */
 102      public static function sort(
 103          $title,
 104          $order,
 105          $direction = 'asc',
 106          $selected = 0,
 107          $task = null,
 108          $newDirection = 'asc',
 109          $tip = '',
 110          $icon = null,
 111          $formName = 'adminForm'
 112      ) {
 113          $direction = strtolower($direction);
 114          $orderIcons = array('icon-caret-up', 'icon-caret-down');
 115          $index = (int) ($direction === 'desc');
 116  
 117          if ($order !== $selected) {
 118              $direction = $newDirection;
 119          } else {
 120              $direction = $direction === 'desc' ? 'asc' : 'desc';
 121          }
 122  
 123          // Create an object to pass it to the layouts
 124          $data            = new \stdClass();
 125          $data->order     = $order;
 126          $data->direction = $direction;
 127          $data->selected  = $selected;
 128          $data->task      = $task;
 129          $data->tip       = $tip;
 130          $data->title     = $title;
 131          $data->orderIcon = $orderIcons[$index];
 132          $data->icon      = $icon;
 133          $data->formName  = $formName;
 134  
 135          return LayoutHelper::render('joomla.searchtools.grid.sort', $data);
 136      }
 137  }


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