Example #1
0
 public function startSearch($query)
 {
     $this->totalResult = null;
     $srcQuery = $query;
     // Если будут ошибки, то выпадет исключение.
     $query = $this->processQuery($query);
     //$this->sqlQuery = $query;
     $criteria = new CDbCriteria();
     $criteria->addCondition("MATCH value AGAINST (:q IN BOOLEAN MODE) AND id_lang=:l");
     $criteria->params = array(":q" => $query, ":l" => $this->idLocale);
     //$from = "da_search_data a";
     //$where = "MATCH (a.value) AGAINST (? IN BOOLEAN MODE)";
     //" AND a.id_lang=".$this->idLocale;
     /*if ($this->idDomain != null || $this->crossDomainSearch) {
           $from .= " JOIN da_object_instance doi ON a.id_object=doi.id_object AND a.id_instance=doi.id_instance";
           if ($this->idDomain != null) {
             $where = "doi.id_domain=".$this->idDomain." AND ".$where;
           }
         }
         $order = "";
         $iAlias = 0;
         foreach ($this->conditionSqlList as $k => $v) {
           $iAlias++;
           $alias = "a".$iAlias;
           if ($iAlias == 1) {
             $where .= " AND (";
           }
           
           $ob = Object::getById($k);
           $table = $ob->getTableName();
           $pk = ObjectParam::getPrimaryKey($k);
           
           $v = str_replace("<<table_alias>>", $alias, str_replace("<<main_alias>>", "a", $v));
           // $from = $from." LEFT JOIN ".$table." ".$alias." ON a.id_object=".$k." AND a.id_instance=".$alias.".".$pk->getFieldname()." AND ".$v;
           $from = $from." LEFT JOIN ".$table." ".$alias." ON a.id_object=".$k." AND a.id_instance=".$alias.".".$pk->getFieldname();
     
           if ($iAlias > 1) {
             // $where .= " OR ";
             $where .= " AND ";
           }
           $where .= "( (".$v." AND a.id_object=".$k.") OR a.id_object <> ".$k." )";
           
           // AND a1.visible=1
           // AND ((a1.visible=1 AND a.id_object=100) OR (a1.visible IS NULL AND a.id_object <>100))
            //$where .= " AND ((a1.visible=1 AND a.id_object=100) OR (a1.visible IS NULL AND a.id_object <>100))";
           
           if (isset($this->orderSqlList[$k]) && $this->orderSqlList[$k] != null) {
             $v = $this->orderSqlList[$k];
             $v = str_replace("<<table_alias>>", $alias, $v);
             $order .= $v.", ";
             $this->orderSqlList[$k] = null;
           }
         }
         if ($iAlias > 0) {
           $where .= ")";
         }*/
     // Сортировка данных по объекту.
     /*$selectSortColumn = "";
         $c = count($this->objectsOrder);
         for ($i = 0; $i < $c; $i++) {
           $selectSortColumn .= "WHEN ".$this->objectsOrder[$i]." THEN ".$i." ";
         }
         if ($c > 0) {
           $order = "sort_column ASC, ".$order;
           $selectSortColumn = ", (CASE a.id_object ".$selectSortColumn."ELSE ".$c." END) AS sort_column";
         }
         
         foreach ($this->orderSqlList as $k => $v) {
           if ($v == null) continue;
     
           $iAlias++;
           $alias = "a".$iAlias;
     
           $ob = Object::getById($k);
           $table = $ob->getTableName();
           $pk = ObjectParam::getPrimaryKey($k);
     
           $v = str_replace("<<table_alias>>", $alias, $v);
           $from = $from." LEFT JOIN ".$table." ".$alias." ON a.id_object=".$k." AND a.id_instance=".$alias.".".$pk->getFieldname();
     
           $order .= $v.", ";
           $this->orderSqlList[$k] = null;
         }
         if ($order != "") {
           $order = mb_substr($order, 0, mb_strlen($order) - 2);
         }*/
     if (count($this->objectSearchList) > 0) {
         //$str = array2QueryString($this->objectSearchList);
         //$where .= " AND a.id_object IN ($str)";
         $criteria->addInCondition("id_object", $this->objectSearchList);
     }
     if (count($this->objectNotSearchList) > 0) {
         //$str = array2QueryString($this->objectNotSearchList);
         //$where .= " AND a.id_object NOT IN ($str)";
         $criteria->addNotInCondition("id_object", $this->objectNotSearchList);
     }
     if (is_null($this->lenPreviewText) || !is_numeric($this->lenPreviewText) || $this->lenPreviewText < 1) {
         $this->lenPreviewText = 50;
     }
     if ($this->paginator == null) {
         $this->paginator = new CPagination();
         $this->paginator->pageSize = 20;
     }
     $this->paginator->applyLimit($criteria);
     if ($this->criteria !== null) {
         $criteria->mergeWith($this->criteria);
     }
     $this->baseCriteria = $criteria;
     if ($this->logQuery) {
         self::addSearchLog($srcQuery, $criteria->condition . " (params: " . print_r($criteria->params, true) . ")", "page=" . $this->paginator->getCurrentPage() . "; count=" . $this->paginator->getPageSize());
     }
     $result = Search::model()->findAll($criteria);
     foreach ($result as $r) {
         $r->value = HText::highlightText($r->value, $this->queryArray, $this->highlightTemplate, $this->lenPreviewText);
     }
     return $result;
 }