<?php
/**
 * Copyright (C) LJCMS All rights reserved.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2016.03.19
 */

class control extends indexbase {
    private $type = null; 
    private $keyword = null; 
    private $_tplname = NULL;
    private $_tplfile = NULL;
    private $fieldname = null; 

    private function _getSearchItems() {
        $this->type = XRequest::getArgs('type');
        $this->keyword = XRequest::getArgs('keyword');
        if (!in_array($this->type, array('article', 'product', 'photo', 'hr', 'download'))) {
            XHandle::halt("请选择要搜索的类型", "", 1);
        }
        if (empty($this->keyword)) {
            XHandle::halt("请输入要搜索的关键字", "", 1);
        }
        if ($this->type == 'product') {
            $this->fieldname = 'productname';
        }
        else {
            $this->fieldname = 'title';
        }
        $this->_tplname = $this->type.'_search';
    }

    
    public function control_run() {
       $language=$_SESSION["lang"];
        if($language==''){
            $language='cn';
        }
        $searchsql = "";
        $orderby = "";
        $contet=$_REQUEST['content'];
        $res=$_REQUEST['res'];
        $searchsql .= " AND v.title LIKE '%$contet%' AND v.lang='$language'";
        $searchsql1 .= " AND v.productname LIKE '%$contet%'  AND v.lang='$language'";
        $searchsq3 .= " AND v.title LIKE '%$contet%'  AND v.lang='$language'";
            $articpagesize = parent::$cfg['articlepagesize'];

            $productpagesize = parent::$cfg['productpagesize'];

            $photopagesize = parent::$cfg['photopagesize'];
//新闻搜索
        $indexmodel = M('index', "im");
        if($res==1){
            $info=$indexmodel->getinfo($_SESSION['user_id'],$contet);
            if(empty($info)){
                $arr['user_id']=$_SESSION['user_id'];
                $arr['keyword']=$contet;
                $arr['time']=time();
                $indexmodel->addkerword($arr);
            }

        }
        $userid=$_SESSION['user_id'];
        list($keytotal,$keydata) = $indexmodel->keyword($userid);
        $model = M('article', "im");
        list($total, $data1) = $model->getList(
            array(
                'page'=>$this->page,
                'pagesize'=>$articpagesize,
                'searchsql'=>$searchsql,
                'orderby'=>$orderby,
            )
        );

         list($total, $data2) = $model->getList(
            array(
                'page'=>$this->page,
                'pagesize'=>5,
                'searchsql'=>" AND v.lang='$language'",
                'orderby'=>$orderby,
            )
        );
         //图片搜索
        $model3 = M('photo', "im");
        list($total, $data3) = $model3->getList(
            array(
                'page'=>$this->page,
                'pagesize'=>$photopagesize,
                'searchsql'=>$searchsq3,
                'orderby'=>$orderby,
            )
        );

        $model1 = M('product', "im");
        list($total, $data) = $model1->getList(
            array(
                'page'=>$this->page, 
                'pagesize'=>$productpagesize, 
                'searchsql'=>$searchsql1,
                'orderby'=>$orderby,
            )
        );
        //产品搜索
       $model1 = M('product', "im");
        list($total, $data4) = $model1->getList(
            array(
                'page'=>$this->page,
                'pagesize'=>5,
                'searchsql'=>" AND v.lang='$language'",
                'orderby'=>$orderby,
            )
        );
      //  print_r($data);
        unset($model);
        
        $model2= parent::model('index', 'im');
        
       
        $newsid = $model2->getnewsid($language);
        $productid= $model2->getproductid($language);
        $photoid= $model2->getphotoid($language);
        $var_array = array(
            'product'=>$data,
            'keyword'=>$this->keyword,
            'newnews'=>$data1,
            'photo'=>$data3,
            'data2'=>$data2,
            'data4'=>$data4,
            'newsid'=>$newsid,
            'productid'=>$productid,
            'photoid'=>$photoid,
            'keydata'=>$keydata
        );
        $this->view($var_array, $this->_tplname);
    }

    public function control_delkeyword(){
        $indexmodel = M('index', "im");
        $userid=$_SESSION['user_id'];
        $res=$indexmodel->delkeyword($userid);
        if($res){
            $data=array(
                'response'=>1,
                'result'=>'操作成功',
            );
            echo json_encode($data);
            die;
        } else {
            $data=array(
                'response'=>0,
                'result'=>'操作失败'
            );
            echo json_encode($data);
            die;
        }
    }
}
?>
