|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (C) 2020-2022 Jan Böhmer |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as published |
|
7
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace App\Admin\Filter; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
use App\Entity\Department; |
|
23
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
24
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Filter\FilterInterface; |
|
25
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto; |
|
26
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto; |
|
27
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Dto\FilterDataDto; |
|
28
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Dto\FilterDto; |
|
29
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Filter\FilterTrait; |
|
30
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Form\Filter\Type\BooleanFilterType; |
|
31
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Form\Filter\Type\TextFilterType; |
|
32
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Filter\TextFilter; |
|
33
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
|
34
|
|
|
use Symfony\Component\Uid\Ulid; |
|
35
|
|
|
|
|
36
|
|
|
class ULIDFilter implements FilterInterface |
|
37
|
|
|
{ |
|
38
|
|
|
use FilterTrait; |
|
39
|
|
|
|
|
40
|
|
|
public static function new(string $propertyName, $label = null): self |
|
41
|
|
|
{ |
|
42
|
|
|
return (new self()) |
|
43
|
|
|
->setFilterFqcn(__CLASS__) |
|
44
|
|
|
->setProperty($propertyName) |
|
45
|
|
|
->setLabel($label) |
|
46
|
|
|
->setFormType(TextType::class); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
public function apply( |
|
51
|
|
|
QueryBuilder $queryBuilder, |
|
52
|
|
|
FilterDataDto $filterDataDto, |
|
53
|
|
|
?FieldDto $fieldDto, |
|
54
|
|
|
EntityDto $entityDto |
|
55
|
|
|
): void { |
|
56
|
|
|
$alias = $filterDataDto->getEntityAlias(); |
|
57
|
|
|
$property = $filterDataDto->getProperty(); |
|
58
|
|
|
$parameterName = $filterDataDto->getParameterName(); |
|
59
|
|
|
$value = $filterDataDto->getValue(); |
|
60
|
|
|
|
|
61
|
|
|
//Only apply filter if provided string is a valid ULID, otherwise we run into an exception |
|
62
|
|
|
if(Ulid::isValid($value)) { |
|
63
|
|
|
$queryBuilder->andWhere(sprintf('%s.%s = :%s', $alias, $property, $parameterName)) |
|
64
|
|
|
->setParameter($parameterName, Ulid::fromString($value), 'ulid'); |
|
65
|
|
|
} else { //Otherwise just ensure no results are returned using an impossible SQL statement |
|
66
|
|
|
$queryBuilder->andWhere("0 = 1"); |
|
67
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |