Skip to content

Instantly share code, notes, and snippets.

@laoneo
Created August 17, 2018 09:37
Show Gist options
  • Save laoneo/6dd236c62a01a71cb759a1e38cfaff79 to your computer and use it in GitHub Desktop.
Save laoneo/6dd236c62a01a71cb759a1e38cfaff79 to your computer and use it in GitHub Desktop.
public function countItems() {
if($this instanceof WorkflowServiceInterface) {
return $this->getWFcountItems();
}
// Normal code here
}
@HLeithner
Copy link

	public function countItems(array $items, string $section)
	{
		$sectionTable = $this->getTableNameForSection($section);
		if (!$sectionTable)
		{
			return;
		}

		if ($this instanceof WorkflowServiceInterface)
		{
			// 
			$states = $this->getConditions();
		} else {
			$states = array(
				'-2' => 'count_trashed',
				'0'  => 'count_unpublished',
				'1'  => 'count_published',
				'2'  => 'count_archived',
			);
		}
		$db = Factory::getDbo();

		foreach ($items as $item)
		{
			$item->count_trashed = 0;
			$item->count_archived = 0;
			$item->count_unpublished = 0;
			$item->count_published = 0;
			$query = $db->getQuery(true);

			if ($this instanceof WorkflowServiceInterface) {
				$query->select($db->quoteName('condition', 'state'))
							->select('COUNT(*) AS ' . $db->quoteName('count'))
							->from($db->quoteName('#__content', 'c'))
							->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('catid') . ' = ' . (int) $item->id)
							->where($db->quoteName('a.extension') . '= ' . $db->quote('com_content'))
							->group($db->quoteName('condition'));
			}
			else
			{
				$query->select('state, count(*) AS count')
							->from($db->qn($sectionTable))
							->where('catid = ' . (int) $item->id)
							->group('state');
				$db->setQuery($query);
			}

			$fields = $db->loadObjectList();

			foreach ($fields as $field)
			{
				$property = $states[$field->state];
				$item->{$property} = $field->count;
			}
		}
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment