<?php
/**
 * @Cscms 3.5 open source management system
 * @copyright 2009-2013 chshcms.com. All rights reserved.
 * @Author:Cheng Jie
 * @Dtime:2015-02-8
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class CsdjTopic extends CI_Model
{
    function __construct (){
	       parent:: __construct ();
    }

    //列表
    public function lists($fid,$sid,$page) {
		   $data='';$data_content='';
           if($sid==3){
                  $zjtitle="视频";
                  $CS_Template='topic-vod.html';
           }elseif($sid==2){
                  $zjtitle="文章";
                  $CS_Template='topic-news.html';
           }else{
                  $zjtitle="音乐";
                  $CS_Template='topic-dance.html';
           }
           $this->load->get_templates();
		   $template=$this->load->view($CS_Template,$data,true);
		   $Mark_Text=$this->CsdjSkins->TopandBottom($template);

		   $Mark_Text=str_replace("{cscms:title}",$zjtitle."专集 - ".Web_Name,$Mark_Text);
		   $Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
		   $Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		   $Mark_Text=str_replace("[topic:sort]",$fid,$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])){

			   $sqlstr=$this->CsdjSkins->Mark_Sql('topic',$page_arr[1][0],1,$fid);
			   $Arr=$this->CsdjSkins->SpanPage($sqlstr,$page_arr[2][0],$pagenum,'lists','zj',$fid,$sid,1,$page);//sql
			   $result=$this->CsdjDB->db->query($Arr[2]);
			   $recount=$result->num_rows();
			   if($recount==0){
				   $data_content="<div align=center><strong><font style=font-size:12px;>暂无专集数据！</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);
		   $Mark_Text=str_replace("[topic:clink]",$this->CsdjSkins->LinkUrl('lists','zj','id'),$Mark_Text);
		   $Mark_Text=str_replace("[topic:chlink]",$this->CsdjSkins->LinkUrl('lists','zj','hits'),$Mark_Text);
		   $Mark_Text=str_replace("[topic:ctlink]",$this->CsdjSkins->LinkUrl('lists','zj','reco'),$Mark_Text);
		   $Mark_Text=str_replace("[topic:cylink]",$this->CsdjSkins->LinkUrl('lists','zj','yue'),$Mark_Text);
		   $Mark_Text=str_replace("[topic:czlink]",$this->CsdjSkins->LinkUrl('lists','zj','zhou'),$Mark_Text);
		   $Mark_Text=str_replace("[topic:crlink]",$this->CsdjSkins->LinkUrl('lists','zj','ri'),$Mark_Text);
		   $Mark_Text=str_replace("[topic:cnlink]",$this->CsdjSkins->LinkUrl('lists','zj','time'),$Mark_Text);
           $Mark_Text=$this->CsdjSkins->skins($Mark_Text,$data,true);
		   if (defined('IS_ADMIN')) {
		       $Mark_Text=str_replace(SELF,"index.php",$Mark_Text);
		   }
		   return $Mark_Text;
    }

    //内容页
    public function show($fid,$id,$page,$row,$aliasname='') {
		   $data='';$data_content='';
           if(empty($row[0]->CS_Template)){
                 if($row[0]->CS_SID==3){
                       $CS_Template='topic-vod-show.html';
                 }else if($row[0]->CS_SID==2){
                       $CS_Template='topic-news-show.html';
                 }else{
                       $CS_Template='topic-dance-show.html';
                 }
           }else{
                 $CS_Template=$row[0]->CS_Template;
           }

           $this->load->get_templates();
		   $template=$this->load->view($CS_Template,$data,true);

           //SEO标题
           if (empty($row[0]->CS_Title)){
               		$seo_title=$row[0]->CS_Name." - ".Web_Name;
           }else{
               		$seo_title=$row[0]->CS_Title;
           }
           //SEO关键词
           if (empty($row[0]->CS_Keywords)){
               		$seo_keywords=$row[0]->CS_Name."专集,".$row[0]->CS_Name."专题";
           }else{
              		$seo_keywords=$row[0]->CS_Keywords;
           }
           //SEO描述
           if (empty($row[0]->CS_Description)){
               		$seo_description=$row[0]->CS_Name." 是由".Web_Name."提供视听和下载。";
           }else{
               		$seo_description=$row[0]->CS_Description;
           }

		   $Mark_Text=str_replace("{cscms:title}",$seo_title,$template);
		   $Mark_Text=str_replace("{cscms:keywords}",$seo_keywords,$Mark_Text);
		   $Mark_Text=str_replace("{cscms:description}",$seo_description,$Mark_Text);
		   $Mark_Text=str_replace("[topic:sort]",$fid,$Mark_Text);

		   //预先除了分页
		   $pagenum=$this->CsdjSkins->GetPageNum($Mark_Text);

           if($row[0]->CS_SID==3){ //视频
                 $table="vod";
		         preg_match_all('/{cscms:vod(.*?pagesize=([\S]+).*?)}([\s\S]+?){\/cscms:vod}/',$Mark_Text,$page_arr);//判断是否有分页标识
           }elseif($row[0]->CS_SID==2){ //文章
                 $table="news";
		         preg_match_all('/{cscms:news(.*?pagesize=([\S]+).*?)}([\s\S]+?){\/cscms:news}/',$Mark_Text,$page_arr);//判断是否有分页标识
           }else{  //歌曲
                 $table="dance";
		         preg_match_all('/{cscms:dance(.*?pagesize=([\S]+).*?)}([\s\S]+?){\/cscms:dance}/',$Mark_Text,$page_arr);//判断是否有分页标识
           }
		   if(!empty($page_arr) && !empty($page_arr[2])){

               $sqlstr=$this->CsdjSkins->Mark_Sql($table,$page_arr[1][0],$id,$fid);
			   $Arr=$this->CsdjSkins->SpanPage($sqlstr,$page_arr[2][0],$pagenum,'show','zj',$fid,$id,$id,$page,$aliasname);//sql,每页显示条数
			   $result=$this->CsdjDB->db->query($Arr[2]);
			   $recount=$result->num_rows();
			   if($recount==0){
			        	$data_content="<div align=center><strong><font style=font-size:12px;>该专集暂无数据！</font></strong></div>";
			   }else{
				        if($result){
					        $sorti=1;
					        foreach ($result->result() as $row2) {
                                                     if($row[0]->CS_SID==3){ //视频
						        $datatmp=$this->CsdjSkins->DataVod($page_arr[0][0],$page_arr[3][0],$row2,$sorti);
                                                     }elseif($row[0]->CS_SID==2){ //文章
						        $datatmp=$this->CsdjSkins->DataNews($page_arr[0][0],$page_arr[3][0],$row2,$sorti);
                                                     }else{  //歌曲
						        $datatmp=$this->CsdjSkins->DataDance($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);
           $ids['id']=$row[0]->CS_ID;
		   $ids['cid']=$row[0]->CS_CID;
		   $Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$ids);

           if(Pl_Modes==0){
			   $comment="<script language='javascript' src='".Web_Path."csdj/js/plajax.js'></script><script>var cs_sid='topic';var cs_did='".$id."';var cs_path='".Web_Path."';getpllist('1');</script><div id='cscms_pl'><div style='padding:5px;text-align:center;'><img src='".Web_Path."csdj/images/load.gif'/>&nbsp;&nbsp;评论载入中，请稍候.....</div></div>";
           }elseif(Pl_Modes==2){
			   $comment="<div style='padding:5px;text-align:center;'>&nbsp;&nbsp;评论已经被站长关闭!</div>";
           }else{
			   $comment="<div id=\"uyan_frame\"></div><script type=\"text/javascript\" id=\"UYScript\" src=\"http://v1.uyan.cc/js/iframe.js?UYUserId=0\" async=\"\"></script>";
           }
           $Mark_Text=str_replace('[topic:pl]',$comment,$Mark_Text);
           //静态模式,动态人气
           if(Web_Mode==3){
			   $Mark_Text=str_replace('[topic:hits]',"<script src=".site_url('zj/hits/dt/hits/'.$id)."></script>",$Mark_Text);
			   $Mark_Text=str_replace('[topic:rhits]',"<script src=".site_url('zj/hits/dt/rhits/'.$id)."></script>",$Mark_Text);
			   $Mark_Text=str_replace('[topic:zhits]',"<script src=".site_url('zj/hits/dt/zhits/'.$id)."></script>",$Mark_Text);
			   $Mark_Text=str_replace('[topic:yhits]',"<script src=".site_url('zj/hits/dt/yhits/'.$id)."></script>",$Mark_Text);
           }
		   $Mark_Text=str_replace("[topic:link]",$this->CsdjSkins->LinkUrl('show','zj','id',$row[0]->CS_CID,$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace("[topic:hlink]",$this->CsdjSkins->LinkUrl('show','zj','hits',$row[0]->CS_CID,$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace("[topic:tlink]",$this->CsdjSkins->LinkUrl('show','zj','reco',$row[0]->CS_CID,$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace("[topic:ylink]",$this->CsdjSkins->LinkUrl('show','zj','yue',$row[0]->CS_CID,$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace("[topic:zlink]",$this->CsdjSkins->LinkUrl('show','zj','zhou',$row[0]->CS_CID,$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace("[topic:rlink]",$this->CsdjSkins->LinkUrl('show','zj','ri',$row[0]->CS_CID,$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace("[topic:nlink]",$this->CsdjSkins->LinkUrl('show','zj','new',$row[0]->CS_CID,$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace("[topic:xlink]",$this->CsdjSkins->LinkUrl('show','zj','down',$row[0]->CS_CID,$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace("[topic:slink]",$this->CsdjSkins->LinkUrl('show','zj','fav',$row[0]->CS_CID,$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace("[topic:dlink]",$this->CsdjSkins->LinkUrl('show','zj','ding',$row[0]->CS_CID,$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace("[topic:plink]",$this->CsdjSkins->LinkUrl('show','zj','play',$row[0]->CS_CID,$row[0]->CS_ID),$Mark_Text);

		   $Mark_Text=$this->CsdjSkins->DataTopic($Mark_Text,$Mark_Text,$row[0],'1'); //全部解析
		   $Mark_Text.="<script src=".site_url('zj/hits/id/'.$id)."></script>";//增加人气
           $Mark_Text=$this->CsdjSkins->skins($Mark_Text,$data,true);
		   if (defined('IS_ADMIN')) {
		       $Mark_Text=str_replace(SELF,"index.php",$Mark_Text);
		   }
		   return $Mark_Text;
	}
}

