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