Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
field.php
1<?php
3
8
9Loc::loadMessages(__FILE__);
10
11class Field extends BaseField
12{
19 public function copy($iblockId, $copiedIblockId): Result
20 {
21 $fields = $this->getFieldsToCopy($iblockId, $copiedIblockId);
22 $this->addListsFields($iblockId, $copiedIblockId, $fields);
23
24 return $this->result;
25 }
26
27 private function getFieldsToCopy($iblockId, $copiedIblockId)
28 {
29 $fields = [];
30
31 $object = new \CList($iblockId);
32
33 foreach($object->getFields() as $fieldId => $field)
34 {
35 $copiedField = [
36 "ID" => $fieldId,
37 "NAME" => $field["NAME"],
38 "SORT" => $field["SORT"],
39 "MULTIPLE" => $field["MULTIPLE"],
40 "IS_REQUIRED" => $field["IS_REQUIRED"],
41 "IBLOCK_ID" => $copiedIblockId,
42 "SETTINGS" => $field["SETTINGS"],
43 "DEFAULT_VALUE" => $field["DEFAULT_VALUE"],
44 "TYPE" => $field["TYPE"],
45 "PROPERTY_TYPE" => $field["PROPERTY_TYPE"],
46 ];
47
48 if (!$object->is_field($fieldId))
49 {
50 if ($field["TYPE"] == "L")
51 {
52 $copiedField["VALUES"] = $this->getEnumValues($fieldId);
53 }
54
55 $copiedField["CODE"] = $field["CODE"];
56 $copiedField["LINK_IBLOCK_ID"] = $field["LINK_IBLOCK_ID"];
57 if (!empty($field["PROPERTY_USER_TYPE"]["USER_TYPE"]))
58 $copiedField["USER_TYPE"] = $field["PROPERTY_USER_TYPE"]["USER_TYPE"];
59 if (!empty($field["ROW_COUNT"]))
60 $copiedField["ROW_COUNT"] = $field["ROW_COUNT"];
61 if (!empty($field["COL_COUNT"]))
62 $copiedField["COL_COUNT"] = $field["COL_COUNT"];
63 if (!empty($field["USER_TYPE_SETTINGS"]))
64 $copiedField["USER_TYPE_SETTINGS"] = $field["USER_TYPE_SETTINGS"];
65 }
66 $fields[] = $copiedField;
67 }
68
69 return $fields;
70 }
71
72 private function addListsFields($iblockId, $copiedIblockId, array $fields)
73 {
74 $object = new \CList($copiedIblockId);
75
76 foreach ($fields as $field)
77 {
78 if ($field["ID"] == "NAME")
79 {
80 $result = $object->updateField("NAME", $field);
81 }
82 else
83 {
84 $result = $object->addField($field);
85 }
86
87 if ($result)
88 {
89 $object->save();
90
91 $this->setEnumRatio($iblockId, $field["ID"], $result);
92 }
93 else
94 {
95 $this->result->addError(
96 new Error(
97 Loc::getMessage("COPY_FIELD_ERROR", ["#NAME#" => $field["NAME"]]),
98 self::FIELD_COPY_ERROR
99 )
100 );
101 }
102 }
103 }
104}
setEnumRatio($iblockId, $fieldId, $copiedFieldId)
Definition field.php:130
copy($iblockId, $copiedIblockId)
Definition field.php:19
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29