Source for file category.php

Documentation is available at category.php

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

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