<?php
class rsAction extends FirstendAction {	
	public function _initialize() {
        parent::_initialize();				
		$map['status'] = 1;
		$catelist = D('article_cate')->where($map)->order('ordid asc')->select();
		foreach ($catelist as $key => $val) {
			$catelist[$key] = $val;
		}		
		$this->assign('catelist', $catelist);	
		$res = D('article_cate')->field('id,name,ename')->select();
		$cate_list = array();
            foreach ($res as $val) {
                $cate_list[$val['id']] = array($val['name'],$val['ename']);
            }
		$this->_catelist = 	$cate_list;				
		$hot = $this->gethot();
		$new = $this->getnew();
		$this->assign('hot', $hot);	
		$this->assign('new', $new);	
    }	
	
	public function keyWordCheck($content){
        $str = trim($content);
        $nokw = C('ftx_nokw');
        $nokw = trim($nokw);
        $arr = explode(",", $nokw);
        for($i=0,$k=count($arr);$i<$k;$i++){    
            if($arr[$i]==''){
              continue;    
            }
            if(@strpos($str,$arr[$i])!==false){        
              return $arr[$i];
            }    
        }   
        return false;
    }
    public function index(){		
		$k = I('k');
		if($k){
		$nowds = $this->keyWordCheck($k);
		if($nowds){				
			header("Content-type: text/html; charset=utf-8");             
			die('<script language="javascript">alert("您搜索的关键词不允许，请更换！");history.go(-1);</script>');
		}else{
		$p = I('p',1, 'intval');
		$page_size = 20;				
		$start = $page_size * ($p - 1) ;
		$tagsarr = D('article')->get_tag_by_titles($k);										
		foreach($tagsarr as $key =>$kws){					
			$inum[] = "IF(LOCATE('".$kws."',title)>0,'1','0')";						
			$like[] = "LOCATE('".$kws."',title)>0";
		}				
		$where[]= implode(' OR ',$like);
		$fied = implode('+',$inum).' AS tIndex';
		$where['status'] = 1;		
		$where['fabu'] = 1;	
		$px = C('ftx_px_or')?C('ftx_px_or'):'id desc';
		$order = 'tIndex desc,'.$px;
		$item_list = D('article')->field($fied.',id,mid,mpic,intro,title,author,cate_id,add_time,hits')->where($where)->order($order)->limit($start . ',' . $page_size)->select();
		foreach($item_list as $key=>$val){    
            $item_list[$key] = $val;				
			$item_list[$key]['add_time'] = date('Y-m-d',$val['add_time']);					
			if(C('ftx_a_link')=='md5'){
			$vid = $val['mid'];	
			}else{
			$vid = $val['id'];	
			}
			if(C('ftx_img_put')==1){
			if(!$val['mpic']){	
			$item_list[$key]['pic'] = '/?m=images&id='.$vid.'.'.C('ftx_img_type');	
			}else{
			$item_list[$key]['pic'] = $val['mpic'];		
			}			
			}
			if(C('ftx_img_put')==2){
			$item_list[$key]['pic'] = $val['mpic'];	
			}
			if(C('ftx_url_set')==1){
			$upt = $this->_catelist[$val['cate_id']][1];	
			}else{
			if(C('ftx_c_name')){
			$upt = C('ftx_c_name');	
			}else{
			$upt = 'wen';		
			}			
			}
			$url = '/'.$upt.'/'.$vid.C('URL_HTML_SUFFIX');
			$item_list[$key]['url'] = $url;
        }
		$count = D('article')->where($where)->count();
		$this->assign('item_list',$item_list);
		$pager = $this->_pager($count, $page_size);
        $page = $pager->share_show();
		$page = str_replace('/index/k/','/',$page);		
		$page = str_replace('/p/','/',$page);
		//$page = str_replace('.html','',$page);
		$this->assign('page',$page);
		$this->assign('k',$k);
		$this->_config_seo(C('ftx_seo_config.search'), array(
            'kwd' => $k            
        ));				
		$this->display();	
		}	
		}
		
	} 
	public function gethot(){
		$where['status'] = 1;		
		$where['fabu'] = 1;	
		if(C('ftx_img_put')==2){
		$where['mpic'] = array('exp','is not null');
		}
		$item_list = D('article')->where($where)->order('hits desc')->limit(0,8)->select();		
		foreach($item_list as $key=>$val){    
            $item_list[$key] = $val;					
			$item_list[$key]['add_time'] = date('Y-m-d',$val['add_time']);				
			if(C('ftx_a_link')=='md5'){
			$vid = $val['mid'];	
			}else{
			$vid = $val['id'];	
			}
			if(C('ftx_img_put')==1){
			if(!$val['mpic']){	
			$item_list[$key]['pic'] = '/?m=images&id='.$vid.'.'.C('ftx_img_type');	
			}else{
			$item_list[$key]['pic'] = $val['mpic'];		
			}			
			}
			if(C('ftx_img_put')==2){
			$item_list[$key]['pic'] = $val['mpic'];	
			}
			if(C('ftx_url_set')==1){
			$upt = $this->_catelist[$val['cate_id']][1];	
			}else{
			if(C('ftx_c_name')){
			$upt = C('ftx_c_name');	
			}else{
			$upt = 'wen';		
			}			
			}
			$url = '/'.$upt.'/'.$vid.C('URL_HTML_SUFFIX');
			$item_list[$key]['url'] = $url;
        }
		return $item_list;
	}
	public function getnew(){
		$where['status'] = 1;
		$where['fabu'] = 1;	
		if(C('ftx_img_put')==2){
		$where['mpic'] = array('exp','is not null');
		}
		$item_list = D('article')->where($where)->order('id desc')->limit(0,8)->select();		
		foreach($item_list as $key=>$val){    
            $item_list[$key] = $val;					
			$item_list[$key]['add_time'] = date('Y-m-d',$val['add_time']);	
			if(C('ftx_a_link')=='md5'){
			$vid = $val['mid'];	
			}else{
			$vid = $val['id'];	
			}
			if(C('ftx_img_put')==1){
			if(!$val['mpic']){	
			$item_list[$key]['pic'] = '/?m=images&id='.$vid.'.'.C('ftx_img_type');	
			}else{
			$item_list[$key]['pic'] = $val['mpic'];		
			}			
			}
			if(C('ftx_img_put')==2){
			$item_list[$key]['pic'] = $val['mpic'];	
			}
			if(C('ftx_url_set')==1){
			$upt = $this->_catelist[$val['cate_id']][1];	
			}else{
			if(C('ftx_c_name')){
			$upt = C('ftx_c_name');	
			}else{
			$upt = 'wen';		
			}			
			}
			$url = '/'.$upt.'/'.$vid.C('URL_HTML_SUFFIX');
			$item_list[$key]['url'] = $url;
        }
		return $item_list;
	}	
    
	
   
}

