maksim-korzhov
8/12/2016 - 2:09 PM

Получить подразделы, в которых есть элементы, соответствующие значениям, выбранным в фильтре.

Получить подразделы, в которых есть элементы, соответствующие значениям, выбранным в фильтре.

// ***Показывать разделы, в которых есть отфильтрованные товары
global $USER;
global $arrFilter;
	
// 0. Проверяем, установлен ли фильтр по свойству
$isFilterIsset = false;
foreach( $arrFilter as $key => $filterItem ) {
	if( strpos( $key, "=PROPERTY_") !== FALSE ) {
		$isFilterIsset = true;
	}
}

if( isset( $arrFilter["SECTION_ID"] ) && $isFilterIsset) {

	// 1. Получим информацию о текущем разделе
	$currentSectionLM = 0;
	$currentSectionRM = 0;
	$currentSectionDL = 0;
	$rsCurrentSection = CIBlockSection::GetByID($arrFilter["SECTION_ID"]);
	if($arCurrentSection = $rsCurrentSection->GetNext()) {
		$currentSectionLM = $arCurrentSection["LEFT_MARGIN"];
		$currentSectionRM = $arCurrentSection["RIGHT_MARGIN"];
		$currentSectionDL = $arCurrentSection["DEPTH_LEVEL"];
	}

	// 2. Получаем список подразделов текущего раздела
	$childrenDL = $currentSectionDL + 1;
	$childrenSections = array();
	$arOrder = array("LEFT_MARGIN" => "ASC");
	$arFilter = array("IBLOCK_ID" => $arParams["IBLOCK_ID"], ">LEFT_MARGIN" => $currentSectionLM, "<RIGHT_MARGIN" => $currentSectionRM, ">DEPTH_LEVEL" => $currentSectionDL);
	$arSelect = array("ID", "IBLOCK_ID", "IBLOCK_SECTION_ID", "DEPTH_LEVEL");
	$rsSectionsResult = CIBlockSection::GetList($arOrder, $arFilter, false, $arSelect);
	while ($arSectionsResult = $rsSectionsResult->GetNext()) {
		$childrenSections[] = $arSectionsResult;
	}

	// 3. Группируем массив по вложенности
	$childrenSectionGroupped = array();
	foreach($childrenSections as $key => $childrenSectionItem) {
		if( $childrenSectionItem["DEPTH_LEVEL"] == $childrenDL ) {
			$childrenSectionGroupped[$childrenSectionItem["ID"]] = array();
		} else {
			$childrenSectionGroupped[$childrenSectionItem["IBLOCK_SECTION_ID"]][] = $childrenSectionItem["ID"];
		}
	}
	

	// 4. Если фильтр установлен - получаем разделы элементов, подходящих под фильтр, группируем по разделам
	$childrenSectionsRealID = array();
	$arOrder = array("SORT" => "ASC");
	$arFilter = $arrFilter;
	$arSelect = array("ID", "IBLOCK_ID", "SECTION_ID");
	$arGroup = array("IBLOCK_SECTION_ID");
	$rsElement = CIBlockElement::GetList($arOrder, $arFilter, $arGroup, false, $arSelect);
	while ($arElementResult = $rsElement->GetNext()) {
		$childrenSectionsRealID[] = $arElementResult["IBLOCK_SECTION_ID"];
	}
	
	// 5. Получим разделы, в которых есть хоть 1 элемент, подходящий под фильтр
	$arResultSection = array();
	foreach( $childrenSectionGroupped as $key => $sectionGroup ) {

		if( in_array($key, $childrenSectionsRealID) ) {
			$arResultSection[] = $key;
		} else {
			foreach( $sectionGroup as $sectionID ) {
				if( in_array($sectionID, $childrenSectionsRealID) ) {
					$arResultSection[] = $key;
					break;
				}
			}
		}
	}

	// 6. Выбираем только нужные разделы
	foreach( $arResult['SECTIONS'] as $key => $arSection ) {
		if( !in_array( $arSection["ID"], $arResultSection ) ) {
			unset($arResult['SECTIONS'][$key]);
		}
	}

	// 7. Получаем ссылку фильтра и дополняем её ссылку раздела
	$currentUrl = $_SERVER["REQUEST_URI"];
	if( strlen($currentUrl) > 0 ) {
		$explodedUrl = explode("/filter/", $currentUrl);
		if( isset($explodedUrl[1]) ) {
			foreach ($arResult['SECTIONS'] as $key => $arOneSection) {
				$arResult['SECTIONS'][$key]["SECTION_PAGE_URL"] = $arOneSection["SECTION_PAGE_URL"] . "filter/" . $explodedUrl[1];
			}
		}
	}
}