Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
addtosectiongroupchild.php
1<?php
2
4
6use Bitrix\Iblock\Grid\Panel\UI\Actions\Helpers\ItemFinder;
7use Bitrix\Iblock\Grid\Panel\UI\Actions\Item\ElementGroup\Helpers\SectionSelectControl;
18use CIBlockElement;
19
24{
25 use SectionSelectControl;
26 use ItemFinder;
27
28 public static function getId(): string
29 {
31 }
32
33 public function getName(): string
34 {
35 return Loc::getMessage('IBLOCK_GRID_PANEL_UI_ACTIONS_ELEMENT_GROUP_ADD_TO_SECTION_NAME');
36 }
37
38 public function processRequest(HttpRequest $request, bool $isSelectedAllRows, ?Filter $filter = null): ?Result
39 {
40 $result = new Result();
41
42 $controls = $request->getPost('controls');
43 if (!is_array($controls))
44 {
45 return $result;
46 }
47
48 $sectionId = (int)($controls['section_id'] ?? 0);
49 if ($sectionId <= 0)
50 {
51 return $result;
52 }
53
54 if ($isSelectedAllRows)
55 {
56 $elementIds = $this->getElementIdsByFilter($filter);
57 }
58 else
59 {
60 $ids = $this->getRequestRows($request);
61 if (empty($ids))
62 {
63 return null;
64 }
65
66 [$elementIds,] = RowType::parseIndexList($ids);
67 $elementIds = $this->validateElementIds($elementIds);
68 }
69
70 if ($elementIds)
71 {
72 $result->addErrors(
73 $this->addElementsToSection($sectionId, $elementIds)->getErrors()
74 );
75 }
76
77 return $result;
78 }
79
80 protected function getOnchange(): Onchange
81 {
82 return new Onchange([
83 [
84 'ACTION' => Actions::RESET_CONTROLS,
85 ],
86 [
87 'ACTION' => Actions::CREATE,
88 'DATA' => [
89 $this->getSectionSelectControl(false),
90 (new Snippet)->getSendSelectedButton(),
91 ],
92 ],
93 ]);
94 }
95
96 private function addElementsToSection(int $sectionId, array $ids): Result
97 {
98 $result = new Result();
99 $entity = new CIBlockElement();
100
101 if (!$this->getIblockRightsChecker()->canBindElementToSection($sectionId))
102 {
103 $message = Loc::getMessage('IBLOCK_GRID_PANEL_UI_ADD_TO_SECTION_GROUP_CHILD_ACCESS_DENIED_BIND_ELEMENT', [
104 '#ID#' => $sectionId,
105 ]);
106 $result->addError(
107 new Error($message)
108 );
109
110 return $result;
111 }
112
113 foreach ($ids as $id)
114 {
115 if (!$this->getIblockRightsChecker()->canEditElement($id))
116 {
117 $message = Loc::getMessage('IBLOCK_GRID_PANEL_UI_ADD_TO_SECTION_GROUP_CHILD_ACCESS_DENIED_EDIT_ELEMENT', [
118 '#ID#' => $sectionId,
119 ]);
120 $result->addError(
121 new Error($message)
122 );
123
124 continue;
125 }
126
127 $fields = [
128 'IBLOCK_SECTION' => [
129 $sectionId,
130 ...$this->getElementSectionsIds($id),
131 ],
132 ];
133 $updateResult = $entity->Update($id, $fields);
134 if (!$updateResult)
135 {
136 if ($entity->getLastError())
137 {
138 $result->addError(
139 new Error($entity->getLastError())
140 );
141 }
142 }
143 else
144 {
145 $ipropValues = new ElementValues($this->getIblockId(), $id);
146 $ipropValues->clearValues();
147 }
148 }
149
150 return $result;
151 }
152
153 private function getElementSectionsIds(int $elementId): array
154 {
155 $result = [];
156
157 $rows = CIBlockElement::GetElementGroups($elementId, true, ['ID']);
158 while ($row = $rows->Fetch())
159 {
160 $result[] = (int)$row['ID'];
161 }
162 unset($rows);
163
164 return $result;
165 }
166}
processRequest(HttpRequest $request, bool $isSelectedAllRows, ?Filter $filter=null)
static parseIndexList(array $ids)
Definition rowtype.php:52
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29