<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Zj extends CI_Controller {

	function __construct(){
		    parent::__construct();
        	$this->load->helper('url');
        	$this->load->helper('form');
            $this->load->library('session'); //加载session
		    $this->load->model('CsdjDB');
		    $this->load->model('CsdjSkins');
		    $this->load->model('CsdjTopic');
		    $this->load->model('CsdjCache');
	}

    //分类列表
	public function lists()
	{
            $data='';$data_content='';
            $fid = $this->uri->segment(3);   //方式
            $sid = intval($this->uri->segment(4));   //SID
            $page  = intval($this->uri->segment(5));   //页数
            if($page==0) $page=1;
            if($sid==0) $sid=1;
               
            //判断运行模式
            $cscms=$this->input->get('cs', TRUE);
	        if(Web_Mode==3 && $cscms!="html"){
                    $Htmllink=$this->CsdjSkins->LinkUrl('lists','zj',$fid,$id,$page);
                    header("Location: ".$Htmllink.""); 
                    exit;
            }

	        $cache_id ="zj_list_".$fid."_".$sid."_".$page;
	        if(!($this->CsdjCache->start($cache_id))){
                echo $this->CsdjTopic->lists($fid,$sid,$page);
			    $this->CsdjCache->end();
			}
	}

    //搜索列表
	public function so()
	{
            $data='';$data_content='';
            $fid = $this->uri->segment(3);   //方式
            $key = $this->input->get_post('key');   //关键字
            $page  = intval($this->input->get('p', TRUE));   //页数
            if($page==0) $page=1;
            if($fid=='zm' && empty($key)){
                 $key=$this->uri->segment(4); 
            }

	        $key = $this->CsdjSkins->safe_replace(trim($key));
	        $key = htmlspecialchars(strip_tags($key));
	        $key = str_replace('%', '', $key);	//过滤'%'，用户全文搜索
            $key=$this->CsdjSkins->rurlencode($key);
                
	        $cache_id ="topic_so_".$fid."_".$key."_".$page;
	        if(!($this->CsdjCache->start($cache_id))){

                $CS_Template='so-topic-'.$fid.'.html';
                $this->load->get_templates();
		        $template=$this->load->view($CS_Template,$data,true);

		        $Mark_Text=$this->CsdjSkins->TopandBottom($template);
		        $Mark_Text=str_replace("{cscms:sokey}",$key,$Mark_Text);
                if($fid=='tags'){
		             $Mark_Text=str_replace("{cscms:title}","搜索标签".$key." - ".Web_Name,$Mark_Text);
                }else{
		             $Mark_Text=str_replace("{cscms:title}","搜索关键词".$key." - ".Web_Name,$Mark_Text);
                }
		        $Mark_Text=str_replace("{cscms:keywords}",$key,$Mark_Text);
		        $Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);

		        //预先除了分页
		        $pagenum=$this->CsdjSkins->GetPageNum($Mark_Text);
		        preg_match_all('/{cscms:topic(.*?pagesize=([\S]+).*?)}([\s\S]+?){\/cscms:topic}/',$Mark_Text,$page_arr);
		        if(!empty($page_arr) && !empty($page_arr[2])){

		           if($fid=='zm'){
			            $zimu_arr=array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
			            $zimu_arr1=array(-20319,-20283,-19775,-19218,-18710,-18526,-18239,-17922,-1,-17417,-16474,-16212,-15640,-15165,-14922,-14914,-14630,-14149,-14090,-13318,-1,-1,-12838,-12556,-11847,-11055);
			            $zimu_arr2=array(-20284,-19776,-19219,-18711  ,-18527,-18240,-17923,-17418,-1,-16475,-16213,-15641,-15166,-14923,-14915,-14631,-14150,-14091,-13319,-12839,-1,-1,-12557,-11848,-11056,-2050);
                
                        if(in_array(strtoupper($key),$zimu_arr)){  //按字母搜索
			                     $posarr=array_keys($zimu_arr,strtoupper($key));
			                     $pos=$posarr[0];
			                     $sqlstr="SELECT * FROM ".CS_SqlPrefix."topic where CS_YID=0 and (((ord( substring( CS_Name, 1, 1 ) ) -65536>=".($zimu_arr1[$pos])." and  ord( substring( CS_Name, 1, 1 ) ) -65536<=".($zimu_arr2[$pos]).")) or UPPER(substring( CS_Name, 1, 1 ))='".$zimu_arr[$pos]."')";
		                }
		           }elseif($fid=='tags'){
                        $sqlstr="select * from ".CS_SqlPrefix."topic where CS_YID=0 and CS_Tags like '%".$key."%'  order by CS_AddTime desc";
		           }else{
                        $sqlstr="select * from ".CS_SqlPrefix."topic where CS_YID=0 and (CS_Name like '%".$key."%' or CS_Tags like '%".$key."%' or CS_Year like '%".$key."%' or CS_Diqu like '%".$key."%' or CS_Yuyan like '%".$key."%')  order by CS_AddTime desc";
                   }

			        $Arr=$this->CsdjSkins->SpanPage($sqlstr,$page_arr[2][0],$pagenum,'so','zj',$fid,urlencode($key),1,$page);//sql,每页显示条数
			        $result=$this->CsdjDB->db->query($Arr[2]);
			        $recount=$result->num_rows();
			        if($recount==0){
				        $data_content.="<div align=center><strong>对不起,没有找到《<font color=#FF0000>".$key."</font>》相关专集!</strong></div>";
			        }else{
				        if($result){
				        	$sorti=1;
					        foreach ($result->result() as $row2) {
						        $datatmp=$this->CsdjSkins->DataTopic($page_arr[0][0],$page_arr[3][0],$row2,$sorti);
						        $sorti=$sorti+1;
						        $data_content.=$datatmp;
					        }
				        }
			        }// end if recount
			        $Mark_Text=$this->CsdjSkins->Page_Mark($Mark_Text,$Arr);	
			        $Mark_Text=str_replace($page_arr[0][0],$data_content,$Mark_Text);
			        unset($Arr);
                }
		        unset($page_arr);
		        $this->CsdjSkins->skins($Mark_Text,$data);
		        $this->CsdjCache->end();
			}
	}

    //专集内容页面
	public function show()
	{
            $data='';$data_content='';
            $fid = $this->uri->segment(3);   //方式
            $id = intval($this->uri->segment(4));   //ID
            $page  = intval($this->uri->segment(5));   //页数
            if($page==0) $page=1;
  
            if($id==0){
                exit($this->CsdjSkins->Msg_url('出错了，ID不能为空！',Web_Path));
            }

            //判断运行模式
            $cscms=$this->security->xss_clean($this->input->get('cs', TRUE));
	        if(Web_Mode==3 && $cscms!="html"){
                    $Htmllink=$this->CsdjSkins->LinkUrl('show','zj',$fid,$id,$page);
                    header("Location: ".$Htmllink.""); 
                    exit;
            }

            $aliasname="";
		    $row=$this->CsdjDB->get_select ('topic','CS_ID','*',''.$id.'');
		    if(!$row){
                 $aliasname=$id;
                 $row=$this->CsdjDB->get_select ('topic','CS_AliasName','*',''.$id.'');
                 if(!$row){
                       exit($this->CsdjSkins->Msg_url('出错了，该专集不存在！',Web_Path));
                 }
			}
            if($row[0]->CS_YID==1){
                 exit($this->CsdjSkins->Msg_url('抱歉，该专集还没有审核！',Web_Path));
            }

	        $cache_id ="zj_show_".$fid."_".$id."_".$page;
	        if(!($this->CsdjCache->start($cache_id))){
                echo $this->CsdjTopic->show($fid,$id,$page,$row,$aliasname);
			    $this->CsdjCache->end();
			}
	}

    //增加人气
	public function hits()
	{
           $ac = $this->uri->segment(3);   //方式
           if($ac=='id'){

                $id = intval($this->uri->segment(4));   //ID
		        $row=$this->CsdjDB->get_select ('topic','CS_ID','CS_Hits,CS_Yhits,CS_Zhits,CS_Rhits,CS_LookTime',''.$id.'');
		        if(!$row){
			       show_404();
		        }
                //增加专集人气
                $daytime =$this->CsdjSkins->DateBj("d",$row[0]->CS_LookTime,date('Y-m-d H:i:s'));
	            if($daytime==0){
	                $updata['CS_Rhits']=$row[0]->CS_Rhits+13;
	            }else{
	                $updata['CS_Rhits']=1;
	            }
	            if($daytime < 7){
	                $updata['CS_Zhits']=$row[0]->CS_Zhits+13;
	            }else{
	                $updata['CS_Zhits']=1;
	            }
	            if($daytime <31){
	                $updata['CS_Yhits']=$row[0]->CS_Yhits+13;
	            }else{
	                $updata['CS_Yhits']=1;
	            }
	            $updata['CS_Hits']=$row[0]->CS_Hits+13;
	            $updata['CS_LookTime']=date('Y-m-d H:i:s');
                $this->CsdjDB->get_update ('topic',$id,$updata);

           }elseif($ac=='dt'){   //静态模式、动态人气

                $op = $this->uri->segment(4);   //类型
                $id = intval($this->uri->segment(5));   //ID
		        $row=$this->CsdjDB->get_select ('topic','CS_ID','CS_Hits,CS_Yhits,CS_Zhits,CS_Rhits',''.$id.'');
		        if(!$row){
			        show_404();
		        }

                if($op=='yhits'){
                      echo "document.write('".$row[0]->CS_Yhits."');";
                }elseif($op=='zhits'){
                      echo "document.write('".$row[0]->CS_Zhits."');";
                }elseif($op=='rhits'){
                      echo "document.write('".$row[0]->CS_Rhits."');";
                }else{
                      echo "document.write('".$row[0]->CS_Hits."');";
                }
           }
	}
}


