<?php
if ( ! defined('CSCMSPATH')) exit('No direct script access allowed');
class Sflw_Admin_module extends CI_Module {

	function __construct(){
		parent::__construct();
        	$this->load->helper('url');
	        $this->load->database();
	        $this->load->model('CsdjDB'); //加载数据操作模型
                $this->load->model('CsdjAdmin'); //加载后台模型  
	        $this->CsdjAdmin->Admin_Login(); //判断是否登入
			require_once PLUGINDIR.'sflw/conxiu.php'; 
	        $dir = $this->uri->segment(2);
                $sql="SELECT CS_SID FROM ".CS_SqlPrefix."plugins where cs_dir='".$dir."'";
	        $row=$this->CsdjDB->get_all($sql);
	        if($row){
                      if($row[0]->CS_SID==1){
                             $this->CsdjAdmin->Admin_Url('<font color=red>该插件尚未开启！</font>','javascript:history.back();');
                      }
                }else{
                      $this->CsdjAdmin->Admin_Url('<font color=red>该插件尚未安装！</font>','javascript:history.back();');
                }
	}

	public function index()

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

	    $sql_string = "SELECT * FROM ".CS_SqlPrefix."lw_type where 1";
	    $query = $this->db->query($sql_string); 
	    $total = $query->num_rows();

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

            $this->load->view('type',$data);
        
        }
          public function type_edit()
	{
	    $id = $this->uri->segment(5);
            $data['title']="修改";
			if($id!=''){
            $sql="SELECT * FROM ".CS_SqlPrefix."lw_type 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_Cion']=$row[0]->CS_Cion;
                 $data['CS_Lwpic']=$row[0]->CS_Lwpic;
                 $data['CS_Lwtime']=$row[0]->CS_Lwtime;
            }
			
			}else{
			$data['CS_ID']='';
                 $data['CS_Name']='';
                 $data['CS_Cid']='1';
                 $data['CS_Cion']='';
                 $data['CS_Lwpic']='';
                 $data['CS_Lwtime']='';

			}
            $this->load->view('type_edit',$data);
        }
			public function type_save()
	{
            $id=$this->input->post('CS_ID', TRUE);
            $CS_Name=$this->input->post('CS_Name', TRUE);
            $cid=$this->input->post('CS_Cid', TRUE);
            $cion=$this->input->post('CS_Cion', TRUE);
            $pic=$this->input->post('CS_Lwpic', TRUE);
            $time=$this->input->post('CS_Lwtime', TRUE);


            if(empty($CS_Name)||empty($pic)) $this->CsdjAdmin->Admin_Url('<font color=red>礼物名字、图片不能为空！</font>','javascript:history.back();');

            $data['CS_Name']=$CS_Name;
      
            $data['CS_Cid']=$cid;
            $data['CS_Cion']=$cion;
            $data['CS_Lwpic']=$pic;
			if($time=='1'){
            $data['CS_Lwtime']=date("Y-m-d H:i:s");
           }
            if($id==0 || empty($id)){
                 $this->CsdjDB->get_insert('lw_type',$data);
            }else{
                 $this->CsdjDB->get_update('lw_type',$id,$data);
            }
            $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，操作成功！</font>',site_url('m/sflw/admin/index'),'ok');
        }
			public function del()
	{            $id = $this->security->xss_clean($this->uri->segment(5));   //ID
            if(empty($id)) $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，ID为空！</font>','javascript:history.back();');

   
	         $row=$this->CsdjDB->get_select ('lw_type','CS_ID','*',''.$id.'');
	         if($row){
		     $pic="/attachment/music/".$row[0]->CS_Lwpic;

                     if(FTP_Fun==1 && $row[0]->CS_FID){ //远程附件
                            $this->load->library('ftp');
                            $FTPUP['hostname'] = FTP_Server;
                            $FTPUP['port']     = FTP_Port;
                            $FTPUP['username'] = FTP_Name;
                            $FTPUP['password'] = FTP_Pass;
                            //$FTPUP['debug']    = TRUE;

                            $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/music/".$row[0]->CS_Lwpic;

                            $FTPUP['passive']  = FTP_Ive;
                            $this->ftp->connect($FTPUP);
                            $this->ftp->delete_file(".".$pic);
                            $this->ftp->close();
                     }else{
	                 //删除歌曲本地数据		            
					 @unlink(".".$pic);
                     } 
	         }

            $this->db->query("delete from ".CS_SqlPrefix."lw_type where CS_ID in (".$id.")");

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

}
