Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
groupaction.php
1<?php
3
8
15{
16 public const GRID_TYPE_UI = 'main.ui.grid';
17 public const GRID_TYPE_LIST = 'adminList';
18 public const GRID_TYPE_SUBLIST = 'subList';
19
20 private const PREFIX_ID = 'iblock_grid_action_';
21
23 protected $entityId = '';
24
26
28 protected $options = [];
29
31 protected $iblockId = null;
32
34 protected $iblockConfig = [
35 'SECTIONS' => 'N',
36 'SECTION_CHOOSER' => Iblock\IblockTable::SECTION_CHOOSER_SELECT
37 ];
38
40 protected $mainSnippet = null;
41
43 protected $request = null;
44
46 protected $sections = null;
47
48 protected $actionHandlers = [];
49
50 public function __construct(array $options)
51 {
52 $this->options = $options;
53
54 $this->entityId = $options['ENTITY_ID'];
55 $this->iblockId = $options['IBLOCK_ID'];
56
57 if (isset($options['GRID_TYPE']))
58 {
59 $this->setGridType($options['GRID_TYPE']);
60 }
61
62 $this->mainSnippet = new Main\Grid\Panel\Snippet();
63 $this->request = Main\Context::getCurrent()->getRequest();
64
65 $this->initConfig();
66
67 $this->initActions();
68 }
69
74 public function getList(?array $actions = null): array
75 {
76 $result = [];
77
78 $actions ??= array_keys($this->actionHandlers);
79
80 if (!empty($actions))
81 {
82 foreach ($actions as $code => $params)
83 {
84 if (is_string($params))
85 {
86 $code = $params;
87 $params = [];
88 }
89 if (is_string($code) && is_array($params))
90 {
91 $row = $this->get($code, $params);
92 if ($row !== null)
93 {
94 $result[$code] = $row;
95 }
96 }
97 }
98 unset($row, $code, $params);
99 }
100
101 return $result;
102 }
103
109 public function get(string $code, array $params = [])
110 {
111 $code = trim($code);
112 if ($code === '' || !isset($this->actionHandlers[$code]))
113 {
114 return null;
115 }
116
117 $method = 'action'.$this->actionHandlers[$code].'Panel';
118 if (is_callable([$this, $method]))
119 {
120 return call_user_func_array([$this, $method], [$params]);
121 }
122
123 return [];
124 }
125
130 public function getRequest(string $code): ?array
131 {
132 $code = trim($code);
133 if ($code === '' || !isset($this->actionHandlers[$code]))
134 {
135 return null;
136 }
137
138 $method = 'action'.$this->actionHandlers[$code].'Request';
139 if (is_callable([$this, $method]))
140 {
141 return call_user_func_array([$this, $method], []);
142 }
143
144 return [];
145 }
146
150 public function getEntityId(): string
151 {
152 return $this->entityId;
153 }
154
158 public function getIblockId(): int
159 {
160 return $this->iblockId;
161 }
162
166 public function getOptions(): array
167 {
168 return $this->options;
169 }
170
174 protected function initConfig()
175 {
176 $iterator = Iblock\IblockTable::getList([
177 'select' => [
178 'ID',
179 'SECTION_CHOOSER',
180 'SECTIONS' => 'TYPE.SECTIONS'
181 ],
182 'filter' => ['=ID' => $this->iblockId],
183 ]);
184 $row = $iterator->fetch();
185 if (!empty($row))
186 {
187 $this->iblockConfig['SECTIONS'] = $row['SECTIONS'];
188 $this->iblockConfig['SECTION_CHOOSER'] = $row['SECTION_CHOOSER'];
189 }
190 unset($row, $iterator);
191 }
192
196 protected function initActions(): void
197 {
198 $this->actionHandlers = $this->getActionHandlers();
199 }
200
205 protected function setGridType(string $value): void
206 {
207 if (
208 $value === self::GRID_TYPE_UI
209 || $value === self::GRID_TYPE_LIST
210 || $value === self::GRID_TYPE_SUBLIST
211 )
212 {
213 $this->gridType = $value;
214 }
215 }
216
220 public function getGridType(): string
221 {
222 return $this->gridType;
223 }
224
228 public function isUiGrid(): bool
229 {
230 return $this->getGridType() === self::GRID_TYPE_UI;
231 }
232
236 protected function getActionHandlers()
237 {
238 $result = [];
239
240 $result[Iblock\Grid\ActionType::EDIT] = 'Edit';
241 $result[Iblock\Grid\ActionType::SELECT_ALL] = 'SelectAll';
242 $result[Iblock\Grid\ActionType::DELETE] = 'Delete';
243 $result[Iblock\Grid\ActionType::ACTIVATE] = 'Activate';
244 $result[Iblock\Grid\ActionType::DEACTIVATE] = 'Deactivate';
245 $result[Iblock\Grid\ActionType::CLEAR_COUNTER] = 'ClearCounter';
246 $result[Iblock\Grid\ActionType::CODE_TRANSLIT] = 'CodeTranslit';
247 $result[Iblock\Grid\ActionType::MOVE_TO_SECTION] = 'AdjustSection';
248 $result[Iblock\Grid\ActionType::ADD_TO_SECTION] = 'AddSection';
249 $result[Iblock\Grid\ActionType::ELEMENT_UNLOCK] = 'ElementUnlock';
250 $result[Iblock\Grid\ActionType::ELEMENT_LOCK] = 'ElementLock';
251 $result[Iblock\Grid\ActionType::ELEMENT_WORKFLOW_STATUS] = 'ElementWorkflowStatus';
252
253 return $result;
254 }
255
259 public function getDefaultApplyAction(): array
260 {
261 return ['JS' => "BX.adminUiList.SendSelected('{$this->getEntityId()}')"];
262 }
263
268 public function getElementId(string $id): string
269 {
270 return self::PREFIX_ID.$this->getEntityId().'_'.strtolower($id);
271 }
272
277 public function getApplyButton(array $params): array
278 {
279 $result = $this->mainSnippet->getApplyButton([]);
280 $result['id'] = $this->getElementId($params['APPLY_BUTTON_ID']);
281 $this->mainSnippet->setButtonActions(
282 $result,
283 [
284 [
285 'ACTION' => Main\Grid\Panel\Actions::CALLBACK,
286 'DATA' => [
287 $this->getDefaultApplyAction(),
288 ],
289 ],
290 ]
291 );
292 return $result;
293 }
294
299 public function getApplyButtonWithConfirm(array $params): array
300 {
301 $confirmMessage = null;
302 if (
303 isset($params['CONFIRM_MESSAGE'])
304 && is_string($params['CONFIRM_MESSAGE'])
305 && $params['CONFIRM_MESSAGE'] !== ''
306 )
307 {
308 $confirmMessage = $params['CONFIRM_MESSAGE'];
309 }
310 elseif (
311 isset($params['DEFAULT_CONFIRM_MESSAGE'])
312 && is_string($params['DEFAULT_CONFIRM_MESSAGE'])
313 && $params['DEFAULT_CONFIRM_MESSAGE'] !== ''
314 )
315 {
316 $confirmMessage = $params['DEFAULT_CONFIRM_MESSAGE'];
317 }
318
319 $result = $this->mainSnippet->getApplyButton([]);
320 $result['id'] = $this->getElementId($params['APPLY_BUTTON_ID']);
321 $this->mainSnippet->setButtonActions(
322 $result,
323 [
324 [
325 'ACTION' => Main\Grid\Panel\Actions::CALLBACK,
326 'CONFIRM' => true,
327 'CONFIRM_MESSAGE' => $confirmMessage,
328 'DATA' => [
329 $this->getDefaultApplyAction(),
330 ],
331 ],
332 ]
333 );
334
335 return $result;
336 }
337
341 protected function loadSections(): void
342 {
343 if ($this->sections === null)
344 {
345 $this->sections = [];
346 if ($this->iblockId > 0)
347 {
348 $iterator = \CIBlockSection::getTreeList(
349 ['IBLOCK_ID' => $this->iblockId],
350 ['ID', 'NAME', 'DEPTH_LEVEL', 'LEFT_MARGIN']
351 );
352 while ($row = $iterator->Fetch())
353 {
354 $this->sections[] = [
355 'NAME' => str_repeat(' . ', $row['DEPTH_LEVEL']).$row['NAME'],
356 'VALUE' => $row['ID']
357 ];
358 }
359 unset($row, $iterator);
360 }
361 }
362 }
363
368 protected function getSections(bool $addTop = false): array
369 {
370 $this->loadSections();
371 $result = $this->sections;
372 if ($addTop)
373 {
374 $result = array_merge(
375 [
376 [
377 'NAME' => Loc::getMessage('IBLOCK_GRID_PANEL_ACTION_MESS_SECTION_TOP_LEVEL'),
378 'VALUE' => '0',
379 ],
380 ],
381 $result
382 );
383 }
384 return $result;
385 }
386
391 protected function getAddSectionList(array $action): array
392 {
393 return [
394 'name' => $action['NAME'],
395 'type' => 'multicontrol',
396 'action' => [
397 [
398 'ACTION' => Main\Grid\Panel\Actions::RESET_CONTROLS,
399 ],
400 [
401 'ACTION' => Main\Grid\Panel\Actions::CREATE,
402 'DATA' => [
403 [
404 'TYPE' => Main\Grid\Panel\Types::DROPDOWN,
405 'ID' => $this->getElementId($action['SECTION_LIST_ID']),
406 'NAME' => 'section_to_move',
407 'ITEMS' => $this->getSections(),
408 ],
409 $this->getApplyButton($action),
410 ],
411 ],
412 ],
413 ];
414 }
415
420 protected function getAddSectionDialog(array $action): array
421 {
422 return [
423 'name' => $action['NAME'],
424 'type' => 'multicontrol',
425 'action' => [
426 [
427 'ACTION' => Main\Grid\Panel\Actions::RESET_CONTROLS,
428 ],
429 [
430 'ACTION' => Main\Grid\Panel\Actions::CREATE,
431 'DATA' => [
432 [
433 'TYPE' => Main\Grid\Panel\Types::TEXT,
434 'ID' => '',
435 'NAME' => '',
436 'TITLE' => '',
437 ],
438 /*[
439 'TYPE' => Main\Grid\Panel\Types::DATE,
440 'ID' => '',
441 'NAME' => ''
442 ], */
443 [
444 'TYPE' => Main\Grid\Panel\Types::BUTTON,
445 'ID' => '',
446 'NAME' => ''
447 ],
448 $this->getApplyButton($action)
449 ]
450 ]
451 ]
452 ];
453 }
454
459 protected function getAdjustSectionList(array $action): array
460 {
461 return [
462 'name' => $action['NAME'],
463 'type' => 'multicontrol',
464 'action' => [
465 [
466 'ACTION' => Main\Grid\Panel\Actions::RESET_CONTROLS
467 ],
468 [
469 'ACTION' => Main\Grid\Panel\Actions::CREATE,
470 'DATA' => [
471 [
472 'TYPE' => Main\Grid\Panel\Types::DROPDOWN,
473 'ID' => $this->getElementId($action['SECTION_LIST_ID']),
474 'NAME' => 'section_to_move',
475 'ITEMS' => $this->getSections(true)
476 ],
477 $this->getApplyButton($action)
478 ]
479 ]
480 ]
481 ];
482 }
483
488 protected function actionEditPanel(array $params = []): string
489 {
490 return (isset($params['NAME']) && $params['NAME'] != ''
491 ? $params['NAME']
492 : Loc::getMessage('IBLOCK_GRID_PANEL_ACTION_EDIT')
493 );
494 }
495
500 protected function actionSelectAllPanel(array $params = []): bool
501 {
502 return true;
503 }
504
509 protected function actionDeletePanel(array $params = []): string
510 {
511 return (isset($params['NAME']) && $params['NAME'] != ''
512 ? $params['NAME']
513 : Loc::getMessage('IBLOCK_GRID_PANEL_ACTION_DELETE')
514 );
515 }
516
521 protected function actionActivatePanel(array $params = []): string
522 {
523 return (isset($params['NAME']) && $params['NAME'] != ''
524 ? $params['NAME']
525 : Loc::getMessage('IBLOCK_GRID_PANEL_ACTION_ACTIVATE_MSGVER_2')
526 );
527 }
528
533 protected function actionDeactivatePanel(array $params = []): string
534 {
535 return (isset($params['NAME']) && $params['NAME'] != ''
536 ? $params['NAME']
537 : Loc::getMessage('IBLOCK_GRID_PANEL_ACTION_DEACTIVATE_MSGVER_2')
538 );
539 }
540
545 protected function actionClearCounterPanel(array $params = [])
546 {
547 $name = (isset($params['NAME']) && $params['NAME'] !== ''
548 ? $params['NAME']
549 : Loc::getMessage('IBLOCK_GRID_PANEL_ACTION_CLEAR_COUNTER')
550 );
551
552 $params['APPLY_BUTTON_ID'] = 'clear_counter_confirm';
553 $params['DEFAULT_CONFIRM_MESSAGE'] = Loc::getMessage('IBLOCK_GRID_PANEL_ACTION_CLEAR_COUNTER_CONFIRM');
554
555 if ($this->isUiGrid())
556 {
557 return [
558 'name' => $name,
559 'type' => 'multicontrol',
560 'action' => [
561 [
562 'ACTION' => Main\Grid\Panel\Actions::RESET_CONTROLS
563 ],
564 [
565 'ACTION' => Main\Grid\Panel\Actions::CREATE,
566 'DATA' => [$this->getApplyButtonWithConfirm($params)]
567 ]
568 ]
569 ];
570 }
571 else
572 {
573 return $name;
574 }
575 }
576
581 public function actionCodeTranslitPanel(array $params = [])
582 {
583 $name = (isset($params['NAME']) && $params['NAME'] !== ''
584 ? $params['NAME']
585 : Loc::getMessage('IBLOCK_GRID_PANEL_ACTION_CODE_TRANSLITERATION_MSGVER_1')
586 );
587
588 $params['APPLY_BUTTON_ID'] = 'code_translit_confirm';
589 $params['DEFAULT_CONFIRM_MESSAGE'] = Loc::getMessage('IBLOCK_GRID_PANEL_ACTION_CODE_TRANSLITERATION_CONFIRM');
590
591 if ($this->isUiGrid())
592 {
593 return [
594 'name' => $name,
595 'type' => 'multicontrol',
596 'action' => [
597 [
598 'ACTION' => Main\Grid\Panel\Actions::RESET_CONTROLS,
599 ],
600 [
601 'ACTION' => Main\Grid\Panel\Actions::CREATE,
602 'DATA' => [$this->getApplyButtonWithConfirm($params)],
603 ],
604 ],
605 ];
606 }
607 else
608 {
609 return $name;
610 }
611 }
612
617 protected function actionAdjustSectionPanel(array $params = []): ?array
618 {
619 if (!$this->isUiGrid())
620 {
621 return null;
622 }
623 if ($this->iblockConfig['SECTIONS'] != 'Y')
624 {
625 return null;
626 }
627 if (!isset($params['NAME']) || $params['NAME'] == '')
628 {
629 $params['NAME'] = Loc::getMessage('IBLOCK_GRID_PANEL_ACTION_ADJUST_SECTION');
630 }
631
632 $params['APPLY_BUTTON_ID'] = 'send_adjust_list';
633 if ($this->iblockConfig['SECTION_CHOOSER'] == Iblock\IblockTable::SECTION_CHOOSER_PATH)
634 {
635 $params['SECTION_LIST_ID'] = 'set_sections';
636 return $this->getAdjustSectionList($params);
637 }
638 else
639 {
640 $params['SECTION_LIST_ID'] = 'set_sections';
641 return $this->getAdjustSectionList($params);
642 }
643 }
644
648 protected function actionAdjustSectionRequest(): ?array
649 {
650 $sectionId = $this->request->get('section_to_move');
651 return (is_string($sectionId) ? ['SECTION_ID' => $sectionId] : null);
652 }
653
658 protected function actionAddSectionPanel(array $params = []): ?array
659 {
660 if (!$this->isUiGrid())
661 {
662 return null;
663 }
664 if ($this->iblockConfig['SECTIONS'] != 'Y')
665 {
666 return null;
667 }
668 if (!isset($params['NAME']) || $params['NAME'] === '')
669 {
670 $params['NAME'] = Loc::getMessage('IBLOCK_GRID_PANEL_ACTION_ADD_SECTION');
671 }
672
673 $params['APPLY_BUTTON_ID'] = 'send_add_list';
674 if ($this->iblockConfig['SECTION_CHOOSER'] == Iblock\IblockTable::SECTION_CHOOSER_PATH)
675 {
676 $params['SECTION_LIST_ID'] = 'additional_sections';
677 return $this->getAddSectionList($params);
678 }
679 else
680 {
681 $params['SECTION_LIST_ID'] = 'additional_sections';
682 return $this->getAddSectionList($params);
683 }
684 }
685
689 protected function actionAddSectionRequest(): ?array
690 {
691 $sectionId = $this->request->get('section_to_move');
692
693 return (is_string($sectionId) ? ['SECTION_ID' => $sectionId] : null);
694 }
695
700 protected function actionElementUnlockPanel(array $params = []): string
701 {
702 return (isset($params['NAME']) && $params['NAME'] !== ''
703 ? $params['NAME']
704 : Loc::getMessage('IBLOCK_GRID_PANEL_ACTION_ELEMENT_UNLOCK')
705 );
706 }
707
712 protected function actionElementLockPanel(array $params = []): string
713 {
714 return (isset($params['NAME']) && $params['NAME'] !== ''
715 ? $params['NAME']
716 : Loc::getMessage('IBLOCK_GRID_PANEL_ACTION_ELEMENT_LOCK')
717 );
718 }
719
724 protected function actionElementWorkflowStatusPanel(array $params = []): ?array
725 {
726 if (!Loader::includeModule('workflow'))
727 {
728 return null;
729 }
730
731 $name = (isset($params['NAME']) && $params['NAME'] !== ''
732 ? $params['NAME']
733 : Loc::getMessage('IBLOCK_GRID_PANEL_ACTION_ELEMENT_WORKFLOW_STATUS')
734 );
735
736 $statusList = [];
737 $iterator = \CWorkflowStatus::getDropDownList('N', 'desc');
738 while ($row = $iterator->Fetch())
739 {
740 $statusList[] = [
741 'NAME' => $row['REFERENCE'],
742 'VALUE' => $row['REFERENCE_ID'],
743 ];
744 }
745 unset($row, $iterator);
746 if (empty($statusList))
747 {
748 return null;
749 }
750
751 $params['APPLY_BUTTON_ID'] = 'send_workflow_status';
752 $data = [];
753 $data[] = [
754 'TYPE' => Main\Grid\Panel\Types::DROPDOWN,
755 'ID' => $this->getElementId('workflow_status'),
756 'NAME' => 'wf_status_id',
757 'ITEMS' => $statusList,
758 ];
759 if ($this->isUiGrid())
760 {
761 $data[] = $this->getApplyButton($params);
762 }
763
764 return [
765 'name' => $name,
766 'type' => 'multicontrol',
767 'action' => [
768 [
769 'ACTION' => Main\Grid\Panel\Actions::RESET_CONTROLS,
770 ],
771 [
772 'ACTION' => Main\Grid\Panel\Actions::CREATE,
773 'DATA' => $data,
774 ],
775 ],
776 ];
777 }
778
782 protected function actionElementWorkflowStatusRequest(): ?array
783 {
784 $result = $this->request->get('wf_status_id');
785
786 return (is_string($result) ? ['WF_STATUS_ID' => $result] : null);
787 }
788}
actionElementWorkflowStatusPanel(array $params=[])
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29