• Модуль: iblock
  • Путь к файлу: ~/bitrix/modules/iblock/lib/component/base.php
  • Класс: BitrixIblockComponentBase
  • Вызов: Base::filterIdBySection
protected function filterIdBySection($elementIds, $iblockId, $sectionId, $limit, $depth = 0)
{
	$map = array();

	MainTypeCollection::normalizeArrayValuesByInt($elementIds);

	if (empty($elementIds))
		return $map;

	$iblockId = (int)$iblockId;
	$sectionId = (int)$sectionId;
	$limit = (int)$limit;
	$depth = (int)$depth;

	if ($iblockId <= 0 ||$depth < 0)
		return $map;

	$subSections = array();
	if ($depth > 0)
	{
		$parentSectionId = CatalogProductViewed::getParentSection($sectionId, $depth);
		if ($parentSectionId !== null)
		{
			$subSections[$parentSectionId] = $parentSectionId;
		}
		unset($parentSectionId);
	}

	if (empty($subSections) && $sectionId <= 0)
	{
		$getListParams = array(
			'select' => array('ID'),
			'filter' => array(
				'@ID' => $elementIds,
				'=IBLOCK_ID' => $iblockId,
				'=WF_STATUS_ID' => 1,
				'=WF_PARENT_ELEMENT_ID' => null
			),
		);
		if ($limit > 0)
		{
			$getListParams['limit'] = $limit;
		}

		$iterator = IblockElementTable::getList($getListParams);
	}
	else
	{
		if (empty($subSections))
		{
			$subSections[$sectionId] = $sectionId;
		}

		$sectionQuery = new MainEntityQuery(IblockSectionTable::getEntity());
		$sectionQuery->setTableAliasPostfix('_parent');
		$sectionQuery->setSelect(array('ID', 'LEFT_MARGIN', 'RIGHT_MARGIN'));
		$sectionQuery->setFilter(array('@ID' => $subSections));

		$subSectionQuery = new MainEntityQuery(IblockSectionTable::getEntity());
		$subSectionQuery->setTableAliasPostfix('_sub');
		$subSectionQuery->setSelect(array('ID'));
		$subSectionQuery->setFilter(array('=IBLOCK_ID' => $iblockId));
		$subSectionQuery->registerRuntimeField(
			'',
			new MainEntityReferenceField(
				'BS',
				MainEntityBase::getInstanceByQuery($sectionQuery),
				array('>=this.LEFT_MARGIN' => 'ref.LEFT_MARGIN', '<=this.RIGHT_MARGIN' => 'ref.RIGHT_MARGIN'),
				array('join_type' => 'INNER')
			)
		);

		$sectionElementQuery = new MainEntityQuery(IblockSectionElementTable::getEntity());
		$sectionElementQuery->setSelect(array('IBLOCK_ELEMENT_ID'));
		$sectionElementQuery->setGroup(array('IBLOCK_ELEMENT_ID'));
		$sectionElementQuery->setFilter(array('=ADDITIONAL_PROPERTY_ID' => null));
		$sectionElementQuery->registerRuntimeField(
			'',
			new MainEntityReferenceField(
				'BSUB',
				MainEntityBase::getInstanceByQuery($subSectionQuery),
				array('=this.IBLOCK_SECTION_ID' => 'ref.ID'),
				array('join_type' => 'INNER')
			)
		);

		$elementQuery = new MainEntityQuery(IblockElementTable::getEntity());
		$elementQuery->setSelect(array('ID'));
		$elementQuery->setFilter(array('=IBLOCK_ID' => $iblockId, '=WF_STATUS_ID' => 1, '=WF_PARENT_ELEMENT_ID' => null));
		$elementQuery->registerRuntimeField(
			'',
			new MainEntityReferenceField(
				'BSE',
				MainEntityBase::getInstanceByQuery($sectionElementQuery),
				array('=this.ID' => 'ref.IBLOCK_ELEMENT_ID'),
				array('join_type' => 'INNER')
			)
		);
		if ($limit > 0)
		{
			$elementQuery->setLimit($limit);
		}

		$iterator = $elementQuery->exec();

		unset($elementQuery, $sectionElementQuery, $subSectionQuery, $sectionQuery);
	}

	while ($row = $iterator->fetch())
	{
		$map[] = $row['ID'];
	}
	unset($row, $iterator);

	return $map;
}