[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

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

   1  <?php
   2  /**
   3   * @version        $Id: section.php 21043 2011-03-31 16:01:13Z dextercowley $
   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 Section Model
  22   *
  23   * @package        Joomla
  24   * @subpackage    Content
  25   * @since 1.5
  26   */
  27  class ContentModelSection extends JModel
  28  {
  29      /**
  30       * Category id
  31       *
  32       * @var int
  33       */
  34      var $_id = null;
  35  
  36      /**
  37       * Frontpage data array
  38       *
  39       * @var array
  40       */
  41      var $_data = null;
  42  
  43      /**
  44       * Frontpage total
  45       *
  46       * @var integer
  47       */
  48      var $_total = null;
  49  
  50      /**
  51       * Section data
  52       *
  53       * @var object
  54       */
  55      var $_section = null;
  56  
  57      /**
  58       * Categories data
  59       *
  60       * @var array
  61       */
  62      var $_categories = null;
  63  
  64  
  65      /**
  66       * Constructor
  67       *
  68       * @since 1.5
  69       */
  70  	function __construct( )
  71      {
  72          parent::__construct();
  73  
  74          $id = JRequest::getVar('id', 0, '', 'int');
  75          $this->setId((int)$id);
  76      }
  77  
  78      /**
  79       * Method to set the section id
  80       *
  81       * @access    public
  82       * @param    int    Section ID number
  83       */
  84  	function setId($id)
  85      {
  86          // Set new ID and wipe data
  87          $this->_id            = $id;
  88          $this->_data        = array();
  89          $this->_total         = null;
  90          $this->_section        = null;
  91          $this->_categories    = null;
  92  
  93      }
  94  
  95      /**
  96       * Method to get content item data for the section
  97       *
  98       * @param    int    $state    The content state to pull from for the current
  99       * section
 100       * @since 1.5
 101       */
 102  	function getData($state = 1)
 103      {
 104          // Load the Category data
 105          if ($this->_loadSection() && $this->_loadData($state))
 106          {
 107              // Initialize some variables
 108              $user    =& JFactory::getUser();
 109  
 110              // Make sure the category is published
 111              if (!$this->_section->published) {
 112                  JError::raiseError(404, JText::_("Resource Not Found"));
 113                  return false;
 114              }
 115  
 116              // check whether category access level allows access
 117              if ($this->_section->access > $user->get('aid', 0)) {
 118                  JError::raiseError(403, JText::_("ALERTNOTAUTH"));
 119                  return false;
 120              }
 121          }
 122          return $this->_data[$state];
 123      }
 124  
 125      /**
 126       * Method to get the total number of content items for the section
 127       *
 128       * @access public
 129       * @return integer
 130       */
 131  	function getTotal($state = 1)
 132      {
 133          // Lets load the content if it doesn't already exist
 134          if (empty($this->_total))
 135          {
 136              $query = $this->_buildQuery($state);
 137              $this->_total[$state] = $this->_getListCount($query);
 138          }
 139  
 140          return $this->_total[$state];
 141      }
 142  
 143      /**
 144       * Method to get section data for the current section
 145       *
 146       * @since 1.5
 147       */
 148  	function getSection()
 149      {
 150          // Initialize some variables
 151          $user    =& JFactory::getUser();
 152  
 153          // Load the Category data
 154          if ($this->_loadSection())
 155          {
 156              // Make sure the category is published
 157              if (!$this->_section->published) {
 158                  JError::raiseError(404, JText::_("Resource Not Found"));
 159                  return false;
 160              }
 161  
 162              // check whether category access level allows access
 163              if ($this->_section->access > $user->get('aid', 0)) {
 164                  JError::raiseError(403, JText::_("ALERTNOTAUTH"));
 165                  return false;
 166              }
 167          }
 168          return $this->_section;
 169      }
 170  
 171      /**
 172       * Method to get sibling category data for the current category
 173       *
 174       * @since 1.5
 175       */
 176  	function getCategories()
 177      {
 178          // Initialize some variables
 179          $user    =& JFactory::getUser();
 180  
 181          // Load the Category data
 182          if ($this->_loadSection() && $this->_loadCategories())
 183          {
 184              // Make sure the category is published
 185              if (!$this->_section->published) {
 186                  JError::raiseError(404, JText::_("Resource Not Found"));
 187                  return false;
 188              }
 189  
 190              // check whether category access level allows access
 191              if ($this->_section->access > $user->get('aid', 0)) {
 192                  JError::raiseError(403, JText::_("ALERTNOTAUTH"));
 193                  return false;
 194              }
 195          }
 196          return $this->_categories;
 197      }
 198  
 199      /**
 200       * Method to get archived article data for the current section
 201       *
 202       * @param    int    $state    The content state to pull from for the current section
 203       * @since 1.5
 204       */
 205  	function getArchives($state = -1)
 206      {
 207          return $this->getData(-1);
 208      }
 209  
 210      /**
 211       * Method to get archived article data for the current section
 212       *
 213       * @param    int    $state    The content state to pull from for the current section
 214       * @since 1.5
 215       */
 216  	function getTree()
 217      {
 218          return $this->_loadTree();
 219      }
 220  
 221      /**
 222       * Method to load section data if it doesn't exist.
 223       *
 224       * @access    private
 225       * @return    boolean    True on success
 226       */
 227  	function _loadSection()
 228      {
 229          if (empty($this->_section))
 230          {
 231              // Lets get the information for the current section
 232              if ($this->_id) {
 233                  $where = ' WHERE id = '. (int) $this->_id;
 234              } else {
 235                  $where = null;
 236              }
 237  
 238              $query = 'SELECT *' .
 239                      ' FROM #__sections' .
 240                      $where;
 241              $this->_db->setQuery($query, 0, 1);
 242              $this->_section = $this->_db->loadObject();
 243          }
 244          return true;
 245      }
 246  
 247      /**
 248       * Method to load sibling category data if it doesn't exist.
 249       *
 250       * @access    private
 251       * @return    boolean    True on success
 252       */
 253  	function _loadCategories()
 254      {
 255          global $mainframe;
 256          // Lets load the siblings if they don't already exist
 257          if (empty($this->_categories))
 258          {
 259              $user    =& JFactory::getUser();
 260  
 261              // Get the page/component configuration
 262              $params = &$mainframe->getParams();
 263  
 264              $noauth    = !$params->get('show_noauth');
 265              $gid        = $user->get('aid', 0);
 266              $now        = $mainframe->get('requestTime');
 267              $nullDate    = $this->_db->getNullDate();
 268  
 269              // Ordering control
 270              $orderby = $params->get('orderby', '');
 271              $orderby = ContentHelperQuery::orderbySecondary($orderby);
 272  
 273              // Handle the access permissions part of the main database query
 274              if ($user->authorize('com_content', 'edit', 'content', 'all')) {
 275                  $xwhere = '';
 276                  $xwhere2 = ' AND b.state >= 0';
 277              } else {
 278                  $xwhere = ' AND a.published = 1';
 279                  $xwhere2 = ' AND b.state = 1' .
 280                          ' AND ( b.publish_up = '.$this->_db->Quote($nullDate).' OR b.publish_up <= '.$this->_db->Quote($now).' )' .
 281                          ' AND ( b.publish_down = '.$this->_db->Quote($nullDate).' OR b.publish_down >= '.$this->_db->Quote($now).' )';
 282                  if ($noauth) {
 283                      $xwhere2 .= ' AND b.access <= '.(int) $gid;
 284                  }
 285               }
 286  
 287              // Determine whether to show/hide the empty categories and sections
 288              $empty = null;
 289              $empty_sec = null;
 290  
 291              // show/hide empty categories in section
 292              if (!$params->get('show_empty_categories')) {
 293                  $empty_sec = ' HAVING numitems > 0';
 294              }
 295  
 296              // Handle the access permissions
 297              $access_check = null;
 298              if ($noauth) {
 299                  $access_check = ' AND a.access <= '.(int) $gid;
 300                  //$access_check .= ' AND b.access <= '.(int) $gid;
 301              }
 302  
 303              // Query of categories within section
 304              $query = 'SELECT a.*, COUNT( b.id ) AS numitems,' .
 305                      ' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(\':\', a.id, a.alias) ELSE a.id END as slug'.
 306                      ' FROM #__categories AS a' .
 307                      ' LEFT JOIN #__content AS b ON b.catid = a.id'.
 308                      $xwhere2 .
 309                      ' WHERE a.section = '.(int) $this->_id.
 310                      $xwhere.
 311                      $access_check .
 312                      ' GROUP BY a.id'.$empty.$empty_sec .
 313                      ' ORDER BY '. $orderby;
 314              $this->_db->setQuery($query);
 315              $this->_categories = $this->_db->loadObjectList();
 316          }
 317          return true;
 318      }
 319  
 320      /**
 321       * Method to load content item data for items in the category if they don't
 322       * exist.
 323       *
 324       * @access    private
 325       * @return    boolean    True on success
 326       */
 327  	function _loadData($state = 1)
 328      {
 329          if (empty($this->_section)) {
 330              return false; // TODO: set error -- can't get siblings when we don't know the category
 331          }
 332  
 333          // Lets load the content if it doesn't already exist
 334          if (empty($this->_data[$state]))
 335          {
 336              // Get the pagination request variables
 337  
 338              $limit        = JRequest::getVar('limit', 0, '', 'int');
 339              $limitstart    = JRequest::getVar('limitstart', 0, '', 'int');
 340  
 341              $query = $this->_buildQuery();
 342              $Arows = $this->_getList($query, $limitstart, $limit);
 343  
 344              // Check for db errors
 345              if ($this->_db->getErrorNum())
 346              {
 347                  JError::raiseError(500, $this->_db->stderror());
 348                  return false;
 349              }
 350  
 351  
 352              // special handling required as Uncategorized content does not have a section / category id linkage
 353              $i = $limitstart;
 354              $rows = array();
 355              foreach ($Arows as $row)
 356              {
 357                  // check to determine if section or category has proper access rights
 358                  $rows[$i] = $row;
 359                  $i ++;
 360              }
 361              $this->_data[$state] = $rows;
 362          }
 363          return true;
 364      }
 365  
 366      /**
 367       * Method to load content item data for items in the category if they don't
 368       * exist.
 369       *
 370       * @access    private
 371       * @return    boolean    True on success
 372       */
 373  	function _loadTree()
 374      {
 375          global $mainframe;
 376          // Lets load the content if it doesn't already exist
 377          if (empty($this->_tree))
 378          {
 379              $user        =& JFactory::getUser();
 380              $aid        = $user->get('aid', 0);
 381              $now        = $mainframe->get('requestTime');
 382              $nullDate    = $this->_db->getNullDate();
 383  
 384              // Get the information for the current section
 385              if ($this->_id) {
 386                  $and = ' AND a.section = '.(int) $this->_id;
 387              } else {
 388                  $and = null;
 389              }
 390  
 391              // Query of categories within section
 392              $query = 'SELECT a.name AS catname, a.title AS cattitle, b.* ' .
 393                  ' FROM #__categories AS a' .
 394                  ' INNER JOIN #__content AS b ON b.catid = a.id' .
 395                  ' AND b.state = 1' .
 396                  ' AND ( b.publish_up = '.$this->_db->Quote($nullDate).' OR b.publish_up <= '.$this->_db->Quote($now).' )' .
 397                  ' AND ( b.publish_down = '.$this->_db->Quote($nullDate).' OR b.publish_down >= '.$this->_db->Quote($now).' )';
 398                  ' WHERE a.published = 1' .
 399                  $and .
 400                  ' AND a.access <= '.(int) $aid .
 401                  ' ORDER BY a.catid, a.ordering, b.ordering';
 402              $this->_db->setQuery($query);
 403              $this->_tree = $this->_db->loadObjectList();
 404          }
 405          return true;
 406      }
 407  
 408  	function _buildQuery($state = 1)
 409      {
 410          global $mainframe;
 411  
 412          // Get the page/component configuration
 413          $params = &$mainframe->getParams();
 414  
 415          // If voting is turned on, get voting data as well for the content items
 416          $voting    = ContentHelperQuery::buildVotingQuery($params);
 417  
 418          // Get the WHERE and ORDER BY clauses for the query
 419          $where        = $this->_buildContentWhere($state);
 420          $orderby    = $this->_buildContentOrderBy($state);
 421  
 422          $query = 'SELECT 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,' .
 423                  ' 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,' .
 424                  ' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(\':\', a.id, a.alias) ELSE a.id END as slug,'.
 425                  ' CASE WHEN CHAR_LENGTH(cc.alias) THEN CONCAT_WS(":", cc.id, cc.alias) ELSE cc.id END as catslug,'.
 426                  ' CHAR_LENGTH( a.`fulltext` ) AS readmore, u.name AS author, u.usertype, cc.title AS category, g.name AS groups, u.email as author_email'.$voting['select'] .
 427                  ' FROM #__content AS a' .
 428                  ' INNER JOIN #__categories AS cc ON cc.id = a.catid' .
 429                  ' LEFT JOIN #__sections AS s ON s.id = a.sectionid' .
 430                  ' LEFT JOIN #__users AS u ON u.id = a.created_by' .
 431                  ' LEFT JOIN #__groups AS g ON a.access = g.id'.
 432                  $voting['join'].
 433                  $where.
 434                  $orderby;
 435  
 436          return $query;
 437      }
 438  
 439      /**
 440       * Build the SQL ORDER BY for the section listing.
 441       *
 442       * @param    int        $state    The published state of the articles being listed.
 443       *
 444       * @return    string    The full ORDER BY clause.
 445       * @since    1.5
 446       */
 447  	function _buildContentOrderBy($state = 1)
 448      {
 449          $filter_order        = JRequest::getCmd('filter_order');
 450          $filter_order_Dir    = JRequest::getWord('filter_order_Dir');
 451          $orderby            = '';
 452  
 453          if (!in_array($filter_order, array('a.id', 'a.title', 'a.alias', 'a.title_alias', 'a.sectionid', 'a.state',
 454              'a.catid', 'a.created', 'a.created_by', 'a.created_by_alias', 'a.modified', 'a.modified_by', 'a.checked_out', 'a.checked_out_time',
 455              'a.hits', 'a.ordering', 'a.access'))) {
 456              $filter_order = null;
 457          }
 458  
 459          if (!in_array(trim(strtoupper($filter_order_Dir)), array('ASC', 'DESC'))) {
 460              $filter_order_Dir = 'ASC';
 461          }
 462  
 463          if ($filter_order) {
 464              $orderby .= $filter_order .' '. $filter_order_Dir.', ';
 465          }
 466  
 467          // Get the page/component configuration
 468          $app =& JFactory::getApplication();
 469          $params =& $app->getParams();
 470  
 471          switch ($state)
 472          {
 473              case -1:
 474                  // Special ordering for archive articles
 475                  $orderby_sec    = $params->def('orderby', 'rdate');
 476                  $secondary        = ContentHelperQuery::orderbySecondary($orderby_sec);
 477                  $primary        = '';
 478                  break;
 479  
 480              case 1:
 481              default:
 482                  $orderby_sec    = $params->def('orderby_sec', 'rdate');
 483                  $orderby_sec    = ($orderby_sec == 'front') ? '' : $orderby_sec;
 484                  $orderby_pri    = $params->def('orderby_pri', '');
 485                  $secondary        = ContentHelperQuery::orderbySecondary($orderby_sec);
 486                  $primary        = ContentHelperQuery::orderbyPrimary($orderby_pri);
 487                  break;
 488          }
 489          $orderby .= "$primary $secondary";
 490  
 491          return trim($orderby) ? ' ORDER BY '.$orderby : null;
 492      }
 493  
 494  	function _buildContentWhere($state = 1)
 495      {
 496          global $mainframe;
 497          $user        =& JFactory::getUser();
 498          $aid        = $user->get('aid', 0);
 499  
 500          $jnow        =& JFactory::getDate();
 501          $now        = $jnow->toMySQL();
 502  
 503          // Get the page/component configuration
 504          $params = &$mainframe->getParams();
 505  
 506          $noauth        = !$params->get('show_noauth');
 507          $nullDate    = $this->_db->getNullDate();
 508  
 509          // First thing we need to do is assert that the articles are in the current category
 510          if ($noauth) {
 511              $where = ' WHERE a.access <= '.(int) $aid;
 512          } else {
 513              $where = ' WHERE 1';
 514          }
 515          if ($this->_id) {
 516              $where .= ' AND s.id = '.(int)$this->_id;
 517          }
 518  
 519          $where .= ' AND s.access <= '.(int) $aid;
 520          $where .= ' AND cc.access <= '.(int) $aid;
 521          $where .= ' AND s.published = 1';
 522          $where .= ' AND cc.published = 1';
 523  
 524          // Regular Published Content
 525          switch ($state)
 526          {
 527              case 1:
 528                  if ($user->authorize('com_content', 'edit', 'content', 'all')) {
 529                      $where .= ' AND a.state >= 0';
 530                  } else {
 531                      $where .= ' AND a.state = 1' .
 532                              ' AND ( publish_up = '.$this->_db->Quote($nullDate).' OR publish_up <= '.$this->_db->Quote($now).' )' .
 533                              ' AND ( publish_down = '.$this->_db->Quote($nullDate).' OR publish_down >= '.$this->_db->Quote($now).' )';
 534                  }
 535                  break;
 536  
 537              // Archive Content
 538              case -1:
 539                  // Get some request vars specific to this state
 540                  $year    = JRequest::getInt( 'year', date('Y') );
 541                  $month    = JRequest::getInt( 'month', date('m') );
 542  
 543                  $where .= ' AND a.state = -1';
 544                  $where .= ' AND YEAR( a.created ) = '.(int) $year;
 545                  $where .= ' AND MONTH( a.created ) = '.(int) $month;
 546                  break;
 547  
 548              default:
 549                  $where .= ' AND a.state = '.(int) $state;
 550                  break;
 551          }
 552  
 553          /*
 554           * If we have a filter, and this is enabled... lets tack the AND clause
 555           * for the filter onto the WHERE clause of the content item query.
 556           */
 557          if ($params->get('filter'))
 558          {
 559              $filter = JRequest::getString('filter', '', 'request');
 560              if ($filter) {
 561                  // clean filter variable
 562                  $filter = JString::strtolower($filter);
 563                  $filter    = $this->_db->Quote( '%'.$this->_db->getEscaped( $filter, true ).'%', false );
 564  
 565                  switch ($params->get('filter_type'))
 566                  {
 567                      case 'title' :
 568                          $where .= ' AND LOWER( a.title ) LIKE '.$filter;
 569                          break;
 570  
 571                      case 'author' :
 572                          $where .= ' AND ( ( LOWER( u.name ) LIKE '.$filter.' ) OR ( LOWER( a.created_by_alias ) LIKE '.$filter.' ) )';
 573                          break;
 574  
 575                      case 'hits' :
 576                          $where .= ' AND a.hits LIKE '.$filter;
 577                          break;
 578                  }
 579              }
 580          }
 581          return $where;
 582      }
 583  }
 584  ?>


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