14 private $sectionsRatio = [];
18 $this->result =
new Result();
29 return (isset($this->sectionsRatio[$iblockId]) ? $this->sectionsRatio[$iblockId] : $this->sectionsRatio);
34 $sectionObject = new \CIBlockSection;
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);
45 private function getSections($iblockId, $copiedIblockId)
49 $queryObject = \CIBlockSection::getList([], [
"IBLOCK_ID" => $iblockId,
"CHECK_PERMISSIONS" =>
"N"],
false);
50 while ($section = $queryObject->fetch())
52 $section[
"IBLOCK_ID"] = $copiedIblockId;
53 $sections[] = $section;
59 private function getParentRatioIds(array $sections): array
62 foreach ($sections as $section)
64 if (!empty($section[
"IBLOCK_SECTION_ID"]))
66 $oldRatioIds[$section[
"ID"]] = $section[
"IBLOCK_SECTION_ID"];
72 private function addSections(\CIBlockSection $sectionObject, array $sections): array
75 foreach ($sections as $section)
77 unset($section[
"IBLOCK_SECTION_ID"]);
78 $rationIds[$section[
"ID"]] = $this->addSection($sectionObject, $section);
84 private function updateSections(\CIBlockSection $sectionObject, array $parentRatioIds, array $ratioIds)
86 foreach ($parentRatioIds as $parentId => $childId)
88 if (array_key_exists($parentId, $ratioIds) && array_key_exists($childId, $ratioIds))
90 $copiedSectionId = $ratioIds[$parentId];
91 $sectionObject->update($copiedSectionId, [
"IBLOCK_SECTION_ID" => $ratioIds[$childId]]);
96 private function addSection(\CIBlockSection $sectionObject, $section)
98 $result = $sectionObject->add($section);
101 if ($sectionObject->LAST_ERROR)
103 $this->result->addError(
new Error($sectionObject->LAST_ERROR));
107 $this->result->addError(
new Error(
"Unknown error while copying section"));
113 private function setRatios($iblockId, array $ratioIds)
115 $this->sectionsRatio[$iblockId] = $ratioIds;