Source for file category.php

Documentation is available at category.php

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

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