1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
field.php
См. документацию.
1<?php
2namespace Bitrix\Iblock\Copy\Implement\Children;
3
4use Bitrix\Iblock\PropertyTable;
5use Bitrix\Main\Error;
6use Bitrix\Main\Result;
7
8class Field implements Child
9{
10 const FIELD_COPY_ERROR = "FIELD_COPY_ERROR";
11
12 private $enumRatio = [];
13 private $enumTmpMap = [];
14
15 private array $fieldRatio = [];
16
20 protected $result;
21
22 public function __construct()
23 {
24 $this->result = new Result();
25 }
26
32 public function getEnumRatio()
33 {
34 return $this->enumRatio;
35 }
36
44 public function copy($iblockId, $copiedIblockId): Result
45 {
46 $fields = $this->getFieldsToCopy($iblockId);
47 $this->addFields($copiedIblockId, $fields);
48
49 $properties = $this->getProperty($iblockId);
50 $this->addProperties($copiedIblockId, $properties);
51
52 return $this->result;
53 }
54
55 private function getFieldsToCopy($iblockId)
56 {
57 return \CIBlock::getFields($iblockId);
58 }
59
60 private function addFields($copiedIblockId, $fields)
61 {
62 \CIBlock::setFields($copiedIblockId, $fields);
64 global $stackCacheManager;
65 if (is_object($stackCacheManager))
66 {
67 $stackCacheManager->clear("b_iblock");
68 }
69 }
70
71 private function getProperty($iblockId)
72 {
73 $fields = [];
74 $this->fieldRatio[$iblockId] ??= [];
75
76 $queryObject = \CIBlock::getProperties($iblockId);
77 while ($property = $queryObject->fetch())
78 {
79 $fields[] = $property;
80 }
81
82 return $fields;
83 }
84
85 private function addProperties($copiedIblockId, array $properties)
86 {
87 foreach ($properties as $propertyField)
88 {
89 $iblockId = $propertyField["IBLOCK_ID"];
90 $propertyField["IBLOCK_ID"] = $copiedIblockId;
91
92 $property = new \CIBlockProperty;
93 $propertyId = $property->add($propertyField);
94 if ($propertyId)
95 {
96 if (
97 $propertyField['PROPERTY_TYPE'] === PropertyTable::TYPE_LIST
98 && !empty($propertyField['LIST'])
99 && is_array($propertyField['LIST'])
100 )
101 {
102 $this->addPropertyList($propertyId, $propertyField["LIST"]);
103 }
104 $this->fieldRatio[$iblockId][$propertyField['ID']] = $propertyId;
105 }
106
107 $error = $property->getLastError();
108 if ($error !== '')
109 {
110 $this->result->addError(new Error($error, self::FIELD_COPY_ERROR));
111 }
112 }
113 }
114
115 private function addPropertyList($propertyId, $list)
116 {
117 foreach ($list as $id => $enum)
118 {
119 if (is_array($enum))
120 {
121 $value = trim($enum["VALUE"], " \t\n\r");
122 if($value <> '')
123 {
124 $enum["PROPERTY_ID"] = $propertyId;
125 \CIBlockPropertyEnum::add($enum);
126 }
127 }
128 }
129 }
130
131 protected function getEnumValues($fieldId)
132 {
133 $values = [];
134
135 $this->enumTmpMap[$fieldId] = [];
136
137 $propertyId = mb_substr($fieldId, mb_strlen("PROPERTY_"));
138 $enum = \CIBlockPropertyEnum::getList([], ["PROPERTY_ID" => $propertyId]);
139 while ($listData = $enum->fetch())
140 {
141 $values[] = [
142 "VALUE" => $listData["VALUE"],
143 "DEF" => $listData["DEF"],
144 "SORT" => $listData["SORT"]
145 ];
146
147 $this->enumTmpMap[$fieldId][$listData["VALUE"]] = $listData["ID"];
148 }
149
150 return $values;
151 }
152
153 protected function setEnumRatio($iblockId, $fieldId, $copiedFieldId)
154 {
155 if (array_key_exists($fieldId, $this->enumTmpMap))
156 {
157 $enumTmpMap = $this->enumTmpMap[$fieldId];
158 $this->enumRatio[$iblockId] ??= [];
159 if (!is_array($this->enumRatio[$iblockId]))
160 {
161 $this->enumRatio[$iblockId] = [];
162 }
163 $propertyId = mb_substr($copiedFieldId, mb_strlen("PROPERTY_"));
164 $enum = \CIBlockPropertyEnum::getList([], ["PROPERTY_ID" => $propertyId]);
165 while ($listData = $enum->fetch())
166 {
167 if (array_key_exists($listData["VALUE"], $enumTmpMap))
168 {
169 $this->enumRatio[$iblockId][$enumTmpMap[$listData["VALUE"]]] = $listData["ID"];
170 }
171 }
172 }
173 }
174
180 public function getFieldRatio(): array
181 {
182 return $this->fieldRatio;
183 }
184
193 public function setFieldRatio(int $iblockId, int $fieldId, int $newFieldId): void
194 {
195 $this->fieldRatio[$iblockId] ??= [];
196 $this->fieldRatio[$iblockId][$fieldId] = $newFieldId;
197 }
198}
setFieldRatio(int $iblockId, int $fieldId, int $newFieldId)
Определения field.php:193
copy($iblockId, $copiedIblockId)
Определения field.php:44
setEnumRatio($iblockId, $fieldId, $copiedFieldId)
Определения field.php:153
const TYPE_LIST
Определения propertytable.php:70
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$iblockId
Определения iblock_catalog_edit.php:30
$value
Определения Param.php:39
trait Error
Определения error.php:11
$error
Определения subscription_card_product.php:20
$fields
Определения yandex_run.php:501