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

class News 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('CsdjUser');
		$this->CsdjUser->User_Login();
	}

	public function index()
	{
                $data='';
                $yid = intval($this->input->get('yid', TRUE));   //yid，1为分享，2为待审核，3为回收站
                $cid = intval($this->input->get('cid', TRUE));   //cid
                $page = intval($this->input->get('page', TRUE));   //page
                if($page==0) $page=1;


                $this->load->get_user_templates();
		$template=$this->load->view('news-list.html',$data,true);
		$template=$this->CsdjSkins->TopandBottom($template,'user');

		$Mark_Text=str_replace("{cscms:title}","文章 - 会员管理中心",$template);
		$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("[news:cid]",$cid,$Mark_Text); //CID
		$Mark_Text=str_replace("[news:yid]",$yid,$Mark_Text); //YID

		//预先除了分页
		$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="select * from ".CS_SqlPrefix."news where CS_User='".$this->session->userdata('cs_name')."'";
                        if($yid==3){
                             $sqlstr.=" and cs_hid=1";
                        }else{
                             $sqlstr.=" and cs_hid=0";
                        }
                        if($yid==1 || $yid==2){
                            $yid=$yid-1;
                            $sqlstr.=" and cs_yid='$yid'";
                        }
                        if($cid>0){
                            $sqlstr.=" and cs_cid='$cid'";
                        }

                        $sqlstr.=" order by CS_AddTime desc";
                        $data_content='';
			$Arr=$this->CsdjSkins->SpanUsersPage($sqlstr,$page_arr[2][0],$pagenum,'user/news?cid='.$cid.'&yid='.$yid,$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->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,$this->session->userdata('cs_name'));
	        $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
		$Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
                $this->CsdjSkins->skins($Mark_Text,$data);
        }

	public function add()
	{
                $data='';
                $this->load->get_user_templates();
		$template=$this->load->view('news-add.html',$data,true);
		$template=$this->CsdjSkins->TopandBottom($template,'user');

		$Mark_Text=str_replace("{cscms:title}","文章 - 会员管理中心",$template);
		$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("[user:newsaddsave]",site_url('user/news/add_save'),$Mark_Text); //发表连接
        $token=md5(uniqid(rand(), true));
		$this->session->set_userdata('token',$token);
		$Mark_Text=str_replace("[user:token]",$token,$Mark_Text);

                $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
                if($row[0]->CS_Vip==0 && User_NewsNum>0){
		       $Mark_Text=str_replace("[user:newsaddcount]",User_NewsNum,$Mark_Text); //普通会员限制数量
                }else{
		       $Mark_Text=str_replace("[user:newsaddcount]",'不限制',$Mark_Text); //VIP会员限制数量
                }
		$Mark_Text=str_replace("[user:dayaddcount]",$this->CsdjSkins->getusercount($row[0]->CS_Name,'news','day'),$Mark_Text); //会员今日数量
		$Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
		$Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
                $this->CsdjSkins->skins($Mark_Text,$data);
        }

	public function add_save()
	{
                $news['cs_name']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_name', TRUE));   //名称
                $news['cs_cid']=intval($this->input->post('cs_cid', TRUE));   //分类
                $news['cs_tid']=intval($this->input->post('cs_tid', TRUE));   //专题
                $news['cs_tags']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_tags', TRUE));   //关键词
                $news['cs_content']=$this->CsdjSkins->uhtml($this->input->post('cs_content'));   //内容

				//token check
                $token=$this->input->post('token', TRUE);
		

                if(empty($news['cs_name']))  $this->CsdjSkins->Msg_url('文章标题不能为空!','javascript:history.back();'); 
                if($news['cs_cid']==0)  $this->CsdjSkins->Msg_url('请选择文章分类!','javascript:history.back();'); 
                if(empty($news['cs_content']))  $this->CsdjSkins->Msg_url('文章内容不能为空!','javascript:history.back();'); 

                if(empty($news['cs_tid'])) $news['cs_tid']=0;
                $news['CS_User']=$this->session->userdata('cs_name');
                $news['CS_Laiyuan']=Web_Name;
                $news['CS_AddTime']=date('Y-m-d H:i:s');

                //判断审核
                if(User_NewsFun==1){
                    $title='，请等待管理员审核';
                    $news['CS_YID']=1;
                    $url=site_url('user/news').'?yid=2';
                }else{
                    $title='';
                    $news['CS_YID']=0;
                    $url=site_url('user/news');
                }

                //获取当天上传成功数量
                $daycount=$this->CsdjSkins->getusercount($this->session->userdata('cs_name'),'news','day');
                if(User_NewsNum>0 && $daycount >= User_NewsNum){
                     $this->CsdjSkins->Msg_url('今天已经上传了('.$daycount.')编文章,明天在发吧!','javascript:history.back();'); 
                }

                $res=$this->CsdjDB->get_insert('news',$news);
                if($res>0){

                       if($daycount < Cion_Num && Cion_UpNews>0 && User_NewsFun==2){  //奖励金币
                              $title.='，系统奖励给您'.Cion_UpNews.'个金币';
                              $this->db->query("update ".CS_SqlPrefix."user set CS_Cion=CS_Cion+".Cion_UpNews." where CS_ID='".$this->session->userdata('cs_id')."'");
                       }

                       //写入动态
                       $dt['CS_CID']=3;
                       $dt['CS_DID']=$res;
                       $dt['CS_YID']=$news['CS_YID'];
                       $dt['CS_User']=$this->session->userdata('cs_name');
                       $dt['CS_Title']=$news['cs_name'];
                       $dt['CS_AddTime']=date('Y-m-d H:i:s');
                       $this->CsdjDB->get_insert('dt',$dt);
                       $this->session->unset_userdata('token');
                       $this->CsdjSkins->Msg_url('恭喜您，文章发表成功'.$title.'!',$url); 

                }else{
                       $this->CsdjSkins->Msg_url('抱歉，文章发表失败，请联系管理员!','javascript:history.back();'); 
                }
        }

	public function del()
	{
                $id=intval($this->input->get('id', TRUE));   //id
                $hid=intval($this->input->get('hid', TRUE));   //hid 等于1时为还原
                if($id==0) $this->CsdjSkins->Msg_url('抱歉，参数错误，ID为空!','javascript:history.back();'); 
 
                if($hid==1){
                     $this->db->query("update ".CS_SqlPrefix."news set cs_hid=0 where cs_user='".$this->session->userdata('cs_name')."' and cs_id='".$id."'");
                     $this->CsdjSkins->Msg_url('恭喜您，还原成功!',@$_SERVER['HTTP_REFERER']);  
                }else{
                     $this->db->query("update ".CS_SqlPrefix."news set cs_hid=1 where cs_user='".$this->session->userdata('cs_name')."' and cs_id='".$id."'");
                     $this->CsdjSkins->Msg_url('恭喜您，删除成功!',@$_SERVER['HTTP_REFERER']); 
                }
        }
}


