Source for file categories.php

Documentation is available at categories.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_categories
  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.  * Categories Component Categories Model
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_categories
  17.  * @since       1.6
  18.  */
  19. {
  20.     /**
  21.      * Constructor.
  22.      *
  23.      * @param   array  $config  An optional associative array of configuration settings.
  24.      *
  25.      * @see     JController
  26.      * @since   1.6
  27.      */
  28.     public function __construct($config array())
  29.     {
  30.         if (empty($config['filter_fields']))
  31.         {
  32.             $config['filter_fields'array(
  33.                 'id''a.id',
  34.                 'title''a.title',
  35.                 'alias''a.alias',
  36.                 'published''a.published',
  37.                 'access''a.access''access_level',
  38.                 'language''a.language',
  39.                 'checked_out''a.checked_out',
  40.                 'checked_out_time''a.checked_out_time',
  41.                 'created_time''a.created_time',
  42.                 'created_user_id''a.created_user_id',
  43.                 'lft''a.lft',
  44.                 'rgt''a.rgt',
  45.                 'level''a.level',
  46.                 'path''a.path',
  47.                 'tag'
  48.             );
  49.         }
  50.  
  51.         parent::__construct($config);
  52.     }
  53.  
  54.     /**
  55.      * Method to auto-populate the model state.
  56.      *
  57.      * Note. Calling getState in this method will result in recursion.
  58.      *
  59.      * @param   string  $ordering   An optional ordering field.
  60.      * @param   string  $direction  An optional direction (asc|desc).
  61.      *
  62.      * @return  void 
  63.      *
  64.      * @since   1.6
  65.      */
  66.     protected function populateState($ordering null$direction null)
  67.     {
  68.         $app JFactory::getApplication();
  69.         $context $this->context;
  70.  
  71.         $extension $app->getUserStateFromRequest('com_categories.categories.filter.extension''extension''com_content''cmd');
  72.  
  73.         $this->setState('filter.extension'$extension);
  74.         $parts explode('.'$extension);
  75.  
  76.         // Extract the component name
  77.         $this->setState('filter.component'$parts[0]);
  78.  
  79.         // Extract the optional section name
  80.         $this->setState('filter.section'(count($parts1$parts[1null);
  81.  
  82.         $search $this->getUserStateFromRequest($context '.search''filter_search');
  83.         $this->setState('filter.search'$search);
  84.  
  85.         $level $this->getUserStateFromRequest($context '.filter.level''filter_level'0'int');
  86.         $this->setState('filter.level'$level);
  87.  
  88.         $access $this->getUserStateFromRequest($context '.filter.access''filter_access'0'int');
  89.         $this->setState('filter.access'$access);
  90.  
  91.         $published $this->getUserStateFromRequest($context '.filter.published''filter_published''');
  92.         $this->setState('filter.published'$published);
  93.  
  94.         $language $this->getUserStateFromRequest($context '.filter.language''filter_language''');
  95.         $this->setState('filter.language'$language);
  96.  
  97.         $tag $this->getUserStateFromRequest($this->context . '.filter.tag''filter_tag''');
  98.         $this->setState('filter.tag'$tag);
  99.  
  100.         // List state information.
  101.         parent::populateState('a.lft''asc');
  102.  
  103.         // Force a language
  104.         $forcedLanguage $app->input->get('forcedLanguage');
  105.  
  106.         if (!empty($forcedLanguage))
  107.         {
  108.             $this->setState('filter.language'$forcedLanguage);
  109.             $this->setState('filter.forcedLanguage'$forcedLanguage);
  110.         }
  111.     }
  112.  
  113.     /**
  114.      * Method to get a store id based on model configuration state.
  115.      *
  116.      * This is necessary because the model is used by the component and
  117.      * different modules that might need different sets of data or different
  118.      * ordering requirements.
  119.      *
  120.      * @param   string  $id  A prefix for the store id.
  121.      *
  122.      * @return  string  A store id.
  123.      *
  124.      * @since   1.6
  125.      */
  126.     protected function getStoreId($id '')
  127.     {
  128.         // Compile the store id.
  129.         $id .= ':' $this->getState('filter.search');
  130.         $id .= ':' $this->getState('filter.extension');
  131.         $id .= ':' $this->getState('filter.published');
  132.         $id .= ':' $this->getState('filter.language');
  133.  
  134.         return parent::getStoreId($id);
  135.     }
  136.  
  137.     /**
  138.      * @return  string 
  139.      *
  140.      * @since   1.6
  141.      */
  142.     protected function getListQuery()
  143.     {
  144.         // Create a new query object.
  145.         $db $this->getDbo();
  146.         $query $db->getQuery(true);
  147.         $user JFactory::getUser();
  148.  
  149.         // Select the required fields from the table.
  150.         $query->select(
  151.             $this->getState(
  152.                 'list.select',
  153.                 'a.id, a.title, a.alias, a.note, a.published, a.access' .
  154.                 ', a.checked_out, a.checked_out_time, a.created_user_id' .
  155.                 ', a.path, a.parent_id, a.level, a.lft, a.rgt' .
  156.                 ', a.language'
  157.             )
  158.         );
  159.         $query->from('#__categories AS a');
  160.  
  161.         // Join over the language
  162.         $query->select('l.title AS language_title')
  163.             ->join('LEFT'$db->quoteName('#__languages'' AS l ON l.lang_code = a.language');
  164.  
  165.         // Join over the users for the checked out user.
  166.         $query->select('uc.name AS editor')
  167.             ->join('LEFT''#__users AS uc ON uc.id=a.checked_out');
  168.  
  169.         // Join over the asset groups.
  170.         $query->select('ag.title AS access_level')
  171.             ->join('LEFT''#__viewlevels AS ag ON ag.id = a.access');
  172.  
  173.         // Join over the users for the author.
  174.         $query->select('ua.name AS author_name')
  175.             ->join('LEFT''#__users AS ua ON ua.id = a.created_user_id');
  176.  
  177.         // Join over the associations.
  178.         $assoc $this->getAssoc();
  179.         if ($assoc)
  180.         {
  181.             $query->select('COUNT(asso2.id)>1 as association')
  182.                 ->join('LEFT''#__associations AS asso ON asso.id = a.id AND asso.context=' $db->quote('com_categories.item'))
  183.                 ->join('LEFT''#__associations AS asso2 ON asso2.key = asso.key')
  184.                 ->group('a.id');
  185.         }
  186.  
  187.         // Filter by extension
  188.         if ($extension $this->getState('filter.extension'))
  189.         {
  190.             $query->where('a.extension = ' $db->quote($extension));
  191.         }
  192.  
  193.         // Filter on the level.
  194.         if ($level $this->getState('filter.level'))
  195.         {
  196.             $query->where('a.level <= ' . (int) $level);
  197.         }
  198.  
  199.         // Filter by access level.
  200.         if ($access $this->getState('filter.access'))
  201.         {
  202.             $query->where('a.access = ' . (int) $access);
  203.         }
  204.  
  205.         // Implement View Level Access
  206.         if (!$user->authorise('core.admin'))
  207.         {
  208.             $groups implode(','$user->getAuthorisedViewLevels());
  209.             $query->where('a.access IN (' $groups ')');
  210.         }
  211.  
  212.         // Filter by published state
  213.         $published $this->getState('filter.published');
  214.         if (is_numeric($published))
  215.         {
  216.             $query->where('a.published = ' . (int) $published);
  217.         }
  218.         elseif ($published === '')
  219.         {
  220.             $query->where('(a.published IN (0, 1))');
  221.         }
  222.  
  223.         // Filter by search in title
  224.         $search $this->getState('filter.search');
  225.         if (!empty($search))
  226.         {
  227.             if (stripos($search'id:'=== 0)
  228.             {
  229.                 $query->where('a.id = ' . (int) substr($search3));
  230.             }
  231.             elseif (stripos($search'author:'=== 0)
  232.             {
  233.                 $search $db->quote('%' $db->escape(substr($search7)true'%');
  234.                 $query->where('(ua.name LIKE ' $search ' OR ua.username LIKE ' $search ')');
  235.             }
  236.             else
  237.             {
  238.                 $search $db->quote('%' $db->escape($searchtrue'%');
  239.                 $query->where('(a.title LIKE ' $search ' OR a.alias LIKE ' $search ' OR a.note LIKE ' $search ')');
  240.             }
  241.         }
  242.  
  243.         // Filter on the language.
  244.         if ($language $this->getState('filter.language'))
  245.         {
  246.             $query->where('a.language = ' $db->quote($language));
  247.         }
  248.  
  249.         // Filter by a single tag.
  250.         $tagId $this->getState('filter.tag');
  251.         if (is_numeric($tagId))
  252.         {
  253.             $query->where($db->quoteName('tagmap.tag_id'' = ' . (int) $tagId)
  254.                 ->join(
  255.                     'LEFT'$db->quoteName('#__contentitem_tag_map''tagmap')
  256.                     . ' ON ' $db->quoteName('tagmap.content_item_id'' = ' $db->quoteName('a.id')
  257.                     . ' AND ' $db->quoteName('tagmap.type_alias'' = ' $db->quote($extension '.category')
  258.                 );
  259.         }
  260.  
  261.         // Add the list ordering clause
  262.         $listOrdering $this->getState('list.ordering''a.lft');
  263.         $listDirn $db->escape($this->getState('list.direction''ASC'));
  264.         if ($listOrdering == 'a.access')
  265.         {
  266.             $query->order('a.access ' $listDirn ', a.lft ' $listDirn);
  267.         }
  268.         else
  269.         {
  270.             $query->order($db->escape($listOrdering' ' $listDirn);
  271.         }
  272.  
  273.         //echo nl2br(str_replace('#__','jos_',$query));
  274.         return $query;
  275.     }
  276.  
  277.     /**
  278.      * Method to determine if an association exists
  279.      *
  280.      * @return  boolean  True if the association exists
  281.      *
  282.      * @since  3.0
  283.      */
  284.  
  285.     public function getAssoc()
  286.     {
  287.         static $assoc null;
  288.  
  289.         if (!is_null($assoc))
  290.         {
  291.             return $assoc;
  292.         }
  293.  
  294.         $app JFactory::getApplication();
  295.         $extension $this->getState('filter.extension');
  296.  
  297.         $assoc JLanguageAssociations::isEnabled();
  298.         $extension explode('.'$extension);
  299.         $component array_shift($extension);
  300.         $cname str_replace('com_'''$component);
  301.  
  302.         if (!$assoc || !$component || !$cname)
  303.         {
  304.             $assoc false;
  305.         }
  306.         else
  307.         {
  308.             $hname $cname 'HelperAssociation';
  309.             JLoader::register($hnameJPATH_SITE '/components/' $component '/helpers/association.php');
  310.  
  311.             $assoc class_exists($hname&& !empty($hname::$category_association);
  312.         }
  313.  
  314.         return $assoc;
  315.     }
  316. }

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