• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/location/search/chain.php
  • Класс: BitrixSaleLocationSearchChainTable
  • Вызов: ChainTable::initializeData
public function initializeData()
{
	$dbConnection = MainHttpApplication::getConnection();

	$res = LocationLocationTable::getList(array(
		'select' => array(
			'ID',
			'TYPE_ID',
			'DEPTH_LEVEL',
			'SORT'
		),
		//'filter' => static::getFilterForInitData(array('TYPES' => $this->procData['ALLOWED_TYPES'])),
		'order' => array(
			'LEFT_MARGIN' => 'asc'
		),
		'limit' => self::STEP_SIZE,
		'offset' => $this->procData['OFFSET']
	));

	$this->procData['TYPE_SORT'] = $this->rarefact($this->procData['TYPE_SORT']);

	$cnt = 0;
	while($item = $res->fetch())
	{
		// tmp!!!!
		//$name = LocationNameLocationTable::getList(array('select' => array('NAME'), 'filter' => array('=LOCATION_ID' => $item['ID'], '=LANGUAGE_ID' => 'ru')))->fetch();

		if($item['DEPTH_LEVEL'] < $this->procData['DEPTH'])
		{
			$newPC = array();

			foreach($this->procData['PATH'] as $dl => $id)
			{
				if($dl >= $item['DEPTH_LEVEL'])
					break;

				$newPC[$dl] = $id;
			}

			$this->procData['PATH'] = $newPC;
		}

		$this->procData['PATH'][$item['DEPTH_LEVEL']] = array(
			'TYPE' => $item['TYPE_ID'],
			'ID' => $item['ID']
		);

		if(is_array($this->procData['ALLOWED_TYPES']) && in_array($item['TYPE_ID'], $this->procData['ALLOWED_TYPES']))
		{
			$data = array(
				'LOCATION_ID' => $item['ID'],
				'RELEVANCY' => $this->procData['TYPE_SORT'][$item['TYPE_ID']] + $item['SORT'],// * $item['DEPTH_LEVEL'], // tmp, will be more complicated calc here later
			);
			$wordsAdded = array();

			/*
			_dump_r('############################');
			_dump_r('LOCATION: '.$name['NAME']);
			_dump_r('TYPE RELEVANCY: '.$data['RELEVANCY']);
			_dump_r('PATH:');
			_dump_r($this->procData['PATH']);
			*/

			$this->procData['DEPTH'] = $item['DEPTH_LEVEL'];

			// pre-load missing words
			$wordCount = 0;
			foreach($this->procData['PATH'] as &$pathItem)
			{
				if(!isset($pathItem['WORDS'])) // words were not loaded previously for this part of the path
				{
					$sql = "
						select WS.POSITION from ".WordTable::getTableNameWord2Location()." WL
							inner join ".WordTable::getTableName()." WS on WL.WORD_ID = WS.ID
						where
							WL.LOCATION_ID = '".intval($pathItem['ID'])."'
					";

					$wordRes = $dbConnection->query($sql);

					$pathItem['WORDS'] = array();
					while($wordItem = $wordRes->fetch())
					{
						$pathItem['WORDS'][] = $wordItem['POSITION'];
					}
					$pathItem['WORDS'] = array_unique($pathItem['WORDS']);
				}

				$wordCount += count($pathItem['WORDS']);
			}

			// count words
			//_dump_r('Words total: '.$wordCount);

			$wOffset = 0;
			foreach($this->procData['PATH'] as &$pathItem)
			{
				foreach($pathItem['WORDS'] as $i => $position)
				{
					$wordWeight = $wordCount - $wOffset;

					$tmpData = $data;
					$tmpData['RELEVANCY'] += $wordWeight;

					//_dump_r('	Word relevancy: '.$data['RELEVANCY'].' ==>> '.$tmpData['RELEVANCY']);

					if(!isset($wordsAdded[$position]))
					{
						$this->indexInserter->insert(array_merge(array('POSITION' => $position), $tmpData));
						$wordsAdded[$position] = true;
					}

					$wOffset++;
				}
			}
			unset($pathItem);

		}

		$cnt++;
	}

	$this->indexInserter->flush();

	$this->procData['OFFSET'] += self::STEP_SIZE;

	return !$cnt;
}