[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/components/com_content/models/ -> article.php (source)

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


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1