Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
element.php
1<?php
3
8
10{
11 const ELEMENT_COPY_ERROR = "ELEMENT_COPY_ERROR";
12
20 public function add(Container $container, array $fields)
21 {
22 $elementObject = new \CIBlockElement;
23 $elementId = $elementObject->add($fields, false, true, true);
24 if ($elementId)
25 {
26 return $elementId;
27 }
28 else
29 {
30 if ($elementObject->LAST_ERROR)
31 {
32 $this->result->addError(new Error($elementObject->LAST_ERROR, self::ELEMENT_COPY_ERROR));
33 }
34 else
35 {
36 $this->result->addError(new Error("Unknown error", self::ELEMENT_COPY_ERROR));
37 }
38 return false;
39 }
40 }
41
49 public function getFields(Container $container, $entityId)
50 {
51 $fields = [];
52
53 $filter = [
54 "ID" => $entityId,
55 "CHECK_PERMISSIONS" => "N"
56 ];
57 $queryObject = \CIBlockElement::getList([], $filter, false, false);
58 if ($element = $queryObject->fetch())
59 {
60 $fields = $element;
61 $propertyValuesObject = \CIblockElement::getPropertyValues(
62 $element["IBLOCK_ID"], ["ID" => $entityId]);
63 while ($propertyValues = $propertyValuesObject->fetch())
64 {
65 foreach ($propertyValues as $propertyId => $propertyValue)
66 {
67 if ($propertyId == "IBLOCK_ELEMENT_ID")
68 continue;
69 $fields["PROPERTY_".$propertyId] = $propertyValue;
70 }
71 }
72 }
73
74 return $fields;
75 }
76
84 public function prepareFieldsToCopy(Container $container, array $inputFields)
85 {
86 $fields = [
87 "PROPERTY_VALUES" => []
88 ];
89
90 foreach ($inputFields as $fieldId => $fieldValue)
91 {
92 if (mb_substr($fieldId, 0, 9) == "PROPERTY_")
93 {
94 $propertyId = mb_substr($fieldId, mb_strlen("PROPERTY_"));
95 $fields["PROPERTY_VALUES"][$propertyId] = $this->getPropertyFieldValue(
96 $container, $fieldId, $fieldValue);
97 }
98 else
99 {
100 $fields[$fieldId] = $this->getFieldValue($fieldId, $fieldValue);
101 }
102 }
103
104 unset($fields["DATE_CREATE"]);
105 unset($fields["TIMESTAMP_X"]);
106 unset($fields["XML_ID"]);
107
108 $dictionary = $container->getDictionary();
109
110 if (array_key_exists($fields["IBLOCK_SECTION_ID"], $dictionary["sectionsRatio"]))
111 {
112 $fields["IBLOCK_SECTION_ID"] = $dictionary["sectionsRatio"][$fields["IBLOCK_SECTION_ID"]];
113 }
114
115 $fields["RIGHTS"] = $this->getRights($fields["IBLOCK_ID"], $fields["ID"]);
116
117 if (!empty($dictionary["targetIblockId"]))
118 {
119 $fields["IBLOCK_ID"] = $dictionary["targetIblockId"];
120 $fields = $this->convertPropertyId($fields, $dictionary["targetIblockId"]);
121 }
122
123 return $fields;
124 }
125
134 public function copyChildren(Container $container, $elementId, $copiedElementId)
135 {
136 return $this->getResult();
137 }
138
139 private function getFieldValue($fieldId, $fieldValue)
140 {
141 switch ($fieldId)
142 {
143 case "PREVIEW_PICTURE":
144 case "DETAIL_PICTURE":
145 return $this->getPictureValue($fieldValue);
146 break;
147 default:
148 return $this->getBaseValue($fieldValue);
149 }
150 }
151
152 private function getPropertyFieldValue(Container $container, $fieldId, $fieldValue)
153 {
154 $propertyId = mb_substr($fieldId, mb_strlen("PROPERTY_"));
155 $fieldValue = (is_array($fieldValue) ? $fieldValue : [$fieldValue]);
156
157 $queryObject = \CIBlockProperty::getList([], ["ID" => $propertyId]);
158 if ($property = $queryObject->fetch())
159 {
160 if (!empty($property["USER_TYPE"]))
161 {
162 $userType = \CIBlockProperty::getUserType($property["USER_TYPE"]);
163 if ($userType["ConvertFromDB"] && is_callable($userType["ConvertFromDB"]))
164 {
165 $fieldValue = $this->getValueFromPropertyClass($fieldValue, $userType["ConvertFromDB"]);
166 }
167 else
168 {
169 $fieldValue = $this->getPropertyValue($fieldValue);
170 }
171 }
172 else
173 {
174 switch ($property["PROPERTY_TYPE"])
175 {
176 case "F":
177 $fieldValue = $this->getFileValue($fieldValue);
178 break;
179 case "N":
180 $fieldValue = $this->getIntegerValue($fieldValue);
181 break;
182 case "L":
183 $fieldValue = $this->getListValue($container, $fieldValue);
184 break;
185 default:
186 $fieldValue = $this->getPropertyValue($fieldValue);
187 }
188 }
189 }
190
191 return $fieldValue;
192 }
193
194 private function getPictureValue($fieldValue)
195 {
196 return \CFile::makeFileArray($fieldValue);
197 }
198
199 private function getBaseValue($fieldValue)
200 {
201 return (is_array($fieldValue) ? current($fieldValue) : $fieldValue);
202 }
203
204 private function getFileValue(array $fieldValue)
205 {
206 array_walk($fieldValue, function(&$value) {
207 $value = ["VALUE" => \CFile::makeFileArray($value)];
208 });
209 return $fieldValue;
210 }
211
212 private function getListValue(Container $container, array $inputValue)
213 {
214 $values = [];
215
216 $dictionary = $container->getDictionary();
217 $enumRatio = $dictionary["enumRatio"];
218
219 if ($enumRatio)
220 {
221 foreach ($inputValue as $value)
222 {
223 if ($value && array_key_exists($value, $enumRatio))
224 {
225 $values[] = $enumRatio[$value];
226 }
227 }
228 }
229
230 return $values;
231 }
232
233 private function getPropertyValue(array $inputValue)
234 {
235 $values = [];
236 foreach ($inputValue as $key => $value)
237 {
238 if (is_array($value))
239 {
240 foreach ($value as $k => $v)
241 $values[$k]["VALUE"] = $v;
242 }
243 else
244 {
245 $values[$key]["VALUE"] = $value;
246 }
247 }
248 return $values;
249 }
250
251 private function getIntegerValue(array $fieldValue)
252 {
253 array_walk($fieldValue, function(&$value) {
254 $value = [
255 "VALUE" => ($value === false ? "" : floatval(str_replace(" ", "", str_replace(",", ".", $value))))
256 ];
257 });
258 return $fieldValue;
259 }
260
261 private function convertPropertyId(array $fields, $targetIblockId)
262 {
263 $targetProperties = [];
264 $queryObject = \CIBlockProperty::getList([], ["IBLOCK_ID" => $targetIblockId]);
265 while ($property = $queryObject->fetch())
266 {
267 $targetProperties[$property["ID"]] = $property["CODE"];
268 }
269
270 foreach ($fields["PROPERTY_VALUES"] as $propertyId => $propertyValue)
271 {
272 $queryObject = \CIBlockProperty::getList([], ["ID" => $propertyId]);
273 if ($property = $queryObject->fetch())
274 {
275 foreach ($targetProperties as $targetPropertyId => $targetPropertyCode)
276 {
277 if ($targetPropertyCode == $property["CODE"])
278 {
279 $fields["PROPERTY_VALUES"][$targetPropertyId] = $propertyValue;
280 }
281 }
282 unset($fields["PROPERTY_VALUES"][$propertyId]);
283 }
284 }
285
286 return $fields;
287 }
288
289 private function getValueFromPropertyClass(array $fieldValue, $callback)
290 {
291 $listValues = [];
292 foreach ($fieldValue as $value)
293 {
294 $listValues[] = call_user_func_array($callback, [[], ["VALUE" => $value]]);
295 }
296 $fieldValue = $listValues;
297
298 return $fieldValue;
299 }
300
301 private function getRights(int $iblockId, int $elementId)
302 {
303 $rights = [];
304
305 $objectRights = new \CIBlockElementRights($iblockId, $elementId);
306
307 $groupCodeIgnoreList = $this->getGroupCodeIgnoreList($iblockId);
308
309 foreach ($objectRights->getRights() as $right)
310 {
311 if (!in_array($right["GROUP_CODE"], $groupCodeIgnoreList))
312 {
313 $rights["n".(count($rights))] = [
314 "GROUP_CODE" => $right["GROUP_CODE"],
315 "DO_CLEAN" => "N",
316 "TASK_ID" => $right["TASK_ID"],
317 ];
318 }
319 }
320
321 return $rights;
322 }
323
324 private function getGroupCodeIgnoreList(int $iblockId): array
325 {
326 $groupCodeIgnoreList = [];
327
328 $rightObject = new \CIBlockRights($iblockId);
329 foreach ($rightObject->getRights() as $right)
330 {
331 $groupCodeIgnoreList[] = $right["GROUP_CODE"];
332 }
333
334 return $groupCodeIgnoreList;
335 }
336}
getFields(Container $container, $entityId)
Definition element.php:49
prepareFieldsToCopy(Container $container, array $inputFields)
Definition element.php:84
copyChildren(Container $container, $elementId, $copiedElementId)
Definition element.php:134
add(Container $container, array $fields)
Definition element.php:20