22 $elementObject = new \CIBlockElement;
23 $elementId = $elementObject->add($fields,
false,
true,
true);
30 if ($elementObject->LAST_ERROR)
32 $this->result->addError(
new Error($elementObject->LAST_ERROR, self::ELEMENT_COPY_ERROR));
36 $this->result->addError(
new Error(
"Unknown error", self::ELEMENT_COPY_ERROR));
55 "CHECK_PERMISSIONS" =>
"N"
57 $queryObject = \CIBlockElement::getList([], $filter,
false,
false);
58 if ($element = $queryObject->fetch())
61 $propertyValuesObject = \CIblockElement::getPropertyValues(
62 $element[
"IBLOCK_ID"], [
"ID" => $entityId]);
63 while ($propertyValues = $propertyValuesObject->fetch())
65 foreach ($propertyValues as $propertyId => $propertyValue)
67 if ($propertyId ==
"IBLOCK_ELEMENT_ID")
69 $fields[
"PROPERTY_".$propertyId] = $propertyValue;
87 "PROPERTY_VALUES" => []
90 foreach ($inputFields as $fieldId => $fieldValue)
92 if (mb_substr($fieldId, 0, 9) ==
"PROPERTY_")
94 $propertyId = mb_substr($fieldId, mb_strlen(
"PROPERTY_"));
95 $fields[
"PROPERTY_VALUES"][$propertyId] = $this->getPropertyFieldValue(
96 $container, $fieldId, $fieldValue);
100 $fields[$fieldId] = $this->getFieldValue($fieldId, $fieldValue);
104 unset($fields[
"DATE_CREATE"]);
105 unset($fields[
"TIMESTAMP_X"]);
106 unset($fields[
"XML_ID"]);
110 if (array_key_exists($fields[
"IBLOCK_SECTION_ID"], $dictionary[
"sectionsRatio"]))
112 $fields[
"IBLOCK_SECTION_ID"] = $dictionary[
"sectionsRatio"][$fields[
"IBLOCK_SECTION_ID"]];
115 $fields[
"RIGHTS"] = $this->getRights($fields[
"IBLOCK_ID"], $fields[
"ID"]);
117 if (!empty($dictionary[
"targetIblockId"]))
119 $fields[
"IBLOCK_ID"] = $dictionary[
"targetIblockId"];
120 $fields = $this->convertPropertyId($fields, $dictionary[
"targetIblockId"]);
139 private function getFieldValue($fieldId, $fieldValue)
143 case "PREVIEW_PICTURE":
144 case "DETAIL_PICTURE":
145 return $this->getPictureValue($fieldValue);
148 return $this->getBaseValue($fieldValue);
152 private function getPropertyFieldValue(Container $container, $fieldId, $fieldValue)
154 $propertyId = mb_substr($fieldId, mb_strlen(
"PROPERTY_"));
155 $fieldValue = (is_array($fieldValue) ? $fieldValue : [$fieldValue]);
157 $queryObject = \CIBlockProperty::getList([], [
"ID" => $propertyId]);
158 if ($property = $queryObject->fetch())
160 if (!empty($property[
"USER_TYPE"]))
162 $userType = \CIBlockProperty::getUserType($property[
"USER_TYPE"]);
163 if ($userType[
"ConvertFromDB"] && is_callable($userType[
"ConvertFromDB"]))
165 $fieldValue = $this->getValueFromPropertyClass($fieldValue, $userType[
"ConvertFromDB"]);
169 $fieldValue = $this->getPropertyValue($fieldValue);
174 switch ($property[
"PROPERTY_TYPE"])
177 $fieldValue = $this->getFileValue($fieldValue);
180 $fieldValue = $this->getIntegerValue($fieldValue);
183 $fieldValue = $this->getListValue($container, $fieldValue);
186 $fieldValue = $this->getPropertyValue($fieldValue);
194 private function getPictureValue($fieldValue)
196 return \CFile::makeFileArray($fieldValue);
199 private function getBaseValue($fieldValue)
201 return (is_array($fieldValue) ? current($fieldValue) : $fieldValue);
204 private function getFileValue(array $fieldValue)
206 array_walk($fieldValue,
function(&$value) {
207 $value = [
"VALUE" => \CFile::makeFileArray($value)];
212 private function getListValue(Container $container, array $inputValue)
216 $dictionary = $container->getDictionary();
217 $enumRatio = $dictionary[
"enumRatio"];
221 foreach ($inputValue as $value)
223 if ($value && array_key_exists($value, $enumRatio))
225 $values[] = $enumRatio[$value];
233 private function getPropertyValue(array $inputValue)
236 foreach ($inputValue as $key => $value)
238 if (is_array($value))
240 foreach ($value as $k => $v)
241 $values[$k][
"VALUE"] = $v;
245 $values[$key][
"VALUE"] = $value;
251 private function getIntegerValue(array $fieldValue)
253 array_walk($fieldValue,
function(&$value) {
255 "VALUE" => ($value ===
false ?
"" : floatval(str_replace(
" ",
"", str_replace(
",",
".", $value))))
261 private function convertPropertyId(array $fields, $targetIblockId)
263 $targetProperties = [];
264 $queryObject = \CIBlockProperty::getList([], [
"IBLOCK_ID" => $targetIblockId]);
265 while ($property = $queryObject->fetch())
267 $targetProperties[$property[
"ID"]] = $property[
"CODE"];
270 foreach ($fields[
"PROPERTY_VALUES"] as $propertyId => $propertyValue)
272 $queryObject = \CIBlockProperty::getList([], [
"ID" => $propertyId]);
273 if ($property = $queryObject->fetch())
275 foreach ($targetProperties as $targetPropertyId => $targetPropertyCode)
277 if ($targetPropertyCode == $property[
"CODE"])
279 $fields[
"PROPERTY_VALUES"][$targetPropertyId] = $propertyValue;
282 unset($fields[
"PROPERTY_VALUES"][$propertyId]);
289 private function getValueFromPropertyClass(array $fieldValue, $callback)
292 foreach ($fieldValue as $value)
294 $listValues[] = call_user_func_array($callback, [[], [
"VALUE" => $value]]);
296 $fieldValue = $listValues;
301 private function getRights(
int $iblockId,
int $elementId)
305 $objectRights = new \CIBlockElementRights($iblockId, $elementId);
307 $groupCodeIgnoreList = $this->getGroupCodeIgnoreList($iblockId);
309 foreach ($objectRights->getRights() as $right)
311 if (!in_array($right[
"GROUP_CODE"], $groupCodeIgnoreList))
313 $rights[
"n".(count($rights))] = [
314 "GROUP_CODE" => $right[
"GROUP_CODE"],
316 "TASK_ID" => $right[
"TASK_ID"],
324 private function getGroupCodeIgnoreList(
int $iblockId): array
326 $groupCodeIgnoreList = [];
328 $rightObject = new \CIBlockRights($iblockId);
329 foreach ($rightObject->getRights() as $right)
331 $groupCodeIgnoreList[] = $right[
"GROUP_CODE"];
334 return $groupCodeIgnoreList;