• Модуль: iblock
  • Путь к файлу: ~/bitrix/modules/iblock/lib/propertyindex/facet.php
  • Класс: BitrixIblockPropertyIndexFacet
  • Вызов: Facet::whereToSql
public function whereToSql(array $where, $tableAlias, $subsectionsCondition = "")
{
	$sqlWhere = "";
	$sectionCondition = "$tableAlias.SECTION_ID = ".$this->sectionId;
	$facetCondition   = "$tableAlias.FACET_ID = ".$where["FACET_ID"];
	switch ($where["TYPE"])
	{
	case Storage::DICTIONARY:
	case Storage::STRING:
		if ($where["OP"] == "=")
		{
			$sqlWhere = $sectionCondition
				." AND ".$facetCondition
				." AND $tableAlias.VALUE_NUM = 0"
				." AND $tableAlias.VALUE in (".implode(", ", $where["VALUES"]).")"
			;
		}
		break;
	case Storage::NUMERIC:
		if ($where["OP"] == ">=" || $where["OP"] == "<=")
		{
			$sqlWhere = $sectionCondition
				." AND ".$facetCondition
				." AND $tableAlias.VALUE_NUM ".$where["OP"]." ".$where["VALUES"][0]
				." AND $tableAlias.VALUE = 0"
			;
		}
		elseif ($where["OP"] == "><")
		{
			$sqlWhere = $sectionCondition
				." AND ".$facetCondition
				." AND $tableAlias.VALUE_NUM BETWEEN ".$where["VALUES"][0]." AND ".$where["VALUES"][1]
				." AND $tableAlias.VALUE = 0"
			;
		}
		break;
	case Storage::DATETIME:
		if ($where["OP"] == ">=" || $where["OP"] == "<=")
		{
			$sqlWhere = $sectionCondition
				." AND ".$facetCondition
				." AND $tableAlias.VALUE_NUM ".$where["OP"]." ".$where["VALUES"][0]
			;
		}
		elseif ($where["OP"] == "><")
		{
			$sqlWhere = $sectionCondition
				." AND ".$facetCondition
				." AND $tableAlias.VALUE_NUM BETWEEN ".$where["VALUES"][0]." AND ".$where["VALUES"][1]
			;
		}
		elseif ($where["OP"] == "=")
		{
			$sqlWhere = $sectionCondition
				." AND ".$facetCondition
				." AND $tableAlias.VALUE_NUM in (".implode(", ", $where["VALUES"]).")"
			;
		}
		break;
	case Storage::PRICE: //TODO AND FC.VALUE = 0
		if (($where["OP"] == ">=" || $where["OP"] == "<="))
		{
			$sqlWhere = $sectionCondition
				." AND ".$facetCondition
			;
			if ($this->toCurrencyId && $this->convertCurrencyId)
			{
				$sqlOr = array();
				foreach ($this->convertCurrencyId as $currency => $currencyDictionaryId)
				{
					$convertedPrice = CCurrencyRates::convertCurrency($where["VALUES"][0], $this->toCurrencyId, $currency);
					$sqlOr[] = "($tableAlias.VALUE = $currencyDictionaryId AND $tableAlias.VALUE_NUM ".$where["OP"]." $convertedPrice)";
				}
				$sqlWhere .= " AND (".implode(" OR ", $sqlOr).")";
			}
			else
			{
				$sqlWhere .= " AND $tableAlias.VALUE_NUM ".$where["OP"]." ".$where["VALUES"][0];
			}
		}
		elseif ($where["OP"] == "><")
		{
			$sqlWhere = $sectionCondition
				." AND ".$facetCondition
			;
			if ($this->toCurrencyId && $this->convertCurrencyId)
			{
				$sqlOr = array();
				foreach ($this->convertCurrencyId as $currency => $currencyDictionaryId)
				{
					$convertedPrice1 = CCurrencyRates::convertCurrency($where["VALUES"][0], $this->toCurrencyId, $currency);
					$convertedPrice2 = CCurrencyRates::convertCurrency($where["VALUES"][1], $this->toCurrencyId, $currency);
					$sqlOr[] = "($tableAlias.VALUE = $currencyDictionaryId AND $tableAlias.VALUE_NUM BETWEEN $convertedPrice1 AND $convertedPrice2)";
				}
				$sqlWhere .= " AND (".implode(" OR ", $sqlOr).")";
			}
			else
			{
				$sqlWhere .= " AND $tableAlias.VALUE_NUM BETWEEN ".$where["VALUES"][0]." AND ".$where["VALUES"][1];
			}
		}
		break;
	}

	if ($sqlWhere && $subsectionsCondition !== '')
	{
		$sqlWhere .= " and ".$tableAlias.".".$subsectionsCondition;
	}

	return $sqlWhere;
}