[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/administrator/components/com_content/models/ -> articles.php (source)

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


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