Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
movetosectiongroupchild.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;
17use CIBlockElement;
18use CIBlockSection;
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_MOVE_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 $destinationSectionId = (int)($controls['section_id'] ?? -1);
49 if ($destinationSectionId < 0)
50 {
51 return $result;
52 }
53
54 [$elementIds, $sectionIds] = $this->prepareItemIds($request, $isSelectedAllRows, $filter);
55
56 if ($elementIds)
57 {
58 $result->addErrors(
59 $this->moveElementsToSection($destinationSectionId, $elementIds)->getErrors()
60 );
61 }
62
63 if ($sectionIds)
64 {
65 $result->addErrors(
66 $this->moveSectionsToSection($destinationSectionId, $sectionIds)->getErrors()
67 );
68 }
69
70 return $result;
71 }
72
73 protected function getOnchange(): Onchange
74 {
75 return new Onchange([
76 [
77 'ACTION' => Actions::RESET_CONTROLS,
78 ],
79 [
80 'ACTION' => Actions::CREATE,
81 'DATA' => [
82 $this->getSectionSelectControl(true),
83 (new Snippet)->getSendSelectedButton(),
84 ],
85 ],
86 ]);
87 }
88
89 private function moveElementsToSection(int $sectionId, array $ids): Result
90 {
91 $result = new Result();
92 $entity = new CIBlockElement();
93
94 if (!$this->getIblockRightsChecker()->canBindElementToSection($sectionId))
95 {
96 $message = Loc::getMessage('IBLOCK_GRID_PANEL_UI_MOVE_TO_SECTION_GROUP_CHILD_ACCESS_DENIED_BIND_ELEMENT', [
97 '#ID#' => $sectionId,
98 ]);
99 $result->addError(
100 new Error($message)
101 );
102
103 return $result;
104 }
105
106 foreach ($ids as $id)
107 {
108 if (!$this->getIblockRightsChecker()->canEditElement($id))
109 {
110 $message = Loc::getMessage('IBLOCK_GRID_PANEL_UI_MOVE_TO_SECTION_GROUP_CHILD_ACCESS_DENIED_EDIT_ELEMENT', [
111 '#ID#' => $id,
112 ]);
113 $result->addError(
114 new Error($message)
115 );
116
117 continue;
118 }
119
120 $fields = [
121 'IBLOCK_SECTION_ID' => $sectionId,
122 'IBLOCK_SECTION' => [
123 $sectionId,
124 ],
125 ];
126 $updateResult = $entity->Update($id, $fields);
127 if (!$updateResult)
128 {
129 if ($entity->getLastError())
130 {
131 $result->addError(
132 new Error($entity->getLastError())
133 );
134 }
135 }
136 else
137 {
138 $ipropValues = new ElementValues($this->getIblockId(), $id);
139 $ipropValues->clearValues();
140 }
141 }
142
143 return $result;
144 }
145
146 private function moveSectionsToSection(int $sectionId, array $ids): Result
147 {
148 $result = new Result();
149 $entity = new CIBlockSection();
150
151 if (!$this->getIblockRightsChecker()->canBindSectionToSection($sectionId))
152 {
153 $message = Loc::getMessage('IBLOCK_GRID_PANEL_UI_MOVE_TO_SECTION_GROUP_CHILD_ACCESS_DENIED_BIND_ELEMENT', [
154 '#ID#' => $sectionId,
155 ]);
156 $result->addError(
157 new Error($message)
158 );
159
160 return $result;
161 }
162
163 foreach ($ids as $id)
164 {
165 if (!$this->getIblockRightsChecker()->canEditElement($id))
166 {
167 $message = Loc::getMessage('IBLOCK_GRID_PANEL_UI_MOVE_TO_SECTION_GROUP_CHILD_ACCESS_DENIED_EDIT_ELEMENT', [
168 '#ID#' => $id,
169 ]);
170 $result->addError(
171 new Error($message)
172 );
173
174 continue;
175 }
176
177 $fields = [
178 'IBLOCK_SECTION_ID' => $sectionId,
179 ];
180 $updateResult = $entity->Update($id, $fields);
181 if (!$updateResult)
182 {
183 if ($entity->getLastError())
184 {
185 $result->addError(
186 new Error($entity->getLastError())
187 );
188 }
189 }
190 else
191 {
192 $ipropValues = new ElementValues($this->getIblockId(), $id);
193 $ipropValues->clearValues();
194 }
195 }
196
197 return $result;
198 }
199}
processRequest(HttpRequest $request, bool $isSelectedAllRows, ?Filter $filter=null)
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29