Exemple #1
0
 function search()
 {
     global $display;
     $catid = (int) Url::get('catid', 0);
     $total = (int) Url::get('total', 0);
     $page = (int) Url::get('page', 0);
     $tt = (int) Url::get('tt', 0);
     $keywords = Url::get('keywords');
     require "./includes/sphinxapi.class.php";
     EClassApi::getCats();
     foreach (CGlobal::$allCategories as $value) {
         if ($value['parent_id'] == 0) {
             $cat_list[$value['id']] = $value['brief_name'];
         }
     }
     $category = $this->getCategory($keywords);
     foreach ($category as $row) {
         $row['brief_name'] = isset($cat_list[$row['level_1_category_id']]) && $cat_list[$row['level_1_category_id']] ? $cat_list[$row['level_1_category_id']] : "Khác";
         $listCat[$row['level_1_category_id']] = array("level_1_category_id" => $row["level_1_category_id"], "brief_name" => $row['brief_name'], "count_ad" => $row['@count']);
     }
     $catActive = '';
     $total_cat = 0;
     if ($listCat) {
         $catActive = $listCat[$catid]['brief_name'];
         $total_cat = count($listCat);
     }
     $i = 0;
     $allrecord = 0;
     $otherCat = '';
     $cat_content = "";
     foreach ($listCat as $cat) {
         $allrecord = $allrecord + $cat['count_ad'];
     }
     $display->add("cat_content", $cat_content);
     $display->add("CatActiveId", $catid);
     $display->add("catActive", $catActive);
     $display->add("listCat", $listCat);
     $display->add("allrecord", $allrecord);
     $display->add("total_cat", $total_cat);
     $pager = new Pager();
     $limit = SEARCH_LIMIT;
     $pager->type = "search";
     $pager->catid = $catid;
     $pager->total = $total;
     $pager->limit = $limit;
     $pager->page_param = 'page';
     $pager->page = $page;
     $offset = $pager->get_offset();
     $limit_from = $pager->limit_from();
     $limit_to = $pager->limit_to();
     //Sphinx search by Nova
     $q = $keywords;
     //$mode = SPH_MATCH_ALL;
     $mode = SPH_MATCH_EXTENDED2;
     //Init config
     $host = SPHINX_SERVER;
     $port = SPHINX_PORT;
     $index = 'enbac';
     $filtervals = array();
     //$ranker = SPH_RANK_PROXIMITY_BM25;
     $ranker = SPH_RANK_WORDCOUNT;
     $cl = new SphinxClient();
     $cl->SetServer($host, $port);
     $cl->SetConnectTimeout(1);
     $cl->SetWeights(array(100, 1));
     $cl->SetMatchMode($mode);
     $cl->SetFilter('level_1_category_id', array($catid));
     $cl->SetFilter('status', array('1'));
     $cl->SetFieldWeights(array('user_name' => 10000, 'name' => 1000, 'description' => 1));
     //$cl->SetSortMode( SPH_SORT_EXTENDED, 'up_time DESC' );
     //$cl->SetSortMode( SPH_SORT_RELEVANCE);//Sort theo kq chính xác nhất
     //$cl->SetSortMode ( SPH_SORT_EXPR, "@weight + ( user_karma + ln(pageviews) )*0.1");
     $cl->SetSortMode(SPH_SORT_EXPR, "@weight");
     //Sort theo trọng số
     $cl->SetLimits($offset, $limit, 10000);
     $cl->SetRankingMode($ranker);
     $cl->SetArrayResult(true);
     $res = $cl->Query($q, $index);
     if ($res && isset($res["matches"])) {
         if (is_array($res["matches"])) {
             foreach ($res["matches"] as $results) {
                 $list_item_id[] = $results['id'];
             }
         }
         $comma_separated = join(",", $list_item_id);
     }
     if ($total) {
         if ($limit_to > $total) {
             $limit_to = $total;
         }
         $comma_separated = join(",", $list_item_id);
         if ($comma_separated) {
             //$sql = "SELECT id,name,up_time,price,user_id,user_name, level_1_category_id,category_id,description,original_image_url, img_server FROM item WHERE id IN ($comma_separated) AND status=1 ORDER BY up_time DESC";
             $sql = "SELECT id,name,up_time,price,user_id,user_name, level_1_category_id,category_id,description,original_image_url, img_server FROM item WHERE id IN ({$comma_separated}) AND status=1 ORDER BY find_in_set(id,'{$comma_separated}')";
             $search_result = DB::query($sql);
             $pager->total = $total;
             $paging = $pager->page_link();
         }
     }
     $item_array = array();
     if ($search_result) {
         EClassApi::getCats();
         $highlight = '';
         if ($keywords) {
             $highlight = $str_search = str_replace(array('+', '/', '|', '-', '*'), "", $keywords);
             $highlight = EClassApi::trimSpace($highlight);
             $highlight = str_replace("'", '', $highlight);
             $highlight = str_replace("'", '', $highlight);
             $highlight = str_replace(""", '', $highlight);
         }
         $highlight1 = '';
         if ($highlight) {
             $arr = explode(' ', $highlight);
             if ($arr) {
                 $highlight = "";
                 foreach ($arr as $word) {
                     $highlight = ($highlight ? $highlight . ', ' : '') . "'{$word}'";
                     $highlight1 = ($highlight1 ? $highlight1 . ',' : '') . $word;
                 }
             }
         }
         while ($item = mysql_fetch_assoc($search_result)) {
             $item['profile_url'] = WEB_DIR . $item['user_name'];
             //$item['name'] = EClassApi::subString(strip_tags(EClassApi::filter_title($item['name'])), 0, 115, true);
             $item['name_hl'] = EClassApi::HighLightKeyword(strip_tags(EClassApi::filter_title($item['name'])), $highlight1);
             $item_time = TIME_NOW - $item['up_time'];
             //neu nho hon 1h thi tinh ra phut
             if ($item_time < 3600) {
                 $item['item_time'] = floor($item_time / 60) . " phút trước đây";
             } elseif ($item_time < 86400) {
                 $item['item_time'] = floor($item_time / 3600) . " giờ trước đây";
             } else {
                 $item['item_time'] = date('\\n\\gà\\y j \\t\\há\\n\\g n', $item['up_time']);
             }
             //$item['description'] = String::display_sort_title(EClassApi::delDoubleSpace(EClassApi::trimSpace(strip_tags(EClassApi::post_db_parse_html($item['description'])))), 35);
             $item['description'] = EClassApi::HighLightKeyword(EClassApi::delDoubleSpace(EClassApi::trimSpace(strip_tags(EClassApi::post_db_parse_html(preg_replace('/\\[[0-9]{1,3}\\]/', '', $item['description']))))), $highlight1, 35, "background:yellow;font-size:14px;font-weight:bold;color:blue;");
             $ebname = EClassApi::safe_title($item['name']);
             $ebname_tmp = substr(EClassApi::safe_title($item['name']), 0, 20);
             if (isset(CGlobal::$allCategories[$item['category_id']])) {
                 $item['item_url'] = WEB_DIR . ECRewrite::formatUrl('?page=item_detail&id=' . $item['id'] . '&ebname=' . $ebname . '&nice_name=' . CGlobal::$allCategories[$item['category_id']]['nice_name']);
                 $item['item_url_tmp'] = WEB_ROOT . CGlobal::$allCategories[$item['category_id']]['nice_name'] . '/p' . $item['id'] . '/' . $ebname_tmp . '...';
             } else {
                 $item['item_url'] = WEB_DIR . ECRewrite::formatUrl('?page=item_detail&id=' . $item['id'] . '&ebname=' . $ebname);
                 $item['item_url_tmp'] = WEB_ROOT . 'p' . $item['id'] . '/' . $ebname_tmp . '...';
             }
             if ($item['original_image_url']) {
                 $item['original_image_url'] = EClassApi::getImageThumb($item['original_image_url'], 110, 0, 1, $item['img_server']);
             }
             $item['price'] = number_format($item['price'], 0, ',', '.');
             $item_array[] = $item;
         }
     }
     $display->add('total_item_cat', $total);
     $display->add('name_item_cat', $catActive);
     $display->add('paging', $paging);
     $display->add('keywords', $keywords);
     $display->add('items', $item_array);
     $display->output('sphinx_search_ajax', false, 'sphinx_search');
 }
Exemple #2
0
 static function descriptionText($str)
 {
     $meta_desc = EClassApi::post_db_parse_html($str);
     $meta_desc = EClassApi::plainText(html_entity_decode($meta_desc, ENT_QUOTES, "UTF-8"));
     $meta_desc = str_replace('\'', '', $meta_desc);
     $meta_desc = str_replace('"', '', $meta_desc);
     return EClassApi::delDoubleSpace(EClassApi::trimSpace($meta_desc));
 }