| Server IP : 103.48.194.25 / Your IP : 216.73.216.47 Web Server : Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.33 System : Linux VMHostDefault 3.10.0-1160.42.2.el7.x86_64 #1 SMP Tue Sep 7 14:49:57 UTC 2021 x86_64 User : sieuthimay ( 1016) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/zomuaban.com/public_html/site/models/ |
Upload File : |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Search_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
# Hàm này sử dụng khi user nhập từ khóa để tìm sản phẩm, sử lý khác với hàm thêm từ khóa
public function set_keyword($keyword, $cateId, $total) {
# Không xử lý những keyword này
if ($keyword == '' || strlen($keyword) <= 3) return;
/**
* (!Important : Phải set keyword vào DB trước khi set Cookie (để check trường hợp bên dưới)
*
* > Nếu keyword == cookie : User đang chọn lại danh mục, khu vực > Tình huống này quan trọng nhằm xác định chính xác danh mục của keyword
* > Ngược lại : Kiểm tra keyword này tồn tại chưa
* > Nếu có rồi thì update lại thời gian, total ..
* > Nếu chưa thì insert vào
*/
if ($cateId == '' || $cateId == '*')$cateId = 0;
# Nếu user chuyển danh mục khác khi tìm kiếm với từ khóa đó
if ($keyword == get_my_cookie('KEYs')) {
$new_update = array('totalResult' => $total, 'time'=> now());
if ($cateId != 0) $new_update['cateId'] = $cateId;
$this->db->where('word', $keyword);
$this->db->update(tableConf('keyword'),$new_update);
}else {
# Nếu user nhập từ khóa mới -> update . Nếu ko update được thì insert
$this->db->set('numSearch', 'numSearch+1', FALSE);
$this->db->where('word', $keyword);
$this->db->update(tableConf('keyword'), array( 'totalResult' => $total,
'timeSearch' => now()));
#Update thất bại > insert record
if ($this->db->affected_rows() <= 0) {
$this->db->insert(tableConf('keyword'), array( 'word' => $keyword,
'cateId' => $cateId,
'totalResult' => $total,
'numSearch' => 1,
'timeSearch' => now()
));
}
}
}
# Lấy những từ khóa có lượt tìm nhiều nhất trong 1 danh mục
# (Tính tổng các từ khóa giống nhau)
public function get_search_trend($cateId=null) {
if (isset($cateId) && !empty($cateId) && $cateId != 0)$this->db->where('cateId', $cateId);
$this->db->where('typeId !=', 'error');
$this->db->select('word');
$this->db->select_sum('numSearch', 'sum_num');
$this->db->group_by('word');
$this->db->order_by('sum_num', 'desc');
$this->db->limit(8);
$q = $this->db->get(tableConf('keyword'));
return $q->result();
}
# Lấy những từ khóa đang có người seach trong 1 danh mục & khu vực get_searching
# (Từ khóa mới nhất được thêm vào hơặc update lại thời gian)
public function get_searching($cateId=null, $cityId=null, $limit=50) {
if ($cateId && $cateId != 0){
$all_sub_cate = ($cateId && $cateId != 0) ? cate_all_child($cateId) : null;
if ($all_sub_cate) {
array_push($all_sub_cate, $cateId);
$this->db->where_in('cateId', $all_sub_cate);
}else $this->db->where('cateId', $cateId);
}
if ($cityId)
$this->db->where('cityId', $cityId);
#$this->db->where('totalResult >' , 0);
$this->db->where('typeId !=', 'error');
$this->db->order_by('timeSearch desc');
$this->db->limit($limit);
$q =$this->db->get(tableConf('keyword'));
return $q->result();
}
public function search($strSearch, $keys = null, $cateId = 'all', $cityId = '0', $ta = 'anytime', $start = 0, $limit = -1, $notdone=false) {
# IMPORTANT! đặt lại limit cho kết quả tìm kiếm theo mặc định
$limit = $limit == -1 ? siteConf('resultPerPage'): $limit;
# Xử lý từ khóa tìm kiếm
$strSearch = remove_whitespace($strSearch);
# Tìm trong cached trước
//$cacheQueryId = siteConf('domain').('/search?s='.alias($strSearch).'&cate='.$cateId.'&city='.$cityId.'&start='.$start.'&ta='.$ta);
//$cachedResult = $this->cache-> memcached->get($cacheQueryId);
//set_my_data('search_query_id', $cacheQueryId);
# Check là Array vì cần Cached nếu search ko có kết quả
//if (!is_array($cachedResult)) {
$query = 'SELECT id ';
$query .= $this->searchquery($strSearch, $keys, $cateId, $cityId, $ta, $notdone);
# ORDER : Thực hiện ORDER kết quả
$query .= " ORDER BY ";
if (!empty($strSearch)) {
$query .= " CASE ";
$query .= " WHEN ( keyword LIKE '%".$strSearch."%' ) THEN 1 ";
$query .= " WHEN ( alias LIKE '%".removesign($strSearch)."%' ) THEN 2 ";
if(is_array($keys) && count($keys) > 0 ) {
for ( $i = 0 ; $i < count($keys) ; $i++ ) {
$kword = removesign($keys[$i]);
if(!empty($kword)){
$query .= ' WHEN ( ';
$query .= " alias LIKE '%".$kword."%' " ;
$query .= ' ) THEN ' . (int)($i + 3);
}
}
}
if (is_array($keys) && count($keys) > 0 ) {
for ( $i = 0 ; $i < count($keys) ; $i++ ) {
$kword = removesign($keys[$i]);
if(!empty($kword)){
$query .= ' WHEN ( ';
$query .= " alias LIKE '%".$kword."%' " ;
$query .= ' ) THEN ' . (int)($i + 3);
}
}
}
$i = isset($i) ?$i:0;
# SORT search từng từ trong chuỗi nhập vào
$words = explode(' ', $strSearch);
$k = 0;
foreach($words as $word) {
if (!empty($word)){
$query .= ($k > 0) ? ' AND ':' WHEN ( ';
$query .= " alias LIKE '%".removesign($word)."%' " ;
$k++;
}
}
$query .= ' ) THEN ' . (int)($i + 3 );
# END ORDER SORT
$query .= " END ASC, ";
$query .= " lastestUpdate DESC ";
}else {
# Nếu ko có từ khóa thì sort theo lastestActive
$query .= " lastestUpdate DESC ";
}
$query .= " LIMIT ".$start." , ".$limit;
$q = $this->db->query($query);
#Logger($this->db->last_query());
# STEP2 : select item IN ( itemIds )
$ids = array();
foreach($q->result() as $row) {
array_push($ids, $row->id);
}
$cachedResult = array();
if (count($ids) > 0) {
$this->db->_protect_identifiers = false;
$qget = $this->db->where_in('id', $ids)->order_by('FIELD (id, '. implode(',', $ids) .')')->get(tableConf('item'));
foreach($qget->result() as $item) {
$cachedItem = full_item_prop($item);
array_push($cachedResult,$cachedItem);
}
}
//$this->cache-> memcached->save($cacheQueryId, $cachedResult , siteConf('cachedSTime'));
//}else {
#Logger('SEARCH : Lay tu memcached ra : '. $cacheQueryId);
//}
return $cachedResult;
}
public function totalResult($strSearch, $keys = null, $cateId = null, $cityId = null, $ta = null, $notdone = false ) {
/**
$ss = "SELECT FOUND_ROWS()";
$s = mysql_fetch_row(mysql_query($ss));
Logger($s);
return $s[0];
**/
//$cacheTotalId = siteConf('domain').'/total-search'.('?s='.alias($strSearch).'&cate='.$cateId.'&city='.$cityId.'&ta='.$ta);
//$totalsearch = $this->cache->memcached->get($cacheTotalId);
//if (!$totalsearch) {
$query = "SELECT count(id)";
$query .= $this->searchquery($strSearch, $keys, $cateId, $cityId, $ta, $notdone);
$q = $this->db->query($query);
# Get total
$totalsearch = $q->num_rows() ;
# Lưu cached
//$this->cache-> memcached->save($cacheTotalId, $totalsearch , 5 * siteConf('cachedSTime'));
//}
return $totalsearch;
}
protected function searchquery($strSearch, $keys = null, $cateId = null, $cityId = null, $ta = null, $notdone=false) {
$extendQuery = "";
if (!isset($ta) || empty($ta) || $ta == 'anytime') {
# Nếu ko filter thời gian > search tất cả item createTime < NOW
$extendQuery .= " AND ( lastestUpdate < " .now()." ) ";
# Nếu notdone ? Không lấy những tin đã hết hạn và đã hoàn thành
if($notdone)$extendQuery .= " AND ( expirationDate > " .now()." ) ";
}else {
# Fitler thời gian
if ($ta == 'yesterday') {
# Search hôm qua
$today2sec = strtotime(date("Y-m-d"));
$yesterday = (int)($today2sec - 86400);
$extendQuery .= " AND ( lastestUpdate < " .$today2sec." AND lastestUpdate > ". $yesterday . " ) ";
}else{
$extendQuery .= " AND ( lastestUpdate < " .now() ." AND lastestUpdate > ". ta2stamp($ta) . " ) ";
}
}
# Nếu lấy cả những tin chưa hết hạn thì comment dòng bên dưới
# $extendQuery .= " AND expirationDate > ". now() . " ";
# Tìm trong danh mục
if (isset($cateId) && !empty($cateId) && $cateId != 'all') {
$cates = cate_all_child($cateId);
$catelist = "";
for ($i = 0; $i < count($cates); $i++) {
if ($i == 0)$catelist .= "'".$cates[$i]."'";
else $catelist .= ",'".$cates[$i]."'";
}
$extendQuery .= " AND cateId IN (".$catelist.") ";
}
# Tìm trong các thành phố
if (isset($cityId) && $cityId != 0) {
$extendQuery .= ' AND city IN ("0", "'.$cityId.'") ';
}
$query = " FROM " . tableConf('item');
$query .= " WHERE active = 1 ";
$query .= $extendQuery;
if (!empty($strSearch)) {
$query .= " AND ( keyword LIKE '%".$strSearch."%' ";
$query .= " OR alias LIKE '%".removesign($strSearch)."%' ";
if(is_array($keys) && count($keys) > 0 ) {
for ( $i = 0 ; $i < count($keys) ; $i++ ) {
$kword = removesign($keys[$i]);
if(!empty($kword)){
$query .= ' OR ';
$query .= " alias LIKE '%".$kword."%' " ;
}
}
}
# search từng từ trong chuỗi nhập vào
$words = explode(' ', $strSearch);
$i = 0;
foreach($words as $word) {
if (!empty($word)){
$query .= ($i > 0) ? ' AND ':' OR ( ';
$query .= " alias LIKE '%".removesign($word)."%' " ;
$i++;
}
}
$query .= ' ) ) ';
}
return $query;
}
}