1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Admin\Http\Service\RepoGrid; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Databases\Queries\FoundRows; |
8
|
|
|
use AbterPhp\Framework\Grid\Factory\IBase as GridFactory; |
9
|
|
|
use AbterPhp\Framework\Grid\Grid; |
10
|
|
|
use AbterPhp\Framework\Grid\IGrid; |
11
|
|
|
use AbterPhp\Framework\Http\Service\RepoGrid\IRepoGrid; |
12
|
|
|
use AbterPhp\Framework\I18n\ITranslator; |
13
|
|
|
use AbterPhp\Framework\Orm\IGridRepo; |
14
|
|
|
use Casbin\Enforcer; |
15
|
|
|
use Opulence\Http\Collection; |
16
|
|
|
|
17
|
|
|
abstract class RepoGridAbstract implements IRepoGrid |
18
|
|
|
{ |
19
|
|
|
/** @var Enforcer */ |
20
|
|
|
protected $enforcer; |
21
|
|
|
|
22
|
|
|
/** @var IGridRepo */ |
23
|
|
|
protected $repo; |
24
|
|
|
|
25
|
|
|
/** @var FoundRows */ |
26
|
|
|
protected $foundRows; |
27
|
|
|
|
28
|
|
|
/** @var GridFactory */ |
29
|
|
|
protected $gridFactory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* GridAbstract constructor. |
33
|
|
|
* |
34
|
|
|
* @param Enforcer $enforcer |
35
|
|
|
* @param ITranslator $translator |
36
|
|
|
* @param IGridRepo $repo |
37
|
|
|
* @param FoundRows $foundRows |
38
|
|
|
* @param GridFactory $gridFactory |
39
|
|
|
*/ |
40
|
|
|
public function __construct( |
41
|
|
|
Enforcer $enforcer, |
42
|
|
|
IGridRepo $repo, |
43
|
|
|
FoundRows $foundRows, |
44
|
|
|
GridFactory $gridFactory |
45
|
|
|
) { |
46
|
|
|
$this->enforcer = $enforcer; |
47
|
|
|
$this->repo = $repo; |
48
|
|
|
$this->foundRows = $foundRows; |
49
|
|
|
$this->gridFactory = $gridFactory; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Collection $query |
54
|
|
|
* @param string $baseUrl |
55
|
|
|
* |
56
|
|
|
* @return IGrid |
57
|
|
|
*/ |
58
|
|
|
public function createAndPopulate(Collection $query, string $baseUrl): IGrid |
59
|
|
|
{ |
60
|
|
|
$grid = $this->gridFactory->createGrid($query->getAll(), $baseUrl); |
61
|
|
|
|
62
|
|
|
$pageSize = $grid->getPageSize(); |
63
|
|
|
$limitFrom = $this->getOffset($query, $pageSize); |
64
|
|
|
|
65
|
|
|
$sortBy = $this->getSortConditions($grid); |
66
|
|
|
$where = $this->getWhereConditions($grid); |
67
|
|
|
$params = $this->getSqlParams($grid); |
68
|
|
|
|
69
|
|
|
$entities = $this->repo->getPage($limitFrom, $pageSize, $sortBy, $where, $params); |
70
|
|
|
$maxCount = $this->foundRows->get(); |
71
|
|
|
|
72
|
|
|
$grid->setTotalCount($maxCount)->setEntities($entities); |
73
|
|
|
|
74
|
|
|
return $grid; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param Grid $grid |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
protected function getSortConditions(Grid $grid): array |
83
|
|
|
{ |
84
|
|
|
return $grid->getSortConditions(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param Grid $grid |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
protected function getWhereConditions(Grid $grid): array |
93
|
|
|
{ |
94
|
|
|
return $grid->getWhereConditions(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param Grid $grid |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
protected function getSqlParams(Grid $grid): array |
103
|
|
|
{ |
104
|
|
|
return $grid->getSqlParams(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param Collection $query |
109
|
|
|
* @param int $pageSize |
110
|
|
|
* |
111
|
|
|
* @return int |
112
|
|
|
*/ |
113
|
|
|
protected function getOffset(Collection $query, int $pageSize): int |
114
|
|
|
{ |
115
|
|
|
$page = (int)$query->get('page', 1); |
116
|
|
|
$offset = ($page - 1) * $pageSize; |
117
|
|
|
|
118
|
|
|
return $offset; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|