• Модуль: search
  • Путь к файлу: ~/bitrix/modules/search/classes/general/search.php
  • Класс: CAllSearchQuery
  • Вызов: CAllSearchQuery::PrepareQuery
function PrepareQuery($q)
{
	$state = 0;
	$qu = array();
	$n = 0;
	$this->error = "";

	$t = strtok($q, " ");
	while (($t != "") && ($this->error == ""))
	{
		if ($state == 0)
		{
			if (($t == "||") || ($t == "&&") || ($t == ")"))
			{
				$this->error = GetMessage("SEARCH_ERROR2")." ".$t;
				$this->errorno = 2;
			}
			elseif ($t == "!")
			{
				$state = 0;
				$qu[] = " NOT ";
			}
			elseif ($t == "(")
			{
				$n++;
				$state = 0;
				$qu[] = "(";
			}
			else
			{
				$state = 1;
				$where = $this->BuildWhereClause($t);
				$c = count($qu);
				if (
					$where === "1=1"
					&& (
						($c > 0 && $qu[$c - 1] === " OR ")
						|| ($c > 1 && $qu[$c - 1] === "(" && $qu[$c - 2] === " OR ")
					)
				)
				{
					$where = "1<>1";
				}
				$qu[] = " ".$where." ";
			}
		}
		elseif ($state == 1)
		{
			if (($t == "||") || ($t == "&&"))
			{
				$state = 0;
				if ($t == '||')
					$qu[] = " OR ";
				else
					$qu[] = " AND ";
			}
			elseif ($t == ")")
			{
				$n--;
				$state = 1;
				$qu[] = ")";
			}
			else
			{
				$this->error = GetMessage("SEARCH_ERROR2")." ".$t;
				$this->errorno = 2;
			}
		}
		else
		{

			break;
		}
		$t = strtok(" ");
	}

	if (($this->error == "") && ($n != 0))
	{
		$this->error = GetMessage("SEARCH_ERROR1");
		$this->errorno = 1;
	}

	if ($this->error != "")
	{
		return 0;
	}

	return implode($qu);
}