Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
gridcomponent.php
1<?php
2
4
17use CBitrixComponent;
18use CMain;
19use Iterator;
20use Throwable;
21
25abstract class GridComponent extends CBitrixComponent
26{
27 private array $rowsFilter;
28 private Options $gridOptions;
29
31
39 abstract protected function getRawRows(array $params);
40
46 abstract protected function getFilter(): ?Filter;
47
53 abstract protected function getTotalCount(): int;
54
60 abstract protected function getGridProvider(): BaseProvider;
61
67 protected function getGridOptions(): Options
68 {
69 $this->gridOptions ??= new Options($this->getGridProvider()->getId());
70
71 return $this->gridOptions;
72 }
73
81 protected function init(): void
82 {
83 }
84
90 protected function initRows(): void
91 {
92 $params = [
93 'select' => $this->getRowsSelect(),
94 'filter' => $this->getRowsFilter(),
95 'order' => $this->getRowsSorting(),
96 ];
97
98 $pagination = $this->getRowsPagination();
99 if ($pagination)
100 {
101 $params['limit'] = $pagination->getLimit();
102 $params['offset'] = $pagination->getOffset();
103 }
104
105 $rows = [];
106 foreach ($this->getRawRows($params) as $row)
107 {
108 $rows[] = $this->prepareRow($row);
109 }
110
111 $this->getGridProvider()->setRows($rows);
112
113 if ($pagination)
114 {
115 $this->getGridProvider()->setNavObject($pagination);
116 }
117 }
118
126 protected function prepareRow(array $row): array
127 {
128 return $this->getGridProvider()->prepareRow($row);
129 }
130
136 protected function initResult(): void
137 {
138 $this->arResult['GRID'] = $this->getGridProvider()->toArray();
139 }
140
146 protected function initToolbar(): void
147 {
148 }
149
153 public function executeComponent()
154 {
155 try
156 {
157 $this->init();
158
159 if (!$this->checkReadPermissions())
160 {
161 throw new AccessDeniedException();
162 }
163
164 $this->processActionsGrid();
165
166 $this->initRows();
167 $this->initResult();
168 $this->initToolbar();
169
170 $this->includeComponentTemplate();
171 }
172 catch (AccessDeniedException $e)
173 {
174 $this->arResult['ERROR'] = Loc::getMessage('IBLOCK_GRID_COMPONENT_ERROR_ACCESS_DENIED');
175 $this->includeComponentTemplate('error');
176 }
177 }
178
184 protected function checkReadPermissions(): bool
185 {
186 return true;
187 }
188
194 final protected function getRowsFilter(): array
195 {
196 if (isset($this->rowsFilter))
197 {
198 return $this->rowsFilter;
199 }
200
201 $filterObject = $this->getFilter();
202 if ($filterObject)
203 {
204 $this->rowsFilter = $filterObject->getValue();
205 }
206 else
207 {
208 $this->rowsFilter = [];
209 }
210
211 $additionalFilter = $this->getAdditionalRowsFilter();
212 if (isset($additionalFilter))
213 {
214 $this->rowsFilter[] = $additionalFilter;
215 }
216
217 return $this->rowsFilter;
218 }
219
220 protected function getAdditionalRowsFilter(): ?array
221 {
222 return null;
223 }
224
230 protected function getRowsSelect(): array
231 {
232 $columns = array_column($this->getGridProvider()->getColumns(), 'id');
233
234 return array_filter($columns);
235 }
236
242 protected function getRowsSorting(): array
243 {
244 return $this->getGridOptions()->getSorting()['sort'];
245 }
246
252 protected function getRowsPagination(): ?PageNavigation
253 {
254 if (!isset($this->pagination))
255 {
256 $this->pagination = new PageNavigation('page');
257 $this->pagination->setPageSizes(
258 $this->getPageSizes()
259 );
260 $this->pagination->setRecordCount(
261 $this->getTotalCount()
262 );
263
264 $this->initFromGrid($this->pagination);
265 $this->pagination->initFromUri();
266 }
267
268 return $this->pagination;
269 }
270
276 protected function getPageSizes(): array
277 {
278 $gridSizes = $this->getGridProvider()->getPageSizes();
279 $gridSizes = array_column($gridSizes, 'VALUE');
280
281 \Bitrix\Main\Type\Collection::normalizeArrayValuesByInt($gridSizes);
282
283 return $gridSizes;
284 }
285
293 private function initFromGrid(PageNavigation $pagination): void
294 {
295 $params = $this->getGridOptions()->GetNavParams();
296 $pagination->setPageSize((int)$params['nPageSize']);
297 }
298
304 protected function processActionsGrid(): void
305 {
306 $request = Context::getCurrent()->getRequest();
307 $request->addFilter(new PostDecodeFilter);
308
309 $actionName = 'action_button_' . $this->getGridProvider()->getId();
310 $action = $request->getPost($actionName);
311
312 if (isset($action))
313 {
314 try
315 {
316 $result = $this->processActionGrid($action, $request);
317 if (isset($result) && !$result->isSuccess())
318 {
319 $this->sendErrorsResponse(
320 $result->getErrorMessages()
321 );
322 }
323 }
324 catch (AccessDeniedException $e)
325 {
326 $this->sendErrorsResponse([
327 Loc::getMessage('IBLOCK_GRID_COMPONENT_ERROR_ACCESS_DENIED')
328 ]);
329 }
330 catch (Throwable $e)
331 {
332 $this->sendErrorsResponse([
333 $e->getMessage()
334 ]);
335 }
336 }
337 }
338
346 protected function sendErrorsResponse(array $messages): void
347 {
348 global $APPLICATION;
349
354 $APPLICATION->RestartBuffer();
355
356 foreach ($messages as &$message)
357 {
358 if (is_array($message) && isset($message['TEXT']))
359 {
360 $message = [
361 'TYPE' => $message['TYPE'] ?? MessageType::ERROR,
362 'TEXT' => $message['TEXT'],
363 ];
364 }
365 else
366 {
367 $message = [
368 'TYPE' => MessageType::ERROR,
369 'TEXT' => (string)$message,
370 ];
371 }
372 }
373 unset($message);
374
375 CMain::FinalActions(
376 Json::encode([
377 'messages' => $messages,
378 ])
379 );
380 }
381
390 protected function processActionGrid(string $actionName, HttpRequest $request): ?Result
391 {
392 return null;
393 }
394}
processActionGrid(string $actionName, HttpRequest $request)
static getCurrent()
Definition context.php:241
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29