• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/location/tree.php
  • Класс: BitrixSaleLocationis
  • Вызов: is::resort
static function resort($dontCareEvents = false)
{
	$edges = array();
	$nodes = array();

	$res = parent::getList(array('select' => array('ID', 'PARENT_ID', 'LEFT_MARGIN', 'RIGHT_MARGIN')));
	while($item = $res->Fetch())
	{
		$nodes[$item['ID']] = array(
			'LEFT_MARGIN' => $item['LEFT_MARGIN'],
			'RIGHT_MARGIN' => $item['RIGHT_MARGIN']
		);

		if(!intval($item['PARENT_ID']))
			$edges['ROOT'][] = $item['ID'];
		else
			$edges[$item['PARENT_ID']][] = $item['ID'];
	}

	// walk tree in-deep to obtain correct margins
	self::walkTreeInDeep('ROOT', $edges, $nodes, 0, 0, $dontCareEvents);

	// now massively insert new values into the database, using a temporal table
	$tabName = 'b_sale_location_temp_'.rand(99, 9999);
	$entityTableName = static::getTableName();

	$dbConnection = MainHttpApplication::getConnection();

	$dbConnection->query("create table ".$tabName." (
		ID ".Helper::getSqlForDataType('int').",
		LEFT_MARGIN ".Helper::getSqlForDataType('int').",
		RIGHT_MARGIN ".Helper::getSqlForDataType('int').",
		DEPTH_LEVEL ".Helper::getSqlForDataType('int')."
	)");

	$handle = new BlockInserter(array(
		'tableName' => $tabName,
		'exactFields' => array(
			'ID' => array('data_type' => 'integer'),
			'LEFT_MARGIN' => array('data_type' => 'integer'),
			'RIGHT_MARGIN' => array('data_type' => 'integer'),
			'DEPTH_LEVEL' => array('data_type' => 'integer'),
		),
		'parameters' => array(
			'mtu' => self::BLOCK_INSERT_MTU
		)
	));
	foreach($nodes as $id => $node)
	{
		$node['ID'] = $id;
		$handle->insert($node);
	}
	$handle->flush();

	// merge temp table with location table
	Helper::mergeTables($entityTableName, $tabName, array(
		'LEFT_MARGIN' => 'LEFT_MARGIN',
		'RIGHT_MARGIN' => 'RIGHT_MARGIN',
		'DEPTH_LEVEL' => 'DEPTH_LEVEL'
	), array('ID' => 'ID'));

	$dbConnection->query("drop table {$tabName}");
}