<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Singer 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."singer where cs_yid=0 order by CS_AddTime desc";
	    $query = $this->db->query($sql_string); 
	    $total = $query->num_rows();

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

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

	public function so()
	{
	    $key = $this->input->get('key');
	    $cid = $this->input->get('cid');
	    $page = $this->input->get('page');
            if(empty($page)) $page=1;
	    $sql_string = "SELECT * FROM ".CS_SqlPrefix."singer where 1=1";
            if($key){
	       $sql_string.= " and CS_Name like '%".$key."%'";
            }
            if($cid){
                  if($cid=="-2"){
	               $sql_string.= " and cs_yid=1";
                  }elseif($cid=="-1"){
	               $sql_string.= " and cs_tid=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/singer/so/')."?key=".$key."&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['singer'] = $query->result();
	    $data['pages'] = $this->CsdjAdmin->get_page($base_url,$totalPages,$page,10); //获取分页类

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

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

	public function listsadd()
	{
            $data['title']="新增";
            $data['CS_ID']=0;
            $data['CS_Name']="";
            $data['CS_XID']=50;
            $data['CS_AliasName']="";
            $data['CS_Template']="singer-list.html";
            $this->load->view('admin/slist_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/singer/lists/'));
            $data['title']="修改";
            $sql="SELECT * FROM ".CS_SqlPrefix."gslist 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_XID']=$row[0]->CS_XID;
                 $data['CS_AliasName']=$row[0]->CS_AliasName;
                 $data['CS_Template']=$row[0]->CS_Template;
            }
            $this->load->view('admin/slist_edit',$data);
        }

	public function listssave()
	{
            $this->CsdjAdmin->Admin_Qx('22');
            $ids=$this->input->post('ids', TRUE);
            $CS_Name=$this->input->post('CS_Name', 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);

            if(empty($CS_XID)) $CS_XID=50;
            if(empty($CS_Template)) $CS_Template="singer-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_XID']=$CS_XID;
            $data['CS_AliasName']=$CS_AliasName;
            $data['CS_Template']=$CS_Template;

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

	public function listspledit()
	{
            $this->CsdjAdmin->Admin_Qx('22');
            $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('gslist',$arr[$j],$data);
                  }
            }
            $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，操作成功！</font>',site_url('admin/singer/lists/'),'ok');
        }

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

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

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


	public function add()
	{
            $data['title']="新增";
            $data['CS_ID']=0;
            $data['CS_CID']=0;
            $data['CS_Color']="";
            $data['CS_TID']=0;
            $data['CS_Name']="";
            $data['CS_Content']="";
            $data['CS_Hits']=0;
            $data['CS_Yhits']=0;
            $data['CS_Zhits']=0;
            $data['CS_Pic']="";
            $data['CS_Template']="singer-show.html";
            $this->load->view('admin/singer_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 ('singer','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_Name']=$row[0]->CS_Name;
            $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;
            $this->load->view('admin/singer_edit',$data);
        }


	public function save()
	{
            $this->CsdjAdmin->Admin_Qx('22');
            $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_Name']=$this->input->post('CS_Name');
		$data['CS_TID']=$this->input->post('CS_TID');
		$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_Pic']=$this->input->post('CS_Pic');
                $data['CS_Content']=$this->input->post('CS_Content');

                if(empty($data['CS_Name'])||empty($data['CS_Content'])){
                        $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_TID'])) $data['CS_TID']=0;
                if(empty($data['CS_Template'])) $data['CS_Template']='singer-show.html';

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

	public function del()
	{
            $this->CsdjAdmin->Admin_Qx('22');
            $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($this->CsdjSkins->getsingercount($id)>0){
                 $this->CsdjAdmin->Admin_Url('<font color=red>该歌手下有歌曲数据，请先移除歌曲！</font>','javascript:history.back();');
            }

            //-------------------------------删除歌手文件---------------------------------------//
            if(Web_Mode==3){
                 $fileurl=$this->CsdjSkins->LinkUrl('show','singer','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 ('singer','CS_ID','*',''.$id.'');
	    if($row){
		     $pic="/attachment/singer/".$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/singer/".$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."singer where CS_ID in (".$id.")");
            $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，删除成功！</font>',$_SERVER['HTTP_REFERER'],'ok');
        }

	public function pldel()
	{
            $this->CsdjAdmin->Admin_Qx('22');
	    $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($this->CsdjSkins->getsingercount($Arr[$i])>0){
                    $result=$this->db->query("SELECT CS_Name FROM ".CS_SqlPrefix."singer where CS_ID=".$Arr[$i]."");
                    $row=$result->row_array();
                    $this->CsdjAdmin->Admin_Url('<font color=red>歌手:'.$row['CS_Name'].' 下有歌曲数据，请先移除歌曲！</font>','javascript:history.back();');
                    break;
               }
                 //-------------------------------删除歌手文件---------------------------------------//
                 if(Web_Mode==3){
                      $fileurl=$this->CsdjSkins->LinkUrl('show','singer','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 ('singer','CS_ID','*',''.$Arr[$i].'');
	         if($row){
		     $pic="/attachment/singer/".$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/singer/".$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."singer 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/singer_html');
        }

	public function pledit()
	{
            $id=$this->input->get('id');
            $data['id']=$id;
            $data['link']=site_url('admin/singer/plsave/');
            $this->load->view('admin/pl_singer',$data);
        }

	public function plsave()
	{
            $this->CsdjAdmin->Admin_Qx('22');
            $CS_ID=$this->input->post('CS_ID');
            $CS_CSID=$this->input->post('CS_CSID');
            $CS_CSID=implode(',', $CS_CSID);
		$CS_Color=$this->input->post('CS_Color');
		$CS_CID=$this->input->post('CS_CID');
		$CS_TID=$this->input->post('CS_TID');
		$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($CS_ID==""){
			$this->CsdjAdmin->Admin_Url('<font color=red>抱歉,请填写要修改的歌手ID!</font>','javascript:history.back();');
		}
 		$Arr=explode(',',$CS_CSID);
                              for($i=0;$i<count($Arr);$i++){
                                     if($Arr[$i]=="CS_Color"){
                                         $this->db->query("update ".CS_SqlPrefix."singer set CS_Color='".$CS_Color."' where CS_ID in (".$CS_ID.")");
                                     }elseif($Arr[$i]=="CS_CID"){
                                         $this->db->query("update ".CS_SqlPrefix."singer set CS_CID=".$CS_CID." where CS_ID in (".$CS_ID.")");
                                     }elseif($Arr[$i]=="CS_TID"){
                                         $this->db->query("update ".CS_SqlPrefix."singer set CS_TID=".$CS_TID." where CS_ID in (".$CS_ID.")");
                                     }elseif($Arr[$i]=="CS_Hits"){
                                         $this->db->query("update ".CS_SqlPrefix."singer set CS_Hits=".$CS_Hits." where CS_ID in (".$CS_ID.")");
                                     }elseif($Arr[$i]=="CS_Yhits"){
                                         $this->db->query("update ".CS_SqlPrefix."singer set CS_Yhits=".$CS_Yhits." where CS_ID in (".$CS_ID.")");
                                     }elseif($Arr[$i]=="CS_Zhits"){
                                         $this->db->query("update ".CS_SqlPrefix."singer set CS_Zhits=".$CS_Zhits." where CS_ID in (".$CS_ID.")");
                                     }elseif($Arr[$i]=="CS_Rhits"){
                                         $this->db->query("update ".CS_SqlPrefix."singer set CS_Rhits=".$CS_Rhits." where CS_ID in (".$CS_ID.")");
                                     }elseif($Arr[$i]=="CS_Template"){
                                         $this->db->query("update ".CS_SqlPrefix."singer set CS_Template='".$CS_Template."' where CS_ID in (".$CS_ID.")");
                                     }
                              }
                 echo '<script>
                        alert("恭喜您，操作成功！");
                        parent.document.location.reload();
                 </script>';
        }
}

