Skip to content

Instantly share code, notes, and snippets.

@Isa3v
Created November 14, 2019 11:55
Show Gist options
  • Select an option

  • Save Isa3v/6e3b1c9adc9b229f57ed372ca87deac4 to your computer and use it in GitHub Desktop.

Select an option

Save Isa3v/6e3b1c9adc9b229f57ed372ca87deac4 to your computer and use it in GitHub Desktop.
Вызов древа инфоблока через замыкание
<?
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/header.php");
$APPLICATION->SetTitle("Карта сайта");
?>
function getSectionsIblock(int $iblockID)
{
// Получаем инфоблок для создание ссылок по правилам ЧПУ
$obIblock = \Bitrix\Iblock\IblockTable::getList(["filter" => ["ID" => $iblockID], 'limit' => 1]);
if ($arIBlock = $obIblock->fetch()) {
$arIBlock['SECTION_PAGE_URL'] = str_replace(["#SITE_DIR#", "#IBLOCK_CODE#"], ["", $arIBlock['CODE']], $arIBlock['SECTION_PAGE_URL']);
$clIBlockSection = \Bitrix\Iblock\Model\Section::compileEntityByIblock($iblockID);
// Собираем разделы
$rsSection = $clIBlockSection::getList([
'order' => [
'LEFT_MARGIN' => 'ASC',
'SORT' => 'ASC'
],
'filter' => [
'IBLOCK_ID' => $arIBlock['ID'],
'ACTIVE' => 'Y',
'<DEPTH_LEVEL' => 5
],
'select' => [
"ID",
"NAME",
"CODE",
"DEPTH_LEVEL",
"IBLOCK_SECTION_ID"
],
'cache' => [
'ttl' => 3600,
'cache_joins' => true,
]
]);
$arSections = [];
while ($arSection = $rsSection->fetch()) {
$arSection["SUBSECTIONS"] = [];
$arSection['SECTION_CODE_PATH'] = $arSection['CODE'];
$arSections[$arSection["ID"]] = $arSection;
}
foreach ($arSections as &$arSection) {
if (!empty($arSection["IBLOCK_SECTION_ID"]) && !empty($arSections[$arSection["IBLOCK_SECTION_ID"]])) {
$arSection['SECTION_CODE_PATH'] = $arSections[$arSection["IBLOCK_SECTION_ID"]]['SECTION_CODE_PATH'] . '/' . $arSection['CODE'];
$arSections[$arSection["IBLOCK_SECTION_ID"]]["SUBSECTIONS"][] = &$arSections[$arSection["ID"]];
}
$arSection['SECTION_PAGE_URL'] = str_replace(['#SECTION_CODE_PATH#', '#SECTION_CODE#', '#SECTION_ID#',], [$arSection['SECTION_CODE_PATH'], $arSection['CODE'],$arSection['ID'] ], $arIBlock['SECTION_PAGE_URL']);
}
}
return $arSections;
}
// Замыкание и рекурсия
$renderList = function ($arSitemap, $intLevel = 1) use (&$renderList) {?>
<ul class="html_sitemap map-level-<?= $intLevel -1 ?>">
<?
foreach ($arSitemap as &$arSection) {
if ($arSection['DEPTH_LEVEL'] != $intLevel){
continue;
}
?>
<li class="html_sitemap__section l<?= $intLevel ?>" id="ms<?= $arSection["ID"]?>">
<a class="name" href="<?= $arSection['SECTION_PAGE_URL'] ?>">
<? echo $arSection["NAME"]; ?>
</a>
<?if (!empty($arSection['SUBSECTIONS'])) {?>
<ul class="html_sitemap__subsections">
<? $renderList($arSection['SUBSECTIONS'], $intLevel + 1);?>
</ul>
<?}?>
</li>
<?
}
?>
</ul>
<?
};
echo $renderList(getSectionsIblock(16, $arReplace));
?>
<?require($_SERVER["DOCUMENT_ROOT"]."/bitrix/footer.php");?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment