Server : Apache/2.4.18 (Ubuntu) System : Linux canvaswebdesign 3.13.0-71-generic #114-Ubuntu SMP Tue Dec 1 02:34:22 UTC 2015 x86_64 User : oppastar ( 1041) PHP Version : 7.0.33-0ubuntu0.16.04.15 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority, Directory : /proc/self/root/var/www/laciasmara.com/public_html/shop/application/controllers/admin/ |
Upload File : |
<?php if (!defined('BASEPATH')) { exit('No direct script access allowed'); } class Search extends Admin_Controller { function __construct() { parent::__construct(); $this->load->model('configuration_m'); $this->load->helper('form'); } function index() { show_404(); } public function get_suggestions() { $query = $this->input->get('query'); if (empty($query)) { echo json_encode([]); return; } // Query dengan partial match $this->db->select('id, title, link, ( CASE WHEN title LIKE "%' . $this->db->escape_like_str($query) . '%" THEN 2 WHEN query LIKE "%' . $this->db->escape_like_str($query) . '%" THEN 1 ELSE 0 END + search_count ) as match_score', FALSE); $this->db->from('search_suggestions'); $this->db->group_start() ->like('query', $query, 'both') ->or_like('title', $query, 'both') ->group_end(); $this->db->order_by('match_score', 'DESC'); $this->db->limit(5); $results = $this->db->get()->result_array(); $user_id = $this->session->userdata('user_id') ?? NULL; foreach ($results as $row) { $this->db->insert('search_logs', [ 'suggestion_id' => $row['id'], 'search_query' => $query, 'user_id' => $user_id ]); } // Update search count untuk suggestion yang cocok if (!empty($results)) { $ids = array_column($results, 'id'); $this->db->where_in('id', $ids); $this->db->set('search_count', 'search_count + 1', FALSE); $this->db->set('last_searched', 'CURRENT_TIMESTAMP', FALSE); $this->db->update('search_suggestions'); } echo json_encode($results); } // Fungsi untuk menurunkan relevance score secara berkala public function decay_search_counts() { $this->db->query(' UPDATE search_suggestions SET search_count = GREATEST(0, search_count * 0.9) WHERE DATEDIFF(NOW(), last_searched) > 30 '); } /** * Search products based on query */ public function product() { $query = $this->input->post('query'); if (empty($query)) { $this->output->set_status_header(400) ->set_content_type('application/json') ->set_output(json_encode(['error' => 'Query parameter is required'])); return; } try { $results = $this->search_products($query); // Format results $formatted_results = array_map(function ($product) { return [ 'id' => $product->id_products, 'name' => $product->title, 'url' => $this->generateProductUrl($product->alias) // Helper function to generate URL ]; }, $results); $this->output->set_content_type('application/json') ->set_output(json_encode($formatted_results)); } catch (Exception $e) { $this->output->set_status_header(500) ->set_content_type('application/json') ->set_output(json_encode(['error' => 'Internal server error'])); } } /** * Search brands based on query */ public function brand() { $query = $this->input->post('query'); if (empty($query)) { $this->output->set_status_header(400) ->set_content_type('application/json') ->set_output(json_encode(['error' => 'Query parameter is required'])); return; } try { $results = $this->search_brands($query); // Format results $formatted_results = array_map(function ($brand) { return [ 'id' => $brand->id_brands, 'name' => $brand->brand, 'url' => $this->generateBrandUrl($brand->alias) // Helper function to generate URL ]; }, $results); $this->output->set_content_type('application/json') ->set_output(json_encode($formatted_results)); } catch (Exception $e) { $this->output->set_status_header(500) ->set_content_type('application/json') ->set_output(json_encode(['error' => 'Internal server error'])); } } /** * Search categories based on query */ public function category() { $query = $this->input->post('query'); if (empty($query)) { $this->output->set_status_header(400) ->set_content_type('application/json') ->set_output(json_encode(['error' => 'Query parameter is required'])); return; } try { $results = $this->search_categories($query); // Format results $formatted_results = array_map(function ($category) { return [ 'id' => $category->id_categories, 'name' => $category->category, 'url' => $this->generateCategoryUrl($category->alias) // Helper function to generate URL ]; }, $results); $this->output->set_content_type('application/json') ->set_output(json_encode($formatted_results)); } catch (Exception $e) { $this->output->set_status_header(500) ->set_content_type('application/json') ->set_output(json_encode(['error' => 'Internal server error'])); } } private function search_products($query) { $this->db->like('title', $query); $this->db->where('product_status', 1); // Hanya produk aktif $this->db->limit(10); // Batasi hasil return $this->db->get('products')->result(); } private function search_brands($query) { $this->db->like('brand', $query); $this->db->where('status', 1); // Hanya brand aktif $this->db->limit(10); // Batasi hasil return $this->db->get('brands')->result(); } private function search_categories($query) { $this->db->like('category', $query); $this->db->where('status', 1); // Hanya kategori aktif $this->db->limit(10); // Batasi hasil return $this->db->get('categories')->result(); } function generateProductUrl($slug) { return base_url('product/' . strtolower($slug)); } function generateBrandUrl($slug) { return base_url('brand/' . strtolower($slug)); } function generateCategoryUrl($slug) { return base_url('category/' . strtolower($slug)); } }