Skip to content

Commit

Permalink
[imp] Adds Item count to Category Manager. Closes #6916.
Browse files Browse the repository at this point in the history
  • Loading branch information
pe7er authored and Thomas Hunziker committed Jun 3, 2015
1 parent 993aca9 commit c57e644
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 3 deletions.
15 changes: 15 additions & 0 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,6 +280,17 @@ protected function getListQuery()
$query->order($db->escape($listOrdering) . ' ' . $listDirn);
}

// Group by on Categories for JOIN with component tables to count items
$query->group('a.id');

// Load Helper file of the component for which com_categories displays the categories
$classname = ucfirst(substr($extension, 4)) . 'Helper';
if (class_exists($classname) && method_exists($classname, 'countItems'))
{
// Get the SQL to extend the com_category $query object with item count (published, unpublished, trashed)
$classname::countItems($query);
}

return $query;
}

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

if ($saveOrder)
{
Expand Down Expand Up @@ -61,10 +63,35 @@
<th>
<?php echo JHtml::_('searchtools.sort', 'JGLOBAL_TITLE', 'a.title', $listDirn, $listOrder); ?>
</th>
<?php if (isset($this->items[0]) && property_exists($this->items[0], 'count_published')) :
$columns++; ?>
<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')) :
$columns++; ?>
<th width="1%" class="nowrap center hidden-phone">
<i class="icon-unpublish"></i>
</th>
<?php endif;?>
<?php if (isset($this->items[0]) && property_exists($this->items[0], 'count_archived')) :
$columns++; ?>
<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')) :
$columns++; ?>
<th width="1%" class="nowrap center hidden-phone">
<i class="icon-trash"></i>
</th>
<?php endif;?>
<th width="10%" class="nowrap hidden-phone">
<?php echo JHtml::_('searchtools.sort', 'JGRID_HEADING_ACCESS', 'a.access', $listDirn, $listOrder); ?>
</th>
<?php if ($this->assoc) : ?>
<?php if ($this->assoc) :
$columns++; ?>
<th width="5%" class="hidden-phone">
<?php echo JHtml::_('searchtools.sort', 'COM_CATEGORY_HEADING_ASSOCIATION', 'association', $listDirn, $listOrder); ?>
</th>
Expand All @@ -79,7 +106,7 @@
</thead>
<tfoot>
<tr>
<td colspan="<?php echo ($this->assoc) ? '8' : '7'; ?>">
<td colspan="<?php echo $columns; ?>">
<?php echo $this->pagination->getListFooter(); ?>
</td>
</tr>
Expand Down Expand Up @@ -164,6 +191,31 @@
<?php endif; ?>
</span>
</td>
<?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=' . $component . '&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=' . $component . '&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=' . $component . '&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=' . $component . '&filter[category_id]=' . (int) $item->id . '&filter[published]=-2' . '&filter[level]=' . (int) $item->level);?>">
<?php echo $item->count_trashed; ?></a>
</td>
<?php endif;?>

<td class="small hidden-phone">
<?php echo $this->escape($item->access_level); ?>
</td>
Expand Down
31 changes: 31 additions & 0 deletions administrator/components/com_content/helpers/content.php
Expand Up @@ -60,4 +60,35 @@ public static function filterText($text)

return JComponentHelper::filterText($text);
}

