|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Admin\Http\Controllers\Admin; |
|
6
|
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Assets\AssetManager; |
|
8
|
|
|
use AbterPhp\Framework\Constant\Event; |
|
9
|
|
|
use AbterPhp\Framework\Databases\Queries\FoundRows; |
|
10
|
|
|
use AbterPhp\Framework\Domain\Entities\IStringerEntity; |
|
11
|
|
|
use AbterPhp\Framework\Events\GridReady; |
|
12
|
|
|
use AbterPhp\Framework\Grid\Factory\IBase as GridFactory; |
|
13
|
|
|
use AbterPhp\Framework\Grid\Pagination\Options as PaginationOptions; |
|
14
|
|
|
use AbterPhp\Framework\Http\Service\RepoGrid\IRepoGrid; |
|
15
|
|
|
use AbterPhp\Framework\I18n\ITranslator; |
|
16
|
|
|
use AbterPhp\Framework\Orm\IGridRepo; |
|
17
|
|
|
use AbterPhp\Framework\Session\FlashService; |
|
18
|
|
|
use Opulence\Events\Dispatchers\IEventDispatcher; |
|
19
|
|
|
use Opulence\Http\Responses\Response; |
|
20
|
|
|
use Opulence\Routing\Urls\UrlGenerator; |
|
21
|
|
|
use Psr\Log\LoggerInterface; |
|
22
|
|
|
|
|
23
|
|
|
abstract class GridAbstract extends AdminAbstract |
|
24
|
|
|
{ |
|
25
|
|
|
const ENTITY_PLURAL = ''; |
|
26
|
|
|
const ENTITY_TITLE_PLURAL = ''; |
|
27
|
|
|
|
|
28
|
|
|
const VIEW_LIST = 'contents/backend/grid'; |
|
29
|
|
|
|
|
30
|
|
|
const VAR_GRID = 'grid'; |
|
31
|
|
|
const VAR_CREATE_URL = 'createUrl'; |
|
32
|
|
|
|
|
33
|
|
|
const TITLE_SHOW = 'framework:titleList'; |
|
34
|
|
|
|
|
35
|
|
|
const URL_CREATE = '%s-create'; |
|
36
|
|
|
|
|
37
|
|
|
const RESOURCE_DEFAULT = '%s-grid'; |
|
38
|
|
|
const RESOURCE_HEADER = '%s-header-grid'; |
|
39
|
|
|
const RESOURCE_FOOTER = '%s-footer-grid'; |
|
40
|
|
|
const RESOURCE_TYPE = 'grid'; |
|
41
|
|
|
|
|
42
|
|
|
/** @var IGridRepo */ |
|
43
|
|
|
protected $gridRepo; |
|
44
|
|
|
|
|
45
|
|
|
/** @var FoundRows */ |
|
46
|
|
|
protected $foundRows; |
|
47
|
|
|
|
|
48
|
|
|
/** @var GridFactory */ |
|
49
|
|
|
protected $gridFactory; |
|
50
|
|
|
|
|
51
|
|
|
/** @var PaginationOptions */ |
|
52
|
|
|
protected $paginationOptions; |
|
53
|
|
|
|
|
54
|
|
|
/** @var AssetManager */ |
|
55
|
|
|
protected $assets; |
|
56
|
|
|
|
|
57
|
|
|
/** @var IRepoGrid */ |
|
58
|
|
|
protected $repoGrid; |
|
59
|
|
|
|
|
60
|
|
|
/** @var IEventDispatcher */ |
|
61
|
|
|
protected $eventDispatcher; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* GridAbstract constructor. |
|
65
|
|
|
* |
|
66
|
|
|
* @param FlashService $flashService |
|
67
|
|
|
* @param ITranslator $translator |
|
68
|
|
|
* @param UrlGenerator $urlGenerator |
|
69
|
|
|
* @param LoggerInterface $logger |
|
70
|
|
|
* @param AssetManager $assets |
|
71
|
|
|
* @param IRepoGrid $repoGrid |
|
72
|
|
|
* @param IEventDispatcher $eventDispatcher |
|
73
|
|
|
*/ |
|
74
|
|
|
public function __construct( |
|
75
|
|
|
FlashService $flashService, |
|
76
|
|
|
ITranslator $translator, |
|
77
|
|
|
UrlGenerator $urlGenerator, |
|
78
|
|
|
LoggerInterface $logger, |
|
79
|
|
|
AssetManager $assets, |
|
80
|
|
|
IRepoGrid $repoGrid, |
|
81
|
|
|
IEventDispatcher $eventDispatcher |
|
82
|
|
|
) { |
|
83
|
|
|
parent::__construct($flashService, $translator, $urlGenerator, $logger); |
|
84
|
|
|
|
|
85
|
|
|
$this->assets = $assets; |
|
86
|
|
|
$this->repoGrid = $repoGrid; |
|
87
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return Response |
|
92
|
|
|
* @throws \Casbin\Exceptions\CasbinException |
|
93
|
|
|
* @throws \Throwable |
|
94
|
|
|
*/ |
|
95
|
|
|
public function show(): Response |
|
96
|
|
|
{ |
|
97
|
|
|
$grid = $this->repoGrid->createAndPopulate($this->request->getQuery(), $this->getBaseUrl()); |
|
98
|
|
|
|
|
99
|
|
|
$this->eventDispatcher->dispatch(Event::GRID_READY, new GridReady($grid)); |
|
100
|
|
|
|
|
101
|
|
|
$grid->setTranslator($this->translator); |
|
102
|
|
|
|
|
103
|
|
|
$title = $this->translator->translate(static::TITLE_SHOW, static::ENTITY_TITLE_PLURAL); |
|
104
|
|
|
|
|
105
|
|
|
$this->view = $this->viewFactory->createView(static::VIEW_LIST); |
|
106
|
|
|
$this->view->setVar(static::VAR_GRID, $grid); |
|
107
|
|
|
$this->view->setVar(static::VAR_CREATE_URL, $this->getCreateUrl()); |
|
108
|
|
|
|
|
109
|
|
|
$this->addCustomAssets(); |
|
110
|
|
|
|
|
111
|
|
|
return $this->createResponse($title); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return Response |
|
116
|
|
|
* @throws \Casbin\Exceptions\CasbinException |
|
117
|
|
|
* @throws \Throwable |
|
118
|
|
|
*/ |
|
119
|
|
|
public function list(): Response |
|
120
|
|
|
{ |
|
121
|
|
|
$grid = $this->repoGrid->createAndPopulate($this->request->getQuery(), $this->getBaseUrl()); |
|
122
|
|
|
|
|
123
|
|
|
$this->eventDispatcher->dispatch(Event::GRID_READY, new GridReady($grid)); |
|
124
|
|
|
|
|
125
|
|
|
$grid->setTranslator($this->translator); |
|
126
|
|
|
|
|
127
|
|
|
$title = $this->translator->translate(static::TITLE_SHOW, static::ENTITY_TITLE_PLURAL); |
|
128
|
|
|
|
|
129
|
|
|
$this->view = $this->viewFactory->createView(static::VIEW_LIST); |
|
130
|
|
|
$this->view->setVar(static::VAR_GRID, $grid); |
|
131
|
|
|
$this->view->setVar(static::VAR_CREATE_URL, $this->getCreateUrl()); |
|
132
|
|
|
|
|
133
|
|
|
$this->addCustomAssets(); |
|
134
|
|
|
|
|
135
|
|
|
return $this->createResponse($title); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param IStringerEntity|null $entity |
|
140
|
|
|
*/ |
|
141
|
|
|
protected function addCustomAssets(?IStringerEntity $entity = null) |
|
142
|
|
|
{ |
|
143
|
|
|
$this->prepareCustomAssets(); |
|
144
|
|
|
|
|
145
|
|
|
$this->addTypeAssets(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
protected function addTypeAssets() |
|
149
|
|
|
{ |
|
150
|
|
|
$groupName = $this->getResourceTypeName(static::RESOURCE_FOOTER); |
|
151
|
|
|
|
|
152
|
|
|
$this->assets->addJs($groupName, '/admin-assets/js/hideable-container.js'); |
|
153
|
|
|
$this->assets->addJs($groupName, '/admin-assets/js/filters.js'); |
|
154
|
|
|
$this->assets->addJs($groupName, '/admin-assets/js/tooltips.js'); |
|
155
|
|
|
$this->assets->addJs($groupName, '/admin-assets/js/pagination.js'); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @return string |
|
160
|
|
|
* @throws \Opulence\Routing\Urls\URLException |
|
161
|
|
|
*/ |
|
162
|
|
|
protected function getBaseUrl(): string |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->urlGenerator->createFromName(static::ROUTING_PATH) . '?'; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @return string |
|
169
|
|
|
* @throws \Opulence\Routing\Urls\URLException |
|
170
|
|
|
*/ |
|
171
|
|
|
protected function getCreateUrl(): string |
|
172
|
|
|
{ |
|
173
|
|
|
$urlName = sprintf(static::URL_CREATE, static::ROUTING_PATH); |
|
174
|
|
|
$url = $this->urlGenerator->createFromName($urlName); |
|
175
|
|
|
|
|
176
|
|
|
return $url; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|