Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
section.php
1<?php
3
6
7class Section implements Child
8{
12 private $result;
13
14 private $sectionsRatio = [];
15
16 public function __construct()
17 {
18 $this->result = new Result();
19 }
20
27 public function getSectionsRatio($iblockId = 0): array
28 {
29 return (isset($this->sectionsRatio[$iblockId]) ? $this->sectionsRatio[$iblockId] : $this->sectionsRatio);
30 }
31
32 public function copy($iblockId, $copiedIblockId): Result
33 {
34 $sectionObject = new \CIBlockSection;
35
36 $sections = $this->getSections($iblockId, $copiedIblockId);
37 $parentRatioIds = $this->getParentRatioIds($sections);
38 $ratioIds = $this->addSections($sectionObject, $sections);
39 $this->updateSections($sectionObject, $parentRatioIds, $ratioIds);
40 $this->setRatios($iblockId, $ratioIds);
41
42 return $this->result;
43 }
44
45 private function getSections($iblockId, $copiedIblockId)
46 {
47 $sections = [];
48
49 $queryObject = \CIBlockSection::getList([], ["IBLOCK_ID" => $iblockId, "CHECK_PERMISSIONS" => "N"], false);
50 while ($section = $queryObject->fetch())
51 {
52 $section["IBLOCK_ID"] = $copiedIblockId;
53 $sections[] = $section;
54 }
55
56 return $sections;
57 }
58
59 private function getParentRatioIds(array $sections): array
60 {
61 $oldRatioIds = [];
62 foreach ($sections as $section)
63 {
64 if (!empty($section["IBLOCK_SECTION_ID"]))
65 {
66 $oldRatioIds[$section["ID"]] = $section["IBLOCK_SECTION_ID"];
67 }
68 }
69 return $oldRatioIds;
70 }
71
72 private function addSections(\CIBlockSection $sectionObject, array $sections): array
73 {
74 $rationIds = [];
75 foreach ($sections as $section)
76 {
77 unset($section["IBLOCK_SECTION_ID"]);
78 $rationIds[$section["ID"]] = $this->addSection($sectionObject, $section);
79 }
80
81 return $rationIds;
82 }
83
84 private function updateSections(\CIBlockSection $sectionObject, array $parentRatioIds, array $ratioIds)
85 {
86 foreach ($parentRatioIds as $parentId => $childId)
87 {
88 if (array_key_exists($parentId, $ratioIds) && array_key_exists($childId, $ratioIds))
89 {
90 $copiedSectionId = $ratioIds[$parentId];
91 $sectionObject->update($copiedSectionId, ["IBLOCK_SECTION_ID" => $ratioIds[$childId]]);
92 }
93 }
94 }
95
96 private function addSection(\CIBlockSection $sectionObject, $section)
97 {
98 $result = $sectionObject->add($section);
99 if (!$result)
100 {
101 if ($sectionObject->LAST_ERROR)
102 {
103 $this->result->addError(new Error($sectionObject->LAST_ERROR));
104 }
105 else
106 {
107 $this->result->addError(new Error("Unknown error while copying section"));
108 }
109 }
110 return $result;
111 }
112
113 private function setRatios($iblockId, array $ratioIds)
114 {
115 $this->sectionsRatio[$iblockId] = $ratioIds;
116 }
117}
copy($iblockId, $copiedIblockId)
Definition section.php:32