1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Pfilsx\DataGrid\Grid\Providers; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use DateTime; |
8
|
|
|
use Doctrine\Common\Collections\Criteria; |
9
|
|
|
use Doctrine\ORM\EntityRepository; |
10
|
|
|
use Pfilsx\DataGrid\Grid\Items\EntityGridItem; |
11
|
|
|
use Symfony\Bridge\Doctrine\ManagerRegistry; |
12
|
|
|
|
13
|
|
|
class RepositoryDataProvider extends DataProvider |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var EntityRepository |
18
|
|
|
*/ |
19
|
|
|
protected $repository; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Criteria |
23
|
|
|
*/ |
24
|
|
|
protected $criteria; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
public function __construct(EntityRepository $repository, ManagerRegistry $manager) |
28
|
|
|
{ |
29
|
|
|
$this->repository = $repository; |
30
|
|
|
parent::__construct($manager); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
public function getItems(): array |
35
|
|
|
{ |
36
|
|
|
$this->getCriteria() |
37
|
|
|
->setMaxResults($this->getPager()->getLimit()) |
38
|
|
|
->setFirstResult($this->getPager()->getFirst()); |
39
|
|
|
$result = $this->repository->matching($this->getCriteria())->toArray(); |
40
|
|
|
if (!empty($result)){ |
41
|
|
|
$identifier = $this->getEntityIdentifier(get_class($result[0])); |
42
|
|
|
return array_map(function ($entity) use ($identifier) { |
43
|
|
|
$item = new EntityGridItem($entity, $identifier); |
44
|
|
|
return $item; |
45
|
|
|
}, $result); |
46
|
|
|
} |
47
|
|
|
return $result; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getTotalCount(): int |
51
|
|
|
{ |
52
|
|
|
return $this->repository->matching($this->getCriteria())->count(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return Criteria |
57
|
|
|
*/ |
58
|
|
|
public function getCriteria() |
59
|
|
|
{ |
60
|
|
|
return $this->criteria ?? ($this->criteria = Criteria::create()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function setSort(array $sort): DataProviderInterface |
64
|
|
|
{ |
65
|
|
|
$this->getCriteria()->orderBy($sort); |
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function addEqualFilter(string $attribute, $value): DataProviderInterface |
70
|
|
|
{ |
71
|
|
|
if ($value === null) { |
72
|
|
|
$this->getCriteria()->andWhere(Criteria::expr()->isNull($attribute)); |
73
|
|
|
} else { |
74
|
|
|
$this->getCriteria()->andWhere(Criteria::expr()->eq($attribute, $value)); |
75
|
|
|
} |
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function addLikeFilter(string $attribute, $value): DataProviderInterface |
80
|
|
|
{ |
81
|
|
|
$this->getCriteria()->andWhere(Criteria::expr()->contains($attribute, $value)); |
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function addRelationFilter(string $attribute, $value, string $relationClass): DataProviderInterface |
86
|
|
|
{ |
87
|
|
|
$identifier = $this->getEntityIdentifier($relationClass); |
88
|
|
|
if ($identifier !== null) { |
89
|
|
|
$repository = $this->entityManager->getRepository($relationClass); |
90
|
|
|
$entity = $repository->findOneBy([$identifier => $value]); |
91
|
|
|
$this->getCriteria()->andWhere(Criteria::expr()->eq($attribute, $entity)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function addCustomFilter(string $attribute, $value, callable $callback): DataProviderInterface |
98
|
|
|
{ |
99
|
|
|
$criteria = $this->getCriteria(); |
100
|
|
|
call_user_func_array($callback, [&$criteria, $attribute, $value]); |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
protected function equalDate($attribute, $value): void |
105
|
|
|
{ |
106
|
|
|
$date = new DateTime($value); |
107
|
|
|
$nextDate = (clone $date)->modify('+1 day'); |
108
|
|
|
$this->getCriteria() |
109
|
|
|
->andWhere(Criteria::expr()->gte($attribute, $date)) |
110
|
|
|
->andWhere(Criteria::expr()->lt($attribute, $nextDate)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function ltDate($attribute, $value): void |
114
|
|
|
{ |
115
|
|
|
$date = new DateTime($value); |
116
|
|
|
$this->getCriteria() |
117
|
|
|
->andWhere(Criteria::expr()->lt($attribute, $date)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
protected function lteDate($attribute, $value): void |
121
|
|
|
{ |
122
|
|
|
$date = (new DateTime($value))->modify('+1 day'); |
123
|
|
|
$this->getCriteria() |
124
|
|
|
->andWhere(Criteria::expr()->lt($attribute, $date)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
protected function gtDate($attribute, $value): void |
128
|
|
|
{ |
129
|
|
|
$date = (new DateTime($value))->modify('+1 day'); |
130
|
|
|
$this->getCriteria() |
131
|
|
|
->andWhere(Criteria::expr()->gte($attribute, $date)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
protected function gteDate($attribute, $value): void |
135
|
|
|
{ |
136
|
|
|
$date = new DateTime($value); |
137
|
|
|
$this->getCriteria() |
138
|
|
|
->andWhere(Criteria::expr()->gte($attribute, $date)); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|