Skip to content

Commit 45668ab

Browse files
committed
add parent category flare filter
1 parent 2ebc388 commit 45668ab

9 files changed

Lines changed: 106 additions & 18 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This bundle allows to assign nestable categories to arbitrary entities in Contao
1111
- conveniently add single category fields (radio button) or multiple categories fields (checkbox) via a simple function call
1212
- specify overridable properties in categories and compute the correct result depending on the given context easily
1313
- [DC_Multilingual](https://github.com/terminal42/contao-DC_Multilingual) support
14-
- categories filter type for [Contao Filter Bundle](https://github.com/heimrichhannot/contao-filter-bundle)
14+
- [Flare Bundle](https://github.com/heimrichhannot/contao-flare-bundle) support
1515

1616
## Impressions
1717

config/flare.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
HeimrichHannot\CategoriesBundle\Flare\:
3+
resource: '../src/Flare/*'
4+
autowire: true
5+
autoconfigure: true

contao/languages/de/default.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* @license LGPL-3.0-or-later
77
*/
88

9+
use HeimrichHannot\CategoriesBundle\Flare\FilterElement\ParentCategoryFilterElement;
10+
911
$GLOBALS['TL_LANG']['MSC']['categoriesBundle'] = [
1012
'configsAvailable' => 'Konfigurationen verfügbar',
1113
'primaryCategory' => 'Primäre Kategorie',
@@ -16,3 +18,6 @@
1618
*/
1719
$GLOBALS['TL_LANG']['MSC']['cm_resetCategories'] = ['Alle Kategorien', 'Zeigt Nachrichten aus allen Kategorien'];
1820
$GLOBALS['TL_LANG']['MSC']['categoryPicker'] = 'Kategorien';
21+
22+
$flare['filter'][ParentCategoryFilterElement::TYPE][0] = 'Elternkategorie (Kindkategorien der ausgewählten Kategorie)';
23+
$flare['filter'][ParentCategoryFilterElement::TYPE][1] = 'Filtert Einträge nach den Kindkategorien der ausgewählten Kategorie.';

contao/languages/en/default.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* @license LGPL-3.0-or-later
77
*/
88

9+
use HeimrichHannot\CategoriesBundle\Flare\FilterElement\ParentCategoryFilterElement;
10+
911
$GLOBALS['TL_LANG']['MSC']['categoriesBundle'] = [
1012
'configsAvailable' => 'Configuration available',
1113
'primaryCategory' => 'Primary category',
@@ -16,3 +18,6 @@
1618
*/
1719
$GLOBALS['TL_LANG']['MSC']['cm_resetCategories'] = ['All categories', 'Show news from all categories'];
1820
$GLOBALS['TL_LANG']['MSC']['categoryPicker'] = 'Categories';
21+
22+
$flare['filter'][ParentCategoryFilterElement::TYPE][0] = 'Parent Category (child categories of the selected category)';
23+
$flare['filter'][ParentCategoryFilterElement::TYPE][1] = 'Filters entries by the child categories of the selected category.';

src/CategoriesBundle.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,24 @@
88

99
namespace HeimrichHannot\CategoriesBundle;
1010

11-
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
12-
use HeimrichHannot\CategoriesBundle\DependencyInjection\CategoriesExtension;
13-
use Symfony\Component\HttpKernel\Bundle\Bundle;
11+
use Composer\InstalledVersions;
12+
use Symfony\Component\DependencyInjection\ContainerBuilder;
13+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
14+
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
1415

15-
class CategoriesBundle extends Bundle
16+
class CategoriesBundle extends AbstractBundle
1617
{
17-
/**
18-
* {@inheritdoc}
19-
*/
20-
public function getContainerExtension(): CategoriesExtension|ExtensionInterface|null
18+
public function getPath(): string
2119
{
22-
return new CategoriesExtension();
20+
return \dirname(__DIR__);
2321
}
2422

25-
public function getPath(): string
23+
public function loadExtension(array $config, ContainerConfigurator $configurator, ContainerBuilder $builder): void
2624
{
27-
return \dirname(__DIR__);
25+
$configurator->import('../config/services.yaml');
26+
27+
if (InstalledVersions::isInstalled('heimrichhannot/contao-flare-bundle')) {
28+
$configurator->import('../config/flare.yaml');
29+
}
2830
}
2931
}

src/DependencyInjection/CategoriesExtension.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,25 @@
88

99
namespace HeimrichHannot\CategoriesBundle\DependencyInjection;
1010

11+
use Composer\InstalledVersions;
1112
use Symfony\Component\Config\FileLocator;
1213
use Symfony\Component\DependencyInjection\ContainerBuilder;
1314
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
1415
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
1516
use Symfony\Component\DependencyInjection\Extension\Extension;
1617

17-
class CategoriesExtension extends Extension implements PrependExtensionInterface
18+
class CategoriesExtension extends Extension
1819
{
1920
/**
2021
* {@inheritdoc}
2122
*/
2223
public function load(array $configs, ContainerBuilder $container):void
23-
{
24-
25-
}
26-
public function prepend(ContainerBuilder $container): void
2724
{
2825
$loader = new YamlFileLoader($container, new FileLocator(\dirname(__DIR__) . '/../config'));
26+
$loader->load('services.yaml');
2927

30-
$loader->load('services.yml');
28+
if (InstalledVersions::isInstalled('heimrichhannot/contao-flare-bundle')) {
29+
$loader->load('flare.yaml');
30+
}
3131
}
3232
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace HeimrichHannot\CategoriesBundle\Flare\FilterElement;
4+
5+
use Contao\StringUtil;
6+
use HeimrichHannot\CategoriesBundle\Model\CategoryModel;
7+
use HeimrichHannot\FlareBundle\DependencyInjection\Attribute\AsFilterElement;
8+
use HeimrichHannot\FlareBundle\Filter\FilterContext;
9+
use HeimrichHannot\FlareBundle\Filter\FilterQueryBuilder;
10+
use HeimrichHannot\FlareBundle\FilterElement\AbstractFilterElement;
11+
use HeimrichHannot\FlareBundle\Form\ChoicesBuilder;
12+
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
13+
14+
#[AsFilterElement(
15+
alias: self::TYPE,
16+
palette: '{fieldGeneric_legend},fieldGeneric,filterCategory;{form_legend},placeholder',
17+
formType: ChoiceType::class,
18+
)]
19+
class ParentCategoryFilterElement extends AbstractFilterElement
20+
{
21+
public const TYPE = 'parent_category';
22+
23+
public function __invoke(FilterContext $context, FilterQueryBuilder $qb): void
24+
{
25+
if (!$context->getSubmittedData()) {
26+
return;
27+
}
28+
29+
if (!$targetField = $context->getFilterModel()->fieldGeneric) {
30+
$qb->abort();
31+
}
32+
33+
$qb->where($qb->expr()->like($targetField, ':category'))
34+
->setParameter('category', '%"' . $context->getSubmittedData() . '"%');
35+
}
36+
37+
public function getFormTypeOptions(FilterContext $context, ChoicesBuilder $choices): array
38+
{
39+
$options = $this->defaultFormTypeOptions(
40+
$context,
41+
['placeholder' => true]
42+
);
43+
$options['choices'] = [];
44+
45+
$categoryIds = StringUtil::deserialize($context->getFilterModel()->filterCategory, true);
46+
47+
if (empty($categoryIds)) {
48+
return $options;
49+
}
50+
51+
$categories = CategoryModel::findByPids($categoryIds);
52+
53+
while ($categories->next()) {
54+
$options['choices'][$categories->title] = (string) $categories->id;
55+
}
56+
57+
return $options;
58+
}
59+
}

src/Model/CategoryModel.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ class CategoryModel extends AbstractModel
3030
{
3131
protected static $strTable = 'tl_category';
3232

33+
public static function findByPids(array $pids, array $options = []): ?Collection
34+
{
35+
$t = static::getTable();
36+
$parents = implode(',', array_map('intval', $pids));
37+
38+
return static::findBy(
39+
["$t.pid IN (".$parents.")",],
40+
[],
41+
$options
42+
);
43+
}
44+
3345
/**
3446
* @return CategoryModel|CategoryModel[]|Collection|null
3547
*/

0 commit comments

Comments
 (0)