Skip to content

Commit

Permalink
Working commit. Added draft nested set support to com_content.
Browse files Browse the repository at this point in the history
git-svn-id: http://joomlacode.org/svn/joomla/development/trunk@12416 6f6e1ebd-4c2b-0410-823f-f34bde69bce9
  • Loading branch information
eddieajau committed Jul 3, 2009
1 parent e8d5a4c commit 50ed2ba
Show file tree
Hide file tree
Showing 18 changed files with 789 additions and 1,008 deletions.
2 changes: 1 addition & 1 deletion components/com_content/models/article.php
Expand Up @@ -27,7 +27,7 @@ class ContentModelArticle extends JModelItem
*
* @var string
*/
protected $_context = 'com_content.item';
protected $_context = 'com_content.article';

/**
* Method to auto-populate the model state.
Expand Down
204 changes: 204 additions & 0 deletions components/com_content/models/articles.php
@@ -0,0 +1,204 @@
<?php
/**
* @version $Id$
* @copyright Copyright (C) 2005 - 2009 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die;

jimport('joomla.application.component.modellist');

/**
* This models supports retrieving lists of articles.
*
* @package Joomla.Administrator
* @subpackage com_content
* @since 1.6
*/
class ContentModelArticles extends JModelList
{
/**
* Model context string.
*
* @var string
*/
public $_context = 'com_content.articles';

/**
* Method to auto-populate the model state.
*
* @since 1.6
*/
protected function _populateState()
{
$app = &JFactory::getApplication();

// List state information
//$limit = $app->getUserStateFromRequest('global.list.limit', 'limit', $app->getCfg('list_limit'));
$limit = JRequest::getInt('limit', $app->getCfg('list_limit', 0));
$this->setState('list.limit', $limit);

//$limitstart = $app->getUserStateFromRequest($this->_context.'.limitstart', 'limitstart', 0);
$limitstart = JRequest::getInt('limitstart', 0);
$this->setState('list.limitstart', $limitstart);

//$orderCol = $app->getUserStateFromRequest($this->_context.'.ordercol', 'filter_order', 'a.lft');
$orderCol = JRequest::getCmd('filter_order', 'a.ordering');
$this->setState('list.ordering', $orderCol);

//$orderDirn = $app->getUserStateFromRequest($this->_context.'.orderdirn', 'filter_order_Dir', 'asc');
$orderDirn = JRequest::getWord('filter_order_Dir', 'asc');
$this->setState('list.direction', $orderDirn);

$params = $app->getParams();
$this->setState('params', $params);

$this->setState('filter.published', 1);
$this->setState('filter.access', true);
}

/**
* Method to get a store id based on model configuration state.
*
* This is necessary because the model is used by the component and
* different modules that might need different sets of data or different
* ordering requirements.
*
* @param string $id A prefix for the store id.
*
* @return string A store id.
*/
protected function _getStoreId($id = '')
{
// Compile the store id.
$id .= ':'.$this->getState('list.start');
$id .= ':'.$this->getState('list.limit');
$id .= ':'.$this->getState('list.ordering');
$id .= ':'.$this->getState('list.direction');
$id .= ':'.$this->getState('filter.published');
$id .= ':'.$this->getState('filter.access');

return md5($id);
}

/**
* @param boolean True to join selected foreign information
*
* @return string
*/
function _getListQuery()
{
// Create a new query object.
$query = new JQuery;

// Select the required fields from the table.
$query->select($this->getState(
'list.select',
'a.id, a.title, a.alias, a.title_alias, a.introtext, a.state, a.catid, a.created, a.created_by, a.created_by_alias,' .
' a.modified, a.modified_by,a.publish_up, a.publish_down, a.attribs, a.metadata, a.metakey, a.metadesc, a.access,' .
' LENGTH(a.fulltext) AS readmore'
));
$query->from('#__content AS a');

// Join over the categories.
$query->select('c.title AS category_title, c.path AS category_route, c.access AS category_access');
$query->join('LEFT', '#__categories AS c ON c.id = a.catid');

// Join over the users for the author.
$query->select('ua.name AS author_name');
$query->join('LEFT', '#__users AS ua ON ua.id = a.created_by');

// Filter by access level.
if ($access = $this->getState('filter.access'))
{
$user = &JFactory::getUser();
$groups = implode(',', $user->authorisedLevels());
$query->where('a.access IN ('.$groups.')');
}

// Filter by published state
$published = $this->getState('filter.published');
if (is_numeric($published)) {
$query->where('a.state = ' . (int) $published);
}
else if (is_array($published))
{
JArrayHelper::toInteger($published);
$published = implode(',', $published);
$query->where('a.state IN ('.$published.')');
}

// Filter by a single or group of categories.
$categoryId = $this->getState('filter.category_id');
if (is_numeric($categoryId)) {
$query->where('a.catid = ' . (int) $categoryId);
}
else if (is_array($categoryId))
{
JArrayHelper::toInteger($categoryId);
$categoryId = implode(',', $categoryId);
$query->where('a.catid IN ('.$categoryId.')');
}

// Filter by start and end dates.
$nullDate = $this->_db->Quote($this->_db->getNullDate());
$nowDate = $this->_db->Quote(JFactory::getDate()->toMySQL());

$query->where('(a.publish_up = '.$nullDate.' OR a.publish_up <= '.$nowDate.')');
$query->where('(a.publish_down = '.$nullDate.' OR a.publish_down >= '.$nowDate.')');

// Add the list ordering clause.
$query->order($this->_db->getEscaped($this->getState('list.ordering', 'a.ordering')).' '.$this->_db->getEscaped($this->getState('list.direction', 'ASC')));

//echo nl2br(str_replace('#__','jos_',$query));
return $query;
}


/**
* Method to get a list of articles.
*
* Overriden to inject convert the attribs field into a JParameter object.
*
* @return mixed An array of objects on success, false on failure.
*/
public function &getItems()
{
$items = &parent::getItems();
$user = &JFactory::getUser();
$groups = $user->authorisedLevels();

// Contvert the parameter fields into objects.
foreach ($items as &$item)
{
$registry = new JRegistry;
$registry->loadJSON($item->attribs);
$item->params = clone $this->getState('params');
$item->params->merge($registry);

// TODO: Embed the access controls in here
$item->params->set('access-edit', false);

$access = $this->getState('filter.access');
if ($access = $this->getState('filter.access'))
{
// If the access filter has been set, we already have only the articles this user can view.
$item->params->set('access-view', true);
}
else
{
// If no access filter is set, the layout takes some responsibility for display of limited information.
if ($item->catid == 0 || $item->category_access === null) {
$item->params->set('access-view', in_array($item->access, $groups));
}
else {
$item->params->set('access-view', in_array($item->access, $groups) && in_array($item->category_access, $groups));
}
}
}

return $items;
}
}
117 changes: 87 additions & 30 deletions components/com_content/models/categories.php
Expand Up @@ -11,7 +11,7 @@
jimport('joomla.application.component.modellist');

