Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
createcodegroupchild.php
1<?php
2
4
6use Bitrix\Iblock\Grid\Helpers\CodeTranslator;
7use Bitrix\Iblock\Grid\Panel\UI\Actions\Helpers\ItemFinder;
17use CIBlock;
18use CIBlockElement;
19use CIBlockSection;
20use CUtil;
21
26{
27 use CodeTranslator;
28 use ItemFinder;
29
30 public static function getId(): string
31 {
33 }
34
35 public function getName(): string
36 {
37 return Loc::getMessage('IBLOCK_GRID_PANEL_UI_ACTIONS_ELEMENT_GROUP_CREATE_CODE_NAME_MSGVER_1');
38 }
39
40 public function processRequest(HttpRequest $request, bool $isSelectedAllRows, ?Filter $filter = null): ?Result
41 {
42 $result = new Result();
43
44 $select = [
45 'ID',
46 'NAME',
47 ];
48
49 if ($isSelectedAllRows)
50 {
51 [$elements, $sections] = $this->getItemsByFilter($select, $filter);
52 }
53 else
54 {
55 $ids = $this->getRequestRows($request);
56 if (empty($ids))
57 {
58 return null;
59 }
60
61 [$elementIds, $sectionIds] = RowType::parseIndexList($ids);
62
63 $elements = $this->getElementsByIdList($select, $elementIds);
64 $sections = $this->getSectionsByIdList($select, $sectionIds);
65
66 unset($elementIds, $sectionIds);
67 }
68
69 if ($elements)
70 {
71 $result->addErrors(
72 $this->processCodeTranslitElements($elements)->getErrors()
73 );
74 }
75
76 if ($sections)
77 {
78 $result->addErrors(
79 $this->processCodeTranslitSections($sections)->getErrors()
80 );
81 }
82
83 return $result;
84 }
85
86 protected function getOnchange(): Onchange
87 {
88 $confirmMessage = Loc::getMessage('IBLOCK_GRID_PANEL_UI_ACTIONS_ELEMENT_GROUP_CREATE_CODE_CONFIRM');
89
90 return new Onchange([
91 [
92 'ACTION' => Actions::RESET_CONTROLS,
93 ],
94 [
95 'ACTION' => Actions::CREATE,
96 'DATA' => [
97 (new Snippet)->getSendSelectedButton($confirmMessage),
98 ],
99 ],
100 ]);
101 }
102
103 private function processCodeTranslitElements(array $elements): Result
104 {
105 $result = new Result();
106 $entity = new CIBlockElement();
107 $translitSettings = $this->getElementTranslitSettings();
108
109 foreach ($elements as $row)
110 {
111 $id = $row['ID'];
112
113 if (!$this->getIblockRightsChecker()->canEditElement($id))
114 {
115 $message = Loc::getMessage('IBLOCK_GRID_PANEL_UI_CREATE_CODE_GROUP_CHILD_ACCESS_DENIED_ELEMENT', [
116 '#ID#' => $id,
117 ]);
118 $result->addError(
119 new Error($message)
120 );
121
122 continue;
123 }
124
125 $fields = [
126 'CODE' => CUtil::translit(
127 $row['NAME'],
128 LANGUAGE_ID,
129 $translitSettings
130 ),
131 ];
132 $updateResult = $entity->Update($id, $fields);
133 if (!$updateResult && $entity->getLastError())
134 {
135 $result->addError(
136 new Error($entity->getLastError())
137 );
138 }
139 }
140
141 return $result;
142 }
143
144 private function processCodeTranslitSections(array $sections): Result
145 {
146 $result = new Result();
147 $entity = new CIBlockSection();
148 $translitSettings = $this->getSectionTranslitSettings();
149
150 foreach ($sections as $row)
151 {
152 $id = $row['ID'];
153
154 if (!$this->getIblockRightsChecker()->canEditSection($id))
155 {
156 $message = Loc::getMessage('IBLOCK_GRID_PANEL_UI_CREATE_CODE_GROUP_CHILD_ACCESS_DENIED_SECTION', [
157 '#ID#' => $id,
158 ]);
159 $result->addError(
160 new Error($message)
161 );
162
163 continue;
164 }
165
166 $fields = [
167 'CODE' => CUtil::translit(
168 $row['NAME'],
169 LANGUAGE_ID,
170 $translitSettings
171 ),
172 ];
173 $updateResult = $entity->Update($id, $fields);
174 if (!$updateResult && $entity->getLastError())
175 {
176 $result->addError(
177 new Error($entity->getLastError())
178 );
179 }
180 }
181
182 return $result;
183 }
184}
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