Skip to content

Commit

Permalink
Made "Count Items" in Category Manager generic
Browse files Browse the repository at this point in the history
  • Loading branch information
pe7er committed May 13, 2015
1 parent c7b7fea commit 9bd6ab4
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 20 deletions.
51 changes: 51 additions & 0 deletions administrator/components/com_banners/helpers/countitems.php
@@ -0,0 +1,51 @@
<?php
/**
* @package Joomla.Administrator
* @subpackage com_content
*
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;

/**
* Contentitem component helper.
*
* @since 1.6
*/
class ContentitemsHelper extends JHelperContent
{
public static $extension = 'com_content';

/**
* Adds Count Items for Category Manager.
*
* @param object $query The query object of com_categories
*
* @return object
*
* @since 3.4
*/
public static function countItems($query)
{
// Join articles to categories and
// Count published articles
$query->select('COUNT(DISTINCT cp.id) AS count_published');
$query->join('LEFT', '#__content AS cp ON cp.catid = a.id AND cp.state = 1');

// Count unpublished articles
$query->select('COUNT(DISTINCT cu.id) AS count_unpublished');
$query->join('LEFT', '#__content AS cu ON cu.catid = a.id AND cu.state = 0');

// Count archived articles
$query->select('COUNT(DISTINCT ca.id) AS count_archived');
$query->join('LEFT', '#__content AS ca ON ca.catid = a.id AND ca.state = 2');

// Count trashed articles
$query->select('COUNT(DISTINCT ct.id) AS count_trashed');
$query->join('LEFT', '#__content AS ct ON ct.catid = a.id AND ct.state = -2');

return $query;
}
}
24 changes: 10 additions & 14 deletions administrator/components/com_categories/models/categories.php
Expand Up @@ -147,6 +147,10 @@ protected function getListQuery()
$query = $db->getQuery(true);
$user = JFactory::getUser();

// Determine for which component the category manager retrieves its categories (for item count)
$jinput = JFactory::getApplication()->input;
$countitemhelper = JPATH_ADMINISTRATOR . "/components/" . $jinput->get('extension') . '/helpers/countitems.php';

