<?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('CsdjSinger');
		    $this->load->model('CsdjCache');
	}

    //歌手主页
	public function index()
	{

            //判断运行模式
            $cscms=$this->input->get('cs', TRUE);
	        if(Web_Mode==3 && $cscms!="html"){ 
                    header("Location: ".str_replace('//','/',Web_Path.Html_Tindex).""); 
                    exit;
            }

	        $cache_id ="singer_index";
	        if(!($this->CsdjCache->start($cache_id))){
                echo $this->CsdjSinger->index();
		        $this->CsdjCache->end();
			}
	}

    //分类列表
	public function lists()
	{
            $fid = $this->uri->segment(3);   //方式
            $id = intval($this->uri->segment(4));   //ID
            $page  = intval($this->uri->segment(5));   //页数
            if($page==0) $page=1;
                
            if($id==0){
                exit($this->CsdjSkins->Msg_url('出错了，ID不能为空！',Web_Path));
            }

            //判断运行模式
            $cscms=$this->input->get('cs', TRUE);
	        if(Web_Mode==3 && $cscms!="html"){ 
                    $Htmllink=$this->CsdjSkins->LinkUrl('lists','singer',$fid,$id,$page);
                    header("Location: ".$Htmllink.""); 
                    exit;
            }

            $aliasname="";
		    $row=$this->CsdjDB->get_select ('gslist','CS_ID','*',''.$id.'');
		    if(!$row){
                    $aliasname=$id;
                    $row=$this->CsdjDB->get_select ('gslist','CS_AliasName','*',''.$id.'');
                    if(!$row){
                           exit($this->CsdjSkins->Msg_url('出错了，该分类不存在！',Web_Path));
                    }
		    }

	        $cache_id ="singer_list_".$fid."_".$id."_".$page;
	        if(!($this->CsdjCache->start($cache_id))){
                echo $this->CsdjSinger->lists($fid,$id,$page,$row,$aliasname);
			    $this->CsdjCache->end();
			}
	}

    //搜索列表
	public function so()
	{
            $data='';$data_content='';
            $fid = $this->uri->segment(3);   //方式
            $key = $this->input->get_post('key');   //关键字
            $page  = intval($this->input->get('p', TRUE));   //页数
            if($page==0) $page=1;

            if($fid=='zm' && empty($key)){
                 $key=$this->uri->segment(4); 
            }

	        $key = $this->CsdjSkins->safe_replace(trim($key));
	        $key = htmlspecialchars(strip_tags($key));
	        $key = str_replace('%', '', $key);	//过滤'%'，用户全文搜索
            $key=$this->CsdjSkins->rurlencode($key);

	        $cache_id ="singer_so_".$fid."_".$key."_".$page;
	        if(!($this->CsdjCache->start($cache_id))){

                $CS_Template='so-singer-'.$fid.'.html';
                $this->load->get_templates();
		        $template=$this->load->view($CS_Template,$data,true);

		        $Mark_Text=$this->CsdjSkins->TopandBottom($template);
		        $Mark_Text=str_replace("{cscms:sokey}",$key,$Mark_Text);
                if($fid=='zm'){
		             $Mark_Text=str_replace("{cscms:title}","按字母".$key."搜索 - ".Web_Name,$Mark_Text);
                }else{
		             $Mark_Text=str_replace("{cscms:title}","搜索歌手".$key." - ".Web_Name,$Mark_Text);
                }
		        $Mark_Text=str_replace("{cscms:keywords}",$key,$Mark_Text);
		        $Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);

		        //预先除了分页
		        $pagenum=$this->CsdjSkins->GetPageNum($Mark_Text);
		        preg_match_all('/{cscms:singer(.*?pagesize=([\S]+).*?)}([\s\S]+?){\/cscms:singer}/',$Mark_Text,$page_arr);//判断是否有分页标识
		        if(!empty($page_arr) && !empty($page_arr[2])){

			          $zimu_arr=array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
			          $zimu_arr1=array(-20319,-20283,-19775,-19218,-18710,-18526,-18239,-17922,-1,-17417,-16474,-16212,-15640,-15165,-14922,-14914,-14630,-14149,-14090,-13318,-1,-1,-12838,-12556,-11847,-11055);
			          $zimu_arr2=array(-20284,-19776,-19219,-18711  ,-18527,-18240,-17923,-17418,-1,-16475,-16213,-15641,-15166,-14923,-14915,-14631,-14150,-14091,-13319,-12839,-1,-1,-12557,-11848,-11056,-2050);
                
		              if($fid=='zm' && in_array(strtoupper($key),$zimu_arr)){  //按字母搜索
			                $posarr=array_keys($zimu_arr,strtoupper($key));
			                $pos=$posarr[0];
			                $sqlstr="SELECT * FROM ".CS_SqlPrefix."singer where CS_YID=0 and (((ord( substring( CS_Name, 1, 1 ) ) -65536>=".($zimu_arr1[$pos])." and  ord( substring( CS_Name, 1, 1 ) ) -65536<=".($zimu_arr2[$pos]).")) or UPPER(substring( CS_Name, 1, 1 ))='".$zimu_arr[$pos]."')";
		              }else{
                            $sqlstr="select * from ".CS_SqlPrefix."singer where CS_YID=0 and CS_Name like '%".$key."%' order by CS_AddTime desc";
					  }

			          $Arr=$this->CsdjSkins->SpanPage($sqlstr,$page_arr[2][0],$pagenum,'so','singer',$fid,urlencode($key),1,$page);//sql,每页显示条数
			          $result=$this->CsdjDB->db->query($Arr[2]);
			          $recount=$result->num_rows();
			          if($recount==0){
                              if($fid=='zm'){
				                     $data_content.="<div align=center><strong>对不起,没有找到字母《<font color=#FF0000>".$key."</font>》开头的相关歌手!</strong></div>";
                              }else{
				                     $data_content.="<div align=center><a href=http://so.fzl57.com target='so'><strong>对不起,没有找到《<font color=#FF0000>".$key."</font>》相关歌手,去舞曲库搜索!</strong></a></div>";
                              }
					  }else{
				          if($result){
					          $sorti=1;
					          foreach ($result->result() as $row2) {
						            $datatmp=$this->CsdjSkins->DataSinger($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);
		        $this->CsdjSkins->skins($Mark_Text,$data);
		        $this->CsdjCache->end();
			}
	}

    //歌手内容页面
	public function show()
	{
            $fid = $this->uri->segment(3);   //方式
            $id = intval($this->uri->segment(4));   //ID
            $page  = intval($this->uri->segment(5));   //页数
            if($page==0) $page=1;

            //判断运行模式
            $cscms=$this->input->get('cs', TRUE);
	        if(Web_Mode==3 && $cscms!="html"){
                    $Htmllink=$this->CsdjSkins->LinkUrl('show','singer',$fid,$id,$page);
                    header("Location: ".$Htmllink.""); 
                    exit;
            }

            if(empty($id)){
                exit($this->CsdjSkins->Msg_url('出错了，ID不能为空！',Web_Path));
            }

		    $row=$this->CsdjDB->get_select ('singer','CS_ID','*',''.$id.'');
		    if(!$row){
                exit($this->CsdjSkins->Msg_url('出错了，该歌手不存在！',Web_Path));
		    }
            if($row[0]->CS_YID==1){
                exit($this->CsdjSkins->Msg_url('出错了，该歌手没有审核！',Web_Path));
            }

	        $cache_id ="singer_show_".$fid."_".$id."_".$page;
	        if(!($this->CsdjCache->start($cache_id))){
                echo $this->CsdjSinger->show($fid,$id,$page,$row);
			    $this->CsdjCache->end();
			}
	}

    //增加人气
	public function hits()
	{
           $ac = $this->uri->segment(3);   //方式
           if($ac=='id'){

                $id = intval($this->uri->segment(4));   //ID
		        $row=$this->CsdjDB->get_select ('singer','CS_ID','CS_Hits',''.$id.'');
		        if(!$row){
			           show_404();
		        }
                //增加人气
	            $updata['CS_Hits']=$row[0]->CS_Hits+1;
                $this->CsdjDB->get_update ('singer',$id,$updata);

		   }elseif($ac=='dt'){   //静态模式、动态人气

                $op = $this->uri->segment(4);   //类型
                $id = intval($this->uri->segment(5));   //ID
		        $row=$this->CsdjDB->get_select ('topic','CS_ID','CS_Hits',''.$id.'');
		        if(!$row){
			         show_404();
		        }
                echo "document.write('".$row[0]->CS_Hits."');";
           }
	}

	//AJAX
	public function ajax()
	{
            @header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
            @header("Cache-Control: no-cache, must-revalidate"); 
            @header("Pragma: no-cache");
            if(@!eregi($_SERVER['SERVER_NAME'],$_SERVER['HTTP_REFERER'])){exit('QQ:848769359');}

            $cid=intval($this->input->get('id', TRUE));
            $zm=$this->input->get('zm', TRUE);
			if(empty($zm)) $zm='A';

			$zimu_arr=array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
			$zimu_arr1=array(-20319,-20283,-19775,-19218,-18710,-18526,-18239,-17922,-1,-17417,-16474,-16212,-15640,-15165,-14922,-14914,-14630,-14149,-14090,-13318,-1,-1,-12838,-12556,-11847,-11055);
			$zimu_arr2=array(-20284,-19776,-19219,-18711  ,-18527,-18240,-17923,-17418,-1,-16475,-16213,-15641,-15166,-14923,-14915,-14631,-14150,-14091,-13319,-12839,-1,-1,-12557,-11848,-11056,-2050);

			$posarr=array_keys($zimu_arr,strtoupper($zm));
			$pos=$posarr[0];
			$sqlstr="SELECT CS_ID,CS_Name FROM ".CS_SqlPrefix."singer where CS_YID=0 and CS_CID=".$cid." and (((ord( substring( CS_Name, 1, 1 ) ) -65536>=".($zimu_arr1[$pos])." and  ord( substring( CS_Name, 1, 1 ) ) -65536<=".($zimu_arr2[$pos]).")) or UPPER(substring( CS_Name, 1, 1 ))='".$zimu_arr[$pos]."')";
			$result=$this->db->query($sqlstr);

            echo '<option value="">--选择歌手--</option>';
			foreach ($result->result() as $row) {
                 echo '<option value="'.$row->CS_ID.'">'.$row->CS_Name.'</option>';
			}
	}
}