/**
* Categories Component Categories Model
* This models supports retrieving lists of article categories.
*
* @package Joomla.Administrator
* @subpackage com_content
Expand All @@ -34,35 +34,35 @@ class ContentModelCategories extends JModelList
protected $_extension = 'com_content';

/**
* Overridden method to lazy load data from the request/session as necessary
* Method to auto-populate the model state.
*
* @access public
* @param string $key The key of the state item to return
* @param mixed $default The default value to return if it does not exist
* @return mixed The requested value by key
* @since 1.0
* @since 1.6
*/
function _populateState()
protected function _populateState()
{
$app = &JFactory::getApplication();

$this->setState('filter.extension', $this->_extension);

// Get the parent id
// Get the parent id if defined.
$parentId = JRequest::getInt('id');
$this->setState('list.parentId', $parentId);
$this->setState('filter.parentId', $parentId);

// List state information
$limit = $app->getUserStateFromRequest('global.list.limit', 'limit', $app->getCfg('list_limit'));
//$limit = $app->getUserStateFromRequest('global.list.limit', 'limit', $app->getCfg('list_limit'));
$limit = JRequest::getInt('limit', $app->getCfg('list_limit', 0));
$this->setState('list.limit', $limit);

$limitstart = $app->getUserStateFromRequest($this->_context.'.limitstart', 'limitstart', 0);
//$limitstart = $app->getUserStateFromRequest($this->_context.'.limitstart', 'limitstart', 0);
$limitstart = JRequest::getInt('limitstart', 0);
$this->setState('list.limitstart', $limitstart);

$orderCol = $app->getUserStateFromRequest($this->_context.'.ordercol', 'filter_order', 'a.lft');
//$orderCol = $app->getUserStateFromRequest($this->_context.'.ordercol', 'filter_order', 'a.lft');
$orderCol = JRequest::getCmd('filter_order', 'a.lft');
$this->setState('list.ordering', $orderCol);

$orderDirn = $app->getUserStateFromRequest($this->_context.'.orderdirn', 'filter_order_Dir', 'asc');
//$orderDirn = $app->getUserStateFromRequest($this->_context.'.orderdirn', 'filter_order_Dir', 'asc');
$orderDirn = JRequest::getWord('filter_order_Dir', 'asc');
$this->setState('list.direction', $orderDirn);

$params = $app->getParams();
Expand All @@ -83,7 +83,7 @@ function _populateState()
*
* @return string A store id.
*/
public function _getStoreId($id = '')
protected function _getStoreId($id = '')
{
// Compile the store id.
$id .= ':'.$this->getState('list.start');
Expand Down Expand Up @@ -112,24 +112,12 @@ function _getListQuery($resolveFKs = true)
$this->getState(
'list.select',
'a.id, a.title, a.alias, a.access, a.published, a.access' .
', a.path, a.parent_id, a.level, a.lft, a.rgt' .
', a.path AS route, a.parent_id, a.level, a.lft, a.rgt' .
', a.description'
)
);
$query->from('#__categories AS a');

// Retrieve a sub tree
if ($parentId = $this->getState('list.parentId'))
{
$query->join('LEFT', '#__categories AS p ON p.id = '.(int) $parentId);
$query->where('(a.lft > p.lft AND a.rgt < p.rgt)');
}

// Filter by extension
if ($extension = $this->getState('filter.extension')) {
$query->where('a.extension = '.$this->_db->quote($extension));
}

// Filter by access level.
if ($access = $this->getState('filter.access'))
{
Expand All @@ -138,19 +126,88 @@ function _getListQuery($resolveFKs = true)
$query->where('a.access IN ('.$groups.')');
}

// Filter by published state
// Filter by published state.
$published = $this->getState('filter.published');
if (is_numeric($published)) {
$query->where('a.published = ' . (int) $published);
}
else if (is_array($published))
{
JArrayHelper::toInteger($published);
$published = implode(',', $published);
$query->where('a.published IN ('.$published.')');
}

// Filter by extension.
$query->where('a.extension = '.$this->_db->quote($this->_extension));

// Retrieve a sub tree or lineage.
if ($parentId = $this->getState('filter.parent_id'))
{
if ($levels = $this->getState('filter.get_children'))
{
// Optionally get all the child categories for given parent.
$query->leftJoin('#__categories AS p ON p.id = '.(int) $parentId);
$query->where('a.lft > p.lft AND a.rgt < p.rgt');
if ((int) $levels > 0)
{
// Only go to a certain depth.
$query->where('a.level <= p.level + '.(int) $levels);
}
}
else if ($this->getState('filter.get_parents'))
{
// Optionally get all the parents to the category.
$query->leftJoin('#__categories AS p ON p.id = '.(int) $parentId);
$query->where('a.lft < p.lft AND a.rgt > p.rgt');
}
else
{
// Only looking for categories with this parent.
$query->where('a.parent_id = '.(int) $parentId);
}
}

// Inclusive/exclusive filters (-ve id's are to be excluded).
$categoryId = $this->getState('filter.category_id');
if (is_numeric($categoryId))
{
if ($categoryId > 0) {
$query->where('a.id = ' . (int) $categoryId);
}
else {
$query->where('a.id <> ' . -(int) $categoryId);
}
}
else if (is_array($categoryId))
{
JArrayHelper::toInteger($categoryId);
// Find the include/excludes
$include = array();
$exclude = array();
foreach ($categoryId as $id)
{
if ($id > 0) {
$include[] = $id;
}
else {
$exclude[] = $id;
}
}
if (!empty($include))
{
$include = implode(',', $include);
$query->where('a.id IN ('.$include.')');
}
else
{
$include = implode(',', $include);
$query->where('a.id NOT IN ('.$include.')');
}
}

// Add the list ordering clause.
$query->order($this->_db->getEscaped($this->getState('list.ordering', 'a.title')).' '.$this->_db->getEscaped($this->getState('list.direction', 'ASC')));
$query->order($this->_db->getEscaped($this->getState('list.ordering', 'a.lft')).' '.$this->_db->getEscaped($this->getState('list.direction', 'ASC')));

//echo nl2br(str_replace('#__','jos_',$query));
return $query;
Expand Down

0 comments on commit 50ed2ba

Please sign in to comment.