Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
editactionsitem.php
1<?php
2
4
16use CIBlockElement;
17use CIBlockSection;
18
20{
21 private int $iblockId;
22 private Columns $columns;
23 private IblockRightsChecker $rights;
24 private CIBlockElement $elementEntity;
25 private CIBlockSection $sectionEntity;
26
27 public static function getId(): string
28 {
29 return ActionType::EDIT;
30 }
31
32 public function __construct(int $iblockId, Columns $columns, IblockRightsChecker $rights)
33 {
34 $this->iblockId = $iblockId;
35 $this->columns = $columns;
36 $this->rights = $rights;
37 }
38
39 private function getElementEntity(): CIBlockElement
40 {
41 if (!isset($this->elementEntity))
42 {
43 $this->elementEntity = new CIBlockElement();
44 $this->elementEntity->setIblock($this->getIblockId());
45 }
46
47 return $this->elementEntity;
48 }
49
50 private function getSectionEntity(): CIBlockSection
51 {
52 if (!isset($this->sectionEntity))
53 {
54 $this->sectionEntity = new CIBlockSection();
55 $this->sectionEntity->setIblock($this->getIblockId());
56 }
57
58 return $this->sectionEntity;
59 }
60
61 final protected function getIblockId(): int
62 {
63 return $this->iblockId;
64 }
65
66 final protected function getColumns(): Columns
67 {
68 return $this->columns;
69 }
70
71 public function processRequest(HttpRequest $request, bool $isSelectedAllRows, ?Filter $filter): ?Result
72 {
73 $result = new Result();
74
75 $rows = $request->getPost('FIELDS');
76 if (!is_array($rows))
77 {
78 return $result;
79 }
80 $rows = $this->appendFilesToRows($request, $rows);
81
82 foreach ($rows as $id => $fields)
83 {
84 $index = RowType::parseIndex($id);
85 if ($index === null)
86 {
87 continue;
88 }
89 [$type, $id] = $index;
90
91 $fields = $this->getColumns()->prepareEditableColumnsValues($fields);
92 if (empty($fields))
93 {
94 continue;
95 }
96
97 if ($type === RowType::ELEMENT)
98 {
99 if (!$this->rights->canEditElement($id))
100 {
101 $message = Loc::getMessage('', [
102 '#ID#' => $id,
103 ]);
104 $result->addError(
105 new Error($message)
106 );
107
108 continue;
109 }
110
111 $result->addErrors(
112 $this->saveElement($id, $fields)->getErrors()
113 );
114 }
115 elseif ($type === RowType::SECTION)
116 {
117 if (!$this->rights->canEditSection($id))
118 {
119 $message = Loc::getMessage('IBLOCK_GRID_PANEL_UI_EDIT_ACTIONS_ITEM_ACCESS_DENIED', [
120 '#ID#' => $id,
121 ]);
122 $result->addError(
123 new Error($message)
124 );
125
126 continue;
127 }
128
129 $result->addErrors(
130 $this->saveSection($id, $fields)->getErrors()
131 );
132 }
133 }
134
135 return $result;
136 }
137
138 private function splitElementFields(array $fields): array
139 {
140 $elementFields = [];
141 $propertyFields = [];
142
143 $propertyColumnIds = ElementPropertyProvider::getPropertyIdsFromColumnsIds(array_keys($fields));
144 foreach ($fields as $name => $value)
145 {
146 $propertyId = $propertyColumnIds[$name] ?? null;
147 if (isset($propertyId))
148 {
149 $propertyFields[$propertyId] = $value;
150 }
151 else
152 {
153 $elementFields[$name] = $value;
154 }
155 }
156
157 return [$elementFields, $propertyFields];
158 }
159
160 protected function saveElement(int $id, array $fields): Result
161 {
162 $result = new Result();
163
164 $fields = $this->prepareColumnsTypesValues($fields);
165
166 [$elementFields, $propertyFields] = $this->splitElementFields($fields);
167
168 if (!empty($elementFields))
169 {
170 $entity = $this->getElementEntity();
171 $entity->Update($id, $fields);
172 if ($entity->getLastError())
173 {
174 $result->addError(
175 new Error($entity->getLastError())
176 );
177 }
178 }
179
180 if ($result->isSuccess() && !empty($propertyFields))
181 {
182 CIBlockElement::SetPropertyValuesEx($id, 0, $propertyFields);
183 }
184
185 return $result;
186 }
187
188 protected function saveSection(int $id, array $fields): Result
189 {
190 $result = new Result();
191
192 $entity = $this->getSectionEntity();
193 $entity->Update($id, $fields);
194
195 if ($entity->getLastError())
196 {
197 $result->addError(
198 new Error($entity->getLastError())
199 );
200 }
201
202 return $result;
203 }
204
205 private function prepareColumnsTypesValues(array $fields): array
206 {
207 foreach ($this->getColumns() as $column)
208 {
209 $columnId = $column->getId();
210 $value = $fields[$columnId] ?? null;
211 if (!isset($value))
212 {
213 continue;
214 }
215
216 $editable = $column->getEditable();
217 if (!isset($editable))
218 {
219 continue;
220 }
221 elseif ($editable->getType() === Types::MULTISELECT)
222 {
223 if (is_array($value))
224 {
225 $fields[$columnId] = array_column($value, 'VALUE');
226 }
227 }
228 elseif ($editable->getType() === Types::IMAGE)
229 {
230 if ($value === 'null')
231 {
232 $fields[$columnId] = [
233 'VALUE' => [
234 'del' => 'Y',
235 ],
236 ];
237 }
238 }
239 }
240
241 return $fields;
242 }
243
244 protected function appendFilesToRows(HttpRequest $request, array $rows): array
245 {
246 $files = $request->getFile('FIELDS');
247 if (empty($files['name']))
248 {
249 return $rows;
250 }
251
252 foreach ($files['name'] as $rowId => $fields)
253 {
254 foreach ($fields as $fieldName => $fieldValue)
255 {
256 $rows[$rowId] ??= [];
257 $rows[$rowId][$fieldName] = [
258 'name' => $fieldValue,
259 'type' => $files['type'][$rowId][$fieldName] ?? null,
260 'size' => $files['size'][$rowId][$fieldName] ?? null,
261 'error' => $files['error'][$rowId][$fieldName] ?? null,
262 'tmp_name' => $files['tmp_name'][$rowId][$fieldName] ?? null,
263 'full_path' => $files['full_path'][$rowId][$fieldName] ?? null,
264 ];
265 }
266 }
267
268 return $rows;
269 }
270}
processRequest(HttpRequest $request, bool $isSelectedAllRows, ?Filter $filter)
__construct(int $iblockId, Columns $columns, IblockRightsChecker $rights)
static parseIndex(string $index)
Definition rowtype.php:27
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29