<?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 CsdjNews extends CI_Model
{
    function __construct (){
	       parent:: __construct ();
    }

    //主页
    public function index($data='') {
           $this->load->get_templates();
		   $template=$this->load->view('news-index.html',$data,true);
		   $Mark_Text=str_replace("{cscms:title}","文章首页 - ".Web_Name,$template);
           $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 lists($fid,$id,$page,$row,$aliasname='') {
		   $data='';$data_content='';
		   if(empty($row[0]->CS_Template)){
		    $CS_Template='news-list.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;
		   }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=$this->CsdjSkins->TopandBottom($template);
		   $Mark_Text=str_replace("{cscms:title}",$seo_title,$Mark_Text);
		   $Mark_Text=str_replace("{cscms:keywords}",$seo_keywords,$Mark_Text);
		   $Mark_Text=str_replace("{cscms:description}",$seo_description,$Mark_Text);
		   $Mark_Text=str_replace("[news:sort]",$fid,$Mark_Text);
		   $Mark_Text=str_replace("[news:classid]",$row[0]->CS_ID,$Mark_Text);
		   //预先除了分页
		   $pagenum=$this->CsdjSkins->GetPageNum($Mark_Text);
		   preg_match_all('/{cscms:news(.*?pagesize=([\S]+).*?)}([\s\S]+?){\/cscms:news}/',$Mark_Text,$page_arr);//判断是否有分页标识
		   if(!empty($page_arr) && !empty($page_arr[2])){

			   $sqlstr=$this->CsdjSkins->Mark_Sql('news',$page_arr[1][0],$id,$fid);
			   $Arr=$this->CsdjSkins->SpanPage($sqlstr,$page_arr[2][0],$pagenum,'lists','news',$fid,$row[0]->CS_ID,1,$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) {
						   $datatmp=$this->CsdjSkins->DataNews($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=$this->CsdjSkins->Common_Mark($Mark_Text,$id);
		   $Mark_Text=str_replace("[news:classname]",$row[0]->CS_Name,$Mark_Text);
		   $Mark_Text=str_replace("[news:classlink]",$this->CsdjSkins->LinkUrl('lists','news','id',$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace('[news:classnlink]',$this->CsdjSkins->LinkUrl('lists','news','new',$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace('[news:classhlink]',$this->CsdjSkins->LinkUrl('lists','news','hits',$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace('[news:classtlink]',$this->CsdjSkins->LinkUrl('lists','news','reco',$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace('[news:classylink]',$this->CsdjSkins->LinkUrl('lists','news','yue',$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace('[news:classzlink]',$this->CsdjSkins->LinkUrl('lists','news','zhou',$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace('[news:classrlink]',$this->CsdjSkins->LinkUrl('lists','news','ri',$row[0]->CS_ID),$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,$row) {
		   $data='';$data_content='';

           if(empty($row[0]->CS_Template)){
                 $CS_Template='news-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."";
           }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);

           //静态模式,动态人气
           if(Web_Mode==3){
			   $Mark_Text=str_replace('[news:hits]',"<script src=".site_url('news/hits/dt/hits/'.$id)."></script>",$Mark_Text);
			   $Mark_Text=str_replace('[news:rhits]',"<script src=".site_url('news/hits/dt/rhits/'.$id)."></script>",$Mark_Text);
			   $Mark_Text=str_replace('[news:zhits]',"<script src=".site_url('news/hits/dt/zhits/'.$id)."></script>",$Mark_Text);
			   $Mark_Text=str_replace('[news:yhits]',"<script src=".site_url('news/hits/dt/yhits/'.$id)."></script>",$Mark_Text);
           }
		   $Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$id);
		   //上下编开始
           preg_match_all('/news:slink/',$Mark_Text,$arr);
		   if(!empty($arr[0]) && !empty($arr[0][0])){
		      $sqlu="Select CS_ID,CS_Name,CS_CID from ".CS_SqlPrefix."news where CS_YID=0 and CS_HID=0 and CS_ID>".$id." order by cs_id asc limit 1";
		      $rowu=$this->CsdjDB->get_all($sqlu);
		      if($rowu){
		             $Mark_Text=str_replace('[news:sid]',$rowu[0]->CS_ID,$Mark_Text);
		             $Mark_Text=str_replace('[news:sname]',$rowu[0]->CS_Name,$Mark_Text);
		             $Mark_Text=str_replace('[news:slink]',$this->CsdjSkins->LinkUrl('show','news','id',$rowu[0]->CS_CID,$rowu[0]->CS_ID),$Mark_Text);
		      }else{
		             $Mark_Text=str_replace('[news:sid]','0',$Mark_Text);
		             $Mark_Text=str_replace('[news:sname]','没有了',$Mark_Text);
		             $Mark_Text=str_replace('[news:slink]','#',$Mark_Text);
		      }
		   }
           preg_match_all('/news:xlink/',$Mark_Text,$arr);
		   if(!empty($arr[0]) && !empty($arr[0][0])){
		      $sqld="Select CS_ID,CS_Name,CS_CID from ".CS_SqlPrefix."news where CS_YID=0 and CS_HID=0 and CS_ID<".$id." order by cs_id desc limit 1";
		      $rowd=$this->CsdjDB->get_all($sqld);
		      if($rowd){
		             $Mark_Text=str_replace('[news:xid]',$rowd[0]->CS_ID,$Mark_Text);
		             $Mark_Text=str_replace('[news:xname]',$rowd[0]->CS_Name,$Mark_Text);
		             $Mark_Text=str_replace('[news:xlink]',$this->CsdjSkins->LinkUrl('show','news','id',$rowd[0]->CS_CID,$rowd[0]->CS_ID),$Mark_Text);
		      }else{
		             $Mark_Text=str_replace('[news:xid]','0',$Mark_Text);
		             $Mark_Text=str_replace('[news:xname]','没有了',$Mark_Text);
		             $Mark_Text=str_replace('[news:xlink]','#',$Mark_Text);
		      }
		   }
		   $Mark_Text=$this->CsdjSkins->Datanews($Mark_Text,$Mark_Text,$row[0],'1'); //全部解析
		   $Mark_Text.="<script src=".site_url('news/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;
	}
}

