-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
CategoryFilter.php
125 lines (101 loc) · 3.5 KB
/
CategoryFilter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\ClassificationBundle\Admin\Filter;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\AdminBundle\Form\Type\Filter\DefaultType;
use Sonata\ClassificationBundle\Model\CategoryInterface;
use Sonata\ClassificationBundle\Model\CategoryManagerInterface;
use Sonata\DoctrineORMAdminBundle\Filter\Filter;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
final class CategoryFilter extends Filter
{
/**
* @var CategoryManagerInterface
*/
private $categoryManager;
public function __construct(CategoryManagerInterface $categoryManager)
{
$this->categoryManager = $categoryManager;
}
public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data): void
{
if (null === $data || !\is_array($data) || !\array_key_exists('value', $data)) {
return;
}
if (null !== $data['value']) {
$queryBuilder
->andWhere(sprintf('%s.%s = :category', $alias, $field))
->setParameter('category', $data['value'])
;
}
$this->active = null !== $data['value'];
}
public function getDefaultOptions(): array
{
return [
'context' => null,
];
}
public function getFieldType(): string
{
return $this->getOption('field_type', ChoiceType::class);
}
public function getFieldOptions(): array
{
return $this->getOption('choices', [
'choices' => $this->getChoices(),
'choice_translation_domain' => false,
]);
}
public function getRenderSettings(): array
{
return [DefaultType::class, [
'field_type' => $this->getFieldType(),
'field_options' => $this->getFieldOptions(),
'label' => $this->getLabel(),
]];
}
protected function association(ProxyQueryInterface $queryBuilder, $data): array
{
$alias = $queryBuilder->entityJoin($this->getParentAssociationMappings());
$part = strrchr('.'.$this->getFieldName(), '.');
$fieldName = substr(false === $part ? $this->getFieldType() : $part, 1);
return [$alias, $fieldName];
}
/**
* @return array<string, int>
*/
private function getChoices(): array
{
$context = $this->getOption('context');
if (null === $context) {
$categories = $this->categoryManager->getAllRootCategories();
} else {
$categories = $this->categoryManager->getRootCategoriesForContext($context);
}
$choices = [];
foreach ($categories as $category) {
$choices[sprintf('%s (%s)', $category->getName(), $category->getContext()->getId())] = $category->getId();
$this->visitChild($category, $choices);
}
return $choices;
}
private function visitChild(CategoryInterface $category, array &$choices, int $level = 2): void
{
if (0 === \count($category->getChildren())) {
return;
}
foreach ($category->getChildren() as $child) {
$choices[sprintf('%s %s', str_repeat('-', 1 * $level), (string) $child)] = $child->getId();
$this->visitChild($child, $choices, $level + 1);
}
}
}