• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/location/location.php
  • Класс: BitrixSaleLocationLocationTable
  • Вызов: LocationTable::resetLegacyPath
static function resetLegacyPath()
{
	$dbConnection = MainHttpApplication::getConnection();
	$locTable = static::getTableName();

	$types = array();
	$res = TypeTable::getList(array(
		'filter' => array('CODE' => array('COUNTRY', 'REGION', 'CITY')),
		'select' => array('ID', 'CODE')
	));
	while($item = $res->fetch())
		$types[$item['CODE']] = $item['ID'];

	if(!empty($types))
	{
		if(!$dbConnection->isTableExists('b_sale_loc_rebind'))
			$dbConnection->query("create table b_sale_loc_rebind (TARGET_ID ".Helper::getSqlForDataType('int').", LOCATION_ID ".Helper::getSqlForDataType('int').")");
		else
			$dbConnection->query("truncate table b_sale_loc_rebind");

		$sqlWhere = array();
		foreach($types as $code => $id)
			$sqlWhere[] = "'".intval($id)."'";

		$dbConnection->query("update ".$locTable." set COUNTRY_ID = NULL, REGION_ID = NULL, CITY_ID = NULL where TYPE_ID in (".implode(', ', $sqlWhere).")");

		if(intval($types['REGION']) && intval($types['COUNTRY']))
		{
			// countries for regions
			$dbConnection->query("insert into b_sale_loc_rebind (TARGET_ID, LOCATION_ID) select A.ID as ONE, B.ID as TWO from ".$locTable." A inner join ".$locTable." B on A.TYPE_ID = '".intval($types['REGION'])."' and B.TYPE_ID = '".intval($types['COUNTRY'])."' and B.LEFT_MARGIN <= A.LEFT_MARGIN and B.RIGHT_MARGIN >= A.RIGHT_MARGIN");
			Helper::mergeTables($locTable, 'b_sale_loc_rebind', array('COUNTRY_ID' => 'LOCATION_ID'), array('ID' => 'TARGET_ID'));
			$dbConnection->query("truncate table b_sale_loc_rebind");
		}

		if(intval($types['REGION']) && intval($types['CITY']))
		{
			// regions for cities
			$dbConnection->query("insert into b_sale_loc_rebind (TARGET_ID, LOCATION_ID) select A.ID as ONE, B.ID as TWO from ".$locTable." A inner join ".$locTable." B on A.TYPE_ID = '".intval($types['CITY'])."' and B.TYPE_ID = '".intval($types['REGION'])."' and B.LEFT_MARGIN <= A.LEFT_MARGIN and B.RIGHT_MARGIN >= A.RIGHT_MARGIN");
			Helper::mergeTables($locTable, 'b_sale_loc_rebind', array('REGION_ID' => 'LOCATION_ID'), array('ID' => 'TARGET_ID'));
			$dbConnection->query("truncate table b_sale_loc_rebind");
		}

		if(intval($types['COUNTRY']) && intval($types['CITY']))
		{
			// countries for cities
			$dbConnection->query("insert into b_sale_loc_rebind (TARGET_ID, LOCATION_ID) select A.ID as ONE, B.ID as TWO from ".$locTable." A inner join ".$locTable." B on A.TYPE_ID = '".intval($types['CITY'])."' and B.TYPE_ID = '".intval($types['COUNTRY'])."' and B.LEFT_MARGIN <= A.LEFT_MARGIN and B.RIGHT_MARGIN >= A.RIGHT_MARGIN");
			Helper::mergeTables($locTable, 'b_sale_loc_rebind', array('COUNTRY_ID' => 'LOCATION_ID'), array('ID' => 'TARGET_ID'));
		}

		Helper::dropTable('b_sale_loc_rebind');

		if(intval($types['COUNTRY']))
			$dbConnection->query("update ".$locTable." set COUNTRY_ID = ID where TYPE_ID = '".intval($types['COUNTRY'])."'");

		if(intval($types['REGION']))
			$dbConnection->query("update ".$locTable." set REGION_ID = ID where TYPE_ID = '".intval($types['REGION'])."'");

		if(intval($types['CITY']))
			$dbConnection->query("update ".$locTable." set CITY_ID = ID where TYPE_ID = '".intval($types['CITY'])."'");
	}
}