<?php
/*
'**************************************************************************************************
' 软件名称: 程氏音乐系统 Content Management chshdjcms
' 版本编号: V 3.0
' 官方网站: http://www.chshcms.com
' 官方论坛：http://bbs.chshcms.com
' 版权所有: 程氏网络科技    程氏(QQ:848769359)
' 郑重声明:
'    1、任何个人或组织不得在未经授权的情况下删除、修改、拷贝本软件及其他副本上一切关于版权的信息；
'    2、程氏网络科技保留此软件的法律追究权利
'**************************************************************************************************
*/
include_once("../../lib/cs_conn.php");
include_once("../../lib/cs_common.php");
include_once("../loginstate.php");
$ac=CS_Request("ac");
$op=CS_Request("op");
$id=CS_Request("id");
?>
<!doctype html>
<html>
<head>
<meta charset="gbk">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>chshdjcms management</title>
<link href="../res/css/admin_style.css" rel="stylesheet" />
<link href="../../public/css/webox.css" rel="stylesheet" />
<script type="text/javascript" src="../res/js/ajax.js"></script>
<script language="javascript" src="../res/js/jquery.js"></script>
<script src="../../public/js/webox.js"></script>
<script type="text/javascript"> 

function showfields(){
	var exptable = $('#exptable').val();
	$.get("../ajax/ajax.php?ac=zdlist&name="+exptable,function(R){
		$("#zname").html('选择表字段');	
		$("#fields").html(R);												
	})
}

function rpfield(v){
	$('#rpfield').val(v); 
}
$(document).ready(function(){
	$("#tihuan").click(function(){
		var rpfield=document.getElementById('rpfield').value;
		var rpstring=document.getElementById('rpstring').value;
		var tostring=document.getElementById('tostring').value;
	if(rpfield==""){
		$("#Msg").text("请选择表在选择要替换的字段！").css("color","red");
		return false;
		}
	else if(rpstring==""){
		$("#Msg").text("请填写要替换的内容！").css("color","red");
		return false;
		}
	else if(tostring==""){
		$("#Msg").text("请填写替换为的内容！").css("color","red");
		return false;
		}
	});
});
$(document).ready(function(){
	$("#sqlgo").click(function(){
		var sql=document.getElementById('sql').value;
	if(sql==""){
		$("#Msg").text("请填写要执行的语句！").css("color","red");
		return false;
		}
	});
});
</script>
</head>
<body>
<div class="wrap">
	<div class="nav">
		<ul class="cc">
			<li <?php if($ac=='') echo 'class="current"';?>><a href="?">数据批量替换</a></li>
			<li <?php if($ac=='sql') echo 'class="current"';?>><a href="?ac=sql">SQL语句执行</a></li>
			<li <?php if($ac=='hits') echo 'class="current"';?>><a href="?ac=hits">批量随机人气</a></li>
		</ul>
	</div>
