Skip to content

Instantly share code, notes, and snippets.

@HLeithner
Last active August 17, 2018 11:24
Show Gist options
  • Save HLeithner/39264c4eeea6a99493f81222ea603da9 to your computer and use it in GitHub Desktop.
Save HLeithner/39264c4eeea6a99493f81222ea603da9 to your computer and use it in GitHub Desktop.
<?php
class x {
public function countItems(array $items, string $section)
{
$sectionTable = $this->getTableNameForSection($section);
if (!$sectionTable)
{
return;
}
$extension = 'com_content';
if (!empty($section))
{
$extension .= '.' . $section;
}
$db = Factory::getDbo();
$queryBase = $db->getQuery(true);
$queryBase->from($db->quoteName($sectionTable, 'c'))
->select('COUNT(*) AS ' . $db->quoteName('count'))
->group($db->quoteName('state'));
$states = array(
-2 => 'count_trashed',
0 => 'count_unpublished',
1 => 'count_published',
2 => 'count_archived',
);
// Match states with conditions
if ($this instanceof WorkflowServiceInterface)
{
$conditions = $this->getConditions($extension);
foreach ($states as $key => $value)
{
if (!array_key_exists($key, $conditions))
{
unset($states[$key]);
}
}
$queryBase->select($db->quoteName('condition', 'state'))
->from($db->quoteName('#__workflow_stages', 's'))
->from($db->quoteName('#__workflow_associations', 'a'))
->where($db->quoteName('a.item_id') . ' = ' . $db->quoteName('c.id'))
->where($db->quoteName('s.id') . ' = ' . $db->quoteName('a.stage_id'))
->where($db->quoteName('a.extension') . '= ' . $db->quote($extension));
}
else
{
$queryBase->select($db->quoteName('state'));
}
foreach ($items as $item)
{
$query = clone $queryBase;
$query->where($db->quoteName('catid') . ' = ' . (int) $item->id);
$fields = $db->setQuery($query)->loadObjectList('state');
foreach ($states as $state => $property)
{
$item->{$property} = $fields[$state]->count ?? 0;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment