<?php
# Name: PHP版程氏专集CMS管理系统 v3.0
# Author: 程氏<web@chshcms.com> [QQ:848769359]
# Homepage:http://www.chshcms.cn/
include_once("../../lib/cs_label.php");
include_once("../loginstate.php");
SystemPer('8_2');
if(CS_Webmode!=3)  exit(Admin_Error('网站运行模式为动态，不能生成!','javascript:history.go(-1);'));
        global $db;
        $srr="";
        $ac=CS_Request("ac");
        $op=CS_Request("op");
        $id=CS_Request("id");
        $ksid=CS_Request("ksid");
        $jsid=CS_Request("jsid");
        $xid=CS_Request("xid");
        $sday=CS_Request("sday");
        $cid=CS_Request("cid");
        $fid=CS_Request("fid");
        $type=CS_Request("type");
        $name=CS_Request("name");
        $pages=CS_Request("pages");
        if(empty($pages)) $pages=1;
        if(empty($sid)) $sid=1;
        if(empty($xid)) $xid=0;
        if(empty($fid)) $fid=0;
        if(empty($op)) $op='id';
        if(!empty($sday)) $srr=" and DATEDIFF(DATE(CS_AddTime),'".date('Y-m-d')."')>=-$sday";
        if($op=="time") $op='new';
        if($op=='hits'){
            $ttitle='总人气';
        }elseif($op=='ri'){
            $ttitle='日人气';
        }elseif($op=='zhou'){
            $ttitle='周人气';
        }elseif($op=='yue'){
            $ttitle='月人气';
        }elseif($op=='fav'){
            $ttitle='收藏人气';
        }elseif($op=='down'){
            $ttitle='下载人气';
        }elseif($op=='ding'){
            $ttitle='被顶人气';
        }elseif($op=='play'){
            $ttitle='正在播放';
        }elseif($op=='reco'){
            $ttitle='推荐星际';
        }elseif($op=='id'){
            $ttitle='ID';
        }else{
            $ttitle='最新更新';
        }

if($ac=='ids'){  //生成单ID
        $row=$db->getrow("SELECT * FROM ".Getdbname('dance')." Where CS_ID='".$id."'");
        if($row){
              if($row['CS_Template']){
                  $Template=$row['CS_Template'];
              }else{
                  $Template="dance-play.html";
              }
              if(!file_exists(_CSDJ_ROOT_.CS_Templateurl.$Template)) exit("Err_100");

              $Htmllink="../..".LinkUrl('play','dance','id',$row['CS_CID'],$row['CS_ID']);
              $Htmlgo=$Htmllink;
              if(CS_Mark!=0){
                 $Htmllink.="index.".CS_Suffix;
              }
              $Mark_Text=SpanDancePlay($row['CS_ID']);
	      if(write_file($Htmllink, $Mark_Text)){  //生成
                 exit("&nbsp;ID:<font color=red>".$row['CS_ID']."</font>-->&nbsp;<font style=font-size:10pt;>生成歌曲:<font color=#026E12>".$row['CS_Name']."</font>成功,&nbsp;&nbsp;<a href=".$Htmlgo." target=_blank>".$Htmlgo."</a></font>");
              }else{
                 exit('no');
              }
        }else{
             exit("&nbsp;ID:<font color=red>".$id."</font>出错了，该数据不存在!");
        }
}
if($ac=='play_id'){  //按ID生成播放
   if(empty($ksid))  exit(Msg_Error('开始ID不能为空!','javascript:history.go(-1);'));
   if($ksid>=$jsid)  exit(Msg_Error('结束ID应该比开始ID大!','javascript:history.go(-1);'));
   
    echo '<link href="../res/css/admin_style.css" rel="stylesheet" /><br>
          <div class="h_a">&nbsp;&nbsp;正在按ID生成歌曲播放页&nbsp;&nbsp;第&nbsp;<font id="dnum" color=red>'.$pages.'</font>&nbsp;页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="html.php"><font color=#026E12>紧急返回</font></a></div>
         ';
    if($pages==1){
       $k=0;
       $jsids=CS_Createpre;
    }else{
       $k=($pages-1)*CS_Createpre;
       $jsids=$pages*CS_Createpre;
    }

   $ids=$jsid-$ksid;
   
   if($ids<=CS_Createpre){
       $pagesjs=1;
       $jsids=$jsid;
   }else{
       $pagesjs=ceil($ids / CS_Createpre);
   }

   $a='no';
   for($j=$k;$j<$jsids;$j++){
        $id=$ksid+$j;
        $row=$db->getrow("SELECT * FROM ".Getdbname('dance')." Where cs_yid=0 and CS_ID='".$id."'");
        if($row){
              if($row['CS_Template']){
                  $Template=$row['CS_Template'];
              }else{
                  $Template="dance-play.html";
              }
              if(!file_exists(_CSDJ_ROOT_.CS_Templateurl.$Template)) exit("Err_100");

              $Htmllink="../..".LinkUrl('play','dance','id',$row['CS_CID'],$row['CS_ID']);
              $Htmlgo=$Htmllink;
              if(CS_Mark!=0){
                 $Htmllink.="index.".CS_Suffix;
              }
              $Mark_Text=SpanDancePlay($row['CS_ID']);
	      if(write_file($Htmllink, $Mark_Text)){  //生成
                 echo "&nbsp;ID:<font color=red>".$row['CS_ID']."</font>-->&nbsp;<font style=font-size:10pt;>生成歌曲:<font color=#026E12>".$row['CS_Name']."</font>成功,&nbsp;&nbsp;<a href=".$Htmlgo." target=_blank>".$Htmlgo."</a></font><br>";
              }else{
                 echo "&nbsp;ID:<font color=red>".$row['CS_ID']."</font>-->&nbsp;<font style=font-size:10pt;>生成歌曲:<font color=#ff0000>".$row['CS_Name']."</font>失败,&nbsp;&nbsp;<a href=".$Htmlgo." target=_blank>".$Htmlgo."</a></font><br>";
              }
           $a='ok';
        }
    }
    if($pages>=$pagesjs || $a=='no'){
          exit("<br><br><font color=#0000ff>恭喜您，所有歌曲播放页面全部生成完成,<a href=html.php>返回</a></font>");
    }else{
          exit("<br><br><font color=#0000ff>恭喜您，第".$pages."页全部生成完成,暂停3秒，继续下一页...</font><script language='javascript'>setTimeout('ReadGo();',3000);function ReadGo(){location.href='?ac=play_id&ksid=".$ksid."&jsid=".$jsid."&pages=".($pages+1)."';}</script>");
    }
}
if($ac=='listids'){  //生成分类单ID

           $Htmllink="../..".LinkUrl('list','dance',$op,$id,1,$pages);
           $Htmlgo=$Htmllink;
           if(CS_Mark!=0){
              $Htmllink.="index.".CS_Suffix;
           }
           $Mark_Text=SpanDanceList($id,$op,$pages);
	   if(write_file($Htmllink, $Mark_Text)){  //生成
                exit("&nbsp;<font style=font-size:10pt;>&nbsp;歌曲分类<font color=#ff6600>《".$name."》</font>列表&nbsp;<font color=red>按".$ttitle."排序</font>&nbsp;第<font color=#0000ff>".$pages."</font></font>页&nbsp;--生成成功：<a href=".$Htmlgo." target=_blank>".$Htmlgo."</a></font>");
           }else{
                exit("&nbsp;<font style=font-size:10pt;>&nbsp;<font color=#0000ff>歌曲分类《".$name."》列表&nbsp;第".$pages."页&nbsp;按".$ttitle[$j]."排序--生成失败!</font></font>");
           }  
}
?>
<!doctype html>
<html>
<head>
<meta charset=utf-8>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>chshdjcms management</title>
<link href="../res/css/admin_style.css" rel="stylesheet" />
<script language="javascript" src="../res/js/jquery.js"></script>
<script language="javascript"> 
function Htmlgo(ids,num,i){
        strs=ids.split(',');
        $("#html_"+i).html('&nbsp;&nbsp;<img src="../../public/images/load.gif">正在生成第<font color=red>'+(i+1)+'</font>首歌曲,请稍等...');
 	$.get("html_dance.php?ac=ids&id="+strs[i],function(R){
                if(R=="no"){
		   $("#html_"+i).html("<font color=#red>ID为"+strs[i]+"的专集生成失败！</font>");		
                } else if(R=="Err_100"){
		   $("#html_"+i).html("歌曲播放模版文件不存在！");	
                } else if(num==i+1){
               <?php if($ac=='xuan'){ ?>
		   $("#html_"+i).html(R+"<br><br><font color=#0000ff>恭喜您，所有歌曲播放页面全部生成完成,正在返回...</font>");
                   setTimeout("location.href='javascript:history.go(-1);'",3000);
               <?php }else{ ?>
		   $("#html_"+i).html(R+"<br><br><font color=#0000ff>恭喜您，第<?php echo $pages?>页 全部生成完成,暂停<font color=red><?php echo CS_StopTime?></font>秒正在继续下一页...</font>");
                   setTimeout("location.href='?ac=<?php echo $ac?>&cid=<?php echo $cid?>&pages=<?php echo ($pages+1)?>'",<?php echo CS_StopTime?>000);
               <?php } ?>
                } else {
		   $("#html_"+i).html(R);
                   $("#dnum").html(i+2);
                   Htmlgo(ids,num,i+1);	
                }											  
	});
}
function Htmllist(cname,id,num,i){
        $("#cname").html(cname);
        $("#list_"+i).html('&nbsp;&nbsp;<img src="../../public/images/load.gif">正在生成第<font color=red>'+(i+1)+'</font>页歌曲分类,请稍等...');
 	$.get("html_dance.php?ac=listids&name="+cname+"&op=<?php echo $op?>&id="+id+"&pages="+(i+1),function(R){
                if(R=="no"){
		   $("#list_"+i).html("<font color=#red>第"+(i+1)+"页生成失败！</font>");		
                } else if(R=="Err_100"){
		   $("#list_"+i).html("歌曲分类模版文件不存在！");	
                } else if(num==(i+1)){
		   $("#list_"+i).html(R+"<br><br><font color=#0000ff>恭喜您，第<?php echo $pages?>页 分类全部生成完成,暂停<font color=red><?php echo CS_StopTime?></font>秒正在继续下一页...</font>");
                   setTimeout("location.href='?ac=<?php echo $ac?>&op=<?php echo $op?>&fid=<?php echo $fid?>&xid=<?php echo $xid?>&cid=<?php echo $cid?>&pages=<?php echo ($pages+1)?>'",<?php echo CS_StopTime?>000);
                } else {
		   $("#list_"+i).html(R);
                   $("#dnum").html(i+2);
                   Htmllist(cname,id,num,i+1);	
                }											  
	});
}
</script>
</head>
<body>
<div class="wrap">
<?php if($ac=='list'){ ?>
<div class="h_a">&nbsp;&nbsp;正在生成歌曲分类:<font id="cname" color=red></font>&nbsp;按&nbsp;<font color=red><?php echo $ttitle?></font>&nbsp;排序&nbsp;&nbsp;第&nbsp;<font id="dnum" color=red>1</font>&nbsp;页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="html.php"><font color=#026E12>紧急返回</font></a></div>
<?php }else{ ?>
<div class="h_a">&nbsp;&nbsp;正在生成歌曲播放&nbsp;第&nbsp;<font color=#0000ff><?php echo $pages?></font>&nbsp;页&nbsp;第&nbsp;<font id="dnum" color=red>1</font>&nbsp;首&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="html.php"><font color=#026E12>紧急返回</font></a></div>
<?php } ?>
<div class="table_full">
	<table width="100%" class="J_check_wrap">
		<col width="100%" />
