Source for file category.php

Documentation is available at category.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Site
  4.  * @subpackage  com_newsfeeds
  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.  * Newsfeeds Component Category Model
  14.  *
  15.  * @package     Joomla.Site
  16.  * @subpackage  com_newsfeeds
  17.  * @since       1.5
  18.  */
  19. {
  20.     /**
  21.      * Category items data
  22.      *
  23.      * @var array 
  24.      */
  25.     protected $_item = null;
  26.  
  27.     protected $_articles = null;
  28.  
  29.     protected $_siblings = null;
  30.  
  31.     protected $_children = null;
  32.  
  33.     protected $_parent = null;
  34.  
  35.     /**
  36.      * The category that applies.
  37.      *
  38.      * @access    protected
  39.      * @var        object 
  40.      */
  41.     protected $_category = null;
  42.  
  43.     /**
  44.      * The list of other newfeed categories.
  45.      *
  46.      * @access    protected
  47.      * @var        array 
  48.      */
  49.     protected $_categories = null;
  50.  
  51.     /**
  52.      * Constructor.
  53.      *
  54.      * @param   array  An optional associative array of configuration settings.
  55.      * @see     JController
  56.      * @since   1.6
  57.      */
  58.     public function __construct($config array())
  59.     {
  60.         if (empty($config['filter_fields']))
  61.         {
  62.             $config['filter_fields'array(
  63.                 'id''a.id',
  64.                 'name''a.name',
  65.                 'numarticles''a.numarticles',
  66.                 'link''a.link',
  67.                 'ordering''a.ordering',
  68.             );
  69.         }
  70.  
  71.         parent::__construct($config);
  72.     }
  73.  
  74.     /**
  75.      * Method to get a list of items.
  76.      *
  77.      * @return  mixed  An array of objects on success, false on failure.
  78.      */
  79.     public function getItems()
  80.     {
  81.         // Invoke the parent getItems method to get the main list
  82.         $items parent::getItems();
  83.  
  84.         // Convert the params field into an object, saving original in _params
  85.         foreach ($items as $item)
  86.         {
  87.             if (!isset($this->_params))
  88.             {
  89.                 $params new JRegistry;
  90.                 $item->params $params;
  91.                 $params->loadString($item->params);
  92.             }
  93.  
  94.             // Get the tags
  95.             $item->tags new JHelperTags;
  96.             $item->tags->getItemTags('com_newsfeeds.newsfeed'$item->id);
  97.         }
  98.  
  99.         return $items;
  100.     }
  101.  
  102.     /**
  103.      * Method to build an SQL query to load the list data.
  104.      *
  105.      * @return  string    An SQL query
  106.      * @since   1.6
  107.      */
  108.     protected function getListQuery()
  109.     {
  110.         $user JFactory::getUser();
  111.         $groups implode(','$user->getAuthorisedViewLevels());
  112.  
  113.         // Create a new query object.
  114.         $db $this->getDbo();
  115.         $query $db->getQuery(true);
  116.  
  117.         // Select required fields from the categories.
  118.         $query->select($this->getState('list.select''a.*'))
  119.             ->from($db->quoteName('#__newsfeeds'' AS a')
  120.             ->where('a.access IN (' $groups ')');
  121.  
  122.         // Filter by category.
  123.         if ($categoryId $this->getState('category.id'))
  124.         {
  125.             $query->where('a.catid = ' . (int) $categoryId)
  126.                 ->join('LEFT''#__categories AS c ON c.id = a.catid')
  127.                 ->where('c.access IN (' $groups ')');
  128.         }
  129.  
  130.         // Filter by state
  131.         $state $this->getState('filter.published');
  132.         if (is_numeric($state))
  133.         {
  134.             $query->where('a.published = ' . (int) $state);
  135.         }
  136.  
  137.         // Filter by start and end dates.
  138.         $nullDate $db->quote($db->getNullDate());
  139.         $date JFactory::getDate();
  140.         $nowDate $db->quote($date->format($db->getDateFormat()));
  141.  
  142.         if ($this->getState('filter.publish_date'))
  143.         {
  144.             $query->where('(a.publish_up = ' $nullDate ' OR a.publish_up <= ' $nowDate ')')
  145.                 ->where('(a.publish_down = ' $nullDate ' OR a.publish_down >= ' $nowDate ')');
  146.         }
  147.  
  148.         // Filter by search in title
  149.         $search $this->getState('list.filter');
  150.         if (!empty($search))
  151.         {
  152.             $search $db->quote('%' $db->escape($searchtrue'%');
  153.             $query->where('(a.name LIKE ' $search ')');
  154.         }
  155.  
  156.         // Filter by language
  157.         if ($this->getState('filter.language'))
  158.         {
  159.             $query->where('a.language in (' $db->quote(JFactory::getLanguage()->getTag()) ',' $db->quote('*'')');
  160.         }
  161.  
  162.         // Add the list ordering clause.
  163.         $query->order($db->escape($this->getState('list.ordering''a.ordering')) ' ' $db->escape($this->getState('list.direction''ASC')));
  164.  
  165.         return $query;
  166.     }
  167.  
  168.     /**
  169.      * Method to auto-populate the model state.
  170.      *
  171.      * Note. Calling getState in this method will result in recursion.
  172.      *
  173.      * @since   1.6
  174.      */
  175.     protected function populateState($ordering null$direction null)
  176.     {
  177.         $app JFactory::getApplication();
  178.         $params JComponentHelper::getParams('com_newsfeeds');
  179.  
  180.         // List state information
  181.         $limit $app->getUserStateFromRequest('global.list.limit''limit'$app->getCfg('list_limit')'uint');
  182.         $this->setState('list.limit'$limit);
  183.  
  184.         $limitstart $app->input->get('limitstart'0'uint');
  185.         $this->setState('list.start'$limitstart);
  186.  
  187.         // Optional filter text
  188.         $this->setState('list.filter'$app->input->getString('filter-search'));
  189.  
  190.         $orderCol $app->input->get('filter_order''ordering');
  191.         if (!in_array($orderCol$this->filter_fields))
  192.         {
  193.             $orderCol 'ordering';
  194.         }
  195.         $this->setState('list.ordering'$orderCol);
  196.  
  197.         $listOrder $app->input->get('filter_order_Dir''ASC');
  198.         if (!in_array(strtoupper($listOrder)array('ASC''DESC''')))
  199.         {
  200.             $listOrder 'ASC';
  201.         }
  202.         $this->setState('list.direction'$listOrder);
  203.  
  204.         $id $app->input->get('id'0'int');
  205.         $this->setState('category.id'$id);
  206.  
  207.         $user JFactory::getUser();
  208.         if ((!$user->authorise('core.edit.state''com_newsfeeds')) && (!$user->authorise('core.edit''com_newsfeeds')))
  209.         {
  210.             // limit to published for people who can't edit or edit.state.
  211.             $this->setState('filter.published'1);
  212.  
  213.             // Filter by start and end dates.
  214.             $this->setState('filter.publish_date'true);
  215.         }
  216.  
  217.         $this->setState('filter.language'JLanguageMultilang::isEnabled());
  218.  
  219.         // Load the parameters.
  220.         $this->setState('params'$params);
  221.     }
  222.  
  223.     /**
  224.      * Method to get category data for the current category
  225.      *
  226.      * @param   integer  An optional ID
  227.      *
  228.      * @return  object 
  229.      * @since   1.5
  230.      */
  231.     public function getCategory()
  232.     {
  233.         if (!is_object($this->_item))
  234.         {
  235.             $app JFactory::getApplication();
  236.             $menu $app->getMenu();
  237.             $active $menu->getActive();
  238.             $params new JRegistry;
  239.  
  240.             if ($active)
  241.             {
  242.                 $params->loadString($active->params);
  243.             }
  244.  
  245.             $options array();
  246.             $options['countItems'$params->get('show_cat_items'1|| $params->get('show_empty_categories'0);
  247.             $categories JCategories::getInstance('Newsfeeds'$options);
  248.             $this->_item = $categories->get($this->getState('category.id''root'));
  249.             if (is_object($this->_item))
  250.             {
  251.                 $this->_children = $this->_item->getChildren();
  252.                 $this->_parent = false;
  253.                 if ($this->_item->getParent())
  254.                 {
  255.                     $this->_parent = $this->_item->getParent();
  256.                 }
  257.                 $this->_rightsibling $this->_item->getSibling();
  258.                 $this->_leftsibling $this->_item->getSibling(false);
  259.             }
  260.             else
  261.             {
  262.                 $this->_children = false;
  263.                 $this->_parent = false;
  264.             }
  265.         }
  266.  
  267.         return $this->_item;
  268.     }
  269.  
  270.     /**
  271.      * Get the parent category.
  272.      *
  273.      * @param   integer  An optional category id. If not supplied, the model state 'category.id' will be used.
  274.      *
  275.      * @return  mixed  An array of categories or false if an error occurs.
  276.      */
  277.     public function getParent()
  278.     {
  279.         if (!is_object($this->_item))
  280.         {
  281.             $this->getCategory();
  282.         }
  283.         return $this->_parent;
  284.     }
  285.  
  286.     /**
  287.      * Get the sibling (adjacent) categories.
  288.      *
  289.      * @return  mixed  An array of categories or false if an error occurs.
  290.      */
  291.     function &getLeftSibling()
  292.     {
  293.         if (!is_object($this->_item))
  294.         {
  295.             $this->getCategory();
  296.         }
  297.         return $this->_leftsibling;
  298.     }
  299.  
  300.     function &getRightSibling()
  301.     {
  302.         if (!is_object($this->_item))
  303.         {
  304.             $this->getCategory();
  305.         }
  306.         return $this->_rightsibling;
  307.     }
  308.  
  309.     /**
  310.      * Get the child categories.
  311.      *
  312.      * @param   integer  An optional category id. If not supplied, the model state 'category.id' will be used.
  313.      *
  314.      * @return  mixed  An array of categories or false if an error occurs.
  315.      */
  316.     function &getChildren()
  317.     {
  318.         if (!is_object($this->_item))
  319.         {
  320.             $this->getCategory();
  321.         }
  322.         return $this->_children;
  323.     }
  324.  
  325.     /**
  326.      * Increment the hit counter for the category.
  327.      *
  328.      * @param   int  $pk  Optional primary key of the category to increment.
  329.      *
  330.      * @return  boolean True if successful; false otherwise and internal error set.
  331.      */
  332.     public function hit($pk 0)
  333.     {
  334.         $input JFactory::getApplication()->input;
  335.         $hitcount $input->getInt('hitcount'1);
  336.  
  337.         if ($hitcount)
  338.         {
  339.             $pk (!empty($pk)) $pk : (int) $this->getState('category.id');
  340.  
  341.             $table JTable::getInstance('Category''JTable');
  342.             $table->load($pk);
  343.             $table->hit($pk);
  344.         }
  345.  
  346.         return true;
  347.     }
  348. }

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