<?php
class Searching_Model extends CI_Model{
        
        function insert($arr)
        {
                
                $this->db->insert('aboutus_content', $arr);
                return $this->db->insert_id();
                
        }
        
        function update($id, $arr)
        {
        
                $this->db->where('id', $id);
                $this->db->update('aboutus_content', $arr);
        
        }
        
        function get($content)
        {       

                    //$this->db->select('aboutus_content.id,aboutus_content.main_content,aboutus_content.sub_content  ');
                //     $this->db->from('aboutus_content');
                //     $query=$this->db->get();

                // return $query->result();


                $links=array();
                //search contact us
                if($this->searchcontactus($content)==true){
                    $searchlink=array();
                    $searchlink['name']="關於我們";
                    $searchlink['link']=base_url().'/關於我們';
                    array_push($links,$searchlink);
                }
                //end search contact us

                //search content
                $contentResult=$this->searchcontent($content);

                if(!empty($contentResult)){
                    foreach ($contentResult as $contentResultRow) {
                        $searchlink=array();
                        $searchlink['name']=$contentResultRow->h1;
                        $searchlink['link']=base_url().$contentResultRow->mainmenuname.'/'.$contentResultRow->submenuname.'/'.$contentResultRow->h1;
                        array_push($links,$searchlink);
                    }
                }
                //end search content

                //search blog
                $blogResult=$this->searchblog($content);
                if(!empty($blogResult)){
                    foreach ($blogResult as $blogResultRow) {
                        $searchlink=array();
                        $searchlink['name']=$blogResultRow->title;
                        $searchlink['link']=base_url().'美容專欄/內容'.'/'.$blogResultRow->id;
                        array_push($links,$searchlink);
                    }
                }
                //end search blog

                //免費諮詢
                if(strpos($content, "免費諮詢") !== false){
                    $searchlink=array();
                    $searchlink['name']="免費諮詢";
                    $searchlink['link']=base_url().'免費諮詢';
                    array_push($links,$searchlink);
                }

                //聯絡我們
                if(strpos($content, "聯絡我們") !== false){
                    $searchlink=array();
                    $searchlink['name']="聯絡我們";
                    $searchlink['link']=base_url().'聯絡我們';
                    array_push($links,$searchlink);
                }

                // echo $this->db->last_query();
                return $links;


        }
        private function searchcontactus($content){
 

            $this->db->select('id');    
            $this->db->from('aboutus_content');
            $this->db->like('h1', $content);
            $this->db->or_like('h2', $content);
            $this->db->or_like('aboutus_text', $content);
            $query = $this->db->get();
            $num_row=$query->num_rows();

            //end added to determine new customer


            if($num_row>0){
                return true;
            }
            else{
                return false;
            }
        }
        private function searchcontent($content){
            $this->db->select('content.id,content.h1, landing_main.title as mainmenuname, landing_submenu.title as submenuname');
            $this->db->from('landing_content_multistylepage as content');
             $this->db->like('content.h1', $content);
            $this->db->or_like('content.h2', $content);
            $this->db->or_like('content.partbtext', $content);
            $this->db->or_like('content.partcpart2', $content);
            $this->db->or_like('content.partdpart1', $content);
            $this->db->or_like('content.partetext', $content);
            $this->db->or_like('content.partftext', $content);
            $this->db->where('deleted', 0);
            $this->db->join('landing_main', 'landing_main.id = content.main_id');
            $this->db->join('landing_submenu', 'landing_submenu.id = content.submenu_id');
            $query = $this->db->get();
            return $query->result();
        }
        private function searchblog($content){
            $this->db->select('id,title, content');
            $this->db->where('is_deleted', 0);
            $this->db->from('blog_list');
             $this->db->like('title', $content);
            $this->db->or_like('content', $content);
            $query = $this->db->get();
            return $query->result();
        }
        function get_id($id){
                $this->db->where('id', $id);
                $query = $this->db->get('aboutus_content');
                return $query->result();
        }
        
        function get_lang($lang)
        {
                
                $this->db->where('lang', $lang);
                $query = $this->db->get('aboutus_content');
                return $query->result();
                
        }
        
        function delete($id)
        {
                $this->db->where('id', $id);
                $this->db->delete('aboutus_content');
        }
        
}

?>