<?php
/**
 * @Cscms 3.5 open source management system
 * @copyright 2009-2013 chshcms.com. All rights reserved.
 * @Author:Cheng Jie
 * @Dtime:2015-02-8
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class CsdjDance extends CI_Model
{
    function __construct (){
	       parent:: __construct ();
    }

    //主页
    public function index($data='') {
           $this->load->get_templates();
		   $template=$this->load->view('dance-index.html',$data,true);
		   $Mark_Text=str_replace("{cscms:title}","歌曲首页 - ".Web_Name,$template);
           $Mark_Text=$this->CsdjSkins->skins($Mark_Text,$data,true);
		   if (defined('IS_ADMIN')) {
		       $Mark_Text=str_replace(SELF,"index.php",$Mark_Text);
		   }
		   return $Mark_Text;
    }

    //列表
    public function lists($fid,$id,$page,$row,$aliasname='') {
		   $data='';$data_content='';
		   if(empty($row[0]->CS_Template)){
		    $CS_Template='dance-list.html';
		   }else{
		    $CS_Template=$row[0]->CS_Template;
		   }

		   $this->load->get_templates();
		   $template=$this->load->view($CS_Template,$data,true);

		   //SEO标题
		   if (empty($row[0]->CS_Title)){
               		$seo_title=$row[0]->CS_Name." - ".Web_Name;
		   }else{
		       $seo_title=$row[0]->CS_Title;
		   }
		   //SEO关键词
		   if (empty($row[0]->CS_Keywords)){
               		$seo_keywords=$row[0]->CS_Name.",".$row[0]->CS_Name."试听,".$row[0]->CS_Name."下载";
		   }else{
              		$seo_keywords=$row[0]->CS_Keywords;
		   }
		   //SEO描述
		   if (empty($row[0]->CS_Description)){
               		$seo_description=$row[0]->CS_Name." 是由".Web_Name."提供试听和下载。";
		   }else{
               		$seo_description=$row[0]->CS_Description;
		   }
		   $Mark_Text=$this->CsdjSkins->TopandBottom($template);
		   $Mark_Text=str_replace("{cscms:title}",$seo_title,$Mark_Text);
		   $Mark_Text=str_replace("{cscms:keywords}",$seo_keywords,$Mark_Text);
		   $Mark_Text=str_replace("{cscms:description}",$seo_description,$Mark_Text);
		   $Mark_Text=str_replace("[dance:sort]",$fid,$Mark_Text);
		   $Mark_Text=str_replace("[dance:classid]",$row[0]->CS_ID,$Mark_Text);
		   //预先除了分页
		   $pagenum=$this->CsdjSkins->GetPageNum($Mark_Text);
		   preg_match_all('/{cscms:dance(.*?pagesize=([\S]+).*?)}([\s\S]+?){\/cscms:dance}/',$Mark_Text,$page_arr);//判断是否有分页标识
		   if(!empty($page_arr) && !empty($page_arr[2])){

			   $sqlstr=$this->CsdjSkins->Mark_Sql('dance',$page_arr[1][0],$id,$fid);
			   $Arr=$this->CsdjSkins->SpanPage($sqlstr,$page_arr[2][0],$pagenum,'lists','dance',$fid,$row[0]->CS_ID,1,$page,$aliasname);//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->DataDance($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,$id);
		   $Mark_Text=str_replace("[dance:classname]",$row[0]->CS_Name,$Mark_Text);
		   $Mark_Text=str_replace("[dance:classlink]",$this->CsdjSkins->LinkUrl('lists','dance','id',$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace('[dance:classnlink]',$this->CsdjSkins->LinkUrl('lists','dance','new',$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace('[dance:classplink]',$this->CsdjSkins->LinkUrl('lists','dance','play',$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace('[dance:classhlink]',$this->CsdjSkins->LinkUrl('lists','dance','hits',$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace('[dance:classtlink]',$this->CsdjSkins->LinkUrl('lists','dance','reco',$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace('[dance:classxlink]',$this->CsdjSkins->LinkUrl('lists','dance','down',$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace('[dance:classslink]',$this->CsdjSkins->LinkUrl('lists','dance','fav',$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace('[dance:classylink]',$this->CsdjSkins->LinkUrl('lists','dance','yue',$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace('[dance:classzlink]',$this->CsdjSkins->LinkUrl('lists','dance','zhou',$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace('[dance:classrlink]',$this->CsdjSkins->LinkUrl('lists','dance','ri',$row[0]->CS_ID),$Mark_Text);
		   $Mark_Text=str_replace('[dance:classdlink]',$this->CsdjSkins->LinkUrl('lists','dance','ding',$row[0]->CS_ID),$Mark_Text);
           $Mark_Text=$this->CsdjSkins->skins($Mark_Text,$data,true);
		   if (defined('IS_ADMIN')) {
		       $Mark_Text=str_replace(SELF,"index.php",$Mark_Text);
		   }
		   return $Mark_Text;
    }

    //播放页
    public function play($fid,$id,$row) {
		   $data='';$data_content='';
           if(empty($row[0]->CS_Template)){
                 $CS_Template='dance-play.html';
           }else{
                 $CS_Template=$row[0]->CS_Template;
           }

           $this->load->get_templates();
		   $template=$this->load->view($CS_Template,$data,true);

        	//SEO标题
        	if (empty($row[0]->CS_Title)){
               		$seo_title=$row[0]->CS_Name." - ".Web_Name;
        	}else{
               		$seo_title=$row[0]->CS_Title;
        	}
        	//SEO关键词
        	if (empty($row[0]->CS_Keywords)){
               		$seo_keywords=$row[0]->CS_Name."试听,下载";
        	}else{
              		$seo_keywords=$row[0]->CS_Keywords;
        	}
        	//SEO描述
        	if (empty($row[0]->CS_Description)){
               		$seo_description=$row[0]->CS_Name." 是由".Web_Name."提供试听和下载。";
        	}else{
               		$seo_description=$row[0]->CS_Description;
        	}

            //播放器
            if($row[0]->CS_PID==1){ //jw播放器
                  $CS_player="<div id='mediaplayer'></div><script type=\"text/javascript\" src=\"".Web_Path."csdj/player/jwplayer/jwplayer.js\"></script>";
            }elseif($row[0]->CS_PID==2){
                  $CS_player="";
            }else{ //cmp播放器
                  $CS_player="<div id='mediaplayer'></div><script type=\"text/javascript\" src=\"".Web_Path."csdj/player/cmp/cmp.js\"></script>";
            }
            $CS_player.="<script type=\"text/javascript\" src=\"".site_url('dance/player/'.$id)."\"></script>";
		    $Mark_Text=str_replace("{cscms:title}",$seo_title,$template);
		    $Mark_Text=str_replace("{cscms:keywords}",$seo_keywords,$Mark_Text);
		    $Mark_Text=str_replace("{cscms:description}",$seo_description,$Mark_Text);
		    $Mark_Text=str_replace("[dance:player]",$CS_player,$Mark_Text);
		    $Mark_Text=str_replace("[dance:tags]",$this->CsdjSkins->HotSearch($row[0]->CS_Tags),$Mark_Text);
		    $Mark_Text=str_replace("[dance:starss]",$row[0]->CS_Reco,$Mark_Text);
		    $Mark_Text=str_replace("[dance:ids]",$row[0]->CS_ID,$Mark_Text);




            //静态模式,动态人气
            if(Web_Mode==3){
			    $Mark_Text=str_replace('[dance:hits]',"<script src=".site_url('dance/hits/dt/hits/'.$id)."></script>",$Mark_Text);
			    $Mark_Text=str_replace('[dance:shits]',"<script src=".site_url('dance/hits/dt/shits/'.$id)."></script>",$Mark_Text);
			    $Mark_Text=str_replace('[dance:xhits]',"<script src=".site_url('dance/hits/dt/xhits/'.$id)."></script>",$Mark_Text);
			    $Mark_Text=str_replace('[dance:rhits]',"<script src=".site_url('dance/hits/dt/rhits/'.$id)."></script>",$Mark_Text);
			    $Mark_Text=str_replace('[dance:zhits]',"<script src=".site_url('dance/hits/dt/zhits/'.$id)."></script>",$Mark_Text);
			    $Mark_Text=str_replace('[dance:yhits]',"<script src=".site_url('dance/hits/dt/yhits/'.$id)."></script>",$Mark_Text);
			    $Mark_Text=str_replace('[dance:dhits]',"<script src=".site_url('dance/hits/dt/dhits/'.$id)."></script>",$Mark_Text);
			    $Mark_Text=str_replace('[dance:chits]',"<script src=".site_url('dance/hits/dt/chits/'.$id)."></script>",$Mark_Text);
			}
            $sdata['id']=$row[0]->CS_ID;
            $sdata['cid']=$row[0]->CS_CID;
            $sdata['singerid']=$row[0]->CS_SingerID;
            $sdata['user']=$row[0]->CS_User;
		    $Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$sdata);

		    //舞曲上下曲开始
            preg_match_all('/dance:slink/',$Mark_Text,$arr);
		    if(!empty($arr[0]) && !empty($arr[0][0])){
		       $sqlu="Select CS_ID,CS_Name,CS_CID from ".CS_SqlPrefix."dance where CS_YID=0 and CS_HID=0 and CS_ID<".$id." order by cs_id desc limit 1";
		       $rowu=$this->CsdjDB->get_all($sqlu);
		       if($rowu){
		              $Mark_Text=str_replace('[dance:sid]',$rowu[0]->CS_ID,$Mark_Text);
		              $Mark_Text=str_replace('[dance:sname]',$rowu[0]->CS_Name,$Mark_Text);
		              $Mark_Text=str_replace('[dance:slink]',$this->CsdjSkins->LinkUrl('play','dance','id',$rowu[0]->CS_CID,$rowu[0]->CS_ID),$Mark_Text);
		       }else{
		              $Mark_Text=str_replace('[dance:sid]','0',$Mark_Text);
		              $Mark_Text=str_replace('[dance:sname]','没有了',$Mark_Text);
		              $Mark_Text=str_replace('[dance:slink]','#',$Mark_Text);
		       }
			}
            preg_match_all('/dance:xlink/',$Mark_Text,$arr);
		    if(!empty($arr[0]) && !empty($arr[0][0])){
		       $sqld="Select CS_ID,CS_Name,CS_CID from ".CS_SqlPrefix."dance where CS_YID=0 and CS_HID=0 and CS_ID>".$id." order by cs_id asc limit 1";
		       $rowd=$this->CsdjDB->get_all($sqld);
		       if($rowd){
		              $Mark_Text=str_replace('[dance:xid]',$rowd[0]->CS_ID,$Mark_Text);
		              $Mark_Text=str_replace('[dance:xname]',$rowd[0]->CS_Name,$Mark_Text);
		              $Mark_Text=str_replace('[dance:xlink]',$this->CsdjSkins->LinkUrl('play','dance','id',$rowd[0]->CS_CID,$rowd[0]->CS_ID),$Mark_Text);
		       }else{
		              $Mark_Text=str_replace('[dance:xid]','0',$Mark_Text);
		              $Mark_Text=str_replace('[dance:xname]','没有了',$Mark_Text);
		              $Mark_Text=str_replace('[dance:xlink]','#',$Mark_Text);
		       }
			}
		    $Mark_Text=$this->CsdjSkins->DataDance($Mark_Text,$Mark_Text,$row[0],'1'); //全部解析
		    $Mark_Text.="<script src=".site_url('dance/hits/id/'.$id)."></script>";//增加人气
            $Mark_Text=$this->CsdjSkins->skins($Mark_Text,$data,true);
		    if (defined('IS_ADMIN')) {
		       $Mark_Text=str_replace(SELF,"index.php",$Mark_Text);
		    }
		    return $Mark_Text;
	}
}

