[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/components/com_content/src/Helper/ -> QueryHelper.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  com_content
   6   *
   7   * @copyright   (C) 2007 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Content\Site\Helper;
  12  
  13  use Joomla\CMS\Component\ComponentHelper;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Plugin\PluginHelper;
  16  use Joomla\Database\DatabaseInterface;
  17  
  18  // phpcs:disable PSR1.Files.SideEffects
  19  \defined('_JEXEC') or die;
  20  // phpcs:enable PSR1.Files.SideEffects
  21  
  22  /**
  23   * Content Component Query Helper
  24   *
  25   * @since  1.5
  26   */
  27  class QueryHelper
  28  {
  29      /**
  30       * Translate an order code to a field for category ordering.
  31       *
  32       * @param   string  $orderby  The ordering code.
  33       *
  34       * @return  string  The SQL field(s) to order by.
  35       *
  36       * @since   1.5
  37       */
  38      public static function orderbyPrimary($orderby)
  39      {
  40          switch ($orderby) {
  41              case 'alpha':
  42                  $orderby = 'c.path, ';
  43                  break;
  44  
  45              case 'ralpha':
  46                  $orderby = 'c.path DESC, ';
  47                  break;
  48  
  49              case 'order':
  50                  $orderby = 'c.lft, ';
  51                  break;
  52  
  53              default:
  54                  $orderby = '';
  55                  break;
  56          }
  57  
  58          return $orderby;
  59      }
  60  
  61      /**
  62       * Translate an order code to a field for article ordering.
  63       *
  64       * @param   string             $orderby    The ordering code.
  65       * @param   string             $orderDate  The ordering code for the date.
  66       * @param   DatabaseInterface  $db         The database
  67       *
  68       * @return  string  The SQL field(s) to order by.
  69       *
  70       * @since   1.5
  71       */
  72      public static function orderbySecondary($orderby, $orderDate = 'created', DatabaseInterface $db = null)
  73      {
  74          $db = $db ?: Factory::getDbo();
  75  
  76          $queryDate = self::getQueryDate($orderDate, $db);
  77  
  78          switch ($orderby) {
  79              case 'date':
  80                  $orderby = $queryDate;
  81                  break;
  82  
  83              case 'rdate':
  84                  $orderby = $queryDate . ' DESC ';
  85                  break;
  86  
  87              case 'alpha':
  88                  $orderby = 'a.title';
  89                  break;
  90  
  91              case 'ralpha':
  92                  $orderby = 'a.title DESC';
  93                  break;
  94  
  95              case 'hits':
  96                  $orderby = 'a.hits DESC';
  97                  break;
  98  
  99              case 'rhits':
 100                  $orderby = 'a.hits';
 101                  break;
 102  
 103              case 'rorder':
 104                  $orderby = 'a.ordering DESC';
 105                  break;
 106  
 107              case 'author':
 108                  $orderby = 'author';
 109                  break;
 110  
 111              case 'rauthor':
 112                  $orderby = 'author DESC';
 113                  break;
 114  
 115              case 'front':
 116                  $orderby = 'a.featured DESC, fp.ordering, ' . $queryDate . ' DESC ';
 117                  break;
 118  
 119              case 'random':
 120                  $orderby = $db->getQuery(true)->rand();
 121                  break;
 122  
 123              case 'vote':
 124                  $orderby = 'a.id DESC ';
 125  
 126                  if (PluginHelper::isEnabled('content', 'vote')) {
 127                      $orderby = 'rating_count DESC ';
 128                  }
 129                  break;
 130  
 131              case 'rvote':
 132                  $orderby = 'a.id ASC ';
 133  
 134                  if (PluginHelper::isEnabled('content', 'vote')) {
 135                      $orderby = 'rating_count ASC ';
 136                  }
 137                  break;
 138  
 139              case 'rank':
 140                  $orderby = 'a.id DESC ';
 141  
 142                  if (PluginHelper::isEnabled('content', 'vote')) {
 143                      $orderby = 'rating DESC ';
 144                  }
 145                  break;
 146  
 147              case 'rrank':
 148                  $orderby = 'a.id ASC ';
 149  
 150                  if (PluginHelper::isEnabled('content', 'vote')) {
 151                      $orderby = 'rating ASC ';
 152                  }
 153                  break;
 154  
 155              default:
 156                  $orderby = 'a.ordering';
 157                  break;
 158          }
 159  
 160          return $orderby;
 161      }
 162  
 163      /**
 164       * Translate an order code to a field for date ordering.
 165       *
 166       * @param   string             $orderDate  The ordering code.
 167       * @param   DatabaseInterface  $db         The database
 168       *
 169       * @return  string  The SQL field(s) to order by.
 170       *
 171       * @since   1.6
 172       */
 173      public static function getQueryDate($orderDate, DatabaseInterface $db = null)
 174      {
 175          $db = $db ?: Factory::getDbo();
 176  
 177          switch ($orderDate) {
 178              case 'modified':
 179                  $queryDate = ' CASE WHEN a.modified IS NULL THEN a.created ELSE a.modified END';
 180                  break;
 181  
 182              // Use created if publish_up is not set
 183              case 'published':
 184                  $queryDate = ' CASE WHEN a.publish_up IS NULL THEN a.created ELSE a.publish_up END ';
 185                  break;
 186  
 187              case 'unpublished':
 188                  $queryDate = ' CASE WHEN a.publish_down IS NULL THEN a.created ELSE a.publish_down END ';
 189                  break;
 190              case 'created':
 191              default:
 192                  $queryDate = ' a.created ';
 193                  break;
 194          }
 195  
 196          return $queryDate;
 197      }
 198  
 199      /**
 200       * Get join information for the voting query.
 201       *
 202       * @param   \Joomla\Registry\Registry  $params  An options object for the article.
 203       *
 204       * @return  array  A named array with "select" and "join" keys.
 205       *
 206       * @since   1.5
 207       *
 208       * @deprecated  5.0  Deprecated without replacement, not used in core
 209       */
 210      public static function buildVotingQuery($params = null)
 211      {
 212          if (!$params) {
 213              $params = ComponentHelper::getParams('com_content');
 214          }
 215  
 216          $voting = $params->get('show_vote');
 217  
 218          if ($voting) {
 219              // Calculate voting count
 220              $select = ' , ROUND(v.rating_sum / v.rating_count) AS rating, v.rating_count';
 221              $join = ' LEFT JOIN #__content_rating AS v ON a.id = v.content_id';
 222          } else {
 223              $select = '';
 224              $join = '';
 225          }
 226  
 227          return array('select' => $select, 'join' => $join);
 228      }
 229  }


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