Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
grid.php
1<?php
2
3namespace Bitrix\Main\Grid;
4
11use Bitrix\Main\Grid\Pagination\Storage\StorageSupporter;
21
73abstract class Grid
74{
75 use StorageSupporter;
76
77 private array $rawRows;
78 private Options $options;
79 private Settings $settings;
80 private Columns $columns;
81 private Rows $rows;
82
83 // optional
84 private ?Panel $panel = null;
85 private ?Filter $filter = null;
86 private ?PageNavigation $pagination = null;
87
88 // internal
89 private array $actionsMap;
92
96 public function __construct(Settings $settings)
97 {
98 $this->settings = $settings;
99 $this->gridRequestFactory = new GridRequestFactory;
100 $this->gridResponseFactory = new GridResponseFactory;
101 }
102
103 #region public api
104
110 final public function getId(): string
111 {
112 return $this->getSettings()->getId();
113 }
114
120 final public function getSettings(): Settings
121 {
122 return $this->settings;
123 }
124
130 final public function getOptions(): Options
131 {
132 $this->options ??= new Options($this->getId());
133
134 return $this->options;
135 }
136
142 final public function getColumns(): Columns
143 {
144 $this->columns ??= $this->createColumns();
145
146 return $this->columns;
147 }
148
154 final public function getRows(): Rows
155 {
156 $this->rows ??= $this->createRows();
157
158 return $this->rows;
159 }
160
166 final public function getPanel(): ?Panel
167 {
168 $this->panel ??= $this->createPanel();
169
170 return $this->panel;
171 }
172
178 final public function getFilter(): ?Filter
179 {
180 $this->filter ??= $this->createFilter();
181
182 return $this->filter;
183 }
184
190 final public function getPagination(): ?PageNavigation
191 {
192 return $this->pagination;
193 }
194
208 public function initPagination(int $totalRowsCount, ?string $navId = null): void
209 {
210 $navParams = $this->getOptions()->GetNavParams();
211 if (empty($navId))
212 {
213 $navId = $this->getId() . '_nav';
214 }
215
216 $this->pagination = new PageNavigation($navId);
217 $this->pagination->allowAllRecords(false);
218 $this->pagination->setPageSize($navParams['nPageSize']);
219 $this->pagination->setPageSizes($this->getPageSizes());
220 $this->pagination->setRecordCount($totalRowsCount);
221
222 $storage = $this->getPaginationStorage();
223 if (isset($storage))
224 {
225 $storage->fill($this->pagination);
226 }
227 }
228
236 public function setRawRows(iterable $rawValue): void
237 {
238 $this->rawRows = [];
239
240 foreach ($rawValue as $item)
241 {
242 $this->rawRows[] = $item;
243 }
244 }
245
251 final protected function getRawRows(): array
252 {
253 return $this->rawRows;
254 }
255
261 public function prepareRows(): array
262 {
263 return $this->getRows()->prepareRows($this->getRawRows());
264 }
265
271 public function prepareColumns(): array
272 {
273 $result = [];
274
275 foreach ($this->getColumns() as $column)
276 {
277 $result[] = $column;
278 }
279
280 return $result;
281 }
282
292 public function processRequest(?HttpRequest $request = null): void
293 {
294 $request ??= Context::getCurrent()->getRequest();
295 $request->addFilter(new PostDecodeFilter);
296 $gridRequest = $this->gridRequestFactory->createFromRequest($request);
297
298 $response = $this->processGridActionsRequest($gridRequest);
299 if ($response instanceof GridResponse)
300 {
301 if ($response->isSendable())
302 {
303 $response->send();
304 }
305
306 return;
307 }
308
309 $panel = $this->getPanel();
310 if (isset($panel))
311 {
312 $response = $panel->processRequest($gridRequest, $this->getFilter());
313 if ($response instanceof GridResponse)
314 {
315 if ($response->isSendable())
316 {
317 $response->send();
318 }
319
320 return;
321 }
322 }
323
324 $response = $this->getRows()->processRequest($gridRequest);
325 if ($response instanceof GridResponse)
326 {
327 if ($response->isSendable())
328 {
329 $response->send();
330 }
331
332 return;
333 }
334 }
335
336 #region orm
337
345 public function getOrmParams(): array
346 {
347 $params = [
348 'select' => $this->getOrmSelect(),
349 'order' => $this->getOrmOrder(),
350 ];
351
352 $filter = $this->getOrmFilter();
353 if (isset($filter))
354 {
355 $params['filter'] = $filter;
356 }
357
358 $pagination = $this->getPagination();
359 if (isset($pagination))
360 {
361 $params['limit'] = $pagination->getLimit();
362 $params['offset'] = $pagination->getOffset();
363 }
364
365 return $params;
366 }
367
375 public function getOrmSelect(): array
376 {
377 return $this->getColumns()->getSelect(
378 $this->getVisibleColumnsIds()
379 );
380 }
381
389 public function getOrmFilter(): ?array
390 {
391 $filter = $this->getFilter();
392 if (isset($filter))
393 {
394 return $filter->getValue();
395 }
396
397 return null;
398 }
399
407 public function getOrmOrder(): array
408 {
409 $sorting = $this->getOptions()->getSorting(
410 $this->getDefaultSorting()
411 );
412
413 return $sorting['sort'];
414 }
415
416 #endregion orm
417 #endregion public api
418
426 protected function getActions(): array
427 {
428 $result = [];
429
430 $pagination = $this->getPagination();
431 if (isset($pagination))
432 {
433 $result[] = new PaginationAction($pagination, $this->getPaginationStorage());
434 }
435
436 return $result;
437 }
438
446 final protected function getActionById(string $id): ?Action
447 {
448 if (empty($id))
449 {
450 return null;
451 }
452
453 if (!isset($this->actionsMap))
454 {
455 $this->actionsMap = [];
456
457 foreach ($this->getActions() as $action)
458 {
459 $this->actionsMap[$action::getId()] = $action;
460 }
461 }
462
463 return $this->actionsMap[$id] ?? null;
464 }
465
476 {
477 $result = null;
478
479 if (!check_bitrix_sessid())
480 {
481 return null;
482 }
483
484 $requestGridId = $request->getGridId();
485 if ($requestGridId !== $this->getId())
486 {
487 return null;
488 }
489
490 $action = $this->getActionById(
491 $request->getGridActionId() ?? ''
492 );
493 if ($action)
494 {
495 $result = $action->processRequest($request->getHttpRequest());
496 }
497 else
498 {
499 return null;
500 }
501
502 return
503 isset($result)
504 ? $this->gridResponseFactory->createFromResult($result)
505 : null
506 ;
507 }
508
517 public function getVisibleColumnsIds(): array
518 {
519 $visibleColumns = $this->getOptions()->GetVisibleColumns();
520 if (empty($visibleColumns))
521 {
522 $visibleColumns = [];
523
524 foreach ($this->getColumns() as $column)
525 {
526 if ($column->isDefault())
527 {
528 $visibleColumns[] = $column->getId();
529 }
530 }
531 }
532
533 return $visibleColumns;
534 }
535
541 protected function getPageSizes(): array
542 {
543 return [
544 5,
545 10,
546 20,
547 50,
548 100,
549 ];
550 }
551
557 protected function getDefaultSorting(): array
558 {
559 return [
560 'ID' => 'ASC',
561 ];
562 }
563
569 abstract protected function createColumns(): Columns;
570
576 protected function createRows(): Rows
577 {
578 $emptyRowAssembler = new EmptyRowAssembler(
579 $this->getVisibleColumnsIds()
580 );
581
582 return new Rows(
583 $emptyRowAssembler
584 );
585 }
586
592 protected function createPanel(): ?Panel
593 {
594 return null;
595 }
596
602 protected function createFilter(): ?Filter
603 {
604 return null;
605 }
606}
getValue(?array $rawValue=null)
Definition filter.php:168
__construct(Settings $settings)
Definition grid.php:96
getActionById(string $id)
Definition grid.php:446
GridRequestFactory $gridRequestFactory
Definition grid.php:90
setRawRows(iterable $rawValue)
Definition grid.php:236
GridResponseFactory $gridResponseFactory
Definition grid.php:91
processRequest(?HttpRequest $request=null)
Definition grid.php:292
initPagination(int $totalRowsCount, ?string $navId=null)
Definition grid.php:208
processGridActionsRequest(GridRequest $request)
Definition grid.php:475
processRequest(GridRequest $request, ?Filter $filter)
Definition panel.php:97