1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
gridcomponent.php
См. документацию.
1<?php
2
4
16use CBitrixComponent;
17use CMain;
18use Iterator;
19use Throwable;
20
24abstract class GridComponent extends CBitrixComponent
25{
26 private array $rowsFilter;
27 private Options $gridOptions;
28
30
38 abstract protected function getRawRows(array $params);
39
45 abstract protected function getFilter(): ?Filter;
46
52 abstract protected function getTotalCount(): int;
53
59 abstract protected function getGridProvider(): BaseProvider;
60
66 protected function getGridOptions(): Options
67 {
68 $this->gridOptions ??= new Options($this->getGridProvider()->getId());
69
70 return $this->gridOptions;
71 }
72
80 protected function init(): void
81 {
82 }
83
89 protected function initRows(): void
90 {
91 $params = [
92 'select' => $this->getRowsSelect(),
93 'filter' => $this->getRowsFilter(),
94 'order' => $this->getRowsSorting(),
95 ];
96
97 $pagination = $this->getRowsPagination();
98 if ($pagination)
99 {
100 $params['limit'] = $pagination->getLimit();
101 $params['offset'] = $pagination->getOffset();
102 }
103
104 $rows = [];
105 foreach ($this->getRawRows($params) as $row)
106 {
107 $rows[] = $this->prepareRow($row);
108 }
109
110 $this->getGridProvider()->setRows($rows);
111
112 if ($pagination)
113 {
114 $this->getGridProvider()->setNavObject($pagination);
115 }
116 }
117
125 protected function prepareRow(array $row): array
126 {
127 return $this->getGridProvider()->prepareRow($row);
128 }
129
135 protected function initResult(): void
136 {
137 $this->arResult['GRID'] = $this->getGridProvider()->toArray();
138 }
139
145 protected function initToolbar(): void
146 {
147 }
148
152 public function executeComponent()
153 {
154 try
155 {
156 $this->init();
157
158 if (!$this->checkReadPermissions())
159 {
160 throw new AccessDeniedException();
161 }
162
163 $this->processActionsGrid();
164
165 $this->initRows();
166 $this->initResult();
167 $this->initToolbar();
168
170 }
171 catch (AccessDeniedException $e)
172 {
173 $this->arResult['ERROR'] = Loc::getMessage('IBLOCK_GRID_COMPONENT_ERROR_ACCESS_DENIED');
174 $this->includeComponentTemplate('error');
175 }
176 }
177
183 protected function checkReadPermissions(): bool
184 {
185 return true;
186 }
187
193 final protected function getRowsFilter(): array
194 {
195 if (isset($this->rowsFilter))
196 {
197 return $this->rowsFilter;
198 }
199
200 $filterObject = $this->getFilter();
201 if ($filterObject)
202 {
203 $this->rowsFilter = $filterObject->getValue();
204 }
205 else
206 {
207 $this->rowsFilter = [];
208 }
209
210 $additionalFilter = $this->getAdditionalRowsFilter();
211 if (isset($additionalFilter))
212 {
213 $this->rowsFilter[] = $additionalFilter;
214 }
215
216 return $this->rowsFilter;
217 }
218
219 protected function getAdditionalRowsFilter(): ?array
220 {
221 return null;
222 }
223
229 protected function getRowsSelect(): array
230 {
231 $columns = array_column($this->getGridProvider()->getColumns(), 'id');
232
233 return array_filter($columns);
234 }
235
241 protected function getRowsSorting(): array
242 {
243 return $this->getGridOptions()->getSorting()['sort'];
244 }
245
251 protected function getRowsPagination(): ?PageNavigation
252 {
253 if (!isset($this->pagination))
254 {
255 $this->pagination = new PageNavigation('page');
256 $this->pagination->setPageSizes(
257 $this->getPageSizes()
258 );
259 $this->pagination->setRecordCount(
260 $this->getTotalCount()
261 );
262
263 $this->initFromGrid($this->pagination);
264 $this->pagination->initFromUri();
265 }
266
267 return $this->pagination;
268 }
269
275 protected function getPageSizes(): array
276 {
277 $gridSizes = $this->getGridProvider()->getPageSizes();
278 $gridSizes = array_column($gridSizes, 'VALUE');
279
281
282 return $gridSizes;
283 }
284
292 private function initFromGrid(PageNavigation $pagination): void
293 {
294 $params = $this->getGridOptions()->GetNavParams();
295 $pagination->setPageSize((int)$params['nPageSize']);
296 }
297
303 protected function processActionsGrid(): void
304 {
305 $request = Context::getCurrent()->getRequest();
306
307 $actionName = 'action_button_' . $this->getGridProvider()->getId();
308 $action = $request->getPost($actionName);
309
310 if (isset($action))
311 {
312 try
313 {
315 if (isset($result) && !$result->isSuccess())
316 {
317 $this->sendErrorsResponse(
318 $result->getErrorMessages()
319 );
320 }
321 }
322 catch (AccessDeniedException $e)
323 {
324 $this->sendErrorsResponse([
325 Loc::getMessage('IBLOCK_GRID_COMPONENT_ERROR_ACCESS_DENIED')
326 ]);
327 }
328 catch (Throwable $e)
329 {
330 $this->sendErrorsResponse([
331 $e->getMessage()
332 ]);
333 }
334 }
335 }
336
344 protected function sendErrorsResponse(array $messages): void
345 {
346 global $APPLICATION;
347
351
352 $APPLICATION->RestartBuffer();
353
354 foreach ($messages as &$message)
355 {
356 if (is_array($message) && isset($message['TEXT']))
357 {
358 $message = [
359 'TYPE' => $message['TYPE'] ?? MessageType::ERROR,
360 'TEXT' => $message['TEXT'],
361 ];
362 }
363 else
364 {
365 $message = [
366 'TYPE' => MessageType::ERROR,
367 'TEXT' => (string)$message,
368 ];
369 }
370 }
371 unset($message);
372
373 CMain::FinalActions(
374 Json::encode([
375 'messages' => $messages,
376 ])
377 );
378 }
379
388 protected function processActionGrid(string $actionName, HttpRequest $request): ?Result
389 {
390 return null;
391 }
392}
global $APPLICATION
Определения include.php:80
processActionGrid(string $actionName, HttpRequest $request)
Определения gridcomponent.php:388
static normalizeArrayValuesByInt(&$map, $sorted=true)
Определения collection.php:150
Определения json.php:9
includeComponentTemplate($templatePage="", $customTemplatePath="")
Определения component.php:724
$request
Определения component.php:65
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
Определения culture.php:9
$message
Определения payment.php:8
$messages
Определения template.php:8
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$rows
Определения options.php:264
$action
Определения file_dialog.php:21