[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/components/com_content/models/ -> category.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.modellist');
  13  
  14  /**
  15   * This models supports retrieving a category, the articles associated with the category,
  16   * sibling, child and parent categories.
  17   *
  18   * @package        Joomla.Site
  19   * @subpackage    com_content
  20   * @since        1.5
  21   */
  22  class ContentModelCategory extends JModelList
  23  {
  24      /**
  25       * Category items data
  26       *
  27       * @var array
  28       */
  29      protected $_item = null;
  30  
  31      protected $_articles = null;
  32  
  33      protected $_siblings = null;
  34  
  35      protected $_children = null;
  36  
  37      protected $_parent = null;
  38  
  39      /**
  40       * Model context string.
  41       *
  42       * @var        string
  43       */
  44      protected $_context = 'com_content.category';
  45  
  46      /**
  47       * The category that applies.
  48       *
  49       * @access    protected
  50       * @var        object
  51       */
  52      protected $_category = null;
  53  
  54      /**
  55       * The list of other newfeed categories.
  56       *
  57       * @access    protected
  58       * @var        array
  59       */
  60      protected $_categories = null;
  61  
  62      /**
  63       * Constructor.
  64       *
  65       * @param    array    An optional associative array of configuration settings.
  66       * @see        JController
  67       * @since    1.6
  68       */
  69  	public function __construct($config = array())
  70      {
  71          if (empty($config['filter_fields'])) {
  72              $config['filter_fields'] = array(
  73                  'id', 'a.id',
  74                  'title', 'a.title',
  75                  'alias', 'a.alias',
  76                  'checked_out', 'a.checked_out',
  77                  'checked_out_time', 'a.checked_out_time',
  78                  'catid', 'a.catid', 'category_title',
  79                  'state', 'a.state',
  80                  'access', 'a.access', 'access_level',
  81                  'created', 'a.created',
  82                  'created_by', 'a.created_by',
  83                  'modified', 'a.modified',
  84                  'ordering', 'a.ordering',
  85                  'featured', 'a.featured',
  86                  'language', 'a.language',
  87                  'hits', 'a.hits',
  88                  'publish_up', 'a.publish_up',
  89                  'publish_down', 'a.publish_down',
  90                  'author', 'a.author'
  91              );
  92          }
  93  
  94          parent::__construct($config);
  95      }
  96  
  97      /**
  98       * Method to auto-populate the model state.
  99       *
 100       * Note. Calling getState in this method will result in recursion.
 101       *
 102       * return    void
 103       * @since    1.6
 104       */
 105  	protected function populateState($ordering = null, $direction = null)
 106      {
 107          // Initiliase variables.
 108          $app    = JFactory::getApplication('site');
 109          $pk        = JRequest::getInt('id');
 110  
 111          $this->setState('category.id', $pk);
 112  
 113          // Load the parameters. Merge Global and Menu Item params into new object
 114          $params = $app->getParams();
 115          $menuParams = new JRegistry;
 116  
 117          if ($menu = $app->getMenu()->getActive()) {
 118              $menuParams->loadString($menu->params);
 119          }
 120  
 121          $mergedParams = clone $menuParams;
 122          $mergedParams->merge($params);
 123  
 124          $this->setState('params', $mergedParams);
 125          $user        = JFactory::getUser();
 126                  // Create a new query object.
 127          $db        = $this->getDbo();
 128          $query    = $db->getQuery(true);
 129          $groups    = implode(',', $user->getAuthorisedViewLevels());
 130  
 131          if ((!$user->authorise('core.edit.state', 'com_content')) &&  (!$user->authorise('core.edit', 'com_content'))){
 132              // limit to published for people who can't edit or edit.state.
 133              $this->setState('filter.published', 1);
 134              // Filter by start and end dates.
 135              $nullDate = $db->Quote($db->getNullDate());
 136              $nowDate = $db->Quote(JFactory::getDate()->toSQL());
 137  
 138              $query->where('(a.publish_up = ' . $nullDate . ' OR a.publish_up <= ' . $nowDate . ')');
 139              $query->where('(a.publish_down = ' . $nullDate . ' OR a.publish_down >= ' . $nowDate . ')');
 140          }
 141          else {
 142              $this->setState('filter.published', array(0, 1, 2));
 143          }
 144  
 145          // process show_noauth parameter
 146          if (!$params->get('show_noauth')) {
 147              $this->setState('filter.access', true);
 148          }
 149          else {
 150              $this->setState('filter.access', false);
 151          }
 152  
 153          // Optional filter text
 154          $this->setState('list.filter', JRequest::getString('filter-search'));
 155  
 156          // filter.order
 157          $itemid = JRequest::getInt('id', 0) . ':' . JRequest::getInt('Itemid', 0);
 158          $orderCol = $app->getUserStateFromRequest('com_content.category.list.' . $itemid . '.filter_order', 'filter_order', '', 'string');
 159          if (!in_array($orderCol, $this->filter_fields)) {
 160              $orderCol = 'a.ordering';
 161          }
 162          $this->setState('list.ordering', $orderCol);
 163  
 164          $listOrder = $app->getUserStateFromRequest('com_content.category.list.' . $itemid . '.filter_order_Dir',
 165              'filter_order_Dir', '', 'cmd');
 166          if (!in_array(strtoupper($listOrder), array('ASC', 'DESC', ''))) {
 167              $listOrder = 'ASC';
 168          }
 169          $this->setState('list.direction', $listOrder);
 170  
 171          $this->setState('list.start', JRequest::getVar('limitstart', 0, '', 'int'));
 172  
 173          // set limit for query. If list, use parameter. If blog, add blog parameters for limit.
 174          if ((JRequest::getCmd('layout') == 'blog') || $params->get('layout_type') == 'blog') {
 175              $limit = $params->get('num_leading_articles') + $params->get('num_intro_articles') + $params->get('num_links');
 176              $this->setState('list.links', $params->get('num_links'));
 177          }
 178          else {
 179              $limit = $app->getUserStateFromRequest('com_content.category.list.' . $itemid . '.limit', 'limit', $params->get('display_num'));
 180          }
 181  
 182          $this->setState('list.limit', $limit);
 183  
 184          // set the depth of the category query based on parameter
 185          $showSubcategories = $params->get('show_subcategory_content', '0');
 186  
 187          if ($showSubcategories) {
 188              $this->setState('filter.max_category_levels', $params->get('show_subcategory_content', '1'));
 189              $this->setState('filter.subcategories', true);
 190          }
 191  
 192  
 193  
 194          $this->setState('filter.language', $app->getLanguageFilter());
 195  
 196          $this->setState('layout', JRequest::getCmd('layout'));
 197  
 198      }
 199  
 200      /**
 201       * Get the articles in the category
 202       *
 203       * @return    mixed    An array of articles or false if an error occurs.
 204       * @since    1.5
 205       */
 206  	function getItems()
 207      {
 208          $params = $this->getState()->get('params');
 209          $limit = $this->getState('list.limit');
 210  
 211          if ($this->_articles === null && $category = $this->getCategory()) {
 212              $model = JModel::getInstance('Articles', 'ContentModel', array('ignore_request' => true));
 213              $model->setState('params', JFactory::getApplication()->getParams());
 214              $model->setState('filter.category_id', $category->id);
 215              $model->setState('filter.published', $this->getState('filter.published'));
 216              $model->setState('filter.access', $this->getState('filter.access'));
 217              $model->setState('filter.language', $this->getState('filter.language'));
 218              $model->setState('list.ordering', $this->_buildContentOrderBy());
 219              $model->setState('list.start', $this->getState('list.start'));
 220              $model->setState('list.limit', $limit);
 221              $model->setState('list.direction', $this->getState('list.direction'));
 222              $model->setState('list.filter', $this->getState('list.filter'));
 223              // filter.subcategories indicates whether to include articles from subcategories in the list or blog
 224              $model->setState('filter.subcategories', $this->getState('filter.subcategories'));
 225              $model->setState('filter.max_category_levels', $this->setState('filter.max_category_levels'));
 226              $model->setState('list.links', $this->getState('list.links'));
 227  
 228              if ($limit >= 0) {
 229                  $this->_articles = $model->getItems();
 230  
 231                  if ($this->_articles === false) {
 232                      $this->setError($model->getError());
 233                  }
 234              }
 235              else {
 236                  $this->_articles=array();
 237              }
 238  
 239              $this->_pagination = $model->getPagination();
 240          }
 241  
 242          return $this->_articles;
 243      }
 244  
 245      /**
 246       * Build the orderby for the query
 247       *
 248       * @return    string    $orderby portion of query
 249       * @since    1.5
 250       */
 251  	protected function _buildContentOrderBy()
 252      {
 253          $app        = JFactory::getApplication('site');
 254          $db            = $this->getDbo();
 255          $params        = $this->state->params;
 256          $itemid        = JRequest::getInt('id', 0) . ':' . JRequest::getInt('Itemid', 0);
 257          $orderCol    = $app->getUserStateFromRequest('com_content.category.list.' . $itemid . '.filter_order', 'filter_order', '', 'string');
 258          $orderDirn    = $app->getUserStateFromRequest('com_content.category.list.' . $itemid . '.filter_order_Dir', 'filter_order_Dir', '', 'cmd');
 259          $orderby    = ' ';
 260  
 261          if (!in_array($orderCol, $this->filter_fields)) {
 262              $orderCol = null;
 263          }
 264  
 265          if (!in_array(strtoupper($orderDirn), array('ASC', 'DESC', ''))) {
 266              $orderDirn = 'ASC';
 267          }
 268  
 269          if ($orderCol && $orderDirn) {
 270              $orderby .= $db->escape($orderCol) . ' ' . $db->escape($orderDirn) . ', ';
 271          }
 272  
 273          $articleOrderby        = $params->get('orderby_sec', 'rdate');
 274          $articleOrderDate    = $params->get('order_date');
 275          $categoryOrderby    = $params->def('orderby_pri', '');
 276          $secondary            = ContentHelperQuery::orderbySecondary($articleOrderby, $articleOrderDate) . ', ';
 277          $primary            = ContentHelperQuery::orderbyPrimary($categoryOrderby);
 278  
 279          $orderby .= $db->escape($primary) . ' ' . $db->escape($secondary) . ' a.created ';
 280  
 281          return $orderby;
 282      }
 283  
 284  	public function getPagination()
 285      {
 286          if (empty($this->_pagination)) {
 287              return null;
 288          }
 289          return $this->_pagination;
 290      }
 291  
 292      /**
 293       * Method to get category data for the current category
 294       *
 295       * @param    int        An optional ID
 296       *
 297       * @return    object
 298       * @since    1.5
 299       */
 300  	public function getCategory()
 301      {
 302          if (!is_object($this->_item)) {
 303              if( isset( $this->state->params ) ) {
 304                  $params = $this->state->params;
 305                  $options = array();
 306                  $options['countItems'] = $params->get('show_cat_num_articles', 1) || !$params->get('show_empty_categories_cat', 0);
 307              }
 308              else {
 309                  $options['countItems'] = 0;
 310              }
 311  
 312              $categories = JCategories::getInstance('Content', $options);
 313              $this->_item = $categories->get($this->getState('category.id', 'root'));
 314  
 315              // Compute selected asset permissions.
 316              if (is_object($this->_item)) {
 317                  $user    = JFactory::getUser();
 318                  $userId    = $user->get('id');
 319                  $asset    = 'com_content.category.'.$this->_item->id;
 320  
 321                  // Check general create permission.
 322                  if ($user->authorise('core.create', $asset)) {
 323                      $this->_item->getParams()->set('access-create', true);
 324                  }
 325  
 326                  // TODO: Why aren't we lazy loading the children and siblings?
 327                  $this->_children = $this->_item->getChildren();
 328                  $this->_parent = false;
 329  
 330                  if ($this->_item->getParent()) {
 331                      $this->_parent = $this->_item->getParent();
 332                  }
 333  
 334                  $this->_rightsibling = $this->_item->getSibling();
 335                  $this->_leftsibling = $this->_item->getSibling(false);
 336              }
 337              else {
 338                  $this->_children = false;
 339                  $this->_parent = false;
 340              }
 341          }
 342  
 343          return $this->_item;
 344      }
 345  
 346      /**
 347       * Get the parent categorie.
 348       *
 349       * @param    int        An optional category id. If not supplied, the model state 'category.id' will be used.
 350       *
 351       * @return    mixed    An array of categories or false if an error occurs.
 352       * @since    1.6
 353       */
 354  	public function getParent()
 355      {
 356          if (!is_object($this->_item)) {
 357              $this->getCategory();
 358          }
 359  
 360          return $this->_parent;
 361      }
 362  
 363      /**
 364       * Get the left sibling (adjacent) categories.
 365       *
 366       * @return    mixed    An array of categories or false if an error occurs.
 367       * @since    1.6
 368       */
 369      function &getLeftSibling()
 370      {
 371          if (!is_object($this->_item)) {
 372              $this->getCategory();
 373          }
 374  
 375          return $this->_leftsibling;
 376      }
 377  
 378      /**
 379       * Get the right sibling (adjacent) categories.
 380       *
 381       * @return    mixed    An array of categories or false if an error occurs.
 382       * @since    1.6
 383       */
 384      function &getRightSibling()
 385      {
 386          if (!is_object($this->_item)) {
 387              $this->getCategory();
 388          }
 389  
 390          return $this->_rightsibling;
 391      }
 392  
 393      /**
 394       * Get the child categories.
 395       *
 396       * @param    int        An optional category id. If not supplied, the model state 'category.id' will be used.
 397       *
 398       * @return    mixed    An array of categories or false if an error occurs.
 399       * @since    1.6
 400       */
 401      function &getChildren()
 402      {
 403          if (!is_object($this->_item)) {
 404              $this->getCategory();
 405          }
 406  
 407          // Order subcategories
 408          if (sizeof($this->_children)) {
 409              $params = $this->getState()->get('params');
 410              if ($params->get('orderby_pri') == 'alpha' || $params->get('orderby_pri') == 'ralpha') {
 411                  jimport('joomla.utilities.arrayhelper');
 412                  JArrayHelper::sortObjects($this->_children, 'title', ($params->get('orderby_pri') == 'alpha') ? 1 : -1);
 413              }
 414          }
 415  
 416          return $this->_children;
 417      }
 418  }


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