Source for file category.php

Documentation is available at category.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.  * This models supports retrieving a category, the articles associated with the category,
  14.  * sibling, child and parent categories.
  15.  *
  16.  * @package     Joomla.Site
  17.  * @subpackage  com_content
  18.  * @since       1.5
  19.  */
  20. {
  21.     /**
  22.      * Category items data
  23.      *
  24.      * @var array 
  25.      */
  26.     protected $_item = null;
  27.  
  28.     protected $_articles = null;
  29.  
  30.     protected $_siblings = null;
  31.  
  32.     protected $_children = null;
  33.  
  34.     protected $_parent = null;
  35.  
  36.     /**
  37.      * Model context string.
  38.      *
  39.      * @var        string 
  40.      */
  41.     protected $_context = 'com_content.category';
  42.  
  43.     /**
  44.      * The category that applies.
  45.      *
  46.      * @access    protected
  47.      * @var        object 
  48.      */
  49.     protected $_category = null;
  50.  
  51.     /**
  52.      * The list of other newfeed categories.
  53.      *
  54.      * @access    protected
  55.      * @var        array 
  56.      */
  57.     protected $_categories = null;
  58.  
  59.     /**
  60.      * Constructor.
  61.      *
  62.      * @param   array  An optional associative array of configuration settings.
  63.      * @see     JController
  64.      * @since   1.6
  65.      */
  66.     public function __construct($config array())
  67.     {
  68.         if (empty($config['filter_fields']))
  69.         {
  70.             $config['filter_fields'array(
  71.                 'id''a.id',
  72.                 'title''a.title',
  73.                 'alias''a.alias',
  74.                 'checked_out''a.checked_out',
  75.                 'checked_out_time''a.checked_out_time',
  76.                 'catid''a.catid''category_title',
  77.                 'state''a.state',
  78.                 'access''a.access''access_level',
  79.                 'created''a.created',
  80.                 'created_by''a.created_by',
  81.                 'modified''a.modified',
  82.                 'ordering''a.ordering',
  83.                 'featured''a.featured',
  84.                 'language''a.language',
  85.                 'hits''a.hits',
  86.                 'publish_up''a.publish_up',
  87.                 'publish_down''a.publish_down',
  88.                 'author''a.author'
  89.             );
  90.         }
  91.  
  92.         parent::__construct($config);
  93.     }
  94.  
  95.     /**
  96.      * Method to auto-populate the model state.
  97.      *
  98.      * Note. Calling getState in this method will result in recursion.
  99.      *
  100.      * return    void
  101.      * @since   1.6
  102.      */
  103.     protected function populateState($ordering null$direction null)
  104.     {
  105.         $app JFactory::getApplication('site');
  106.         $pk  $app->input->getInt('id');
  107.  
  108.         $this->setState('category.id'$pk);
  109.  
  110.         // Load the parameters. Merge Global and Menu Item params into new object
  111.         $params $app->getParams();
  112.         $menuParams new JRegistry;
  113.  
  114.         if ($menu $app->getMenu()->getActive())
  115.         {
  116.             $menuParams->loadString($menu->params);
  117.         }
  118.  
  119.         $mergedParams clone $menuParams;
  120.         $mergedParams->merge($params);
  121.  
  122.         $this->setState('params'$mergedParams);
  123.         $user        JFactory::getUser();
  124.                 // Create a new query object.
  125.         $db        $this->getDbo();
  126.         $query    $db->getQuery(true);
  127.  
  128.         if ((!$user->authorise('core.edit.state''com_content')) &&  (!$user->authorise('core.edit''com_content'))){
  129.             // limit to published for people who can't edit or edit.state.
  130.             $this->setState('filter.published'1);
  131.             // Filter by start and end dates.
  132.             $nullDate $db->quote($db->getNullDate());
  133.             $nowDate $db->quote(JFactory::getDate()->toSQL());
  134.  
  135.             $query->where('(a.publish_up = ' $nullDate ' OR a.publish_up <= ' $nowDate ')')
  136.                 ->where('(a.publish_down = ' $nullDate ' OR a.publish_down >= ' $nowDate ')');
  137.         }
  138.         else
  139.         {
  140.             $this->setState('filter.published'array(012));
  141.         }
  142.  
  143.         // process show_noauth parameter
  144.         if (!$params->get('show_noauth'))
  145.         {
  146.             $this->setState('filter.access'true);
  147.         }
  148.         else
  149.         {
  150.             $this->setState('filter.access'false);
  151.         }
  152.  
  153.         // Optional filter text
  154.         $this->setState('list.filter'$app->input->getString('filter-search'));
  155.  
  156.         // filter.order
  157.         $itemid $app->input->get('id'0'int'':' $app->input->get('Itemid'0'int');
  158.         $orderCol $app->getUserStateFromRequest('com_content.category.list.' $itemid '.filter_order''filter_order''''string');
  159.         if (!in_array($orderCol$this->filter_fields))
  160.         {
  161.             $orderCol 'a.ordering';
  162.         }
  163.         $this->setState('list.ordering'$orderCol);
  164.  
  165.         $listOrder $app->getUserStateFromRequest('com_content.category.list.' $itemid '.filter_order_Dir',
  166.             'filter_order_Dir''''cmd');
  167.         if (!in_array(strtoupper($listOrder)array('ASC''DESC''')))
  168.         {
  169.             $listOrder 'ASC';
  170.         }
  171.         $this->setState('list.direction'$listOrder);
  172.  
  173.         $this->setState('list.start'$app->input->get('limitstart'0'uint'));
  174.  
  175.         // set limit for query. If list, use parameter. If blog, add blog parameters for limit.
  176.         if (($app->input->get('layout'== 'blog'|| $params->get('layout_type'== 'blog')
  177.         {
  178.             $limit $params->get('num_leading_articles'$params->get('num_intro_articles'$params->get('num_links');
  179.             $this->setState('list.links'$params->get('num_links'));
  180.         }
  181.         else
  182.         {
  183.             $limit $app->getUserStateFromRequest('com_content.category.list.' $itemid '.limit''limit'$params->get('display_num')'uint');
  184.         }
  185.  
  186.         $this->setState('list.limit'$limit);
  187.  
  188.         // set the depth of the category query based on parameter
  189.         $showSubcategories $params->get('show_subcategory_content''0');
  190.  
  191.         if ($showSubcategories)
  192.         {
  193.             $this->setState('filter.max_category_levels'$params->get('show_subcategory_content''1'));
  194.             $this->setState('filter.subcategories'true);
  195.         }
  196.  
  197.         $this->setState('filter.language'JLanguageMultilang::isEnabled());
  198.  
  199.         $this->setState('layout'$app->input->get('layout'));
  200.  
  201.     }
  202.  
  203.     /**
  204.      * Get the articles in the category
  205.      *
  206.      * @return  mixed  An array of articles or false if an error occurs.
  207.      * @since   1.5
  208.      */
  209.     function getItems()
  210.     {
  211.         $limit $this->getState('list.limit');
  212.  
  213.         if ($this->_articles === null && $category $this->getCategory())
  214.         {
  215.             $model JModelLegacy::getInstance('Articles''ContentModel'array('ignore_request' => true));
  216.             $model->setState('params'JFactory::getApplication()->getParams());
  217.             $model->setState('filter.category_id'$category->id);
  218.             $model->setState('filter.published'$this->getState('filter.published'));
  219.             $model->setState('filter.access'$this->getState('filter.access'));
  220.             $model->setState('filter.language'$this->getState('filter.language'));
  221.             $model->setState('list.ordering'$this->_buildContentOrderBy());
  222.             $model->setState('list.start'$this->getState('list.start'));
  223.             $model->setState('list.limit'$limit);
  224.             $model->setState('list.direction'$this->getState('list.direction'));
  225.             $model->setState('list.filter'$this->getState('list.filter'));
  226.             // filter.subcategories indicates whether to include articles from subcategories in the list or blog
  227.             $model->setState('filter.subcategories'$this->getState('filter.subcategories'));
  228.             $model->setState('filter.max_category_levels'$this->setState('filter.max_category_levels'));
  229.             $model->setState('list.links'$this->getState('list.links'));
  230.  
  231.             if ($limit >= 0)
  232.             {
  233.                 $this->_articles = $model->getItems();
  234.  
  235.                 if ($this->_articles === false)
  236.                 {
  237.                     $this->setError($model->getError());
  238.                 }
  239.             }
  240.             else
  241.             {
  242.                 $this->_articles = array();
  243.             }
  244.  
  245.             $this->_pagination $model->getPagination();
  246.         }
  247.  
  248.         return $this->_articles;
  249.     }
  250.  
  251.     /**
  252.      * Build the orderby for the query
  253.      *
  254.      * @return  string    $orderby portion of query
  255.      * @since   1.5
  256.      */
  257.     protected function _buildContentOrderBy()
  258.     {
  259.         $app        JFactory::getApplication('site');
  260.         $db            $this->getDbo();
  261.         $params        $this->state->params;
  262.         $itemid        $app->input->get('id'0'int'':' $app->input->get('Itemid'0'int');
  263.         $orderCol    $app->getUserStateFromRequest('com_content.category.list.' $itemid '.filter_order''filter_order''''string');
  264.         $orderDirn    $app->getUserStateFromRequest('com_content.category.list.' $itemid '.filter_order_Dir''filter_order_Dir''''cmd');
  265.         $orderby    ' ';
  266.  
  267.         if (!in_array($orderCol$this->filter_fields))
  268.         {
  269.             $orderCol null;
  270.         }
  271.  
  272.         if (!in_array(strtoupper($orderDirn)array('ASC''DESC''')))
  273.         {
  274.             $orderDirn 'ASC';
  275.         }
  276.  
  277.         if ($orderCol && $orderDirn)
  278.         {
  279.             $orderby .= $db->escape($orderCol' ' $db->escape($orderDirn', ';
  280.         }
  281.  
  282.         $articleOrderby        $params->get('orderby_sec''rdate');
  283.         $articleOrderDate    $params->get('order_date');
  284.         $categoryOrderby    $params->def('orderby_pri''');
  285.         $secondary            ContentHelperQuery::orderbySecondary($articleOrderby$articleOrderDate', ';
  286.         $primary            ContentHelperQuery::orderbyPrimary($categoryOrderby);
  287.  
  288.         $orderby .= $primary ' ' $secondary ' a.created ';
  289.  
  290.         return $orderby;
  291.     }
  292.  
  293.     public function getPagination()
  294.     {
  295.         if (empty($this->_pagination))
  296.         {
  297.             return null;
  298.         }
  299.         return $this->_pagination;
  300.     }
  301.  
  302.     /**
  303.      * Method to get category data for the current category
  304.      *
  305.      * @param   integer  An optional ID
  306.      *
  307.      * @return  object 
  308.      * @since   1.5
  309.      */
  310.     public function getCategory()
  311.     {
  312.         if (!is_object($this->_item))
  313.         {
  314.             if isset$this->state->params ) )
  315.             {
  316.                 $params $this->state->params;
  317.                 $options array();
  318.                 $options['countItems'$params->get('show_cat_num_articles'1|| !$params->get('show_empty_categories_cat'0);
  319.             }
  320.             else {
  321.                 $options['countItems'0;
  322.             }
  323.  
  324.             $categories JCategories::getInstance('Content'$options);
  325.             $this->_item = $categories->get($this->getState('category.id''root'));
  326.  
  327.             // Compute selected asset permissions.
  328.             if (is_object($this->_item))
  329.             {
  330.                 $user    JFactory::getUser();
  331.                 $asset    'com_content.category.'.$this->_item->id;
  332.  
  333.                 // Check general create permission.
  334.                 if ($user->authorise('core.create'$asset))
  335.                 {
  336.                     $this->_item->getParams()->set('access-create'true);
  337.                 }
  338.  
  339.                 // TODO: Why aren't we lazy loading the children and siblings?
  340.                 $this->_children = $this->_item->getChildren();
  341.                 $this->_parent = false;
  342.  
  343.                 if ($this->_item->getParent())
  344.                 {
  345.                     $this->_parent = $this->_item->getParent();
  346.                 }
  347.  
  348.                 $this->_rightsibling $this->_item->getSibling();
  349.                 $this->_leftsibling $this->_item->getSibling(false);
  350.             }
  351.             else {
  352.                 $this->_children = false;
  353.                 $this->_parent = false;
  354.             }
  355.         }
  356.  
  357.         return $this->_item;
  358.     }
  359.  
  360.     /**
  361.      * Get the parent category.
  362.      *
  363.      * @param   integer  An optional category id. If not supplied, the model state 'category.id' will be used.
  364.      *
  365.      * @return  mixed  An array of categories or false if an error occurs.
  366.      * @since   1.6
  367.      */
  368.     public function getParent()
  369.     {
  370.         if (!is_object($this->_item))
  371.         {
  372.             $this->getCategory();
  373.         }
  374.  
  375.         return $this->_parent;
  376.     }
  377.  
  378.     /**
  379.      * Get the left sibling (adjacent) categories.
  380.      *
  381.      * @return  mixed  An array of categories or false if an error occurs.
  382.      * @since   1.6
  383.      */
  384.     function &getLeftSibling()
  385.     {
  386.         if (!is_object($this->_item))
  387.         {
  388.             $this->getCategory();
  389.         }
  390.  
  391.         return $this->_leftsibling;
  392.     }
  393.  
  394.     /**
  395.      * Get the right sibling (adjacent) categories.
  396.      *
  397.      * @return  mixed  An array of categories or false if an error occurs.
  398.      * @since   1.6
  399.      */
  400.     function &getRightSibling()
  401.     {
  402.         if (!is_object($this->_item))
  403.         {
  404.             $this->getCategory();
  405.         }
  406.  
  407.         return $this->_rightsibling;
  408.     }
  409.  
  410.     /**
  411.      * Get the child categories.
  412.      *
  413.      * @param   integer  An optional category id. If not supplied, the model state 'category.id' will be used.
  414.      *
  415.      * @return  mixed  An array of categories or false if an error occurs.
  416.      * @since   1.6
  417.      */
  418.     function &getChildren()
  419.     {
  420.         if (!is_object($this->_item))
  421.         {
  422.             $this->getCategory();
  423.         }
  424.  
  425.         // Order subcategories
  426.         if (count($this->_children))
  427.         {
  428.             $params $this->getState()->get('params');
  429.             if ($params->get('orderby_pri'== 'alpha' || $params->get('orderby_pri'== 'ralpha')
  430.             {
  431.                 jimport('joomla.utilities.arrayhelper');
  432.                 JArrayHelper::sortObjects($this->_children'title'($params->get('orderby_pri'== 'alpha': -1);
  433.             }
  434.         }
  435.  
  436.         return $this->_children;
  437.     }
  438.  
  439.     /**
  440.      * Increment the hit counter for the category.
  441.      *
  442.      * @param   int  $pk  Optional primary key of the category to increment.
  443.      *
  444.      * @return  boolean True if successful; false otherwise and internal error set.
  445.      */
  446.     public function hit($pk 0)
  447.     {
  448.         $input JFactory::getApplication()->input;
  449.         $hitcount $input->getInt('hitcount'1);
  450.  
  451.         if ($hitcount)
  452.         {
  453.             $pk (!empty($pk)) $pk : (int) $this->getState('category.id');
  454.  
  455.             $table JTable::getInstance('Category''JTable');
  456.             $table->load($pk);
  457.             $table->hit($pk);
  458.         }
  459.  
  460.         return true;
  461.     }
  462. }

Documentation generated on Tue, 19 Nov 2013 14:55:26 +0100 by phpDocumentor 1.4.3