Source for file query.php

Documentation is available at query.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Site
  4.  * @subpackage  com_content
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * Content Component Query Helper
  14.  *
  15.  * @static
  16.  * @package     Joomla.Site
  17.  * @subpackage  com_content
  18.  * @since       1.5
  19.  */
  20. {
  21.     /**
  22.      * Translate an order code to a field for primary category ordering.
  23.      *
  24.      * @param   string    $orderby    The ordering code.
  25.      *
  26.      * @return  string    The SQL field(s) to order by.
  27.      * @since   1.5
  28.      */
  29.     public static function orderbyPrimary($orderby)
  30.     {
  31.         switch ($orderby)
  32.         {
  33.             case 'alpha' :
  34.                 $orderby 'c.path, ';
  35.                 break;
  36.  
  37.             case 'ralpha' :
  38.                 $orderby 'c.path DESC, ';
  39.                 break;
  40.  
  41.             case 'order' :
  42.                 $orderby 'c.lft, ';
  43.                 break;
  44.  
  45.             default :
  46.                 $orderby '';
  47.                 break;
  48.         }
  49.  
  50.         return $orderby;
  51.     }
  52.  
  53.     /**
  54.      * Translate an order code to a field for secondary category ordering.
  55.      *
  56.      * @param   string    $orderby    The ordering code.
  57.      * @param   string    $orderDate    The ordering code for the date.
  58.      *
  59.      * @return  string    The SQL field(s) to order by.
  60.      * @since   1.5
  61.      */
  62.     public static function orderbySecondary($orderby$orderDate 'created')
  63.     {
  64.         $queryDate self::getQueryDate($orderDate);
  65.  
  66.         switch ($orderby)
  67.         {
  68.             case 'date' :
  69.                 $orderby $queryDate;
  70.                 break;
  71.  
  72.             case 'rdate' :
  73.                 $orderby $queryDate ' DESC ';
  74.                 break;
  75.  
  76.             case 'alpha' :
  77.                 $orderby 'a.title';
  78.                 break;
  79.  
  80.             case 'ralpha' :
  81.                 $orderby 'a.title DESC';
  82.                 break;
  83.  
  84.             case 'hits' :
  85.                 $orderby 'a.hits DESC';
  86.                 break;
  87.  
  88.             case 'rhits' :
  89.                 $orderby 'a.hits';
  90.                 break;
  91.  
  92.             case 'order' :
  93.                 $orderby 'a.ordering';
  94.                 break;
  95.  
  96.             case 'author' :
  97.                 $orderby 'author';
  98.                 break;
  99.  
  100.             case 'rauthor' :
  101.                 $orderby 'author DESC';
  102.                 break;
  103.  
  104.             case 'front' :
  105.                 $orderby 'a.featured DESC, fp.ordering';
  106.                 break;
  107.  
  108.             default :
  109.                 $orderby 'a.ordering';
  110.                 break;
  111.         }
  112.  
  113.         return $orderby;
  114.     }
  115.  
  116.     /**
  117.      * Translate an order code to a field for primary category ordering.
  118.      *
  119.      * @param   string    $orderDate    The ordering code.
  120.      *
  121.      * @return  string    The SQL field(s) to order by.
  122.      * @since   1.6
  123.      */
  124.     public static function getQueryDate($orderDate)
  125.     {
  126.         $db JFactory::getDbo();
  127.  
  128.         switch ($orderDate)
  129.         {
  130.             case 'modified' :
  131.                 $queryDate ' CASE WHEN a.modified = ' $db->quote($db->getNullDate()) ' THEN a.created ELSE a.modified END';
  132.                 break;
  133.  
  134.             // use created if publish_up is not set
  135.             case 'published' :
  136.                 $queryDate ' CASE WHEN a.publish_up = ' $db->quote($db->getNullDate()) ' THEN a.created ELSE a.publish_up END ';
  137.                 break;
  138.  
  139.             case 'created' :
  140.             default :
  141.                 $queryDate ' a.created ';
  142.                 break;
  143.         }
  144.  
  145.         return $queryDate;
  146.     }
  147.  
  148.     /**
  149.      * Get join information for the voting query.
  150.      *
  151.      * @param   JRegistry    $param    An options object for the article.
  152.      *
  153.      * @return  array      A named array with "select" and "join" keys.
  154.      * @since   1.5
  155.      */
  156.     public static function buildVotingQuery($params=null)
  157.     {
  158.         if (!$params)
  159.         {
  160.             $params JComponentHelper::getParams('com_content');
  161.         }
  162.  
  163.         $voting $params->get('show_vote');
  164.  
  165.         if ($voting)
  166.         {
  167.             // calculate voting count
  168.             $select ' , ROUND(v.rating_sum / v.rating_count) AS rating, v.rating_count';
  169.             $join ' LEFT JOIN #__content_rating AS v ON a.id = v.content_id';
  170.         }
  171.         else
  172.         {
  173.             $select '';
  174.             $join '';
  175.         }
  176.  
  177.         $results array ('select' => $select'join' => $join);
  178.  
  179.         return $results;
  180.     }
  181.  
  182.     /**
  183.      * Method to order the intro articles array for ordering
  184.      * down the columns instead of across.
  185.      * The layout always lays the introtext articles out across columns.
  186.      * Array is reordered so that, when articles are displayed in index order
  187.      * across columns in the layout, the result is that the
  188.      * desired article ordering is achieved down the columns.
  189.      *
  190.      * @param   array  $articles    Array of intro text articles
  191.      * @param   integer    $numColumns    Number of columns in the layout
  192.      *
  193.      * @return  array  Reordered array to achieve desired ordering down columns
  194.      * @since   1.6
  195.      */
  196.     public static function orderDownColumns(&$articles$numColumns 1)
  197.     {
  198.         $count count($articles);
  199.  
  200.         // just return the same array if there is nothing to change
  201.         if ($numColumns == || !is_array($articles|| $count <= $numColumns)
  202.         {
  203.             $return $articles;
  204.         }
  205.         // we need to re-order the intro articles array
  206.         else {
  207.             // we need to preserve the original array keys
  208.             $keys array_keys($articles);
  209.  
  210.             $maxRows ceil($count $numColumns);
  211.             $numCells $maxRows $numColumns;
  212.             $numEmpty $numCells $count;
  213.             $index array();
  214.  
  215.             // calculate number of empty cells in the array
  216.  
  217.             // fill in all cells of the array
  218.             // put -1 in empty cells so we can skip later
  219.  
  220.             for ($row 1$i 1$row <= $maxRows$row++)
  221.             {
  222.                 for ($col 1$col <= $numColumns$col++)
  223.                 {
  224.                     if ($numEmpty ($numCells $i))
  225.                     {
  226.                         // put -1 in empty cells
  227.                         $index[$row][$col= -1;
  228.                     }
  229.                     else {
  230.                         // put in zero as placeholder
  231.                         $index[$row][$col0;
  232.                     }
  233.                     $i++;
  234.                 }
  235.             }
  236.  
  237.             // layout the articles in column order, skipping empty cells
  238.             $i 0;
  239.             for ($col 1($col <= $numColumns&& ($i $count)$col++)
  240.             {
  241.                 for ($row 1($row <= $maxRows&& ($i $count)$row++)
  242.                 {
  243.                     if ($index[$row][$col!= - 1)
  244.                     {
  245.                         $index[$row][$col$keys[$i];
  246.                         $i++;
  247.                     }
  248.                 }
  249.             }
  250.  
  251.             // now read the $index back row by row to get articles in right row/col
  252.             // so that they will actually be ordered down the columns (when read by row in the layout)
  253.             $return array();
  254.             $i 0;
  255.             for ($row 1($row <= $maxRows&& ($i $count)$row++)
  256.             {
  257.                 for ($col 1($col <= $numColumns&& ($i $count)$col++)
  258.                 {
  259.                     $return[$keys[$i]] $articles[$index[$row][$col]];
  260.                     $i++;
  261.                 }
  262.             }
  263.         }
  264.         return $return;
  265.     }
  266. }

Documentation generated on Tue, 19 Nov 2013 15:11:25 +0100 by phpDocumentor 1.4.3