<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Pic extends CI_Controller {

	function __construct(){
		parent::__construct();
        	$this->load->helper('url');
                $this->load->library('session'); //加载session
		$this->load->model('CsdjDB');
		$this->load->model('CsdjAdmin');
		$this->load->model('CsdjSkins');
                $this->db->cache_off();
	        $this->CsdjAdmin->Admin_Login();
	}

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

	    $sql_string = "SELECT * FROM ".CS_SqlPrefix."pic order by CS_ID desc";
	    $query = $this->db->query($sql_string); 
	    $total = $query->num_rows();

            $base_url = site_url('admin/pic/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['pic'] = $query->result();
	    $data['pages'] = $this->CsdjAdmin->get_page($base_url,$totalPages,$page,10); //获取分页类

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

	public function pldel()
	{
            $this->CsdjAdmin->Admin_Qx('28');
	    $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++){
                
	         $row=$this->CsdjDB->get_select ('pic','CS_ID','CS_YID,CS_Pic,CS_User',''.$Arr[$i].'');
	         if($row){
		     $pic="./attachment/pic/".$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/pic/".$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_DelPic>0 && $row[0]->CS_YID==0){
                     //减去金币
                     $this->db->query("update ".CS_SqlPrefix."user set CS_Cion=CS_Cion-".Cion_DelPic." where cs_name='".$row[0]->CS_User."' and CS_Cion>0");
                     //发送消息
                     $msg['CS_Name']='图片删除消息';
                     $msg['CS_Neir']=$row[0]->CS_User.'，您好，您的图片《'.$row[0]->CS_Pic.'》被管理员删除，同时扣除您'.Cion_DelPic.'个金币，如有问题，请联系管理员。';
                     $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."pic where CS_ID in (".$id.")");
            $this->db->query("delete from ".CS_SqlPrefix."dt where cs_cid=5 and CS_DID in (".$id.")");  //动态
            $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，删除成功！</font>',$_SERVER['HTTP_REFERER'],'ok');
        }
}

