Source for file articles.php

Documentation is available at articles.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  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.  * Methods supporting a list of article records.
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_content
  17.  *
  18.  * @since       1.6
  19.  */
  20. {
  21.     /**
  22.      * Constructor.
  23.      *
  24.      * @param   array  $config  An optional associative array of configuration settings.
  25.      *
  26.      * @since   1.6
  27.      * @see     JController
  28.      */
  29.     public function __construct($config array())
  30.     {
  31.         if (empty($config['filter_fields']))
  32.         {
  33.             $config['filter_fields'array(
  34.                 'id''a.id',
  35.                 'title''a.title',
  36.                 'alias''a.alias',
  37.                 'checked_out''a.checked_out',
  38.                 'checked_out_time''a.checked_out_time',
  39.                 'catid''a.catid''category_title',
  40.                 'state''a.state',
  41.                 'access''a.access''access_level',
  42.                 'created''a.created',
  43.                 'created_by''a.created_by',
  44.                 'created_by_alias''a.created_by_alias',
  45.                 'ordering''a.ordering',
  46.                 'featured''a.featured',
  47.                 'language''a.language',
  48.                 'hits''a.hits',
  49.                 'publish_up''a.publish_up',
  50.                 'publish_down''a.publish_down',
  51.                 'published''a.published',
  52.                 'author_id',
  53.                 'category_id',
  54.                 'level',
  55.                 'tag'
  56.             );
  57.  
  58.             if (JLanguageAssociations::isEnabled())
  59.             {
  60.                 $config['filter_fields']['association';
  61.             }
  62.         }
  63.  
  64.         parent::__construct($config);
  65.     }
  66.  
  67.     /**
  68.      * Method to auto-populate the model state.
  69.      *
  70.      * Note. Calling getState in this method will result in recursion.
  71.      *
  72.      * @param   string  $ordering   An optional ordering field.
  73.      * @param   string  $direction  An optional direction (asc|desc).
  74.      *
  75.      * @return  void 
  76.      *
  77.      * @since   1.6
  78.      */
  79.     protected function populateState($ordering null$direction null)
  80.     {
  81.         $app JFactory::getApplication();
  82.  
  83.         // Adjust the context to support modal layouts.
  84.         if ($layout $app->input->get('layout'))
  85.         {
  86.             $this->context .= '.' $layout;
  87.         }
  88.  
  89.         $search $this->getUserStateFromRequest($this->context . '.filter.search''filter_search');
  90.         $this->setState('filter.search'$search);
  91.  
  92.         $access $this->getUserStateFromRequest($this->context . '.filter.access''filter_access'0'int');
  93.         $this->setState('filter.access'$access);
  94.  
  95.         $authorId $app->getUserStateFromRequest($this->context . '.filter.author_id''filter_author_id');
  96.         $this->setState('filter.author_id'$authorId);
  97.  
  98.         $published $this->getUserStateFromRequest($this->context . '.filter.published''filter_published''');
  99.         $this->setState('filter.published'$published);
  100.  
  101.         $categoryId $this->getUserStateFromRequest($this->context . '.filter.category_id''filter_category_id');
  102.         $this->setState('filter.category_id'$categoryId);
  103.  
  104.         $level $this->getUserStateFromRequest($this->context . '.filter.level''filter_level'0'int');
  105.         $this->setState('filter.level'$level);
  106.  
  107.         $language $this->getUserStateFromRequest($this->context . '.filter.language''filter_language''');
  108.         $this->setState('filter.language'$language);
  109.  
  110.         $tag $this->getUserStateFromRequest($this->context . '.filter.tag''filter_tag''');
  111.         $this->setState('filter.tag'$tag);
  112.  
  113.         // List state information.
  114.         parent::populateState('a.title''asc');
  115.  
  116.         // Force a language
  117.         $forcedLanguage $app->input->get('forcedLanguage');
  118.  
  119.         if (!empty($forcedLanguage))
  120.         {
  121.             $this->setState('filter.language'$forcedLanguage);
  122.             $this->setState('filter.forcedLanguage'$forcedLanguage);
  123.         }
  124.     }
  125.  
  126.     /**
  127.      * Method to get a store id based on model configuration state.
  128.      *
  129.      * This is necessary because the model is used by the component and
  130.      * different modules that might need different sets of data or different
  131.      * ordering requirements.
  132.      *
  133.      * @param   string  $id  A prefix for the store id.
  134.      *
  135.      * @return  string  A store id.
  136.      *
  137.      * @since   1.6
  138.      */
  139.     protected function getStoreId($id '')
  140.     {
  141.         // Compile the store id.
  142.         $id .= ':' $this->getState('filter.search');
  143.         $id .= ':' $this->getState('filter.access');
  144.         $id .= ':' $this->getState('filter.published');
  145.         $id .= ':' $this->getState('filter.category_id');
  146.         $id .= ':' $this->getState('filter.author_id');
  147.         $id .= ':' $this->getState('filter.language');
  148.  
  149.         return parent::getStoreId($id);
  150.     }
  151.  
  152.     /**
  153.      * Build an SQL query to load the list data.
  154.      *
  155.      * @return  JDatabaseQuery 
  156.      *
  157.      * @since   1.6
  158.      */
  159.     protected function getListQuery()
  160.     {
  161.         // Create a new query object.
  162.         $db $this->getDbo();
  163.         $query $db->getQuery(true);
  164.         $user JFactory::getUser();
  165.         $app JFactory::getApplication();
  166.  
  167.         // Select the required fields from the table.
  168.         $query->select(
  169.             $this->getState(
  170.                 'list.select',
  171.                 'a.id, a.title, a.alias, a.checked_out, a.checked_out_time, a.catid' .
  172.                     ', a.state, a.access, a.created, a.created_by, a.created_by_alias, a.ordering, a.featured, a.language, a.hits' .
  173.                     ', a.publish_up, a.publish_down'
  174.             )
  175.         );
  176.         $query->from('#__content AS a');
  177.  
  178.         // Join over the language
  179.         $query->select('l.title AS language_title')
  180.             ->join('LEFT'$db->quoteName('#__languages'' AS l ON l.lang_code = a.language');
  181.  
  182.         // Join over the users for the checked out user.
  183.         $query->select('uc.name AS editor')
  184.             ->join('LEFT''#__users AS uc ON uc.id=a.checked_out');
  185.  
  186.         // Join over the asset groups.
  187.         $query->select('ag.title AS access_level')
  188.             ->join('LEFT''#__viewlevels AS ag ON ag.id = a.access');
  189.  
  190.         // Join over the categories.
  191.         $query->select('c.title AS category_title')
  192.             ->join('LEFT''#__categories AS c ON c.id = a.catid');
  193.  
  194.         // Join over the users for the author.
  195.         $query->select('ua.name AS author_name')
  196.             ->join('LEFT''#__users AS ua ON ua.id = a.created_by');
  197.  
  198.         // Join over the associations.
  199.         if (JLanguageAssociations::isEnabled())
  200.         {
  201.             $query->select('COUNT(asso2.id)>1 as association')
  202.                 ->join('LEFT''#__associations AS asso ON asso.id = a.id AND asso.context=' $db->quote('com_content.item'))
  203.                 ->join('LEFT''#__associations AS asso2 ON asso2.key = asso.key')
  204.                 ->group('a.id');
  205.         }
  206.  
  207.         // Filter by access level.
  208.         if ($access $this->getState('filter.access'))
  209.         {
  210.             $query->where('a.access = ' . (int) $access);
  211.         }
  212.  
  213.         // Implement View Level Access
  214.         if (!$user->authorise('core.admin'))
  215.         {
  216.             $groups implode(','$user->getAuthorisedViewLevels());
  217.             $query->where('a.access IN (' $groups ')');
  218.         }
  219.  
  220.         // Filter by published state
  221.         $published $this->getState('filter.published');
  222.  
  223.         if (is_numeric($published))
  224.         {
  225.             $query->where('a.state = ' . (int) $published);
  226.         }
  227.         elseif ($published === '')
  228.         {
  229.             $query->where('(a.state = 0 OR a.state = 1)');
  230.         }
  231.  
  232.         // Filter by a single or group of categories.
  233.         $baselevel 1;
  234.         $categoryId $this->getState('filter.category_id');
  235.  
  236.         if (is_numeric($categoryId))
  237.         {
  238.             $cat_tbl JTable::getInstance('Category''JTable');
  239.             $cat_tbl->load($categoryId);
  240.             $rgt $cat_tbl->rgt;
  241.             $lft $cat_tbl->lft;
  242.             $baselevel = (int) $cat_tbl->level;
  243.             $query->where('c.lft >= ' . (int) $lft)
  244.                 ->where('c.rgt <= ' . (int) $rgt);
  245.         }
  246.         elseif (is_array($categoryId))
  247.         {
  248.             JArrayHelper::toInteger($categoryId);
  249.             $categoryId implode(','$categoryId);
  250.             $query->where('a.catid IN (' $categoryId ')');
  251.         }
  252.  
  253.         // Filter on the level.
  254.         if ($level $this->getState('filter.level'))
  255.         {
  256.             $query->where('c.level <= ' ((int) $level + (int) $baselevel 1));
  257.         }
  258.  
  259.         // Filter by author
  260.         $authorId $this->getState('filter.author_id');
  261.  
  262.         if (is_numeric($authorId))
  263.         {
  264.             $type $this->getState('filter.author_id.include'true'= ' '<>';
  265.             $query->where('a.created_by ' $type . (int) $authorId);
  266.         }
  267.  
  268.         // Filter by search in title.
  269.         $search $this->getState('filter.search');
  270.  
  271.         if (!empty($search))
  272.         {
  273.             if (stripos($search'id:'=== 0)
  274.             {
  275.                 $query->where('a.id = ' . (int) substr($search3));
  276.             }
  277.             elseif (stripos($search'author:'=== 0)
  278.             {
  279.                 $search $db->quote('%' $db->escape(substr($search7)true'%');
  280.                 $query->where('(ua.name LIKE ' $search ' OR ua.username LIKE ' $search ')');
  281.             }
  282.             else
  283.             {
  284.                 $search $db->quote('%' $db->escape($searchtrue'%');
  285.                 $query->where('(a.title LIKE ' $search ' OR a.alias LIKE ' $search ')');
  286.             }
  287.         }
  288.  
  289.         // Filter on the language.
  290.         if ($language $this->getState('filter.language'))
  291.         {
  292.             $query->where('a.language = ' $db->quote($language));
  293.         }
  294.  
  295.         // Filter by a single tag.
  296.         $tagId $this->getState('filter.tag');
  297.  
  298.         if (is_numeric($tagId))
  299.         {
  300.             $query->where($db->quoteName('tagmap.tag_id'' = ' . (int) $tagId)
  301.                 ->join(
  302.                     'LEFT'$db->quoteName('#__contentitem_tag_map''tagmap')
  303.                     . ' ON ' $db->quoteName('tagmap.content_item_id'' = ' $db->quoteName('a.id')
  304.                     . ' AND ' $db->quoteName('tagmap.type_alias'' = ' $db->quote('com_content.article')
  305.                 );
  306.         }
  307.  
  308.         // Add the list ordering clause.
  309.         $orderCol $this->state->get('list.ordering''a.title');
  310.         $orderDirn $this->state->get('list.direction''asc');
  311.  
  312.         if ($orderCol == 'a.ordering' || $orderCol == 'category_title')
  313.         {
  314.             $orderCol 'c.title ' $orderDirn ', a.ordering';
  315.         }
  316.  
  317.         // SQL server change
  318.         if ($orderCol == 'language')
  319.         {
  320.             $orderCol 'l.title';
  321.         }
  322.  
  323.         if ($orderCol == 'access_level')
  324.         {
  325.             $orderCol 'ag.title';
  326.         }
  327.  
  328.         $query->order($db->escape($orderCol ' ' $orderDirn));
  329.  
  330.         return $query;
  331.     }
  332.  
  333.     /**
  334.      * Build a list of authors
  335.      *
  336.      * @return  JDatabaseQuery 
  337.      *
  338.      * @since   1.6
  339.      */
  340.     public function getAuthors()
  341.     {
  342.         // Create a new query object.
  343.         $db $this->getDbo();
  344.         $query $db->getQuery(true);
  345.  
  346.         // Construct the query
  347.         $query->select('u.id AS value, u.name AS text')
  348.             ->from('#__users AS u')
  349.             ->join('INNER''#__content AS c ON c.created_by = u.id')
  350.             ->group('u.id, u.name')
  351.             ->order('u.name');
  352.  
  353.         // Setup the query
  354.         $db->setQuery($query);
  355.  
  356.         // Return the result
  357.         return $db->loadObjectList();
  358.     }
  359.  
  360.     /**
  361.      * Method to get a list of articles.
  362.      * Overridden to add a check for access levels.
  363.      *
  364.      * @return  mixed  An array of data items on success, false on failure.
  365.      *
  366.      * @since   1.6.1
  367.      */
  368.     public function getItems()
  369.     {
  370.         $items parent::getItems();
  371.  
  372.         if (JFactory::getApplication()->isSite())
  373.         {
  374.             $user JFactory::getUser();
  375.             $groups $user->getAuthorisedViewLevels();
  376.  
  377.             for ($x 0$count count($items)$x $count$x++)
  378.             {
  379.                 // Check the access level. Remove articles the user shouldn't see
  380.                 if (!in_array($items[$x]->access$groups))
  381.                 {
  382.                     unset($items[$x]);
  383.                 }
  384.             }
  385.         }
  386.  
  387.         return $items;
  388.     }
  389. }

Documentation generated on Tue, 19 Nov 2013 14:54:01 +0100 by phpDocumentor 1.4.3