/***********
* Разделы *
***********/
$rs_section = CIBlockSection::GetList(Array("LEFT_MARGIN" => "ASC"), $arFilter, true, Array("UF_*"));
while($ar_section = $rs_section->Fetch())
{
$arResult["SECTIONS"][$ar_section["ID"]] = $ar_section;
}
/************
* Элементы *
************/
$rs_element = CIBlockElement::GetList(Array(), $arFilter, false, false, $arSelect);
while($ar_element = $rs_element->GetNext(false, false))
{
if(array_key_exists($ar_element["IBLOCK_SECTION_ID"], $arResult["ELEMENTS"]))
{
$arResult["ELEMENTS"][$ar_element["IBLOCK_SECTION_ID"]][$ar_element["ID"]] = $ar_element;
}
else
{
$arResult["ELEMENTS"][$ar_element["IBLOCK_SECTION_ID"]] = Array();
$arResult["ELEMENTS"][$ar_element["IBLOCK_SECTION_ID"]][$ar_element["ID"]] = $ar_element;
}
}
/*********************
* Построение дерева *
*********************/
$previousLevel = 0;
$arStack = Array();
foreach($arResult["SECTIONS"] as $arSection)
{
if($previousLevel && $arSection["DEPTH_LEVEL"] <= $previousLevel)
{
$delta = $previousLevel - $arSection["DEPTH_LEVEL"];
for($i = 0; $i < ($delta + 1); $i++)
{
$section_id = array_pop($arStack);
if(array_key_exists($section_id, $arResult["ELEMENTS"]))
{
foreach($arResult["ELEMENTS"][$section_id] as $arElement)
{
$arResult["ITEMS"][] = $arElement;
}
}
}
}
$arResult["ITEMS"][] = $arSection;
$arStack[] = $arSection["ID"];
$previousLevel = $arSection["DEPTH_LEVEL"];
}
|