Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing missing support for "Subcategory Levels" parameter in contacts category view #41618

Merged
merged 11 commits into from Jan 24, 2024
33 changes: 32 additions & 1 deletion components/com_contact/src/Model/CategoryModel.php
Expand Up @@ -177,7 +177,37 @@ protected function getListQuery()
->whereIn($db->quoteName('a.access'), $groups);

// Filter by category.
if ($categoryId = $this->getState('category.id')) {
$categoryId = (int) $this->getState('category.id');
$includeSubcategories = (int) $this->getState('filter.max_category_levels', 1) !== 0;

if ($includeSubcategories) {
$levels = (int) $this->getState('filter.max_category_levels', 1);

// Create a subquery for the subcategory list
$subQuery = $db->getQuery(true)
->select($db->quoteName('sub.id'))
->from($db->quoteName('#__categories', 'sub'))
->join(
'INNER',
$db->quoteName('#__categories', 'this'),
$db->quoteName('sub.lft') . ' > ' . $db->quoteName('this.lft')
. ' AND ' . $db->quoteName('sub.rgt') . ' < ' . $db->quoteName('this.rgt')
)
->where($db->quoteName('this.id') . ' = :subCategoryId');

$query->bind(':subCategoryId', $categoryId, ParameterType::INTEGER);

if ($levels >= 0) {
$subQuery->where($db->quoteName('sub.level') . ' <= ' . $db->quoteName('this.level') . ' + :levels');
$query->bind(':levels', $levels, ParameterType::INTEGER);
}

// Add the subquery to the main query
$query->where(
'(' . $db->quoteName('a.catid') . ' = :categoryId OR ' . $db->quoteName('a.catid') . ' IN (' . $subQuery . '))'
);
$query->bind(':categoryId', $categoryId, ParameterType::INTEGER);
} else {
$query->where($db->quoteName('a.catid') . ' = :acatid')
->whereIn($db->quoteName('c.access'), $groups);
$query->bind(':acatid', $categoryId, ParameterType::INTEGER);
Expand Down Expand Up @@ -302,6 +332,7 @@ protected function populateState($ordering = null, $direction = null)

$id = $input->get('id', 0, 'int');
$this->setState('category.id', $id);
$this->setState('filter.max_category_levels', $params->get('maxLevel', 1));

$user = $this->getCurrentUser();

Expand Down