<?php
if($ac=='list'){  //生成歌曲分类列表页
        $sort=array("id","hits","yue","zhou","ri","reco","down","fav","ding","time");
        $op=$sort[$fid];
        if($op=='time') $type='all';
        $Templateurl="dance-list.html";
        if(empty($cid)){
            $str='all';
	    $sql="select CS_ID,CS_Name,CS_Template from ".Getdbname('class')." where CS_SID=1 Order by CS_ID asc";
	    $class=$db->getAll($sql);
            $cid=$class[$xid]['CS_ID'];
            $cname=$class[$xid]['CS_Name'];
            $Templateurl=$class[$xid]['CS_Template'];
        }else{
	    $sql="select * from ".Getdbname('class')." where CS_ID=$cid";
	    $row=$db->getrow($sql);
            $cname=$row['CS_Name'];
            $Templateurl=$row['CS_Template'];
        }

        if(empty($Templateurl)) $Templateurl='dance-list.html';
	if(!file_exists(_CSDJ_ROOT_.CS_Templateurl.$Templateurl)){
              echo "<tr><td>".$Templateurl."模板文件不存在</td></tr>";
        }else{
              $Mark_Text=@file_get_contents(_CSDJ_ROOT_.CS_Templateurl.$Templateurl);
              preg_match_all('/{csdjcms:dance(.*?pagesize=([\S]+).*?)}([\s\S]+?){\/csdjcms:dance}/',$Mark_Text,$page_arr);//判断是否有分页标识

              if(!empty($page_arr) && !empty($page_arr[2])){
                   $sql="SELECT COUNT(*) AS total FROM ".Getdbname('dance')." WHERE CS_YID=0 and CS_CID=".$cid." ".$srr."";
                   $rows=$db->getRow($sql);
                   $nums = $rows['total']; //获取总数
                   $pagejs = ceil($nums / $page_arr[2][0]); // 列表总页数
                   if($pagejs==0) $pagejs=1;
                   $htmlpage = ceil($pagejs / CS_Createpre); 

                   if($htmlpage<$pages){
                       if($str=='all'){
                           if(($xid+1)>(count($class)-1)){
                                if($type=='all'){  //列表完成继续播放页
                                     echo "<tr><td>&nbsp;<font style=font-size:10pt;><b>歌曲分类列表全部生成完毕,暂停".CS_StopTime."秒后继续生成歌曲播放页．．．</b></font><script language='javascript'>setTimeout('ReadGo();',".CS_StopTime."000);function ReadGo(){location.href='?';}</script></td></tr>";
                                }elseif($op!='time'){
                                      if($op=='id'){
 	                                      echo "<tr><td>&nbsp;<font style=font-size:10pt;><b>按<font color=#ff6600>".$ttitle."排序</font>歌曲分类列表全部生成完毕...</b></font><script>if (confirm(\"是否生成其他排序分类页面!\")){window.location='?ac=list&fid=".($fid+1)."&op=".$sort[($fid+1)]."';}else{window.location='html.php';}</script></td></tr>";
                                      }else{
 	                                    echo "<tr><td>&nbsp;<font style=font-size:10pt;><b>按<font color=#ff6600>".$ttitle."排序</font>歌曲分类列表全部生成完毕,".CS_StopTime."秒后生成其他排序分类页面...</b></font><script language='javascript'>setTimeout('ReadGo();',".CS_StopTime."000);function ReadGo(){location.href='?ac=list&fid=".($fid+1)."&op=".$sort[($fid+1)]."';}</script></td></tr>";
                                      }
                                }else{
 	                              echo "<tr><td>&nbsp;&nbsp;<b><font color=red>恭喜您，全部分页列表页面生成完毕!</font><b></td></tr>";
                                }
                           }else{
 	                           echo "<tr><td>&nbsp;<font style=font-size:10pt;><b><font color=red>《".$cname."》</font>列表生成完毕,暂停".CS_StopTime."秒后继续生成下个分类．．．</b></font><script language='javascript'>setTimeout('ReadGo();',".CS_StopTime."000);function ReadGo(){location.href='?ac=list&xid=".($xid+1)."&fid=".$fid."&op=".$op."';}</script></td></tr>";
                           }
                       }else{
 	                   echo "<tr><td>&nbsp;&nbsp;<b><font color=red>恭喜您，全部分页列表页面生成完毕!</font><b></td></tr>";
                       }
                   }else{

                         for($j=0;$j<$pagejs;$j++){
                              echo "<tr><td id=\"list_".$j."\"></td></tr>";
                         }
                         echo "<script>Htmllist('".$cname."','".$cid."',".$pagejs.",0);</script>";
                   }
              }else{
                  echo "<tr><td>抱歉，歌曲分类列表标签错误，获取不到每页显示条数,或者当前没有记录!</td></tr>";
              }
              unset($page_arr);
        }

}elseif($ac=='xuan'){  //选ID批量生成
        $Arr=explode(",",$id);
	for($j=0;$j<count($Arr);$j++){
 		echo "<tr><td id=\"html_".$j."\"></td></tr>";
	}
        echo "<script>Htmlgo('".$id."',".count($Arr).",0);</script>";

}else{  //歌曲播放页
       $arr=" WHERE CS_YID=0";
       if(!empty($sday)){ //生成今日更新
            $arr.=" and DATEDIFF(DATE(CS_AddTime),'".date('Y-m-d')."')>=-$sday";
       }elseif($ac=='cid' || !empty($cid)){  //按分类生成
            $arr.=" and CS_CID=".$cid."";
       }
       $sql="SELECT COUNT(*) AS total FROM ".Getdbname('dance').$arr."";
       $rows=$db->getRow($sql);
       $nums = $rows['total']; //获取总数
       $pagejs = ceil($nums / CS_Createpre); // 总页数

       if($pagejs<$pages){
 	    echo "<tr><td>&nbsp;&nbsp;<b><font color=red>恭喜您，全部歌曲播放页面生成完毕!</font><b></td></tr>
                 <script>if (confirm(\"是否生成歌曲分类列表页\")){window.location='?ac=list';}else{window.location='html.php';}</script>";
       }else{
            $start = ($pages - 1) * CS_Createpre;
            $sql = "SELECT * FROM ".Getdbname('dance').$arr." ORDER BY cs_addtime ASC LIMIT " . intval($start) . ", " . intval(CS_Createpre);
            $result=$db->query($sql);
            $j=0;$ids='';
            if($result){
	          while ($row = $db ->fetch_array($result)){
                     $ids.=$row['CS_ID'].",";
 		     echo "<tr><td id=\"html_".$j."\"></td></tr>";
                     $j=$j+1;
                  }
                  $ids=$ids.']';
	          $ids=CS_ReplaceStr($ids,",]","");
                  if($nums>CS_Createpre) $nums=CS_Createpre;
                  echo "<script>Htmlgo('".$ids."',".$nums.",0);</script>";
            }
       }
}

?>
</table>
</div>
</div>
</body>
</html>
