Как привязать элемент к разделу ?
Сейчас создаются раздел а элемент не прикрепляется.
public function addSection($iBlockId)
{
$oldSections = $this->getSections($iBlockId);
$propertyEnums = \general\tools\IBlock\Utils::getEnumValues($iBlockId);
$newSections = [
[
"ACTIVE" => '$ACTIVE',
"NAME" => '2000',
"CODE" => '2000',
"PROPERTY_VALUES" => [
]
],
];
foreach ($newSections as $key => $arLoadProductArray) {
$els = new \CIBlockSection();
$arLoadProductArray["IBLOCK_ID"] = $iBlockId;
$arLoadProductArray["SORT"] = $key + 1;
$arLoadProductArray["CODE"] = 'strategy_' . ($key + 1);
$arLoadProductArray["ACTIVE"] = 'Y';
$arLoadProductArray["DETAIL_TEXT_TYPE"] = 'html';
if(!$oldSections[$arLoadProductArray['CODE']]) {
if($SECTION_ID = $els->Add($arLoadProductArray)) {
$this->obResult->addMessageRow('Добавлен раздел');
} else {
$this->obResult->addMessageRow('Ошибка добавления раздела: ' . $els->LAST_ERROR);
}
} else {
if($SECTION_ID = $els->Update($oldSections[$arLoadProductArray['CODE']]["ID"], $arLoadProductArray)) {
$this->obResult->addMessageRow('Изменён раздел');
} else {
$this->obResult->addMessageRow('Ошибка изменения раздела: ' . $els->LAST_ERROR);
}
}
}
}
private function getSections($iBlockId) {
$oldSections = [];
$arSelect = ["ID", "IBLOCK_ID", "NAME", "CODE"];
$arFilter = ["IBLOCK_ID" => $iBlockId];
$res = \CIBlockSection::GetList(["SORT"=>"ASC"], $arFilter, false, false, $arSelect);
while ($dbItem = $res->GetNext()) {
$oldSections[$dbItem['CODE']] = $dbItem;
}
return $oldSections;
}
private function addElement($iBlockId,$oldSections)
{
$oldElements = $this->getElements($iBlockId);
$propertyEnums = \general\tools\IBlock\Utils::getEnumValues($iBlockId);
$newElements = [
[
"NAME" => 'ИМЯ ЭЛЕМЕНТА',
"SECTION_CODE" => '2000',
"DETAIL_TEXT" => '<p>Текст</p>',
"DETAIL_TEXT_TYPE" => 'html',
"PROPERTY_VALUES" => [
]
],
];
foreach ($newElements as $key => $arLoadProductArray) {
$el = new \CIBlockElement();
$arLoadProductArray["IBLOCK_ID"] = $iBlockId;
$arLoadProductArray["SORT"] = $key + 1;
$arLoadProductArray["CODE"] = 'strategy_' . ($key + 1);
$arLoadProductArray["ACTIVE"] = 'Y';
$arLoadProductArray["DETAIL_TEXT_TYPE"] = 'html';
$arLoadProductArray["IBLOCK_SECTION_ID"] = $oldSections[$arLoadProductArray["SECTION_CODE"]]['ID']; unset($arLoadProductArray["SECTION_CODE"]);
if(!$oldElements[$arLoadProductArray['CODE']]) {
if($PRODUCT_ID = $el->Add($arLoadProductArray)) {
$this->obResult->addMessageRow('Добавлен элемент');
} else {
$this->obResult->addMessageRow('Ошибка добавления элемента: ' . $el->LAST_ERROR);
}
} else {
if($PRODUCT_ID = $el->Update($oldElements[$arLoadProductArray['CODE']]["ID"], $arLoadProductArray)) {
$this->obResult->addMessageRow('Изменён элемент');
} else {
$this->obResult->addMessageRow('Ошибка изменения элемента: ' . $el->LAST_ERROR);
}
}
}
}
private function getElements($iBlockId) {
$oldElements = [];
$arSelect = ["ID", "IBLOCK_ID", "NAME", "CODE"];
$arFilter = ["IBLOCK_ID" => $iBlockId];
$res = \CIBlockElement::GetList(["SORT"=>"ASC"], $arFilter, false, false, $arSelect);
while ($dbItem = $res->GetNext()) {
$oldElements[$dbItem['CODE']] = $dbItem;
}
return $oldElements;
}