Skip to content

Commit

Permalink
Categoy manager, get all counters via single query
Browse files Browse the repository at this point in the history
  • Loading branch information
ggppdk committed Oct 12, 2018
1 parent bd027fc commit ed2fce2
Showing 1 changed file with 41 additions and 28 deletions.
69 changes: 41 additions & 28 deletions administrator/components/com_content/helpers/content.php
Expand Up @@ -91,7 +91,7 @@ public static function filterText($text)
/**
* Adds Count Items for Category Manager.
*
* @param stdClass[] $items The banner category objects
* @param stdClass[] $items The category objects
*
* @return stdClass[]
*
Expand All @@ -101,41 +101,54 @@ public static function countItems(&$items)
{
$db = JFactory::getDbo();

$state_column = 'state';
$related_tbl = 'content';

// Index category objects by their ID
$records = array();

foreach ($items as $item)
{
$records[(int) $item->id] = $item;
}

// Get relation counts for all category objects with single query
$query = $db->getQuery(true)
->select('catid, ' . $state_column . ' AS state, count(*) AS count')
->from($db->qn('#__' . $related_tbl))
->where('catid IN (' . implode(',', array_keys($records)) . ')')
->group('catid, state');
$relationsAll = $db->setQuery($query)->loadObjectList();

// Category records without related data need a zero counter value (above query does not return a value in such a case)
foreach ($items as $item)
{
$item->count_trashed = 0;
$item->count_archived = 0;
$item->count_unpublished = 0;
$item->count_published = 0;
$query = $db->getQuery(true);
$query->select('state, count(*) AS count')
->from($db->qn('#__content'))
->where('catid = ' . (int) $item->id)
->group('state');
$db->setQuery($query);
$articles = $db->loadObjectList();

foreach ($articles as $article)
{
if ($article->state == 1)
{
$item->count_published = $article->count;
}

if ($article->state == 0)
{
$item->count_unpublished = $article->count;
}
}

if ($article->state == 2)
{
$item->count_archived = $article->count;
}
// Loop through the DB data overwritting the above zeros with the found count
foreach ($relationsAll as $relation)
{
$id = (int) $relation->catid;

if ($article->state == -2)
{
$item->count_trashed = $article->count;
}
if ($relation->state == 1)
{
$records[$id]->count_published = $relation->count;
}
elseif ($relation->state == 0)
{
$records[$id]->count_unpublished = $relation->count;
}
elseif ($relation->state == 2)
{
$records[$id]->count_archived = $relation->count;
}
elseif ($relation->state == -2)
{
$records[$id]->count_trashed = $relation->count;
}
}

Expand Down

0 comments on commit ed2fce2

Please sign in to comment.