<?php namespace App\Controllers;
  
class Search extends FrontController
{	
	private $result_cnt;
	
	public function result(){
		$this->result_cnt = 0;
		$data['language_code'] = $this->get_requested_lang_code();
		$this->language_code = $data['language_code'];
		$data['meta_data']['title'] = "Search Result";
		$data['meta_data']['description'] = "Search Result";
		$data['meta_data']['keywords'] = "Search Result";
		
		$data['search_term'] = trim($this->request->getGet('q'));
		$data['rec_list'] = [];
		if($data['search_term']!=''){
			$data['rec_list'] = $this->get_search_result($data['language_code'],$data['search_term']);
		}
		$data['result_cnt'] = $this->result_cnt;
		echo $this->view_inner_template('search_result',$data);
	}
	
	private function get_search_result($lang_code,$term){
		$rec_list = [];
		$page_list = $this->search_pages($lang_code,$term);
		$news_list = [];//$this->search_news($lang_code,$term);
		$product_list = $this->search_product($lang_code,$term);
		if($page_list){
			$this->result_cnt += count($page_list); 
			$cat['path'] = $this->lang_url('page/');
			$cat['rec_list'] = $page_list;
			array_push($rec_list,$cat);
		}
		if($news_list){
			$this->result_cnt += count($news_list);
			$cat['path'] = base_url('news/detail').'/';
			$cat['rec_list'] = $news_list;
			array_push($rec_list,$cat);
		}
		if($product_list){
			$this->result_cnt += count($product_list);
			$cat['path'] = $this->lang_url('product/');
			$cat['rec_list'] = $product_list;
			array_push($rec_list,$cat);
		}
		return $rec_list;
	} 
	
	private function search_pages($lang_code,$term){
		$builder = $this->cmObj->get_table_builder('page AS mt');
		$builder->join('page_ln AS ln',"mt.page_id=ln.page_id AND ln.language_code='".$lang_code."'","inner");
		$query = $builder->select('mt.page_id AS id,mt.page_url AS url,ln.title,ln.meta_keywords,ln.meta_description')
			->where('mt.status','Active')
			->where('mt.is_deleted','No')
			->like('ln.meta_keywords',$term)
			->get();
		return $query->getResultArray();
	}
	
	private function search_news($lang_code,$term){
		$builder = $this->cmObj->get_table_builder('news AS mt');
		$builder->join('news_ln AS ln',"mt.news_id=ln.news_id AND ln.language_code='".$lang_code."'","inner");
		$query = $builder->select('mt.news_id AS id,mt.news_url AS url,ln.title,ln.meta_keywords,ln.meta_description')
			->where('mt.status','Active')
			->where('mt.is_deleted','No')
			->like('ln.meta_keywords',$term)
			->get();
		return $query->getResultArray();
	}
	
	private function search_product($lang_code,$term){
		$builder = $this->cmObj->get_table_builder('products AS mt');
		$builder->join('products_ln AS ln',"mt.product_id=ln.product_id AND ln.language_code='".$lang_code."'","inner");
		$builder->select('mt.product_id AS id,mt.product_url AS url,ln.title,ln.meta_keywords,ln.meta_description')
			->where('mt.status','Active')
			->where('mt.is_deleted','No')
			->like('ln.meta_keywords',$term);
		//echo $builder->getCompiledSelect();
		//exit();
		$query = $builder->get();
		return $query->getResultArray();
	}
}