• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/location/connector.php
  • Класс: BitrixSaleLocationConnector
  • Вызов: Connector::getConnectedLocationsQuery
static function getConnectedLocationsQuery($entityPrimary, $parameters = array(), $behaviour = array('GET_LINKED_THROUGH_GROUPS' => false))
{
	$entityPrimary = Assert::expectStringNotNull($entityPrimary, '$entityPrimary');

	if(!is_array($parameters))
		$parameters = array();
	if(!is_array($behaviour))
		$behaviour = array();
	if(!isset($behaviour['GET_LINKED_THROUGH_GROUPS']))
		$behaviour['GET_LINKED_THROUGH_GROUPS'] = false;

	$useGroups = GroupTable::checkGroupUsage() && static::getUseGroups(); // check if we have groups in project and entity uses groups
	$getLinkedThroughGroups = $behaviour['GET_LINKED_THROUGH_GROUPS'];
	if(!$useGroups)
		$getLinkedThroughGroups = false;

	$connType = static::getConnectType();

	// proxy select
	$select = array();
	if (empty($parameters['select']) || !is_array($parameters['select']))
	{
		$select = ['' => 'LOCATION'];
	}
	else
	{
		foreach($parameters['select'] as $k => $v)
		{
			if($v == '*')
			{
				$select[''] = 'LOCATION';
			}
			else
			{
				if(is_numeric($k) && mb_strpos((string)$v, '.') === false) // is NOT a reference
				{
					$k = $v;
				}

				$select[$k] = 'LOCATION.' . $v;
			}
		}
	}

	// proxy filter
	$filter = array();
	if (!empty($parameters['filter']) &&  is_array($parameters['filter']))
	{
		foreach($parameters['filter'] as $k => $v)
		{
			$filter['LOCATION.' . $k] = $v;
		}
	}

	// proxy order
	$order = array();
	if (!empty($parameters['order']) && is_array($parameters['order']))
	{
		//if($getLinkedThroughGroups)
			Assert::announceNotImplemented('Sorry, order-over-union clause is not implemented currently.');

		foreach($parameters['order'] as $k => $v)
		{
			$order['LOCATION.' . $k] = $v;
		}
	}

	if (!empty($parameters['runtime']) && is_array($parameters['runtime']))
	{
		Assert::announceNotImplemented('Sorry, runtime clause is not implemented currently.');
	}

	$sqls = array();

	if(static::checkLinkUsage($entityPrimary, static::DB_LOCATION_FLAG))
	{
		$strictFilter = array_merge($filter, array(
			'='.static::getLinkField() => $entityPrimary
		));

		if($useGroups)
			$strictFilter['='.static::getTypeField()] = static::DB_LOCATION_FLAG;

		$query = new EntityQuery(self::getEntity());
		$query
			->setSelect($select)
			->setFilter($strictFilter)
			->setOrder($order);

		$sqls[] = $query->getQuery();
	}

	if(static::checkLinkUsage($entityPrimary, static::DB_GROUP_FLAG) && $getLinkedThroughGroups)
	{
		$query = new EntityQuery(static::getEntity());

		if($connType == self::LINK_CODE) // entity connected by CODE
		{
			$query
				->registerRuntimeField(
					'G',
					array(
						'data_type' => 'BitrixSaleLocationGroupTable',
						'reference' => array(
							'=this.'.static::getLocationLinkField() => 'ref.CODE',
							'=this.'.static::getTypeField() => array('?', static::DB_GROUP_FLAG)
						),
						'join_type' => 'inner'
					)
				)
				->registerRuntimeField(
					'LG',
					array(
						'data_type' => 'BitrixSaleLocationGroupLocation',
						'reference' => array(
							'=this.G.ID' => 'ref.'.GroupLocationTable::getLinkField(),
						),
						'join_type' => 'inner'
					)
				)
				->registerRuntimeField(
					'LOCATION',
					array(
						'data_type' => 'BitrixSaleLocationLocation',
						'reference' => array(
							'=this.LG.LOCATION_ID' => 'ref.ID'
						),
						'join_type' => 'inner'
					)
				)
				->setSelect($select)
				->setFilter(array_merge($filter, array(
					'='.static::getLinkField() => $entityPrimary
				)))
				->setOrder($order);

			/*
			select LOCATION.*
				from b_sale_delivery2location DL
					inner join b_sale_location_group G on (G.CODE = DL.LOCATION_CODE and DL.LOCATION_TYPE = static::DB_GROUP_FLAG)
					inner join b_sale_location2location_group LG on (LG.LOCATION_GROUP_ID = G.ID)
					inner join b_sale_location LOCATION on (LG.LOCATION_ID = LOCATION.ID)
				where
					DL.DELIVERY_ID = 2;
			*/
		}
		else // entity connected by ID
		{
			$query
				->registerRuntimeField(
					'LG',
					array(
						'data_type' => 'BitrixSaleLocationGroupLocation',
						'reference' => array(
							'=this.'.static::getLocationLinkField() => 'ref.'.GroupLocationTable::getLinkField(),
							'=this.'.static::getTypeField() => array('?', static::DB_GROUP_FLAG)
						),
						'join_type' => 'inner'
					)
				)
				->registerRuntimeField(
					'LOCATION',
					array(
						'data_type' => 'BitrixSaleLocationLocation',
						'reference' => array(
							'=this.LG.LOCATION_ID' => 'ref.ID'
						),
						'join_type' => 'inner'
					)
				)
				->setSelect($select)
				->setFilter(array_merge($filter, array(
					'='.static::getLinkField() => $entityPrimary
				)))
				->setOrder($order);

			/*
			select LOCATION.*
				from b_sale_delivery2location DL
					inner join b_sale_location2location_group LG on (LG.LOCATION_GROUP_ID = DL.LOCATION_ID and DL.LOCATION_TYPE = static::DB_GROUP_FLAG)
					inner join b_sale_location LOCATION on (LG.LOCATION_ID = LOCATION.ID)
				where
					DL.DELIVERY_ID = 2;
			*/
		}

		$sqls[] = $query->getQuery();
	}

	if(empty($sqls)) // entity is not connected, so it means "all connected"
		return false;

	return static::unionize($sqls);
}