Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
field.php
1<?php
3
6
7class Field implements Child
8{
9 const FIELD_COPY_ERROR = "FIELD_COPY_ERROR";
10
11 private $enumRatio = [];
12 private $enumTmpMap = [];
13
17 protected $result;
18
19 public function __construct()
20 {
21 $this->result = new Result();
22 }
23
24 public function getEnumRatio()
25 {
26 return $this->enumRatio;
27 }
28
29 public function copy($iblockId, $copiedIblockId): Result
30 {
31 $fields = $this->getFieldsToCopy($iblockId);
32 $this->addFields($copiedIblockId, $fields);
33
34 $properties = $this->getProperty($iblockId);
35 $this->addProperties($copiedIblockId, $properties);
36
37 return $this->result;
38 }
39
40 private function getFieldsToCopy($iblockId)
41 {
42 return \CIBlock::getFields($iblockId);
43 }
44
45 private function addFields($copiedIblockId, $fields)
46 {
47 \CIBlock::setFields($copiedIblockId, $fields);
49 global $stackCacheManager;
50 if (is_object($stackCacheManager))
51 {
52 $stackCacheManager->clear("b_iblock");
53 }
54 }
55
56 private function getProperty($iblockId)
57 {
58 $fields = [];
59
60 $queryObject = \CIBlock::getProperties($iblockId);
61 while ($property = $queryObject->fetch())
62 {
63 $fields[] = $property;
64 }
65
66 return $fields;
67 }
68
69 private function addProperties($copiedIblockId, array $properties)
70 {
71 foreach ($properties as $propertyField)
72 {
73 $propertyField["IBLOCK_ID"] = $copiedIblockId;
74
75 $property = new \CIBlockProperty;
76 $propertyId = $property->add($propertyField);
77 if ($propertyId)
78 {
79 if ($propertyField["PROPERTY_TYPE"] == "L" && is_array($propertyField["LIST"]))
80 {
81 $this->addPropertyList($propertyId, $propertyField["LIST"]);
82 }
83 }
84
85 if (!empty($property->LAST_ERROR))
86 {
87 $this->result->addError(new Error($property->LAST_ERROR, self::FIELD_COPY_ERROR));
88 }
89 }
90 }
91
92 private function addPropertyList($propertyId, $list)
93 {
94 foreach ($list as $id => $enum)
95 {
96 if (is_array($enum))
97 {
98 $value = trim($enum["VALUE"], " \t\n\r");
99 if($value <> '')
100 {
101 $enum["PROPERTY_ID"] = $propertyId;
102 \CIBlockPropertyEnum::add($enum);
103 }
104 }
105 }
106 }
107
108 protected function getEnumValues($fieldId)
109 {
110 $values = [];
111
112 $this->enumTmpMap[$fieldId] = [];
113
114 $propertyId = mb_substr($fieldId, mb_strlen("PROPERTY_"));
115 $enum = \CIBlockPropertyEnum::getList([], ["PROPERTY_ID" => $propertyId]);
116 while ($listData = $enum->fetch())
117 {
118 $values[] = [
119 "VALUE" => $listData["VALUE"],
120 "DEF" => $listData["DEF"],
121 "SORT" => $listData["SORT"]
122 ];
123
124 $this->enumTmpMap[$fieldId][$listData["VALUE"]] = $listData["ID"];
125 }
126
127 return $values;
128 }
129
130 protected function setEnumRatio($iblockId, $fieldId, $copiedFieldId)
131 {
132 if (array_key_exists($fieldId, $this->enumTmpMap))
133 {
134 $enumTmpMap = $this->enumTmpMap[$fieldId];
135 if (!is_array($this->enumRatio[$iblockId]))
136 {
137 $this->enumRatio[$iblockId] = [];
138 }
139 $propertyId = mb_substr($copiedFieldId, mb_strlen("PROPERTY_"));
140 $enum = \CIBlockPropertyEnum::getList([], ["PROPERTY_ID" => $propertyId]);
141 while ($listData = $enum->fetch())
142 {
143 if (array_key_exists($listData["VALUE"], $enumTmpMap))
144 {
145 $this->enumRatio[$iblockId][$enumTmpMap[$listData["VALUE"]]] = $listData["ID"];
146 }
147 }
148 }
149 }
150}
copy($iblockId, $copiedIblockId)
Definition field.php:29
setEnumRatio($iblockId, $fieldId, $copiedFieldId)
Definition field.php:130