[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

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

   1  <?php
   2  /**
   3   * @version        $Id: category.php 19340 2010-11-03 15:00:55Z ian $
   4   * @package        Joomla
   5   * @subpackage    Content
   6   * @copyright    Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
   7   * @license        GNU/GPL, see LICENSE.php
   8   * Joomla! is free software. This version may have been modified pursuant to the
   9   * GNU General Public License, and as distributed it includes or is derivative
  10   * of works licensed under the GNU General Public License or other free or open
  11   * source software licenses. See COPYRIGHT.php for copyright notices and
  12   * details.
  13   */
  14  
  15  // Check to ensure this file is included in Joomla!
  16  defined('_JEXEC') or die( 'Restricted access' );
  17  
  18  jimport('joomla.application.component.model');
  19  
  20  /**
  21   * Content Component Category Model
  22   *
  23   * @package        Joomla
  24   * @subpackage    Content
  25   * @since 1.5
  26   */
  27  class ContentModelCategory extends JModel
  28  {
  29      /**
  30       * Category id
  31       *
  32       * @var int
  33       */
  34      var $_id = null;
  35  
  36      /**
  37       * Category items data
  38       *
  39       * @var array
  40       */
  41      var $_data = null;
  42  
  43      /**
  44       * Category number items
  45       *
  46       * @var integer
  47       */
  48      var $_total = null;
  49  
  50      /**
  51       * Category data
  52       *
  53       * @var object
  54       */
  55      var $_category = null;
  56  
  57      /**
  58       * Category data
  59       *
  60       * @var array
  61       */
  62      var $_siblings = null;
  63  
  64      /**
  65       * Constructor
  66       *
  67       * @since 1.5
  68       */
  69  	function __construct()
  70      {
  71          parent::__construct();
  72  
  73          global $mainframe;
  74  
  75          $id = JRequest::getVar('id', 0, '', 'int');
  76          $this->setId((int)$id);
  77  
  78          // here we initialize defaults for category model
  79          $params = &$mainframe->getParams();
  80          $params->def('filter',                    1);
  81          $params->def('filter_type',                'title');
  82      }
  83  
  84      /**
  85       * Method to set the category id
  86       *
  87       * @access    public
  88       * @param    int    Category ID number
  89       */
  90  	function setId($id)
  91      {
  92          // Set category ID and wipe data
  93          $this->_id            = $id;
  94          $this->_category    = null;
  95          $this->_siblings    = null;
  96          $this->_data        = array();
  97          $this->_total        = null;
  98      }
  99  
 100      /**
 101       * Method to get content item data for the current category
 102       *
 103       * @param    int    $state    The content state to pull from for the current
 104       * category
 105       * @since 1.5
 106       */
 107  	function getData($state = 1)
 108      {
 109          // Load the Category data
 110          if ($this->_loadCategory() && $this->_loadData($state))
 111          {
 112              // Initialize some variables
 113              $user    =& JFactory::getUser();
 114  
 115              // Make sure the category is published
 116              if (!$this->_category->published)
 117              {
 118                  JError::raiseError(404, JText::_("Resource Not Found"));
 119                  return false;
 120              }
 121  
 122              // check whether category access level allows access
 123              if ($this->_category->access > $user->get('aid', 0))
 124              {
 125                  JError::raiseError(403, JText::_("ALERTNOTAUTH"));
 126                  return false;
 127              }
 128          }
 129          return $this->_data[$state];
 130      }
 131  
 132      /**
 133       * Method to get the total number of content items for the frontpage
 134       *
 135       * @access public
 136       * @return integer
 137       */
 138  	function getTotal($state = 1)
 139      {
 140          // Lets load the content if it doesn't already exist
 141          if (empty($this->_total))
 142          {
 143              $query = $this->_buildQuery($state);
 144              $this->_total[$state] = $this->_getListCount($query);
 145          }
 146  
 147          return $this->_total[$state];
 148      }
 149  
 150      /**
 151       * Method to get category data for the current category
 152       *
 153       * @since 1.5
 154       */
 155  	function getCategory()
 156      {
 157          // Load the Category data
 158          if ($this->_loadCategory())
 159          {
 160              // Initialize some variables
 161              $user = &JFactory::getUser();
 162  
 163              // Make sure the category is published
 164              if (!$this->_category->published) {
 165                  JError::raiseError(404, JText::_("Resource Not Found"));
 166                  return false;
 167              }
 168              // check whether category access level allows access
 169              if ($this->_category->access > $user->get('aid', 0)) {
 170                  JError::raiseError(403, JText::_("ALERTNOTAUTH"));
 171                  return false;
 172              }
 173          }
 174          return $this->_category;
 175      }
 176  
 177      /**
 178       * Method to get sibling category data for the current category
 179       *
 180       * @since 1.5
 181       */
 182  	function getSiblings()
 183      {
 184          // Initialize some variables
 185          $user    =& JFactory::getUser();
 186  
 187          // Load the Category data
 188          if ($this->_loadCategory() && $this->_loadSiblings())
 189          {
 190              // Make sure the category is published
 191              if (!$this->_category->published)
 192              {
 193                  JError::raiseError(404, JText::_("Resource Not Found"));
 194                  return false;
 195              }
 196  
 197              // check whether category access level allows access
 198              if ($this->_category->access > $user->get('aid', 0))
 199              {
 200                  JError::raiseError(403, JText::_("ALERTNOTAUTH"));
 201                  return false;
 202              }
 203          }
 204          return $this->_siblings;
 205      }
 206  
 207      /**
 208       * Method to get archived article data for the current category
 209       *
 210       * @param    int    $state    The content state to pull from for the current section
 211       * @since 1.5
 212       */
 213  	function getArchives($state = -1)
 214      {
 215          return $this->getContent(-1);
 216      }
 217  
 218      /**
 219       * Method to load category data if it doesn't exist.
 220       *
 221       * @access    private
 222       * @return    boolean    True on success
 223       */
 224  	function _loadCategory()
 225      {
 226          if (empty($this->_category))
 227          {
 228              // Lets get the information for the current category
 229              $query = 'SELECT c.*, s.id as sectionid, s.title as sectiontitle,' .
 230                      ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(":", c.id, c.alias) ELSE c.id END as slug'.
 231                      ' FROM #__categories AS c' .
 232                      ' INNER JOIN #__sections AS s ON s.id = c.section' .
 233                      ' WHERE c.id = '. (int) $this->_id;
 234              $this->_db->setQuery($query, 0, 1);
 235              $this->_category = $this->_db->loadObject();
 236          }
 237          return true;
 238      }
 239  
 240      /**
 241       * Method to load sibling category data if it doesn't exist.
 242       *
 243       * @access    private
 244       * @return    boolean    True on success
 245       */
 246  	function _loadSiblings()
 247      {
 248          global $mainframe;
 249  
 250          if (empty($this->_category))
 251          {
 252              return false; // TODO: set error -- can't get siblings when we don't know the category
 253          }
 254  
 255          // Lets load the siblings if they don't already exist
 256          if (empty($this->_siblings))
 257          {
 258              $user     =& JFactory::getUser();
 259  
 260              // Get the page/component configuration
 261              $params = &$mainframe->getParams();
 262  
 263              $noauth     = !$params->get('show_noauth');
 264              $gid         = (int) $user->get('aid', 0);
 265              $now         = $mainframe->get('requestTime');
 266              $nullDate = $this->_db->getNullDate();
 267              $section     = $this->_category->section;
 268  
 269              // Get the parameters of the active menu item
 270              $menu    =& JSite::getMenu();
 271              $item    = $menu->getActive();
 272              $params    =& $menu->getParams($item->id);
 273  
 274              if ($user->authorize('com_content', 'edit', 'content', 'all'))
 275              {
 276                  $xwhere = '';
 277                  $xwhere2 = ' AND b.state >= 0';
 278              }
 279              else
 280              {
 281                  $xwhere = ' AND c.published = 1';
 282                  $xwhere2 = ' AND b.state = 1' .
 283                          ' AND ( publish_up = '.$this->_db->Quote($nullDate).' OR publish_up <= '.$this->_db->Quote($now).' )' .
 284                          ' AND ( publish_down = '.$this->_db->Quote($nullDate).' OR publish_down >= '.$this->_db->Quote($now).' )';
 285              }
 286  
 287              // show/hide empty categories
 288              $empty = null;
 289              if (!$params->get('empty_cat'))
 290              {
 291                  $empty = ' HAVING COUNT( b.id ) > 0';
 292              }
 293  
 294              // Get the list of sibling categories [categories with the same parent]
 295              $query = 'SELECT c.*, COUNT( b.id ) AS numitems' .
 296                      ' FROM #__categories AS c' .
 297                      ' LEFT JOIN #__content AS b ON b.catid = c.id '.
 298                      $xwhere2.
 299                      ($noauth ? ' AND b.access <= '. (int) $gid : '') .
 300                      ' WHERE c.section = '. $this->_db->Quote($section).
 301                      $xwhere.
 302                      ($noauth ? ' AND c.access <= '. (int) $gid : '').
 303                      ' GROUP BY c.id'.$empty.
 304                      ' ORDER BY c.ordering';
 305              $this->_db->setQuery($query);
 306              $this->_siblings = $this->_db->loadObjectList();
 307          }
 308          return true;
 309      }
 310  
 311      /**
 312       * Method to load content item data for items in the category if they don't
 313       * exist.
 314       *
 315       * @access    private
 316       * @return    boolean    True on success
 317       */
 318  	function _loadData($state = 1)
 319      {
 320          if (empty($this->_category)) {
 321              return false; // TODO: set error -- can't get siblings when we don't know the category
 322          }
 323  
 324          // Lets load the siblings if they don't already exist
 325          if (empty($this->_data[$state]))
 326          {
 327              // Get the pagination request variables
 328              $limit        = JRequest::getVar('limit', 0, '', 'int');
 329              $limitstart    = JRequest::getVar('limitstart', 0, '', 'int');
 330  
 331              $query = $this->_buildQuery();
 332              $Arows = $this->_getList($query, $limitstart, $limit);
 333              
 334              // Check for db errors
 335              if ($this->_db->getErrorNum())
 336              {
 337                  JError::raiseError(500, $this->_db->stderror());
 338                  return false;
 339              }
 340  
 341              // special handling required as Uncategorized content does not have a section / category id linkage
 342              $i = $limitstart;
 343              $rows = array();
 344              foreach ($Arows as $row)
 345              {
 346                  // check to determine if section or category has proper access rights
 347                  $rows[$i] = $row;
 348                  $i ++;
 349              }
 350              $this->_data[$state] = $rows;
 351          }
 352          return true;
 353      }
 354  
 355  	function _buildQuery($state = 1)
 356      {
 357          global $mainframe;
 358          // Get the page/component configuration
 359          $params = &$mainframe->getParams();
 360  
 361          // If voting is turned on, get voting data as well for the content items
 362          $voting    = ContentHelperQuery::buildVotingQuery($params);
 363  
 364          // Get the WHERE and ORDER BY clauses for the query
 365          $where        = $this->_buildContentWhere($state);
 366          $orderby    = $this->_buildContentOrderBy($state);
 367  
 368          $query = 'SELECT cc.title AS category, a.id, a.title, a.alias, a.title_alias, a.introtext, a.fulltext, a.sectionid, a.state, a.catid, a.created, a.created_by, a.created_by_alias, a.modified, a.modified_by,' .
 369              ' a.checked_out, a.checked_out_time, a.publish_up, a.publish_down, a.attribs, a.hits, a.images, a.urls, a.ordering, a.metakey, a.metadesc, a.access,' .
 370              ' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug,'.
 371              ' CASE WHEN CHAR_LENGTH(cc.alias) THEN CONCAT_WS(":", cc.id, cc.alias) ELSE cc.id END as catslug,'.
 372              ' CHAR_LENGTH( a.`fulltext` ) AS readmore, u.name AS author, u.usertype, g.name AS groups, u.email as author_email'.$voting['select'] .
 373              ' FROM #__content AS a' .
 374              ' LEFT JOIN #__categories AS cc ON a.catid = cc.id' .
 375              ' LEFT JOIN #__users AS u ON u.id = a.created_by' .
 376              ' LEFT JOIN #__groups AS g ON a.access = g.id'.
 377              $voting['join'].
 378              $where.
 379              $orderby;
 380  
 381          return $query;
 382      }
 383  
 384  	function _buildContentOrderBy($state = 1)
 385      {
 386          global $mainframe;
 387          // Get the page/component configuration
 388          $params = &$mainframe->getParams();
 389          $itemid = JRequest::getInt('id', 0) . ':' . JRequest::getInt('Itemid', 0);
 390          $filter_order  = $mainframe->getUserStateFromRequest('com_content.category.list.' . $itemid . '.filter_order', 'filter_order', '', 'cmd');
 391          $filter_order_Dir = $mainframe->getUserStateFromRequest('com_content.category.list.' . $itemid . '.filter_order_Dir', 'filter_order_Dir', '', 'cmd');
 392  
 393          if (!in_array($filter_order, array('a.title', 'author', 'a.hits', 'a.created', 'a.publish_up', 'a.publish_down', 'a.modified'))) {
 394              $filter_order = '';
 395          }
 396  
 397          if (!in_array(strtoupper($filter_order_Dir), array('ASC', 'DESC'))) {
 398              $filter_order_Dir = 'ASC';
 399          }
 400  
 401          $orderby = ' ORDER BY ';
 402          if ($filter_order && $filter_order_Dir)
 403          {
 404              $orderby .= $filter_order .' '. $filter_order_Dir.', ';
 405          }
 406  
 407          if ($filter_order == 'author')
 408          {
 409              $orderby .= 'created_by_alias '. $filter_order_Dir.', ';
 410          }
 411          switch ($state)
 412          {
 413              case -1:
 414                  // Special ordering for archive articles
 415                  $orderby_sec    = $params->def('orderby', 'rdate');
 416                  $secondary        = ContentHelperQuery::orderbySecondary($orderby_sec).', ';
 417                  $primary        = '';
 418                  break;
 419  
 420              case 1:
 421              default:
 422                  $orderby_sec    = $params->def('orderby_sec', 'rdate');
 423                  $orderby_sec    = ($orderby_sec == 'front') ? '' : $orderby_sec;
 424                  $orderby_pri    = $params->def('orderby_pri', '');
 425                  $secondary        = ContentHelperQuery::orderbySecondary($orderby_sec).', ';
 426                  $primary        = ContentHelperQuery::orderbyPrimary($orderby_pri);
 427                  break;
 428          }
 429          $orderby .= $primary .' '. $secondary .' a.created DESC';
 430  
 431          return $orderby;
 432      }
 433  
 434  	function _buildContentWhere($state = 1)
 435      {
 436          global $mainframe;
 437  
 438          $user        =& JFactory::getUser();
 439          $gid        = $user->get('aid', 0);
 440  
 441          $jnow        =& JFactory::getDate();
 442          $now            = $jnow->toMySQL();
 443  
 444          // Get the page/component configuration
 445          $params = &$mainframe->getParams();
 446          $noauth        = !$params->get('show_noauth');
 447          $nullDate    = $this->_db->getNullDate();
 448  
 449          $where = ' WHERE 1';
 450  
 451          // Does the user have access to view the items?
 452          if ($noauth) {
 453              $where .= ' AND a.access <= '.(int) $gid;
 454          }
 455  
 456          // First thing we need to do is assert that the articles are in the current category
 457          if ($this->_id)
 458          {
 459              $where .= ' AND a.catid = '.(int) $this->_id;
 460          }
 461  
 462          // Regular Published Content
 463          switch ($state)
 464          {
 465              case 1:
 466                  if ($user->authorize('com_content', 'edit', 'content', 'all'))
 467                  {
 468                      $where .= ' AND a.state >= 0';
 469                  }
 470                  else
 471                  {
 472                      $where .= ' AND a.state = 1' .
 473                              ' AND ( publish_up = '.$this->_db->Quote($nullDate).' OR publish_up <= '.$this->_db->Quote($now).' )' .
 474                              ' AND ( publish_down = '.$this->_db->Quote($nullDate).' OR publish_down >= '.$this->_db->Quote($now).' )';
 475                  }
 476                  break;
 477  
 478              // Archive Content
 479              case -1:
 480                  // Get some request vars specific to this state
 481                  $year    = JRequest::getInt( 'year', date('Y') );
 482                  $month    = JRequest::getInt( 'month', date('m') );
 483  
 484                  $where .= ' AND a.state = -1';
 485                  $where .= ' AND YEAR( a.created ) = '.(int) $year;
 486                  $where .= ' AND MONTH( a.created ) = '.(int) $month;
 487                  break;
 488  
 489              default:
 490                  $where .= ' AND a.state = '.(int) $state;
 491                  break;
 492          }
 493  
 494          /*
 495           * If we have a filter, and this is enabled... lets tack the AND clause
 496           * for the filter onto the WHERE clause of the content item query.
 497           */
 498          if ($params->get('filter'))
 499          {
 500              $filter = JRequest::getString('filter', '', 'request');
 501              if ($filter)
 502              {
 503                  // clean filter variable
 504                  $filter = JString::strtolower($filter);
 505                  $hitsFilter = intval($filter);
 506                  $filter    = $this->_db->Quote( '%'.$this->_db->getEscaped( $filter, true ).'%', false );
 507  
 508                  switch ($params->get('filter_type'))
 509                  {
 510                      case 'author' :
 511                          $where .= ' AND ( ( LOWER( u.name ) LIKE '.$filter.' ) OR ( LOWER( a.created_by_alias ) LIKE '.$filter.' ) )';
 512                          break;
 513  
 514                      case 'hits' :
 515                          $where .= ' AND a.hits >= '.$hitsFilter. ' ';
 516                          break;
 517  
 518                      case 'title' :
 519                      default : // default to 'title' if parameter is not valid
 520                          $where .= ' AND LOWER( a.title ) LIKE '.$filter;
 521                          break;
 522                  }
 523              }
 524          }
 525          return $where;
 526      }
 527  }


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1