Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
createcodeitem.php
1<?php
2
4
6use Bitrix\Iblock\Grid\Helpers\CodeTranslator;
13use CIBlockElement;
14use CIBlockSection;
15use CUtil;
16
17final class CreateCodeItem extends BaseItem
18{
19 use CodeTranslator;
20
21 public static function getId(): string
22 {
23 return 'code_translit';
24 }
25
26 protected function getText(): string
27 {
28 return Loc::getMessage('IBLOCK_GRID_ROW_ACTIONS_CREATE_CODE_NAME_MSGVER_1');
29 }
30
31 public function getControl(array $rawFields): ?array
32 {
33 if (empty($rawFields['ID']))
34 {
35 return null;
36 }
37
38 $rowId = (int)$rawFields['ID'];
39 $rowType = (string)($rawFields['ROW_TYPE'] ?? RowType::ELEMENT);
40
41 if ($rowType === RowType::SECTION)
42 {
43 if (
44 empty($this->getSectionTranslitSettings())
45 || !$this->getIblockRightsChecker()->canEditSection($rowId)
46 )
47 {
48 return null;
49 }
50 }
51 elseif ($rowType === RowType::ELEMENT)
52 {
53 if (
54 empty($this->getElementTranslitSettings())
55 || !$this->getIblockRightsChecker()->canEditElement($rowId)
56 )
57 {
58 return null;
59 }
60 }
61 else
62 {
63 // unknown type
64 return null;
65 }
66
67 $actionId = self::getId();
68 $data = CUtil::PhpToJSObject([
69 'id' => RowType::getIndex($rowType, $rowId),
70 ]);
71
72 if ($rowType === RowType::SECTION)
73 {
74 $confirmMessageTitle = \CUtil::JSEscape(Loc::getMessage('IBLOCK_GRID_ROW_ACTIONS_CREATE_CODE_SECTION_CONFIRM_TITLE'));
75 $confirmMessageContent = \CUtil::JSEscape(Loc::getMessage('IBLOCK_GRID_ROW_ACTIONS_CREATE_CODE_SECTION_CONFIRM_CONTENT'));
76 }
77 else
78 {
79 $confirmMessageTitle = \CUtil::JSEscape(Loc::getMessage('IBLOCK_GRID_ROW_ACTIONS_CREATE_CODE_ELEMENT_CONFIRM_TITLE'));
80 $confirmMessageContent = \CUtil::JSEscape(Loc::getMessage('IBLOCK_GRID_ROW_ACTIONS_CREATE_CODE_ELEMENT_CONFIRM_CONTENT'));
81 }
82
83 $confirmButtonMessage = \CUtil::JSEscape(
84 Loc::getMessage('IBLOCK_GRID_ROW_ACTIONS_CREATE_CODE_CONFIRM_BUTTON')
85 );
86 $backButtonMessage = \CUtil::JSEscape(
87 Loc::getMessage('IBLOCK_GRID_ROW_ACTIONS_CREATE_CODE_BACK_BUTTON')
88 );
89
90 $this->onclick = "IblockGridInstance.sendMediumPopupWithConfirm("
91 . "'{$actionId}', "
92 . "{$data}, "
93 . "'{$confirmMessageTitle}', "
94 . "'{$confirmMessageContent}', "
95 . "'{$confirmButtonMessage}', "
96 . "'{$backButtonMessage}')";
97
98 return parent::getControl($rawFields);
99 }
100
101 public function processRequest(HttpRequest $request): ?Result
102 {
103 $result = new Result();
104
105 $rowIndex = (string)$request->get('id');
106 if (empty($rowIndex))
107 {
108 return $result;
109 }
110
111 $index = RowType::parseIndex($rowIndex);
112 if ($index === null)
113 {
114 return $result;
115 }
116 [$type, $id] = $index;
117
118 if ($type === RowType::SECTION)
119 {
120 $result->addErrors(
121 $this->processCodeTranslitSection($id)->getErrors()
122 );
123 }
124 elseif ($type === RowType::ELEMENT)
125 {
126 $result->addErrors(
127 $this->processCodeTranslitElement($id)->getErrors()
128 );
129 }
130
131 return $result;
132 }
133
139 private function processCodeTranslitElement(int $id): Result
140 {
141 $result = new Result();
142
143 if (!$this->getIblockRightsChecker()->canEditElement($id))
144 {
145 $message = Loc::getMessage('IBLOCK_GRID_ROW_ACTIONS_CREATE_CODE_ERROR_ACCESS_DENIED_ELEMENT', [
146 '#ID#' => $id,
147 ]);
148 $result->addError(
149 new Error($message)
150 );
151
152 return $result;
153 }
154
155 $row = ElementTable::getRow([
156 'select' => [
157 'ID',
158 'NAME',
159 ],
160 'filter' => [
161 '=ID' => $id,
162 '=IBLOCK_ID' => $this->getIblockId(),
163 ],
164 ]);
165 if (empty($row))
166 {
167 return $result;
168 }
169
170 $entity = new CIBlockElement();
171 $translitSettings = $this->getElementTranslitSettings();
172
173 $fields = [
174 'CODE' => CUtil::translit(
175 $row['NAME'],
176 LANGUAGE_ID,
177 $translitSettings
178 ),
179 ];
180 $updateResult = $entity->Update($id, $fields);
181 if (!$updateResult && $entity->getLastError())
182 {
183 $result->addError(
184 new Error($entity->getLastError())
185 );
186 }
187
188 return $result;
189 }
190
196 private function processCodeTranslitSection(int $id): Result
197 {
198 $result = new Result();
199
200 if (!$this->getIblockRightsChecker()->canEditSection($id))
201 {
202 $message = Loc::getMessage('IBLOCK_GRID_ROW_ACTIONS_CREATE_CODE_ERROR_ACCESS_DENIED_SECTION', [
203 '#ID#' => $id,
204 ]);
205 $result->addError(
206 new Error($message)
207 );
208
209 return $result;
210 }
211
212 $row = SectionTable::getRow([
213 'select' => [
214 'ID',
215 'NAME',
216 ],
217 'filter' => [
218 '=ID' => $id,
219 '=IBLOCK_ID' => $this->getIblockId(),
220 ],
221 ]);
222 if (empty($row))
223 {
224 return $result;
225 }
226
227 $entity = new CIBlockSection();
228 $translitSettings = $this->getSectionTranslitSettings();
229
230 $fields = [
231 'CODE' => CUtil::translit(
232 $row['NAME'],
233 LANGUAGE_ID,
234 $translitSettings
235 ),
236 ];
237 $updateResult = $entity->Update($id, $fields);
238 if (!$updateResult && $entity->getLastError())
239 {
240 $result->addError(
241 new Error($entity->getLastError())
242 );
243 }
244
245 return $result;
246 }
247}
static getIndex(string $type, string|int $id)
Definition rowtype.php:17
static parseIndex(string $index)
Definition rowtype.php:27
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getRow(array $parameters)