Source for file article.php

Documentation is available at article.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Site
  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.  * Content Component Article Model
  14.  *
  15.  * @package     Joomla.Site
  16.  * @subpackage  com_content
  17.  * @since       1.5
  18.  */
  19. {
  20.     /**
  21.      * Model context string.
  22.      *
  23.      * @var        string 
  24.      */
  25.     protected $_context = 'com_content.article';
  26.  
  27.     /**
  28.      * Method to auto-populate the model state.
  29.      *
  30.      * Note. Calling getState in this method will result in recursion.
  31.      *
  32.      * @since   1.6
  33.      *
  34.      * @return void 
  35.      */
  36.     protected function populateState()
  37.     {
  38.         $app JFactory::getApplication('site');
  39.  
  40.         // Load state from the request.
  41.         $pk $app->input->getInt('id');
  42.         $this->setState('article.id'$pk);
  43.  
  44.         $offset $app->input->getUInt('limitstart');
  45.         $this->setState('list.offset'$offset);
  46.  
  47.         // Load the parameters.
  48.         $params $app->getParams();
  49.         $this->setState('params'$params);
  50.  
  51.         // TODO: Tune these values based on other permissions.
  52.         $user JFactory::getUser();
  53.  
  54.         if ((!$user->authorise('core.edit.state''com_content')) && (!$user->authorise('core.edit''com_content')))
  55.         {
  56.             $this->setState('filter.published'1);
  57.             $this->setState('filter.archived'2);
  58.         }
  59.  
  60.         $this->setState('filter.language'JLanguageMultilang::isEnabled());
  61.     }
  62.  
  63.     /**
  64.      * Method to get article data.
  65.      *
  66.      * @param   integer  $pk  The id of the article.
  67.      *
  68.      * @return  mixed  Menu item data object on success, false on failure.
  69.      */
  70.     public function getItem($pk null)
  71.     {
  72.         $pk (!empty($pk)) $pk : (int) $this->getState('article.id');
  73.  
  74.         if ($this->_item === null)
  75.         {
  76.             $this->_item = array();
  77.         }
  78.  
  79.         if (!isset($this->_item[$pk]))
  80.         {
  81.             try
  82.             {
  83.                 $db $this->getDbo();
  84.                 $query $db->getQuery(true)
  85.                     ->select(
  86.                         $this->getState(
  87.                             'item.select''a.id, a.asset_id, a.title, a.alias, a.introtext, a.fulltext, ' .
  88.                             // If badcats is not null, this means that the article is inside an unpublished category
  89.                             // In this case, the state is set to 0 to indicate Unpublished (even if the article state is Published)
  90.                             'CASE WHEN badcats.id is null THEN a.state ELSE 0 END AS state, ' .
  91.                             'a.catid, a.created, a.created_by, a.created_by_alias, ' .
  92.                             // Use created if modified is 0
  93.                             'CASE WHEN a.modified = ' $db->quote($db->getNullDate()) ' THEN a.created ELSE a.modified END as modified, ' .
  94.                             'a.modified_by, a.checked_out, a.checked_out_time, a.publish_up, a.publish_down, ' .
  95.                             'a.images, a.urls, a.attribs, a.version, a.ordering, ' .
  96.                             'a.metakey, a.metadesc, a.access, a.hits, a.metadata, a.featured, a.language, a.xreference'
  97.                         )
  98.                     );
  99.                 $query->from('#__content AS a');
  100.  
  101.                 // Join on category table.
  102.                 $query->select('c.title AS category_title, c.alias AS category_alias, c.access AS category_access')
  103.                     ->join('LEFT''#__categories AS c on c.id = a.catid');
  104.  
  105.                 // Join on user table.
  106.                 $query->select('u.name AS author')
  107.                     ->join('LEFT''#__users AS u on u.id = a.created_by');
  108.  
  109.                 // Get contact id
  110.                 $subQuery $db->getQuery(true)
  111.                     ->select('MAX(contact.id) AS id')
  112.                     ->from('#__contact_details AS contact')
  113.                     ->where('contact.published = 1')
  114.                     ->where('contact.user_id = a.created_by');
  115.  
  116.                     // Filter by language
  117.                     if ($this->getState('filter.language'))
  118.                     {
  119.                         $subQuery->where('(contact.language in (' $db->quote(JFactory::getLanguage()->getTag()) ',' $db->quote('*'') OR contact.language IS NULL)');
  120.                     }
  121.  
  122.                 $query->select('(' $subQuery ') as contactid');
  123.  
  124.                 // Filter by language
  125.                 if ($this->getState('filter.language'))
  126.                 {
  127.                     $query->where('a.language in (' $db->quote(JFactory::getLanguage()->getTag()) ',' $db->quote('*'')');
  128.                 }
  129.  
  130.                 // Join over the categories to get parent category titles
  131.                 $query->select('parent.title as parent_title, parent.id as parent_id, parent.path as parent_route, parent.alias as parent_alias')
  132.                     ->join('LEFT''#__categories as parent ON parent.id = c.parent_id');
  133.  
  134.                 // Join on voting table
  135.                 $query->select('ROUND(v.rating_sum / v.rating_count, 0) AS rating, v.rating_count as rating_count')
  136.                     ->join('LEFT''#__content_rating AS v ON a.id = v.content_id')
  137.  
  138.                     ->where('a.id = ' . (int) $pk);
  139.  
  140.                 // Filter by start and end dates.
  141.                 $nullDate $db->quote($db->getNullDate());
  142.                 $date JFactory::getDate();
  143.  
  144.                 $nowDate $db->quote($date->toSql());
  145.  
  146.                 $query->where('(a.publish_up = ' $nullDate ' OR a.publish_up <= ' $nowDate ')')
  147.                     ->where('(a.publish_down = ' $nullDate ' OR a.publish_down >= ' $nowDate ')');
  148.  
  149.                 // Join to check for category published state in parent categories up the tree
  150.                 // If all categories are published, badcats.id will be null, and we just use the article state
  151.                 $subquery ' (SELECT cat.id as id FROM #__categories AS cat JOIN #__categories AS parent ';
  152.                 $subquery .= 'ON cat.lft BETWEEN parent.lft AND parent.rgt ';
  153.                 $subquery .= 'WHERE parent.extension = ' $db->quote('com_content');
  154.                 $subquery .= ' AND parent.published <= 0 GROUP BY cat.id)';
  155.                 $query->join('LEFT OUTER'$subquery ' AS badcats ON badcats.id = c.id');
  156.  
  157.                 // Filter by published state.
  158.                 $published $this->getState('filter.published');
  159.                 $archived $this->getState('filter.archived');
  160.  
  161.                 if (is_numeric($published))
  162.                 {
  163.                     $query->where('(a.state = ' . (int) $published ' OR a.state =' . (int) $archived ')');
  164.                 }
  165.  
  166.                 $db->setQuery($query);
  167.  
  168.                 $data $db->loadObject();
  169.  
  170.                 if (empty($data))
  171.                 {
  172.                     return JError::raiseError(404JText::_('COM_CONTENT_ERROR_ARTICLE_NOT_FOUND'));
  173.                 }
  174.  
  175.                 // Check for published state if filter set.
  176.                 if (((is_numeric($published)) || (is_numeric($archived))) && (($data->state != $published&& ($data->state != $archived)))
  177.                 {
  178.                     return JError::raiseError(404JText::_('COM_CONTENT_ERROR_ARTICLE_NOT_FOUND'));
  179.                 }
  180.  
  181.                 // Convert parameter fields to objects.
  182.                 $registry new JRegistry;
  183.                 $registry->loadString($data->attribs);
  184.  
  185.                 $data->params clone $this->getState('params');
  186.                 $data->params->merge($registry);
  187.  
  188.                 $registry new JRegistry;
  189.                 $registry->loadString($data->metadata);
  190.                 $data->metadata $registry;
  191.  
  192.                 // Compute selected asset permissions.
  193.                 $user JFactory::getUser();
  194.  
  195.                 // Technically guest could edit an article, but lets not check that to improve performance a little.
  196.                 if (!$user->get('guest'))
  197.                 {
  198.                     $userId $user->get('id');
  199.                     $asset 'com_content.article.' $data->id;
  200.  
  201.                     // Check general edit permission first.
  202.                     if ($user->authorise('core.edit'$asset))
  203.                     {
  204.                         $data->params->set('access-edit'true);
  205.                     }
  206.  
  207.                     // Now check if edit.own is available.
  208.                     elseif (!empty($userId&& $user->authorise('core.edit.own'$asset))
  209.                     {
  210.                         // Check for a valid user and that they are the owner.
  211.                         if ($userId == $data->created_by)
  212.                         {
  213.                             $data->params->set('access-edit'true);
  214.                         }
  215.                     }
  216.                 }
  217.  
  218.                 // Compute view access permissions.
  219.                 if ($access $this->getState('filter.access'))
  220.                 {
  221.                     // If the access filter has been set, we already know this user can view.
  222.                     $data->params->set('access-view'true);
  223.                 }
  224.                 else
  225.                 {
  226.                     // If no access filter is set, the layout takes some responsibility for display of limited information.
  227.                     $user JFactory::getUser();
  228.                     $groups $user->getAuthorisedViewLevels();
  229.  
  230.                     if ($data->catid == || $data->category_access === null)
  231.                     {
  232.                         $data->params->set('access-view'in_array($data->access$groups));
  233.                     }
  234.                     else
  235.                     {
  236.                         $data->params->set('access-view'in_array($data->access$groups&& in_array($data->category_access$groups));
  237.                     }
  238.                 }
  239.  
  240.                 $this->_item[$pk$data;
  241.             }
  242.             catch (Exception $e)
  243.             {
  244.                 if ($e->getCode(== 404)
  245.                 {
  246.                     // Need to go thru the error handler to allow Redirect to work.
  247.                     JError::raiseError(404$e->getMessage());
  248.                 }
  249.                 else
  250.                 {
  251.                     $this->setError($e);
  252.                     $this->_item[$pkfalse;
  253.                 }
  254.             }
  255.         }
  256.  
  257.         return $this->_item[$pk];
  258.     }
  259.  
  260.     /**
  261.      * Increment the hit counter for the article.
  262.      *
  263.      * @param   integer  $pk  Optional primary key of the article to increment.
  264.      *
  265.      * @return  boolean  True if successful; false otherwise and internal error set.
  266.      */
  267.     public function hit($pk 0)
  268.     {
  269.         $input JFactory::getApplication()->input;
  270.         $hitcount $input->getInt('hitcount'1);
  271.  
  272.         if ($hitcount)
  273.         {
  274.             $pk (!empty($pk)) $pk : (int) $this->getState('article.id');
  275.  
  276.             $table JTable::getInstance('Content''JTable');
  277.             $table->load($pk);
  278.             $table->hit($pk);
  279.         }
  280.  
  281.         return true;
  282.     }
  283.  
  284.     /**
  285.      * Save user vote on article
  286.      *
  287.      * @param   integer  $pk    Joomla Article Id
  288.      * @param   integer  $rate  Voting rate
  289.      *
  290.      * @return  boolean          Return true on success
  291.      */
  292.     public function storeVote($pk 0$rate 0)
  293.     {
  294.         if ($rate >= && $rate <= && $pk 0)
  295.         {
  296.             $userIP $_SERVER['REMOTE_ADDR'];
  297.  
  298.             // Initialize variables.
  299.             $db    JFactory::getDbo();
  300.             $query $db->getQuery(true);
  301.  
  302.             // Create the base select statement.
  303.             $query->select('*')
  304.                 ->from($db->quoteName('#__content_rating'))
  305.                 ->where($db->quoteName('content_id'' = ' . (int) $pk);
  306.  
  307.             // Set the query and load the result.
  308.             $db->setQuery($query);
  309.             $rating $db->loadObject();
  310.  
  311.             // Check for a database error.
  312.             if ($db->getErrorNum())
  313.             {
  314.                 JError::raiseWarning(500$db->getErrorMsg());
  315.  
  316.                 return false;
  317.             }
  318.  
  319.             // There are no ratings yet, so lets insert our rating
  320.             if (!$rating)
  321.             {
  322.                 $query $db->getQuery(true);
  323.  
  324.                 // Create the base insert statement.
  325.                 $query->insert($db->quoteName('#__content_rating'))
  326.                     ->columns(array($db->quoteName('content_id')$db->quoteName('lastip')$db->quoteName('rating_sum')$db->quoteName('rating_count')))
  327.                     ->values((int) $pk ', ' $db->quote($userIP',' . (int) $rate ', 1');
  328.  
  329.                 // Set the query and execute the insert.
  330.                 $db->setQuery($query);
  331.  
  332.                 try
  333.                 {
  334.                     $db->execute();
  335.                 }
  336.                 catch (RuntimeException $e)
  337.                 {
  338.                     JError::raiseWarning(500$e->getMessage());
  339.  
  340.                     return false;
  341.                 }
  342.             }
  343.             else
  344.             {
  345.                 if ($userIP != ($rating->lastip))
  346.                 {
  347.                     $query $db->getQuery(true);
  348.  
  349.                     // Create the base update statement.
  350.                     $query->update($db->quoteName('#__content_rating'))
  351.                         ->set($db->quoteName('rating_count'' = rating_count + 1')
  352.                         ->set($db->quoteName('rating_sum'' = rating_sum + ' . (int) $rate)
  353.                         ->set($db->quoteName('lastip'' = ' $db->quote($userIP))
  354.                         ->where($db->quoteName('content_id'' = ' . (int) $pk);
  355.  
  356.                     // Set the query and execute the update.
  357.                     $db->setQuery($query);
  358.  
  359.                     try
  360.                     {
  361.                         $db->execute();
  362.                     }
  363.                     catch (RuntimeException $e)
  364.                     {
  365.                         JError::raiseWarning(500$e->getMessage());
  366.  
  367.                         return false;
  368.                     }
  369.                 }
  370.                 else
  371.                 {
  372.                     return false;
  373.                 }
  374.             }
  375.  
  376.             return true;
  377.         }
  378.  
  379.         JError::raiseWarning('SOME_ERROR_CODE'JText::sprintf('COM_CONTENT_INVALID_RATING'$rate)"JModelArticle::storeVote($rate)");
  380.  
  381.         return false;
  382.     }
  383. }

Documentation generated on Tue, 19 Nov 2013 14:53:52 +0100 by phpDocumentor 1.4.3