// Select the required fields from the table.
$query->select(
$this->getState(
Expand Down Expand Up @@ -276,22 +280,14 @@ protected function getListQuery()
$query->order($db->escape($listOrdering) . ' ' . $listDirn);
}

// If component is com_content then show number of articles (published, unpublished, trashed)
$jinput = JFactory::getApplication()->input;
if ($jinput->get('extension') == 'com_content')
// Load helper file from the component that uses com_categories
// to extend the $query object with item count (published, unpublished, trashed)
if (JFile::exists($countitemhelper))
{
$query->select('COUNT(DISTINCT cp.id) AS count_published');
$query->join('LEFT', '#__content AS cp ON cp.catid = a.id AND cp.state = 1');

$query->select('COUNT(DISTINCT cu.id) AS count_unpublished');
$query->join('LEFT', '#__content AS cu ON cu.catid = a.id AND cu.state = 0');

$query->select('COUNT(DISTINCT ca.id) AS count_archived');
$query->join('LEFT', '#__content AS ca ON ca.catid = a.id AND ca.state = 2');

$query->select('COUNT(DISTINCT ct.id) AS count_trashed');
$query->join('LEFT', '#__content AS ct ON ct.catid = a.id AND ct.state = -2');
include ($countitemhelper);
ContentitemsHelper::countItems($query);

// Group by on Categories because of the extra joins to other tables
$query->group('a.id');
}

Expand Down
Expand Up @@ -24,8 +24,6 @@
$listDirn = $this->escape($this->state->get('list.direction'));
$ordering = ($listOrder == 'a.lft');
$saveOrder = ($listOrder == 'a.lft' && strtolower($listDirn) == 'asc');
$jinput = JFactory::getApplication()->input;
$component = $jinput->get('extension');

if ($saveOrder)
{
Expand Down Expand Up @@ -63,17 +61,22 @@
<th>
<?php echo JHtml::_('searchtools.sort', 'JGLOBAL_TITLE', 'a.title', $listDirn, $listOrder); ?>
</th>
<?php if ($component == 'com_content') : ?>
<?php if (isset($this->items[0]) && property_exists($this->items[0], count_published)) : ?>
<th width="1%" class="nowrap center hidden-phone">
<i class="icon-publish"></i>
</th>
<?php endif;?>
<?php if (isset($this->items[0]) && property_exists($this->items[0], count_unpublished)) : ?>
<th width="1%" class="nowrap center hidden-phone">
<i class="icon-unpublish"></i>
</th>
</th>
<?php endif;?>
<?php if (isset($this->items[0]) && property_exists($this->items[0], count_archived)) : ?>
<th width="1%" class="nowrap center hidden-phone">
<i class="icon-archive"></i>
</th>
<?php endif;?>
<?php if (isset($this->items[0]) && property_exists($this->items[0], count_trashed)) : ?>
<th width="1%" class="nowrap center hidden-phone">
<i class="icon-trash"></i>
</th>
Expand Down Expand Up @@ -181,24 +184,31 @@
<?php endif; ?>
</span>
</td>
<?php if ($component == 'com_content') : ?>
<?php if (isset($this->items[0]) && property_exists($this->items[0], count_published)) : ?>
<td class="center btns hidden-phone">
<a class="badge <?php if ($item->count_published > 0) echo "badge-success"; ?>" title="<?php echo JText::_('COM_CATEGORY_COUNT_PUBLISHED_ITEMS');?>" href="<?php echo JRoute::_('index.php?option=com_content&view=articles&filter[category_id]=' . (int) $item->id . '&filter[published]=1' . '&filter[level]=' . (int) $item->level);?>">
<?php echo $item->count_published; ?></a>
</td>
<?php endif;?>
<?php if (isset($this->items[0]) && property_exists($this->items[0], count_unpublished)) : ?>
<td class="center btns hidden-phone">
<a class="badge <?php if ($item->count_unpublished > 0) echo "badge-important"; ?>" title="<?php echo JText::_('COM_CATEGORY_COUNT_UNPUBLISHED_ITEMS');?>" href="<?php echo JRoute::_('index.php?option=com_content&view=articles&filter[category_id]=' . (int) $item->id . '&filter[published]=0' . '&filter[level]=' . (int) $item->level);?>">
<?php echo $item->count_unpublished; ?></a>
</td>
<?php endif;?>
<?php if (isset($this->items[0]) && property_exists($this->items[0], count_archived)) : ?>
<td class="center btns hidden-phone">
<a class="badge <?php if ($item->count_archived > 0) echo "badge-info"; ?>" title="<?php echo JText::_('COM_CATEGORY_COUNT_ARCHIVED_ITEMS');?>" href="<?php echo JRoute::_('index.php?option=com_content&view=articles&filter[category_id]=' . (int) $item->id . '&filter[published]=2' . '&filter[level]=' . (int) $item->level);?>">
<?php echo $item->count_archived; ?></a>
</td>
<?php endif;?>
<?php if (isset($this->items[0]) && property_exists($this->items[0], count_trashed)) : ?>
<td class="center btns hidden-phone">
<a class="badge <?php if ($item->count_trashed > 0) echo "badge-inverse"; ?>" title="<?php echo JText::_('COM_CATEGORY_COUNT_TRASHED_ITEMS');?>" href="<?php echo JRoute::_('index.php?option=com_content&view=articles&filter[category_id]=' . (int) $item->id . '&filter[published]=-2' . '&filter[level]=' . (int) $item->level);?>">
<?php echo $item->count_trashed; ?></a>
</td>
<?php endif; ?>
<?php endif;?>

<td class="small hidden-phone">
<?php echo $this->escape($item->access_level); ?>
</td>
Expand Down
50 changes: 50 additions & 0 deletions administrator/components/com_content/helpers/countitems.php
@@ -0,0 +1,50 @@
<?php
/**
* @package Joomla.Administrator
* @subpackage com_content
*
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;

/**
* Contentitem component helper.
*
* @since 1.6
*/
class ContentitemsHelper extends JHelperContent
{
public static $extension = 'com_content';

/**
* Adds Count Items for Category Manager.
*
* @param object $query The query object of com_categories
*
* @return object
*
* @since 3.4
*/
public static function countItems(&$query)
{
// Join articles to categories and count published articles
$query->select('COUNT(DISTINCT cp.id) AS count_published');
$query->join('LEFT', '#__content AS cp ON cp.catid = a.id AND cp.state = 1');

// Count unpublished articles
$query->select('COUNT(DISTINCT cu.id) AS count_unpublished');
$query->join('LEFT', '#__content AS cu ON cu.catid = a.id AND cu.state = 0');

// Count archived articles
$query->select('COUNT(DISTINCT ca.id) AS count_archived');
$query->join('LEFT', '#__content AS ca ON ca.catid = a.id AND ca.state = 2');

// Count trashed articles
$query->select('COUNT(DISTINCT ct.id) AS count_trashed');
$query->join('LEFT', '#__content AS ct ON ct.catid = a.id AND ct.state = -2');

return $query;
}
}

0 comments on commit 9bd6ab4

Please sign in to comment.