<?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('CsdjAdmin');
                $this->load->helper('file');
                $this->CsdjAdmin->Admin_Login();
	}

	public function index()
	{
 	    $page = $this->input->get('page');
            if(empty($page)) $page=1;

	    $sql_string = "SELECT * FROM ".CS_SqlPrefix."news where cs_hid=0 order by CS_AddTime desc";
	    $query = $this->db->query($sql_string); 
	    $total = $query->num_rows();

            $base_url = site_url('admin/news/index/')."?";
	    $per_page = 20; 
            $totalPages = ceil($total / $per_page); // 总页数
	    $data['nums'] = $total;
            if($total<$per_page){
               $per_page=$total;
            }
            $sql_string.=' limit '. $per_page*($page-1) .','. $per_page;
	    $query = $this->db->query($sql_string);

	    $data['news'] = $query->result();
	    $data['pages'] = $this->CsdjAdmin->get_page($base_url,$totalPages,$page,10); //获取分页类

            $this->load->view('admin/news',$data);
        }

	public function huis()
	{
 	    $page = $this->input->get('page');
            if(empty($page)) $page=1;

	    $sql_string = "SELECT * FROM ".CS_SqlPrefix."news where cs_hid=1 order by CS_AddTime desc";
	    $query = $this->db->query($sql_string); 
	    $total = $query->num_rows();

            $base_url = site_url('admin/news/huis/')."?";
	    $per_page = 20; 
            $totalPages = ceil($total / $per_page); // 总页数
	    $data['nums'] = $total;
            if($total<$per_page){
               $per_page=$total;
            }
            $sql_string.=' limit '. $per_page*($page-1) .','. $per_page;
	    $query = $this->db->query($sql_string);

	    $data['news'] = $query->result();
	    $data['pages'] = $this->CsdjAdmin->get_page($base_url,$totalPages,$page,10); //获取分页类

            $this->load->view('admin/news',$data);
        }

	public function so()
	{
	    $key = $this->input->get('key');
	    $user = $this->input->get('user');
	    $cid = $this->input->get('cid');
	    $page = $this->input->get('page');
            if(empty($page)) $page=1;
	    $sql_string = "SELECT * FROM ".CS_SqlPrefix."news where 1=1";
            if($key){
	       $sql_string.= " and CS_Name like '%".$key."%'";
            }
            if($user){
	       $sql_string.= " and CS_User like '%".$user."%'";
            }
            if($cid){
                  if($cid=="-1"){
	               $sql_string.= " and cs_hid=1";
                  }elseif($cid=="-2"){
	               $sql_string.= " and cs_yid=1";
                  }else{
	               $sql_string.= " and CS_CID=".$cid."";
                  }
            }
	    $sql_string.= " order by CS_AddTime desc";
	    $query = $this->db->query($sql_string); 
	    $total = $query->num_rows();

            $base_url = site_url('admin/news/so/')."?key=".$key."&user=".$user."&cid=".$cid."";
	    $per_page = 20; 
            $totalPages = ceil($total / $per_page); // 总页数
	    $data['nums'] = $total;
            if($total<$per_page){
               $per_page=$total;
            }
	    $query = $this->db->query($sql_string .' limit '. $per_page*($page-1) .','. $per_page);
	    $data['news'] = $query->result();
	    $data['pages'] = $this->CsdjAdmin->get_page($base_url,$totalPages,$page,10); //获取分页类

            $this->load->view('admin/news',$data);
        }

	public function lists()
	{
            $this->load->view('admin/nlist');
        }

	public function topic()
	{
 	    $page = $this->input->get('page');
            if(empty($page)) $page=1;

	    $sql_string = "SELECT * FROM ".CS_SqlPrefix."topic where cs_sid=2 order by CS_AddTime desc";
	    $query = $this->db->query($sql_string); 
	    $total = $query->num_rows();

            $base_url = site_url('admin/news/topic/')."?";
	    $per_page = 20; 
            $totalPages = ceil($total / $per_page); // 总页数
	    $data['nums'] = $total;
            if($total<$per_page){
               $per_page=$total;
            }
            $sql_string.=' limit '. $per_page*($page-1) .','. $per_page;
	    $query = $this->db->query($sql_string);

	    $data['topic'] = $query->result();
	    $data['pages'] = $this->CsdjAdmin->get_page($base_url,$totalPages,$page,10); //获取分页类

            $this->load->view('admin/news_zj',$data);
        }

	public function topicadd()
	{
            $data['title']="新增";
            $data['CS_ID']=0;
            $data['CS_Name']="";
            $data['CS_Pic']="";
            $data['CS_CID']=0;
            $data['CS_AliasName']="";
            $data['CS_Template']="topic-news-show.html";
            $data['CS_Content']="";
            $data['CS_Fxgs']="";
            $data['CS_Yuyan']="国语";
            $data['CS_Year']=date('Y');
            $data['CS_User']="admin";
            $data['CS_Title']="";
            $data['CS_Keywords']="";
            $data['CS_Description']="";
            $this->load->view('admin/news_zj_edit',$data);
        }

	public function topicedit()
	{
	    $id = $this->uri->segment(4);
            if(empty($id)) $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，ID为空！</font>',site_url('admin/news/lists/'));
            $data['title']="修改";
            $sql="SELECT * FROM ".CS_SqlPrefix."topic where cs_id=".$id."";
	    $row=$this->CsdjDB->get_all($sql);
	    if($row){
                 $data['CS_ID']=$row[0]->CS_ID;
                 $data['CS_Name']=$row[0]->CS_Name;
                 $data['CS_CID']=$row[0]->CS_CID;
                 $data['CS_AliasName']=$row[0]->CS_AliasName;
                 $data['CS_Template']=$row[0]->CS_Template;
                 $data['CS_Title']=$row[0]->CS_Title;
                 $data['CS_Pic']=$row[0]->CS_Pic;
                 $data['CS_Fxgs']=$row[0]->CS_Fxgs;
                 $data['CS_Yuyan']=$row[0]->CS_Yuyan;
                 $data['CS_Year']=$row[0]->CS_Year;
                 $data['CS_User']=$row[0]->CS_User;
                 $data['CS_Content']=$row[0]->CS_Content;
                 $data['CS_Keywords']=$row[0]->CS_Keywords;
                 $data['CS_Description']=$row[0]->CS_Description;
            }
            $this->load->view('admin/news_zj_edit',$data);
        }

	public function topicsave()
	{
            $this->CsdjAdmin->Admin_Qx('15');
            $ids=$this->input->post('CS_ID', TRUE);
            $data['CS_Name']=$this->input->post('CS_Name', TRUE);
            $data['CS_CID']=$this->input->post('CS_CID', TRUE);
            $data['CS_Pic']=$this->input->post('CS_Pic', TRUE);
            $data['CS_AliasName']=$this->input->post('CS_AliasName', TRUE);
            $data['CS_Year']=$this->input->post('CS_Year', TRUE);
            $data['CS_Fxgs']=$this->input->post('CS_Fxgs', TRUE);
            $data['CS_Yuyan']=$this->input->post('CS_Yuyan', TRUE);
            $data['CS_User']=$this->input->post('CS_User', TRUE);
            $data['CS_Content']=$this->input->post('CS_Content');
            $data['CS_Template']=$this->input->post('CS_Template', TRUE);
            $data['CS_Title']=$this->input->post('CS_Title', TRUE);
            $data['CS_Keywords']=$this->input->post('CS_Keywords', TRUE);
            $data['CS_Description']=$this->input->post('CS_Description', TRUE);
            $data['CS_SID']=2;

            if(empty($data['CS_Template'])) $data['CS_Template']="topic-news-show.html";
            if(empty($data['CS_Name']) || empty($data['CS_AliasName']) || empty($data['CS_User'])) $this->CsdjAdmin->Admin_Url('<font color=red>分类名称、英文别名、会员、不能为空！</font>','javascript:history.back();');
            if(!ctype_alpha($data['CS_AliasName'])) $this->CsdjAdmin->Admin_Url('<font color=red>英文别名只能是字母！</font>','javascript:history.back();');


            $sql="SELECT * FROM ".CS_SqlPrefix."user where cs_name='".$data['CS_User']."'";
	    $row=$this->CsdjDB->get_all($sql); 
	    if(!$row){
                $this->CsdjAdmin->Admin_Url('<font color=red>抱歉，该会员不存在！</font>','javascript:history.back();');
            }


            if($ids==0 || empty($ids)){
                 $data['CS_AddTime']=date('Y-m-d H:i:s');
                 $this->CsdjDB->get_insert('topic',$data);
            }else{
                 if($this->input->post('CS_Addtime')==1) $data['CS_AddTime']=date('Y-m-d H:i:s');
                 $this->CsdjDB->get_update('topic',$ids,$data);
            }
            $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，操作成功！</font>',site_url('admin/news/topic/'),'ok');
        }

	public function topicdel()
	{
            $this->CsdjAdmin->Admin_Qx('15');
	    $id = $this->uri->segment(4);
            if(empty($id)) $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，ID为空！</font>',site_url('admin/news/topic/'));

            IF($this->CsdjSkins->gettopiccount($id,1)>0){
                 $this->CsdjAdmin->Admin_Url('<font color=red>该专集下有文章数据，请先移除文章！</font>',site_url('admin/news/topic/'));
            }

            //-------------------------------删除文件---------------------------------------//
            if(Web_Mode==3){
                 $fileurl=$this->CsdjSkins->LinkUrl('show','zj','id',2,$id);
                 $fileurl=$this->CsdjAdmin->gethtmls($fileurl); //补全HTML地址
	         $fileurl=".".substr($fileurl,strlen(Web_Path));
	         if(file_exists($fileurl)){
		     @unlink($fileurl);
	         }
            }

            //--------------------------------删除图片----------------------------------------//
	    $row=$this->CsdjDB->get_select ('topic','CS_ID','*',''.$id.'');
	    if($row){
		     $pic="./attachment/topic/".$row[0]->CS_Pic;
                     if(FTP_Fun==1){ //远程附件
                            $this->load->library('ftp');
                            $FTPUP['hostname'] = FTP_Server;
                            $FTPUP['port']     = FTP_Port;
                            $FTPUP['username'] = FTP_Name;
                            $FTPUP['password'] = FTP_Pass;
                            //$FTPUP['debug']    = TRUE;
                            $FTPUP['passive']  = FTP_Ive;

                            $Dirs=FTP_Dir;
                            if(substr($Dirs,0,1)=='/') $Dirs=substr($Dirs,1);
                            if(substr($Dirs,-1)=='/') $Dirs=substr($Dirs,0,-1);
                            if(!empty($Dirs)) $Dirs.='/';
		            $pic="/".$Dirs."attachment/topic/".$row[0]->CS_Pic;

                            $this->ftp->connect($FTPUP);
                            $this->ftp->delete_file($pic);
                     }else{
	                 if(file_exists($pic)){
		            @unlink($pic);
	                 }
                     }
	    }

            $this->db->query("delete from ".CS_SqlPrefix."pl where CS_SID=2 and CS_DID in (".$id.")");  //评论
            $this->db->query("delete from ".CS_SqlPrefix."dt where CS_CID=4 and CS_DID in (".$id.")");  //动态
            $this->CsdjDB->get_del('topic',$id);
            $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，删除成功！</font>',site_url('admin/news/topic/'),'ok');
        }

	public function topicdelall()
	{
            $this->CsdjAdmin->Admin_Qx('15');
	    $ids=$this->input->post('CS_ID');
            $id=implode(',', $ids);
            if(empty($id)) $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，ID为空！</font>','javascript:history.back();');

	    $Arr=explode(',',$id);
            for($i=0;$i<count($Arr);$i++){

                  //-------------------------------删除文件---------------------------------------//
                  if(Web_Mode==3){
                       $fileurl=$this->CsdjSkins->LinkUrl('show','zj','id',2,$Arr[$i]);
                       $fileurl=$this->CsdjAdmin->gethtmls($fileurl); //补全HTML地址
	               $fileurl=".".substr($fileurl,strlen(Web_Path));
	               if(file_exists($fileurl)){
		           @unlink($fileurl);
	               }
                  }

                 //--------------------------------删除图片----------------------------------------//
	         $row=$this->CsdjDB->get_select ('topic','CS_ID','*',''.$Arr[$i].'');
	         if($row){
		     $pic="./attachment/topic/".$row[0]->CS_Pic;
                     if(FTP_Fun==1){ //远程附件
                            $this->load->library('ftp');
                            $FTPUP['hostname'] = FTP_Server;
                            $FTPUP['port']     = FTP_Port;
                            $FTPUP['username'] = FTP_Name;
                            $FTPUP['password'] = FTP_Pass;
                            //$FTPUP['debug']    = TRUE;
                            $FTPUP['passive']  = FTP_Ive;

                            $Dirs=FTP_Dir;
                            if(substr($Dirs,0,1)=='/') $Dirs=substr($Dirs,1);
                            if(substr($Dirs,-1)=='/') $Dirs=substr($Dirs,0,-1);
                            if(!empty($Dirs)) $Dirs.='/';
		            $pic="/".$Dirs."attachment/topic/".$row[0]->CS_Pic;

                            $this->ftp->connect($FTPUP);
                            $this->ftp->delete_file($pic);
                            $this->ftp->close();
                     }else{
	                 if(file_exists($pic)){
		            @unlink($pic);
	                 }
                     }
	         }
                  $this->db->query("delete from ".CS_SqlPrefix."dt where CS_CID=4 and CS_DID in (".$Arr[$i].")");  //动态
            }

            $this->db->query("delete from ".CS_SqlPrefix."pl where CS_SID=2 and CS_DID in (".$id.")");  //评论
            $this->db->query("delete from ".CS_SqlPrefix."topic where CS_ID in (".$id.")");  //动态
            $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，删除成功！</font>',site_url('admin/news/topic/'),'ok');
        }

	public function listsadd()
	{
            $data['title']="新增";
            $data['CS_ID']=0;
            $data['CS_Name']="";
            $data['CS_FID']=0;
            $data['CS_XID']=50;
            $data['CS_AliasName']="";
            $data['CS_Template']="news-list.html";
            $data['CS_Title']="";
            $data['CS_Keywords']="";
            $data['CS_Description']="";
            $this->load->view('admin/nlist_edit',$data);
        }

	public function listsedit()
	{
	    $id = $this->uri->segment(4);
            if(empty($id)) $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，ID为空！</font>',site_url('admin/news/lists/'));
            $data['title']="修改";
            $sql="SELECT * FROM ".CS_SqlPrefix."class where cs_id=".$id."";
	    $row=$this->CsdjDB->get_all($sql);
	    if($row){
                 $data['CS_ID']=$row[0]->CS_ID;
                 $data['CS_Name']=$row[0]->CS_Name;
                 $data['CS_FID']=$row[0]->CS_FID;
                 $data['CS_XID']=$row[0]->CS_XID;
                 $data['CS_AliasName']=$row[0]->CS_AliasName;
                 $data['CS_Template']=$row[0]->CS_Template;
                 $data['CS_Title']=$row[0]->CS_Title;
                 $data['CS_Keywords']=$row[0]->CS_Keywords;
                 $data['CS_Description']=$row[0]->CS_Description;
            }
            $this->load->view('admin/nlist_edit',$data);
        }

	public function listssave()
	{
            $this->CsdjAdmin->Admin_Qx('14');
            $ids=$this->input->post('ids', TRUE);
            $CS_Name=$this->input->post('CS_Name', TRUE);
            $CS_FID=$this->input->post('CS_FID', TRUE);
            $CS_XID=$this->input->post('CS_XID', TRUE);
            $CS_AliasName=$this->input->post('CS_AliasName', TRUE);
            $CS_Template=$this->input->post('CS_Template', TRUE);
            $CS_Title=$this->input->post('CS_Title', TRUE);
            $CS_Keywords=$this->input->post('CS_Keywords', TRUE);
            $CS_Description=$this->input->post('CS_Description', TRUE);

            if(empty($CS_XID)) $CS_XID=50;
            if(empty($CS_Template)) $CS_Template="news-list.html";

            if(empty($CS_Name) || empty($CS_AliasName)) $this->CsdjAdmin->Admin_Url('<font color=red>分类名称、英文别名、不能为空！</font>','javascript:history.back();');
            if(!ctype_alpha($CS_AliasName)) $this->CsdjAdmin->Admin_Url('<font color=red>英文别名只能是字母！</font>','javascript:history.back();');

            $data['CS_Name']=$CS_Name;
            $data['CS_SID']=2;
            $data['CS_FID']=$CS_FID;
            $data['CS_XID']=$CS_XID;
            $data['CS_AliasName']=$CS_AliasName;
            $data['CS_Template']=$CS_Template;
            $data['CS_Title']=$CS_Title;
            $data['CS_Keywords']=$CS_Keywords;
            $data['CS_Description']=$CS_Description;

            if($ids==0 || empty($ids)){
                 $this->CsdjDB->get_insert('class',$data);
            }else{
                 $this->CsdjDB->get_update('class',$ids,$data);
            }
            $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，操作成功！</font>',site_url('admin/news/lists/'),'ok');
        }

	public function listspledit()
	{
            $this->CsdjAdmin->Admin_Qx('14');
            $ids=$this->input->post('CS_ID', TRUE);
            $id = implode(',', $ids);

            $arr=explode(",",$id);

            for($j=0;$j<count($arr);$j++){
                  $data['CS_Name']=$this->input->post('CS_Name'.$arr[$j].'', TRUE);
                  $data['CS_YID']=$this->input->post('CS_YID'.$arr[$j].'', TRUE);
                  $data['CS_XID']=$this->input->post('CS_XID'.$arr[$j].'', TRUE);
                  $data['CS_AliasName']=$this->input->post('CS_AliasName'.$arr[$j].'', TRUE);
                  if(empty($data['CS_XID'])) $data['CS_XID']=50;

                  if(!empty($data['CS_Name']) && !empty($data['CS_AliasName'])){
                         $this->CsdjDB->get_update('class',$arr[$j],$data);
                  }
            }
            $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，操作成功！</font>',site_url('admin/news/lists/'),'ok');
        }

	public function listsdel()
	{
            $this->CsdjAdmin->Admin_Qx('14');
	    $id = $this->uri->segment(4);
            if(empty($id)) $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，ID为空！</font>',site_url('admin/news/lists/'));

            $sql="SELECT * FROM ".CS_SqlPrefix."class where cs_fid=".$id."";
	    $row=$this->CsdjDB->get_all($sql);
	    if($row){
                 $this->CsdjAdmin->Admin_Url('<font color=red>该分类下二级分类，请先移除二级分类！</font>',site_url('admin/news/lists/'));
            }

            IF($this->CsdjSkins->getclasscount($id,2)>0){
                 $this->CsdjAdmin->Admin_Url('<font color=red>该分类下有文章数据，请先移除文章！</font>',site_url('admin/news/lists/'));
            }

            $this->CsdjDB->get_del('class',$id);
            $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，删除成功！</font>',site_url('admin/news/lists/'),'ok');
        }


	public function add()
	{
            $data['title']="新增";
            $data['CS_ID']=0;
            $data['CS_CID']=0;
            $data['CS_Color']="";
            $data['CS_TID']=0;
            $data['CS_Pic']="";
            $data['CS_Reco']=0;
            $data['CS_Name']="";
            $data['CS_Tags']="";
            $data['CS_User']="admin";
            $data['CS_Vip']=0;
            $data['CS_Laiyuan']="本站";
            $data['CS_Content']="";
            $data['CS_Hits']=0;
            $data['CS_Yhits']=0;
            $data['CS_Zhits']=0;
            $data['CS_Template']="news-show.html";
            $data['CS_Title']="";
            $data['CS_Keywords']="";
            $data['CS_Description']="";
            $this->load->view('admin/news_edit',$data);
        }

	public function edit()
	{
            $id = $this->security->xss_clean($this->uri->segment(4));   //ID
            if(empty($id)) $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，ID为空！</font>','javascript:history.back();');

	    $row=$this->CsdjDB->get_select ('news','CS_ID','*',''.$id.'');
	    if(!$row){
		 $this->CsdjAdmin->Admin_Url('<font color=red>抱歉，改ID不存在！</font>','javascript:history.back();');
	    }

            $data['title']="修改";
            $data['CS_ID']=$id;
            $data['CS_CID']=$row[0]->CS_CID;
            $data['CS_Pic']=$row[0]->CS_Pic;
            $data['CS_Color']=$row[0]->CS_Color;
            $data['CS_TID']=$row[0]->CS_TID;
            $data['CS_Reco']=$row[0]->CS_Reco;
            $data['CS_Name']=$row[0]->CS_Name;
            $data['CS_Tags']=$row[0]->CS_Tags;
            $data['CS_User']=$row[0]->CS_User;
            $data['CS_Vip']=$row[0]->CS_Vip;
            $data['CS_Laiyuan']=$row[0]->CS_Laiyuan;
            $data['CS_Content']=$row[0]->CS_Content;
            $data['CS_Hits']=$row[0]->CS_Hits;
            $data['CS_Yhits']=$row[0]->CS_Yhits;
            $data['CS_Zhits']=$row[0]->CS_Zhits;
            $data['CS_Template']=$row[0]->CS_Template;
            $data['CS_Title']=$row[0]->CS_Title;
            $data['CS_Keywords']=$row[0]->CS_Keywords;
            $data['CS_Description']=$row[0]->CS_Description;
            $this->load->view('admin/news_edit',$data);
        }


	public function save()
	{
            $this->CsdjAdmin->Admin_Qx('14');
            $CS_ID=$this->input->post('CS_ID');

		$data['CS_Color']=$this->input->post('CS_Color');
		$data['CS_CID']=$this->input->post('CS_CID');
		$data['CS_Tags']=$this->input->post('CS_Tags');
		$data['CS_Name']=$this->input->post('CS_Name');
		$data['CS_TID']=$this->input->post('CS_TID');
		$data['CS_User']=$this->input->post('CS_User');
		$data['CS_Reco']=$this->input->post('CS_Reco');
		$data['CS_Vip']=$this->input->post('CS_Vip');
		$data['CS_Hits']=$this->input->post('CS_Hits');
		$data['CS_Yhits']=$this->input->post('CS_Yhits');
		$data['CS_Zhits']=$this->input->post('CS_Zhits');
		$data['CS_Template']=$this->input->post('CS_Template');
		$data['CS_Title']=$this->input->post('CS_Title');
		$data['CS_Pic']=$this->input->post('CS_Pic');
                $data['CS_Content']=$this->input->post('CS_Content');
		$data['CS_Keywords']=$this->input->post('CS_Keywords');
		$data['CS_Description']=$this->input->post('CS_Description');

                if(empty($data['CS_Name'])||empty($data['CS_User'])||empty($data['CS_Content'])){
                        $this->CsdjAdmin->Admin_Url('<font color=red>名称、会员、内容、不能为空！</font>','javascript:history.back();');
                }

                 $sql="SELECT * FROM ".CS_SqlPrefix."user where cs_name='".$data['CS_User']."'";
	         $row=$this->CsdjDB->get_all($sql); 
	         if(!$row){
                     $this->CsdjAdmin->Admin_Url('<font color=red>抱歉，该会员不存在！</font>','javascript:history.back();');
                 }

                if(empty($data['CS_Hits'])) $data['CS_Hits']=0;
                if(empty($data['CS_Yhits'])) $data['CS_Yhits']=0;
                if(empty($data['CS_Zhits'])) $data['CS_Zhits']=0;
                if(empty($data['CS_Template'])) $data['CS_Template']='news-show.html';

                if($CS_ID==0 || empty($CS_ID)){
                     $data['CS_AddTime']=date('Y-m-d H:i:s');
                     $this->CsdjDB->get_insert('news',$data);
                }else{
                     if($this->input->post('CS_AddTime')==1) $data['CS_AddTime']=date('Y-m-d H:i:s');
                     $this->CsdjDB->get_update('news',$CS_ID,$data);
                }
                $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，操作成功！</font>',site_url('admin/news/index'),'ok');
        }

	public function pledit()
	{
            $id=$this->input->get('id');
            $data['id']=$id;
            $data['sid']=2;
            $data['name']="文章";
            $data['link']=site_url('admin/news/plsave/');
            $this->load->view('admin/pl_edit',$data);
        }

	public function plsave()
	{
            $this->CsdjAdmin->Admin_Qx('14');
            $xid=$this->input->post('xid');
            $CS_CSID=$this->input->post('CS_CSID');
            $CS_ID=$this->input->post('CS_ID');

		$CS_Color=$this->input->post('CS_Color');
		$CS_CID=$this->input->post('CS_CID');
		$CS_HID=$this->input->post('CS_HID');
		$CS_CIDS=$this->input->post('CS_CIDS');
		$CS_TID=$this->input->post('CS_TID');
		$CS_YID=$this->input->post('CS_YID');
		$CS_User=$this->input->post('CS_User');
		$CS_Reco=$this->input->post('CS_Reco');
		$CS_Vip=$this->input->post('CS_Vip');
		$CS_Hits=$this->input->post('CS_Hits');
		$CS_Yhits=$this->input->post('CS_Yhits');
		$CS_Zhits=$this->input->post('CS_Zhits');
		$CS_Rhits=$this->input->post('CS_Rhits');
		$CS_Template=$this->input->post('CS_Template');
                if(!empty($CS_CSID)){
                    $CS_CSID=implode(',', $CS_CSID);
                }

 		$Arr=explode(',',$CS_CSID);
                switch($xid){
	                 case '1':
		              if($CS_ID==""){
			         $this->CsdjAdmin->Admin_Url('<font color=red>抱歉,请填写要修改的文章ID!</font>','javascript:history.back();');
		              }

                              for($i=0;$i<count($Arr);$i++){
                                     if($Arr[$i]=="CS_Color"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_Color='".$CS_Color."' where CS_ID in (".$CS_ID.")");
                                     }elseif($Arr[$i]=="CS_CID"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_CID=".$CS_CID." where CS_ID in (".$CS_ID.")");
                                     }elseif($Arr[$i]=="CS_YID"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_YID=".$CS_YID." where CS_ID in (".$CS_ID.")");
                                     }elseif($Arr[$i]=="CS_TID"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_TID=".$CS_TID." where CS_ID in (".$CS_ID.")");
                                     }elseif($Arr[$i]=="CS_Reco"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_Reco=".$CS_Reco." where CS_ID in (".$CS_ID.")");
                                     }elseif($Arr[$i]=="CS_User"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_User='".$CS_User."' where CS_ID in (".$CS_ID.")");
                                     }elseif($Arr[$i]=="CS_Vip"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_Vip=".$CS_Vip." where CS_ID in (".$CS_ID.")");
                                     }elseif($Arr[$i]=="CS_Hits"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_Hits=".$CS_Hits." where CS_ID in (".$CS_ID.")");
                                     }elseif($Arr[$i]=="CS_Yhits"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_Yhits=".$CS_Yhits." where CS_ID in (".$CS_ID.")");
                                     }elseif($Arr[$i]=="CS_Zhits"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_Zhits=".$CS_Zhits." where CS_ID in (".$CS_ID.")");
                                     }elseif($Arr[$i]=="CS_Rhits"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_Rhits=".$CS_Rhits." where CS_ID in (".$CS_ID.")");
                                     }elseif($Arr[$i]=="CS_Template"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_Template='".$CS_Template."' where CS_ID in (".$CS_ID.")");
                                     }elseif($Arr[$i]=="CS_HID"){
                                           if($CS_HID==2){
                                                  $this->db->query("delete from ".CS_SqlPrefix."news where CS_ID in (".$CS_ID.")");
                                           }else{
                                                  $this->db->query("update ".CS_SqlPrefix."news set CS_HID=".$CS_HID." where CS_ID in (".$CS_ID.")");
                                           }
                                     }
                              }
                         break;
	                 case '2':
		              if($CS_CIDS=="0"){
			         $this->CsdjAdmin->Admin_Url('<font color=red>抱歉,选择要操作的分类!</font>','javascript:history.back();');
		              }
                              for($i=0;$i<count($Arr);$i++){
                                     if($Arr[$i]=="CS_Color"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_Color='".$CS_Color."' where CS_CID in (".$CS_CIDS.")");
                                     }elseif($Arr[$i]=="CS_CID"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_CID=".$CS_CID." where CS_CID in (".$CS_CIDS.")");
                                     }elseif($Arr[$i]=="CS_YID"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_YID=".$CS_YID." where CS_CID in (".$CS_CIDS.")");
                                     }elseif($Arr[$i]=="CS_TID"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_TID=".$CS_TID." where CS_CID in (".$CS_CIDS.")");
                                     }elseif($Arr[$i]=="CS_Reco"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_Reco=".$CS_Reco." where CS_CID in (".$CS_CIDS.")");
                                     }elseif($Arr[$i]=="CS_User"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_User='".$CS_User."' where CS_CID in (".$CS_CIDS.")");
                                     }elseif($Arr[$i]=="CS_Vip"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_Vip=".$CS_Vip." where CS_CID in (".$CS_CIDS.")");
                                     }elseif($Arr[$i]=="CS_Hits"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_Hits=".$CS_Hits." where CS_CID in (".$CS_CIDS.")");
                                     }elseif($Arr[$i]=="CS_Yhits"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_Yhits=".$CS_Yhits." where CS_CID in (".$CS_CIDS.")");
                                     }elseif($Arr[$i]=="CS_Zhits"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_Zhits=".$CS_Zhits." where CS_CID in (".$CS_CIDS.")");
                                     }elseif($Arr[$i]=="CS_Rhits"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_Rhits=".$CS_Rhits." where CS_CID in (".$CS_CIDS.")");
                                     }elseif($Arr[$i]=="CS_Template"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_Template='".$CS_Template."' where CS_CID in (".$CS_CIDS.")");
                                     }elseif($Arr[$i]=="CS_HID"){
                                           if($CS_HID==2){
                                                  $this->db->query("delete from ".CS_SqlPrefix."news where CS_CID in (".$CS_CIDS.")");
                                           }else{
                                                  $this->db->query("update ".CS_SqlPrefix."news set CS_HID=".$CS_HID." where CS_CID in (".$CS_CIDS.")");
                                           }
                                     }
                              }
                         break;
	                 case '3':
		              if($CS_FIDS=="0"){
			         $this->CsdjAdmin->Admin_Url('<font color=red>抱歉,请填写要操作的服务器组!</font>','javascript:history.back();');
		              }
                              for($i=0;$i<count($Arr);$i++){
                                     if($Arr[$i]=="CS_Color"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_Color='".$CS_Color."' where CS_FID in (".$CS_FIDS.")");
                                     }elseif($Arr[$i]=="CS_CID"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_CID=".$CS_CID." where CS_FID in (".$CS_FIDS.")");
                                     }elseif($Arr[$i]=="CS_YID"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_YID=".$CS_YID." where CS_FID in (".$CS_FIDS.")");
                                     }elseif($Arr[$i]=="CS_TID"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_TID=".$CS_TID." where CS_FID in (".$CS_FIDS.")");
                                     }elseif($Arr[$i]=="CS_Reco"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_Reco=".$CS_Reco." where CS_FID in (".$CS_FIDS.")");
                                     }elseif($Arr[$i]=="CS_User"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_User='".$CS_User."' where CS_FID in (".$CS_FIDS.")");
                                     }elseif($Arr[$i]=="CS_Vip"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_Vip=".$CS_Vip." where CS_FID in (".$CS_FIDS.")");
                                     }elseif($Arr[$i]=="CS_Hits"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_Hits=".$CS_Hits." where CS_FID in (".$CS_FIDS.")");
                                     }elseif($Arr[$i]=="CS_Yhits"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_Yhits=".$CS_Yhits." where CS_FID in (".$CS_FIDS.")");
                                     }elseif($Arr[$i]=="CS_Zhits"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_Zhits=".$CS_Zhits." where CS_FID in (".$CS_FIDS.")");
                                     }elseif($Arr[$i]=="CS_Rhits"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_Rhits=".$CS_Rhits." where CS_FID in (".$CS_FIDS.")");
                                     }elseif($Arr[$i]=="CS_Template"){
                                         $this->db->query("update ".CS_SqlPrefix."news set CS_Template='".$CS_Template."' where CS_FID in (".$CS_FIDS.")");
                                     }elseif($Arr[$i]=="CS_HID"){
                                           if($CS_HID==2){
                                                  $this->db->query("delete from ".CS_SqlPrefix."news where CS_FID in (".$CS_FIDS.")");
                                           }else{
                                                  $this->db->query("update ".CS_SqlPrefix."news set CS_HID=".$CS_HID." where CS_FID in (".$CS_FIDS.")");
                                           }
                                     }
                              }
                         break;
                }
                 echo '<script>
                        alert("恭喜您，操作成功！");
                        parent.document.location.reload();
                 </script>';
        }

	public function del()
	{
            $this->CsdjAdmin->Admin_Qx('14');
            $id = $this->security->xss_clean($this->uri->segment(4));   //ID
            if(empty($id)) $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，ID为空！</font>','javascript:history.back();');

            //-------------------------------删除文章文件---------------------------------------//
            if(Web_Mode==3){
                 $fileurl=$this->CsdjSkins->LinkUrl('show','news','id',1,$id);
                 $fileurl=$this->CsdjAdmin->gethtmls($fileurl); //补全HTML地址
	         $fileurl=".".substr($fileurl,strlen(Web_Path));
	         if(file_exists($fileurl)){
		     @unlink($fileurl);
	         }
            }
            //------------------------------删除文章图片--------------------------------------//
	    $row=$this->CsdjDB->get_select ('news','CS_ID','CS_YID,CS_Pic,CS_User,CS_Name',''.$id.'');
	    if($row){
		     $pic="/attachment/news/".$row[0]->CS_Pic;
                     if(FTP_Fun==1){ //远程附件
                            $this->load->library('ftp');
                            $FTPUP['hostname'] = FTP_Server;
                            $FTPUP['port']     = FTP_Port;
                            $FTPUP['username'] = FTP_Name;
                            $FTPUP['password'] = FTP_Pass;
                            //$FTPUP['debug']    = TRUE;
                            $FTPUP['passive']  = FTP_Ive;

                            $Dirs=FTP_Dir;
                            if(substr($Dirs,0,1)=='/') $Dirs=substr($Dirs,1);
                            if(substr($Dirs,-1)=='/') $Dirs=substr($Dirs,0,-1);
                            if(!empty($Dirs)) $Dirs.='/';
		            $pic="/".$Dirs."attachment/news/".$row[0]->CS_Pic;

                            $this->ftp->connect($FTPUP);
                            $this->ftp->delete_file(".".$pic);
                            $this->ftp->close();
                     }else{
	                 if(file_exists(".".$pic)){
		            @unlink(".".$pic);
	                 }
                     }
	    }

            $this->db->query("delete from ".CS_SqlPrefix."pl where CS_SID=2 and CS_DID in (".$id.")");  //评论
            $this->db->query("delete from ".CS_SqlPrefix."dt where CS_CID=3 and CS_DID in (".$id.")");  //动态
            $this->db->query("delete from ".CS_SqlPrefix."news where CS_ID in (".$id.")");

                 if(Cion_DelNews>0 && $row[0]->CS_YID==0){
                     //减去金币
                     $this->db->query("update ".CS_SqlPrefix."user set CS_Cion=CS_Cion-".Cion_DelNews." where cs_name='".$row[0]->CS_User."' and CS_Cion>0");
                     //发送消息
                     $msg['CS_Name']='文章删除消息';
                     $msg['CS_Neir']=$row[0]->CS_User.'，您好，您的文章《'.$row[0]->CS_Name.'》被管理员删除，同时扣除您'.Cion_DelNews.'个金币，如有问题，请联系管理员。';
                     $msg['CS_Usera']=$row[0]->CS_User;
                     $msg['CS_Userb']='管理员';
                     $msg['CS_AddTime']=date('Y-m-d h:i:s');
                     $this->CsdjDB->get_insert('xiaoxi',$msg);
                 }

            $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，删除成功！</font>',$_SERVER['HTTP_REFERER'],'ok');
        }

	public function pldel()
	{
            $this->CsdjAdmin->Admin_Qx('14');
	    $ids=$this->input->post('CS_ID');
            if(empty($ids)) $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，ID为空！</font>','javascript:history.back();');
            $id=implode(',', $ids);

	    $Arr=explode(',',$id);
            for($i=0;$i<count($Arr);$i++){
                 //-------------------------------删除文章文件---------------------------------------//
                 if(Web_Mode==3){
                      $fileurl=$this->CsdjSkins->LinkUrl('show','news','id',1,$Arr[$i]);
                      $fileurl=$this->CsdjAdmin->gethtmls($fileurl); //补全HTML地址
	              $fileurl=".".substr($fileurl,strlen(Web_Path));
	              if(file_exists($fileurl)){
		          @unlink($fileurl);
	              }
                 }
                 //------------------------------删除文章图片--------------------------------------//
	         $row=$this->CsdjDB->get_select ('news','CS_ID','CS_YID,CS_Pic,CS_User,CS_Name',''.$Arr[$i].'');
	         if($row){
		     $pic="/attachment/news/".$row[0]->CS_Pic;
                     if(FTP_Fun==1){ //远程附件
                            $this->load->library('ftp');
                            $FTPUP['hostname'] = FTP_Server;
                            $FTPUP['port']     = FTP_Port;
                            $FTPUP['username'] = FTP_Name;
                            $FTPUP['password'] = FTP_Pass;
                            //$FTPUP['debug']    = TRUE;
                            $FTPUP['passive']  = FTP_Ive;

                            $Dirs=FTP_Dir;
                            if(substr($Dirs,0,1)=='/') $Dirs=substr($Dirs,1);
                            if(substr($Dirs,-1)=='/') $Dirs=substr($Dirs,0,-1);
                            if(!empty($Dirs)) $Dirs.='/';
		            $pic="/".$Dirs."attachment/news/".$row[0]->CS_Pic;

                            $this->ftp->connect($FTPUP);
                            $this->ftp->delete_file(".".$pic);
                            $this->ftp->close();
                     }else{
	                 if(file_exists(".".$pic)){
		            @unlink(".".$pic);
	                 }
                     }
	         }
                 if(Cion_DelNews>0 && $row[0]->CS_YID==0){
                     //减去金币
                     $this->db->query("update ".CS_SqlPrefix."user set CS_Cion=CS_Cion-".Cion_DelNews." where cs_name='".$row[0]->CS_User."' and CS_Cion>0");
                     //发送消息
                     $msg['CS_Name']='文章删除消息';
                     $msg['CS_Neir']=$row[0]->CS_User.'，您好，您的文章《'.$row[0]->CS_Name.'》被管理员删除，同时扣除您'.Cion_DelNews.'个金币，如有问题，请联系管理员。';
                     $msg['CS_Usera']=$row[0]->CS_User;
                     $msg['CS_Userb']='管理员';
                     $msg['CS_AddTime']=date('Y-m-d h:i:s');
                     $this->CsdjDB->get_insert('xiaoxi',$msg);
                 }
            }

            $this->db->query("delete from ".CS_SqlPrefix."pl where CS_SID=2 and CS_DID in (".$id.")");  //评论
            $this->db->query("delete from ".CS_SqlPrefix."dt where CS_CID=3 and CS_DID in (".$id.")");  //动态
            $this->db->query("delete from ".CS_SqlPrefix."news where CS_ID in (".$id.")");
            $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，删除成功！</font>',$_SERVER['HTTP_REFERER'],'ok');
        }


	public function html()
	{
            if(Web_Mode!=3) $this->CsdjAdmin->Admin_Url('<font color=red>网站为动态模式，不用生成！</font>','javascript:history.back();'); 
            $this->load->view('admin/news_html');
        }
}

