• Модуль: catalog
  • Путь к файлу: ~/bitrix/modules/catalog/lib/catalogviewedproduct.php
  • Класс: BitrixCatalogCatalogViewedProductTable
  • Вызов: CatalogViewedProductTable::getProductSkuMap
static function getProductSkuMap($iblockId, $sectionId, $fuserId, $excludeProductId, $limit, $depth = 0, $siteId = null)
{
	$map = array();

	$iblockId = (int)$iblockId;
	$sectionId = (int)$sectionId;
	$fuserId = (int)$fuserId;
	$excludeProductId = (int)$excludeProductId;
	$limit = (int)$limit;
	$depth = (int)$depth;
	if ($iblockId <= 0 || $depth < 0 || $fuserId <= 0)
		return $map;

	if (empty($siteId))
	{
		$context = Application::getInstance()->getContext();
		$siteId = $context->getSite();
	}
	if (empty($siteId))
		return $map;

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

	if (empty($subSections) && $sectionId <= 0)
	{
		$getListParams = array(
			'select' => array('PRODUCT_ID', 'ELEMENT_ID', 'DATE_VISIT'),
			'filter' => array(
				'=FUSER_ID' => $fuserId,
				'=SITE_ID' => $siteId,
				'=PARENT_ELEMENT.IBLOCK_ID' => $iblockId,
				'=PARENT_ELEMENT.WF_STATUS_ID' => 1,
				'=PARENT_ELEMENT.WF_PARENT_ELEMENT_ID' => null
			),
			'order' => array('DATE_VISIT' => 'DESC')
		);
		if ($excludeProductId > 0)
			$getListParams['filter']['!=PARENT_ELEMENT.ID'] = $excludeProductId;
		if ($limit > 0)
			$getListParams['limit'] = $limit;
		$iterator = static::getList($getListParams);
		unset($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'));
		$filter = array('=ADDITIONAL_PROPERTY_ID' => null);
		if ($excludeProductId > 0)
			$filter['!=IBLOCK_ELEMENT_ID'] = $excludeProductId;
		$sectionElementQuery->setFilter($filter);
		unset($filter);
		$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'));
		$filter = array('=IBLOCK_ID' => $iblockId, '=WF_STATUS_ID' => 1, '=WF_PARENT_ELEMENT_ID' => null);
		if ($excludeProductId > 0)
			$filter['!=ID'] = $excludeProductId;
		$elementQuery->setFilter($filter);
		unset($filter);
		$elementQuery->registerRuntimeField(
			'',
			new MainEntityReferenceField(
				'BSE',
				MainEntityBase::getInstanceByQuery($sectionElementQuery),
				array('=this.ID' => 'ref.IBLOCK_ELEMENT_ID'),
				array('join_type' => 'INNER')
			)
		);

		$query = static::query();
		$query->setSelect(array('PRODUCT_ID', 'ELEMENT_ID', 'DATE_VISIT'));
		$query->setFilter(array('=FUSER_ID' => $fuserId, '=SITE_ID' => $siteId));
		$query->setOrder(array('DATE_VISIT' => 'DESC'));

		$query->registerRuntimeField(
			'',
			new MainEntityReferenceField(
				'BE',
				MainEntityBase::getInstanceByQuery($elementQuery),
				array('=this.ELEMENT_ID' => 'ref.ID'),
				array('join_type' => 'INNER')
			)
		);
		if ($limit > 0)
			$query->setLimit($limit);

		$iterator = $query->exec();

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

	while ($row = $iterator->fetch())
		$map[$row['PRODUCT_ID']] = $row['ELEMENT_ID'];
	unset($row, $iterator);
	unset($subSections);

	return $map;
}