/**
* 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 items
$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 items
$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 items
$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 items
$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;
}

}
6 changes: 5 additions & 1 deletion administrator/language/en-GB/en-GB.com_categories.ini
Expand Up @@ -44,7 +44,6 @@ COM_CATEGORIES_FIELDSET_PUBLISHING="Publishing"
COM_CATEGORIES_FIELDSET_RULES="Permissions"
COM_CATEGORIES_HAS_SUBCATEGORY_ITEMS="%d items are assigned to this category's subcategories."
COM_CATEGORIES_HAS_SUBCATEGORY_ITEMS_1="%d item is assigned to one of this category's subcategories."
COM_CATEGORY_HEADING_ASSOCIATION="Association"
COM_CATEGORIES_ITEM_ASSOCIATIONS_FIELDSET_LABEL="Category Item Associations"
COM_CATEGORIES_ITEM_ASSOCIATIONS_FIELDSET_DESC="Multilingual only! This choice will only display if the Language Filter parameter 'Item Associations' is set to 'Yes'. Choose a category item for the target language. This association will let the Language Switcher module redirect to the associated category item in another language. If used, make sure to display the Language switcher module on the concerned pages. A category item set to language 'All' can't be associated."
COM_CATEGORIES_ITEMS_SEARCH_FILTER="Search"
Expand Down Expand Up @@ -72,6 +71,11 @@ COM_CATEGORIES_SELECT_A_CATEGORY="Select a Category"
COM_CATEGORIES_TIP_ASSOCIATION="Associated categories"
COM_CATEGORIES_TIP_ASSOCIATED_LANGUAGE="%s %s"
COM_CATEGORIES_XML_DESCRIPTION="This component manages categories."
COM_CATEGORY_COUNT_ARCHIVED_ITEMS="Archived items"
COM_CATEGORY_COUNT_PUBLISHED_ITEMS="Published items"
COM_CATEGORY_COUNT_TRASHED_ITEMS="Trashed items"
COM_CATEGORY_COUNT_UNPUBLISHED_ITEMS="Unpublished items"
COM_CATEGORY_HEADING_ASSOCIATION="Association"
JGLOBAL_NO_ITEM_SELECTED="No categories selected."
JLIB_HTML_ACCESS_SUMMARY_DESC="Shown below is an overview of the permission settings for this category. Click the tabs above to customise these settings by action."
JLIB_RULES_SETTING_NOTES_ITEM="1. If you change the setting, it will apply to this and all child categories. Note that:<br /><em>Inherited</em> means that the permissions from the parent category will be used if there is a parent category or those from the component if there is no parent category.<br /><em>Denied</em> means that no matter what the parent category setting is, the group being edited can't take this action within this category.<br /><em>Allowed</em> means that the group being edited will be able to take this action within this category (but if this is in conflict with the parent category setting or the component setting it will have no impact; a conflict will be indicated by <em>Not Allowed (Locked)</em> under Calculated Settings).<br />2. If you select a new setting, click <em>Save</em> to refresh the calculated settings."
Expand Up @@ -22,6 +22,8 @@
$listDirn = $this->escape($this->state->get('list.direction'));
$ordering = ($listOrder == 'a.lft');
$saveOrder = ($listOrder == 'a.lft' && $listDirn == 'asc');
$jinput = JFactory::getApplication()->input;
$component = $jinput->get('extension');
?>
<div class="categories">
<form action="<?php echo JRoute::_('index.php?option=com_categories&view=categories'); ?>" method="post" name="adminForm" id="adminForm">
Expand Down Expand Up @@ -96,6 +98,26 @@
<?php echo JHtml::_('grid.order', $this->items, 'filesave.png', 'categories.saveorder'); ?>
<?php endif; ?>
</th>
<?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>
<?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>
<?php endif;?>
<th class="access-col">
<?php echo JHtml::_('grid.sort', 'JGRID_HEADING_ACCESS', 'access_level', $listDirn, $listOrder); ?>
</th>
Expand Down Expand Up @@ -160,6 +182,30 @@
<?php echo $orderkey + 1; ?>
<?php endif; ?>
</td>
<?php if (isset($this->items[0]) && property_exists($this->items[0], count_published)) : ?>
<td class="center">
<a title="<?php echo JText::_('COM_CATEGORY_COUNT_PUBLISHED_ITEMS');?>" href="<?php echo JRoute::_('index.php?option=' . $component . '&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">
<a title="<?php echo JText::_('COM_CATEGORY_COUNT_UNPUBLISHED_ITEMS');?>" href="<?php echo JRoute::_('index.php?option=' . $component . '&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">
<a title="<?php echo JText::_('COM_CATEGORY_COUNT_ARCHIVED_ITEMS');?>" href="<?php echo JRoute::_('index.php?option=' . $component . '&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">
<a title="<?php echo JText::_('COM_CATEGORY_COUNT_TRASHED_ITEMS');?>" href="<?php echo JRoute::_('index.php?option=' . $component . '&filter[category_id]=' . (int) $item->id . '&filter[published]=-2' . '&filter[level]=' . (int) $item->level);?>">
<?php echo $item->count_trashed; ?></a>
</td>
<?php endif;?>
<td class="center">
<?php echo $this->escape($item->access_level); ?>
</td>
Expand Down

0 comments on commit c57e644

Please sign in to comment.