<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Fav 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('CsdjUser');
		$this->CsdjUser->User_Login();
	}

	public function index()
	{
                $data='';
                $cid = intval($this->input->get('cid', TRUE));   //cid
                $page = intval($this->input->get('page', TRUE));   //page
                if($page==0) $page=1;

                $this->load->get_user_templates();
		$template=$this->load->view('fav-list.html',$data,true);
		$template=$this->CsdjSkins->TopandBottom($template,'user');

		$Mark_Text=str_replace("{cscms:title}","收藏歌曲 - 会员管理中心",$template);
		$Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
		$Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		$Mark_Text=str_replace("[fav:cid]",$cid,$Mark_Text); //CID

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

                        $sqlstr="select * from ".CS_SqlPrefix."fav where CS_User='".$this->session->userdata('cs_name')."'";
                        if($cid>0){
                            $sqlstr.=" and cs_cid='$cid'";
                        }
                        $sqlstr.=" order by CS_AddTime desc";
                        $data_content='';
			$Arr=$this->CsdjSkins->SpanUsersPage($sqlstr,$page_arr[2][0],$pagenum,'user/fav?cid='.$cid,$page);//sql,每页显示条数
			$result=$this->CsdjDB->db->query($Arr[2]);
			$recount=$result->num_rows();
			if($recount==0){
				$data_content="<div align=center><strong><font style=font-size:12px;>暂无数据！</font></strong></div>";
			}else{
				if($result){
					$sorti=1;
					foreach ($result->result() as $row2) {
						$datatmp=$this->CsdjSkins->DataFav($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);
		$Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
	        $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
		$Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
                $this->CsdjSkins->skins($Mark_Text,$data);
        }

	public function add()
	{
                $fav['cs_did']=intval($this->uri->segment(4));   //歌曲ID
                if($fav['cs_did']==0)  $this->CsdjSkins->Msg_url('歌曲ID不能为空!','javascript:window.close();'); 

                $sql="SELECT CS_Name,CS_CID FROM ".CS_SqlPrefix."dance where cs_id='".$fav['cs_did']."' and cs_yid=0 and cs_hid=0";
	        $row=$this->CsdjDB->get_all($sql); 
	        if(!$row){
                        $this->CsdjSkins->Msg_url('抱歉，该歌曲不存在或者没有审核!','javascript:window.close();'); 
                }else{
                        $fav['cs_name']=$row[0]->CS_Name;
                        $fav['cs_cid']=$row[0]->CS_CID;
                }
                //判断是否已经收藏
                $sql="SELECT CS_ID FROM ".CS_SqlPrefix."fav where cs_did='".$fav['cs_did']."' and cs_user='".$this->session->userdata('cs_name')."'";
	        $rowv=$this->CsdjDB->get_all($sql); 
	        if($rowv){
                        $this->CsdjSkins->Msg_url('抱歉，您已经收藏了该歌曲!可在个人音乐盒查看!','javascript:window.close();'); 
                }

                $fav['CS_User']=$this->session->userdata('cs_name');
                $fav['CS_AddTime']=date('Y-m-d H:i:s');

                $res=$this->CsdjDB->get_insert('fav',$fav);
                if($res>0){
                       //增加收藏人气
		       $this->db->query("update ".CS_SqlPrefix."dance set CS_Shits=CS_Shits+1 where CS_ID='".$fav['cs_did']."'");
                       //写入动态
                       $dt['CS_CID']=6;
                       $dt['CS_DID']=$fav['cs_did'];
                       $dt['CS_YID']=0;
                       $dt['CS_User']=$this->session->userdata('cs_name');
                       $dt['CS_Title']=$fav['cs_name'];
                       $dt['CS_AddTime']=date('Y-m-d H:i:s');
                       $this->CsdjDB->get_insert('dt',$dt);

                       $this->CsdjSkins->Msg_url('恭喜您，歌曲收藏成功!可在个人音乐盒查看!','javascript:window.close();'); 

                }else{
                       $this->CsdjSkins->Msg_url('抱歉，歌曲收藏失败!','javascript:window.close();'); 
                }
        }

	public function del()
	{
                $id=intval($this->input->get('id', TRUE));   //id
                if($id==0) $this->CsdjSkins->Msg_url('抱歉，参数错误，ID为空!','javascript:history.back();'); 

                $this->db->query("delete from ".CS_SqlPrefix."fav where cs_user='".$this->session->userdata('cs_name')."' and cs_id='$id'");
                $this->CsdjSkins->Msg_url('恭喜您，删除成功!',@$_SERVER['HTTP_REFERER']);  
        }
}