<?php if($ac==""){ ?>
	<div class="h_a">数据批量操作</div>
	<form action="?op=thok" method="post" class="J_ajaxForm" id="J_backup_form">
	<div class="table_full">
		<table width="100%">
			<col class="th" />
			<tr>
				<th>选择数据表</th>
				<td>
                                     <select name="exptable" id="exptable"  size="10" style="height:150px; width:500px" onChange="showfields()">
<?php
		$rs=mysql_query("show tables like '".CS_SqlPrefix.""."%'");
		while ($row = mysql_fetch_array($rs)) {
                    echo '<option value="'.$row[0].'">'.$row[0].'</option>';
                }
?>
                                     </select>

				</td>
			</tr>
			<tr>
				<th id="zname"></th>
				<td id='fields'></td>
			</tr>
			<tr>
				<th>要替换的字段</th>
				<td>
					<input type="text" class="input length_5 mr5" id="rpfield" name="rpfield" value="">
				</td>
				<td><div class="fun_tips">要替换的字段</div></td>
			</tr>
 
			<tr>
				<th>被替换的内容</th>
				<td>
					<input type="text" class="input length_5 mr5" id="rpstring" name="rpstring" value="">
				</td>
				<td><div class="fun_tips">输入要替换的内容</div></td>
			</tr>
 
			<tr>
				<th>替换为的内容</th>
				<td>
					<input type="text" class="input length_5 mr5" id="tostring" name="tostring" value="">
				</td>
				<td><div class="fun_tips">输入替换为的内容</div></td>
			</tr>
 
			<tr>
				<th>选择替换条件</th>
				<td>
					<input type="text" class="input length_5 mr5" id="condition" name="condition" value="">
				</td>
				<td><div class="fun_tips">(为空全部替换 不熟悉SQL语句的不要填写,请遵循SQL的条件语句 如cs_cid=1)</div></td>
			</tr>
		</table>
	</div>
<div class="btn_wrap">
	<div class="btn_wrap_pd">
		<button class="btn btn_submit J_ajax_submit_btn" id="tihuan" onClick="return confirm('一旦执行后将无法恢复，请确定条件语句正确无误，或者备份好数据库以防万一!')" type="submit">确定提交</button>
	<div class="tips_error" id="Msg"></div>
	</div>
</div>
</form>
<?php }elseif($ac=='sql'){ ?>
	<div class="h_a">SQL语句执行</div>
	<form action="?ac=sql&op=sqlok" method="post" class="J_ajaxForm" id="J_backup_form">
	<div class="table_full">
		<table width="100%">
			<col width="200" />
			<tr>
				<th>执行SQL语句：<br><font color=#ff6600>除非确定您的SQL语句安全 否则不要轻易在此处运行!</font></th>
				<td><textarea style="width:500px;height:150px;" id="sql" name="sqlstr"></textarea></td>
			</tr>
			<tr>
				<th>常用语句对照：</th>
				<td>
<br>
        <p>&nbsp;<strong>1.删除数据</strong></p>
        <p>&nbsp;&nbsp; delete  from cs_dance&nbsp;&nbsp; 删除所有歌曲数据</p>
	<p>&nbsp;&nbsp; delete  from cs_dance where CS_ID=1000 &nbsp; 删除指定的第几条歌曲数据</p>
	<p>&nbsp;&nbsp; delete  from cs_dance where CS_Singer Like '%周节伦%'&nbsp;&nbsp; 删除CS_Singer字段里有&quot;周节伦&quot;的歌曲数据</p>
	<p><strong>&nbsp;2.修改数据</strong></p>
	<p>&nbsp;&nbsp; update cs_dance set CS_Hits=1&nbsp;&nbsp; 将所有CS_Hits字段里的值修改成&quot;1&quot;</p>
	<p>&nbsp;&nbsp; update cs_dance set CS_Hits=1 where CS_ID=1000&nbsp; 指定的第几条数据把CS_Hits字段里的值修改成&quot;1&quot;</p>

                                </td>
			</tr>
		</table>
	</div>
<div class="btn_wrap">
	<div class="btn_wrap_pd">
		<button class="btn btn_submit J_ajax_submit_btn" id="sqlgo" onClick="return confirm('一旦执行后将无法恢复，请确定条件语句正确无误，或者备份好数据库以防万一!')" type="submit">确定提交</button>
	<div class="tips_error" id="Msg"></div>
	</div>
</div>
</form>
<?php }elseif($ac=="hits"){ ?>
	<div class="h_a">批量随机人气</div>
	<div class="table_full">
		<table width="100%">
	<?php 
	         if($op=='go'){ 
                         $str='';
		         $kshits=CS_Request("start_hits");
		         $jshits=CS_Request("end_hits");
		         $cid=CS_Request("CS_CID");
		         $sday=CS_Request("sday");
		         $ts=CS_Request("ts");
                         $hitsfl=CS_Request("hitsfl");
		         if(empty($ts)) $ts=30;
		         $page=CS_Request("pages");
		         if(empty($page)) $page=1;
		         if(empty($kshits)) $shits=1;
		         if(empty($jshits)) $ehits=1000;
		        if(!empty($sday))  $str.=" and DATEDIFF(DATE(CS_AddTime),'".date('Y-m-d')."')>=-$sday";
		        if(!empty($cid))  $str.=" and CS_CID=".$cid."";
		        $sql="select * from ".Getdbname($id)." where CS_YID=0 ".$str."";
		        $Arr=getpagerow($sql,$ts);//sql,每页显示条数
		        $result=$db->query($Arr[2]);
	                if($result){
                               echo("<tr><td>&nbsp;正在随机人气第<font style='color:red; font-size:15px; font-style:italic'>".$page."</font>页，共<font style='color:red; font-size:15px; font-style:italic'>".$Arr[3]."</font>页&nbsp;&nbsp;&nbsp;<a href='?ac=hits'><font color=#0000ff>紧急停止</font></a></td></tr>");
                              while ($row = $db ->fetch_array($result)){
			           $Hits=mt_rand($kshits,$jshits);
			           $sql2="update ".Getdbname($id)." set ".$hitsfl."=".$Hits." where CS_ID=".$row['CS_ID']."";
			           $db->query($sql2);	  
			           echo "<tr><td>&nbsp;<font style=font-size:10pt;><font color=red>".$row['CS_Name']."</font>的人气变为了<font color=red>".$Hits."</font></font></td></tr>";
			      }
			      if($page>=$Arr[3]){
				      die("<tr><td>&nbsp;<font style=font-size:10pt;>所有的随机人气完毕．．．．</font><script language='javascript'>setTimeout('ReadGo();',".(5000).");function ReadGo(){location.href='?ac=hits';}</script></td></tr>");
			      }else{	
				      die("<tr><td>&nbsp;<font style=font-size:10pt;>暂停5秒后继续随机人气 ．．．．</font><script language='javascript'>setTimeout('ReadGo();',".(5000).");function ReadGo(){location.href='?ac=hits&op=go&id=".$id."&pages=".($page+1)."&CS_CID=".$cid."&start_hits=".$kshits."&hitsfl=".$hitsfl."&end_hits=".$jshits."&sday=".$sday."&ts=".$ts."';}</script></td></tr>");
			      }
	                }

                 }else{
	?>
			<col width="100" />
			<col width="250"/>
			<col width="210"/>
			<col width="320"/>
 			<tr><td colspan="5"></td></tr>
                        <form action="?ac=hits&op=go&id=dance" method="post" class="J_ajaxForm" id="J_backup_form">
			<tr>
				<th>歌曲随机人气:</th>
				<td>
					人气范围值：：<input type="text" class="input length_1" name="start_hits" value="1">至<input type="text" class="input length_1" name="end_hits" value="1000">
				</td>
				<td>
					 歌曲分类：
					 <select class="select_2" name="CS_CID">
                                                <option value="0">全部分类</option>
            					 <?php
              						$sqlclass="select * from ".Getdbname('class')." where CS_YID=0 and CS_SID=1 and CS_FID=0";
									$results=$db->query($sqlclass);
									if($results){
										while ($row3=$db->fetch_array($results)){
       
										         echo "<option value='".$row3['CS_ID']."' >".$row3['CS_Name']."</option>";

		                                                                     //处理此分类下的子分类
		                                                                     $sql2="select * from ".Getdbname('class')." where CS_FID=".$row3['CS_ID']."";
		                                                                     $class2=$db->query($sql2);
		                                                                     if($class2){
			                                                                     while($row2=$db->fetch_array($class2)){
										                    echo "<option value='".$row2['CS_ID']."' >├ ".$row2['CS_Name']."</option>";
			                                                                     }
		                                                                     }
										}
									}
			  
			  			 ?>            
                                         </select>
				</td>
				<td>
                			 指定天数:
                			 <select name="sday">
                				 <option value="">不限</option>
                   			  <?php
                    				 for($i=1;$i<16;$i++){
							echo "<option value=\"".$i."\">".$i."天内</option>\r\n";
							}
					  ?>
                			 </select>&nbsp;&nbsp;
					 &nbsp;人气分类：
                			 <select name="hitsfl">
                				 <option value="CS_Hits">年度人气</option>
                				 <option value="CS_Rhits">每日人气</option>
                				 <option value="CS_Zhits">每周人气</option>
                				 <option value="CS_Yhits">每月人气</option>
                				 <option value="CS_Shits">收藏人气</option>
                				 <option value="CS_Xhits">下载人气</option>
                				 <option value="CS_Dhits">被顶人气</option>
                			 </select>
				</td>
				<td>
					 每页：<input type="text" class="input length_1" name="ts" value="30">条&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                			 &nbsp; <input name="submit" type="submit" class="btn btn_submit J_ajax_submit_btn" value="开始随机设置人气" />
				</td>
			</tr> </form> 
  			<tr><td colspan="5"></td></tr>
                        <form action="?ac=hits&op=go&id=news" method="post" class="J_ajaxForm" id="J_backup_form">
			<tr>
				<th>文章随机人气:</th>
				<td>
					人气范围值：：<input type="text" class="input length_1" name="start_hits" value="1">至<input type="text" class="input length_1" name="end_hits" value="1000">
				</td>
				<td>
					 文章分类：
					 <select class="select_2" name="CS_CID">
                                                <option value="0">全部分类</option>
            					 <?php
              						$sqlclass="select * from ".Getdbname('class')." where CS_YID=0 and CS_SID=2 and CS_FID=0";
									$results=$db->query($sqlclass);
									if($results){
										while ($row3=$db->fetch_array($results)){
       
										         echo "<option value='".$row3['CS_ID']."' >".$row3['CS_Name']."</option>";

		                                                                     //处理此分类下的子分类
		                                                                     $sql2="select * from ".Getdbname('class')." where CS_FID=".$row3['CS_ID']."";
		                                                                     $class2=$db->query($sql2);
		                                                                     if($class2){
			                                                                     while($row2=$db->fetch_array($class2)){
										                    echo "<option value='".$row2['CS_ID']."' >├ ".$row2['CS_Name']."</option>";
			                                                                     }
		                                                                     }
										}
									}
			  
			  			 ?>            
                                         </select>
				</td>
				<td>
                			 指定天数:
                			 <select name="sday">
                				 <option value="">不限</option>
                   			  <?php
                    				 for($i=1;$i<16;$i++){
							echo "<option value=\"".$i."\">".$i."天内</option>\r\n";
							}
					  ?>
                			 </select>&nbsp;&nbsp;
					 &nbsp;人气分类：
                			 <select name="hitsfl">
                				 <option value="CS_Hits">年度人气</option>
                				 <option value="CS_Rhits">每日人气</option>
                				 <option value="CS_Zhits">每周人气</option>
                				 <option value="CS_Yhits">每月人气</option>
                			 </select>
				</td>
				<td>
					 每页：<input type="text" class="input length_1" name="start_hits" value="20">条&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                			 &nbsp; <input name="submit" type="submit" class="btn btn_submit J_ajax_submit_btn" value="开始随机设置人气" />
				</td>
			</tr> </form> 
 			<tr><td colspan="5"></td></tr>
                        <form action="?ac=hits&op=go&id=topic" method="post" class="J_ajaxForm" id="J_backup_form">
			<tr>
				<th>专集随机人气:</th>
				<td>
					人气范围值：：<input type="text" class="input length_1" name="start_hits" value="1">至<input type="text" class="input length_1" name="end_hits" value="1000">
				</td>
				<td>
					 专集分类：
					 <select class="select_2" name="CS_CID">
                                                <option value="0">全部分类</option>
            					 <?php
              						$sqlclass="select * from ".Getdbname('class')." where CS_YID=0 and CS_FID=0";
									$results=$db->query($sqlclass);
									if($results){
										while ($row3=$db->fetch_array($results)){
       
										         echo "<option value='".$row3['CS_ID']."' >".$row3['CS_Name']."</option>";

		                                                                     //处理此分类下的子分类
		                                                                     $sql2="select * from ".Getdbname('class')." where CS_FID=".$row3['CS_ID']."";
		                                                                     $class2=$db->query($sql2);
		                                                                     if($class2){
			                                                                     while($row2=$db->fetch_array($class2)){
										                    echo "<option value='".$row2['CS_ID']."' >├ ".$row2['CS_Name']."</option>";
			                                                                     }
		                                                                     }
										}
									}
			  
			  			 ?>            
                                         </select>
				</td>
				<td>
                			 指定天数:
                			 <select name="sday">
                				 <option value="">不限</option>
                   			  <?php
                    				 for($i=1;$i<16;$i++){
							echo "<option value=\"".$i."\">".$i."天内</option>\r\n";
							}
					  ?>
                			 </select>&nbsp;&nbsp;
					 &nbsp;人气分类：
                			 <select name="hitsfl">
                				 <option value="CS_Hits">年度人气</option>
                				 <option value="CS_Rhits">每日人气</option>
                				 <option value="CS_Zhits">每周人气</option>
                				 <option value="CS_Yhits">每月人气</option>
                			 </select>
				</td>
				<td>
					 每页：<input type="text" class="input length_1" name="start_hits" value="20">条&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                			 &nbsp; <input name="submit" type="submit" class="btn btn_submit J_ajax_submit_btn" value="开始随机设置人气" />
				</td>
			</tr> </form> 
        <?php
                 }
	?>
		</table>
	</div>
</form>
<?php
}
    if($op=='thok'){
        $exptable=CS_Request("exptable");
        $rpfield=CS_Request("rpfield");
        $rpstring=CS_Request("rpstring");
        $tostring=CS_Request("tostring");
        $condition=CS_Request("condition");
        if(!empty($condition)) $condition=" and ".$condition;
                if(empty($exptable)||empty($rpfield)||empty($rpstring)||empty($tostring)){
                          echo '<script>$("#Msg").text("抱歉，资料不完整!").css("color","red");</script>';
                          exit("<script>setTimeout(\"history.back(1);\",20000);</script>");
                }
        $sql="select ".$rpfield." ,CS_ID from ".$exptable." where ".$rpfield." like '%".$rpstring."%' ".$condition."";
	$result=$db->query($sql);
	if($result){
		while($row=$db->fetch_array($result)){
			$zdneir=CS_ReplaceStr($row[$rpfield],$rpstring,$tostring);
			$sql3="update ".$exptable." set ".$rpfield."='".$zdneir."' where CS_ID=".$row['CS_ID']."";
			$db->query($sql3);
		}
	}
        echo '<script>document.getElementById("Msg").className="tips_success";$("#Msg").text("恭喜,字符已全部替换完成．．．!").css("color","#026E12");</script>';
	echo "<script>setTimeout(\"window.location='?ac'\",3000);</script>";	
    }
    if($op=='sqlok'){
        $sql=CS_Request("sqlstr");
        if(strtolower(substr($sql,0,6))!="update" && strtolower(substr($sql,0,6))!="delete"){
            echo '<script>$("#Msg").text("抱歉，SQL语句执行失败!").css("color","red");</script>';
	    echo "<script>setTimeout(\"history.back(1);\",3000);</script>";
        }else{
            if($db->query($sql)){
	           $num=mysql_affected_rows();
                   echo '<script>document.getElementById("Msg").className="tips_success";$("#Msg").text("恭喜,SQL语句执行成功!").css("color","#026E12");</script>';
	           echo "<script>setTimeout(\"window.location='?ac=sql'\",3000);</script>";
            }else{
                   echo '<script>$("#Msg").text("抱歉，SQL语句执行失败!").css("color","red");</script>';
	           echo "<script>setTimeout(\"history.back(1);\",3000);</script>";
            }
        }
    }
?>
</div>
</body>
</html>

