Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Joomla\CMS\Language\Associations;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\MVC\Model\ListModel;
use Joomla\Database\DatabaseQuery;
use Joomla\Database\ParameterType;
use Joomla\Utilities\ArrayHelper;

Expand Down Expand Up @@ -490,30 +491,28 @@ public function countItems(&$items, $extension)
}

/**
* Is this an empty state, I.e: no items of this type regardless of the searched for states.
* Manipulate the query to be used to evaluate if this is an Empty State to provide specific conditions for this extension.
*
* @return boolean
* @return DatabaseQuery
*
* @since __DEPLOY_VERSION__
*/
public function getisEmptyState()
protected function getEmptyStateQuery(): DatabaseQuery
{
$extension = $this->getState('filter.extension');
$query = parent::getEmptyStateQuery();

$sql = $this->query
->clear('select')
->clear('values')
->clear('bounded')
->clear('limit')
->clear('order')
->clear('where')
->select('count(*)');
// Remove select because it contains fields from joined tables, that have been removed from join.
$query->clear('select');

$sql->where($this->_db->quoteName('extension') . ' = :extension')
->bind(':extension', $extension);
// Add back a simple count select.
$query->select('count(*)');

// Get the extension from the filter
$extension = $this->getState('filter.extension');

$this->_db->setQuery($sql);
$query->where($this->_db->quoteName('extension') . ' = :extension')
->bind(':extension', $extension);

return !($this->_db->loadResult() > 0);
return $query;
}
}
23 changes: 6 additions & 17 deletions administrator/components/com_tags/src/Model/TagsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\MVC\Model\ListModel;
use Joomla\CMS\Tag\TagServiceInterface;
use Joomla\Database\DatabaseQuery;
use Joomla\Database\ParameterType;

/**
Expand Down Expand Up @@ -327,27 +328,15 @@ public function countItems(&$items, $extension)
}

/**
* Is this an empty state, I.e: no items of this type regardless of the searched for states.
* Manipulate the query to be used to evaluate if this is an Empty State to provide specific conditions for this extension.
*
* @return boolean
* @return DatabaseQuery
*
* @since __DEPLOY_VERSION__
*/
public function getisEmptyState()
protected function getEmptyStateQuery(): DatabaseQuery
{
$sql = $this->query
->clear('select')
->clear('values')
->clear('bounded')
->clear('limit')
->clear('order')
->clear('where')
->select('count(*)');

$sql->where($this->_db->quoteName('alias') . ' != ' . $this->_db->quote('root'));

$this->_db->setQuery($sql);

return !($this->_db->loadResult() > 0);
return parent::getEmptyStateQuery()
->where($this->_db->quoteName('alias') . ' != ' . $this->_db->quote('root'));
}
}
58 changes: 44 additions & 14 deletions libraries/src/MVC/Model/ListModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

\defined('JPATH_PLATFORM') or die;

use Exception;
use Joomla\CMS\Factory;
use Joomla\CMS\Filter\InputFilter;
use Joomla\CMS\Form\Form;
Expand Down Expand Up @@ -118,7 +119,7 @@ class ListModel extends BaseDatabaseModel implements ListModelInterface
* @param MVCFactoryInterface $factory The factory.
*
* @since 1.6
* @throws \Exception
* @throws Exception
*/
public function __construct($config = array(), MVCFactoryInterface $factory = null)
{
Expand Down Expand Up @@ -149,27 +150,56 @@ public function __construct($config = array(), MVCFactoryInterface $factory = nu
}
}

/**
* Provide a query to be used to evaluate if this is an Empty State, can be overridden in the model to provide granular control.
*
* @return DatabaseQuery
*
* @since __DEPLOY_VERSION__
*/
protected function getEmptyStateQuery(): DatabaseQuery
{
$query = clone $this->_getListQuery();

if ($query instanceof DatabaseQuery)
{
$query->clear('bounded')
->clear('group')
->clear('having')
->clear('join')
->clear('limit')
->clear('offset')
->clear('values')
->clear('where');
}

return $query;
}

/**
* Is this an empty state, I.e: no items of this type regardless of the searched for states.
*
* @return boolean
*
* @throws Exception
*
* @since __DEPLOY_VERSION__
*/
public function getIsEmptyState()
public function getIsEmptyState(): bool
{
$sql = $this->query
->clear('select')
->clear('values')
->clear('bounded')
->clear('limit')
->clear('order')
->clear('where')
->select('count(*)');

$this->_db->setQuery($sql);

return !($this->_db->loadResult() > 0);
static $state;

if (empty($state))
{
$state = !($this->_getListCount($this->getEmptyStateQuery()));
}

if (true === $state)
{
$this->resetState($this->getActiveFilters(), true);
}

return $state;
}

/**
Expand Down
26 changes: 26 additions & 0 deletions libraries/src/MVC/Model/StateBehaviorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

\defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Object\CMSObject;

/**
Expand Down Expand Up @@ -99,4 +100,29 @@ public function setState($property, $value = null)
protected function populateState()
{
}

/**
* Reset active filter states.
*
* @param array $activefilters The array of filters to clear.
* @param boolean $session Should we clear the userState also.
*
* @throws \Exception
*
* @since __DEPLOY_VERSION__
*/
public function resetState(array $activefilters, bool $session = false): void
{
$app = Factory::getApplication();

foreach ($activefilters as $activefilter => $filterstate)
{
if ($session === true)
{
$app->setUserState($this->context . '.filter.' . $activefilter, '');
}

$this->setState('filter.' . $activefilter, '');
}
}
}