Skip to content

Commit 323a799

Browse files
[5.0] Filter by category - levels (#39950)
--------- Co-authored-by: Harald Leithner <[email protected]>
1 parent f78234c commit 323a799

File tree

3 files changed

+62
-21
lines changed

3 files changed

+62
-21
lines changed

administrator/components/com_categories/forms/filter_categories.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@
6363
>
6464
<option value="">JOPTION_SELECT_MAX_LEVELS</option>
6565
</field>
66+
67+
<field
68+
name="category_id"
69+
type="category"
70+
label="JOPTION_SELECT_CATEGORY"
71+
multiple="true"
72+
extension="dynamic"
73+
layout="joomla.form.field.list-fancy-select"
74+
hint="JOPTION_SELECT_CATEGORY"
75+
published="0,1,2"
76+
class="js-select-submit-on-change"
77+
/>
6678
</fields>
6779

6880
<fields name="list">

administrator/components/com_categories/src/Model/CategoriesModel.php

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Joomla\CMS\Language\Associations;
1717
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
1818
use Joomla\CMS\MVC\Model\ListModel;
19+
use Joomla\CMS\Table\Table;
1920
use Joomla\Database\DatabaseQuery;
2021
use Joomla\Database\ParameterType;
2122
use Joomla\Utilities\ArrayHelper;
@@ -66,6 +67,7 @@ public function __construct($config = [], MVCFactoryInterface $factory = null)
6667
'level', 'a.level',
6768
'path', 'a.path',
6869
'tag',
70+
'category_id', 'a.id',
6971
];
7072
}
7173

@@ -239,8 +241,31 @@ protected function getListQuery()
239241
->bind(':extension', $extension);
240242
}
241243

242-
// Filter on the level.
243-
if ($level = (int) $this->getState('filter.level')) {
244+
// Filter by categories and by level
245+
$categoryId = $this->getState('filter.category_id', []);
246+
$level = $this->getState('filter.level');
247+
248+
if (!is_array($categoryId)) {
249+
$categoryId = $categoryId ? [$categoryId] : [];
250+
}
251+
252+
// Case: Using both categories filter and by level filter
253+
if (count($categoryId)) {
254+
$categoryTable = Table::getInstance('Category', 'JTable');
255+
$subCatItemsWhere = [];
256+
257+
foreach ($categoryId as $filterCatId) {
258+
$categoryTable->load($filterCatId);
259+
$subCatItemsWhere[] = '(' .
260+
($level ? 'a.level <= ' . ((int) $level + (int) $categoryTable->level - 1) . ' AND ' : '') .
261+
'a.lft >= ' . (int) $categoryTable->lft . ' AND ' .
262+
'a.rgt <= ' . (int) $categoryTable->rgt . ')';
263+
}
264+
265+
$query->where('(' . implode(' OR ', $subCatItemsWhere) . ')');
266+
267+
// Case: Using only the by level filter
268+
} elseif ($level) {
244269
$query->where($db->quoteName('a.level') . ' <= :level')
245270
->bind(':level', $level, ParameterType::INTEGER);
246271
}
@@ -355,25 +380,25 @@ protected function getListQuery()
355380

356381
// Group by on Categories for \JOIN with component tables to count items
357382
$query->group('a.id,
358-
a.title,
359-
a.alias,
360-
a.note,
361-
a.published,
362-
a.access,
363-
a.checked_out,
364-
a.checked_out_time,
365-
a.created_user_id,
366-
a.path,
367-
a.parent_id,
368-
a.level,
369-
a.lft,
370-
a.rgt,
371-
a.language,
372-
l.title,
373-
l.image,
374-
uc.name,
375-
ag.title,
376-
ua.name');
383+
a.title,
384+
a.alias,
385+
a.note,
386+
a.published,
387+
a.access,
388+
a.checked_out,
389+
a.checked_out_time,
390+
a.created_user_id,
391+
a.path,
392+
a.parent_id,
393+
a.level,
394+
a.lft,
395+
a.rgt,
396+
a.language,
397+
l.title,
398+
l.image,
399+
uc.name,
400+
ag.title,
401+
ua.name');
377402

378403
return $query;
379404
}

administrator/components/com_categories/src/View/Categories/HtmlView.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ public function display($tpl = null)
146146
}
147147
}
148148

149+
// If filter by category is active we need to know the extension name to filter the categories
150+
$extensionName = $this->escape($this->state->get('filter.extension'));
151+
$this->filterForm->setFieldAttribute('category_id', 'extension', $extensionName, 'filter');
152+
149153
parent::display($tpl);
150154
}
151155

0 commit comments

Comments
 (0)