<?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('1_4');
$ac=CS_Request("ac");
$op=CS_Request("op");
if($ac=='ty'){
   $id=CS_Request("id");
   echo '广告代码:<input id="url" onclick="jscript:copyUrl()" type="text" size="46" value="<script src={csdjcms:path}ads/'.$id.'.js></script>">
         <br><font color=red>＊请将以上代码放置在你网站模板的对应位置！</font><script type="text/javascript"> 
   var obj=document.getElementById("url");
   function copyUrl(){
	if (obj) { 								
	obj.select();
	js=obj.createTextRange();
	js.execCommand("Copy");
	alert("广告代码已经复制，您可以将它粘贴到往摸班相应位置。");
	}
   }
</script>';
   exit();
}
?>
<!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" />
<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 language="javascript"> 
$(document).ready(function(){
	$("#addads").click(function(){
		var CS_Name=$("#CS_Name").val();
		var CS_Html=$("#CS_Html").val();
		var CS_Js=$("#CS_Js").val();
	$("#CS_Name").css("border-color","#666 #ccc #ccc #666");
	$("#CS_Html").css("border-color","#666 #ccc #ccc #666");
	$("#CS_Js").css("border-color","#666 #ccc #ccc #666");
	if(CS_Name==""){
		$("#CS_Name").css("border","1px solid red");
		$("#Msg").text("请填写站点名称！").css("color","red");
		return false;
		}
	else if(CS_Html==""){
		$("#CS_Html").css("border","1px solid red");
		$("#Msg").text("请填写广告代码！").css("color","red");
		return false;
		}
	});
});

function del(_id){ 
  if (confirm("确定删除吗？")){
    window.location='?op=del&id='+_id;
    return true;
  }else{
     return false;
  }		
}

function change(){
 document.getElementById('CS_Js').value="document.writeln(\""+document.getElementById('CS_Html').value.replace(/\\/g,"\\\\").replace(/\\/g,"\\/").replace(/\'/g,"\\\'").replace(/\"/g,"\\\"").split('\r\n').join("\");\ndocument.writeln(\"")+"\");"
}
</script>
</head>
<body>
<div class="wrap">
<div class="nav">
	<ul class="cc">
		<li <?php if($ac!="edit") echo 'class="current"';?>><a href="ads.php">广告列表</a></li>
		<li <?php if($ac=="edit") echo 'class="current"';?>><a href="ads.php?ac=edit">添加广告</a></li>
	</ul>
</div>
<?php
if($ac=='look'){
   $id=CS_Request("id");
?>
<div class="h_a">广告浏览</div>
<div class="table_full">
	<table width="100%">
		<tr>
			<td>
				<script src="../../ads/<?php echo $id?>.js"></script>
			</td>
		</tr>
	</table>
</div>
<?php
}elseif($ac=='edit'){
   $id=CS_Request("id");
   if($id){
	   $row=$db->getrow("SELECT * FROM ".Getdbname('ads')." Where CS_ID='".$id."'");
	   if($row){
		$Arr=array($row['CS_Name'],$row['CS_Html'],$row['CS_Js']);
	   }
   }else{
		$Arr=array('','','');
   }
?>
<form method="post" class="J_ajaxForm" action="?ac=edit&op=go">
<div class="h_a">修改广告</div>
<div class="table_full">
	<table width="100%">
		<col class="th" />
		<col width="500" />
		<col />
		<tr><input name="CS_ID" type="hidden" value="<?php echo $id?>" />
			<th>广告名称</th>
			<td>
				<div class="mb10"><input id="CS_Name" name="CS_Name" type="text" class="input length_5" value="<?php echo $Arr[0]?>"></div>
			</td>
			<td><font color=red>＊如：首页960X60第一排</font></td>
		</tr>
		<tr>
			<th>广告代码(html)</th>
			<td>
				<div class="mb10"><textarea style="width:100%;height:150px;" onKeyUp="change()" onFocus="change()" style="width:80%;font-family: Arial, Helvetica, sans-serif;font-size: 14px;" id="CS_Html" name="CS_Html"><?php echo $Arr[1]?></textarea></div>
			</td>
			<td><font color=red>＊</font></td>
		</tr>
		<tr>
			<th>广告代码( Js )</th>
			<td>
				<div class="mb10"><textarea style="width:100%;height:150px;" style="width:80%;font-family: Arial, Helvetica, sans-serif;font-size: 14px;" id="CS_Js" name="CS_Js"><?php echo $Arr[2]?></textarea></div>
			</td>
			<td><font color=red>＊</font>不用填写</td>
		</tr>
	</table>
</div>
<div class="btn_wrap">
	<div class="btn_wrap_pd">
		<button class="btn btn_submit mr10 J_ajax_submit_btn" id="addads" type="submit">提交保存</button>
                <div class="tips_error" id="Msg"></div>
	</div>
</div></form>
<!-- end -->
<?php
    if($op=='go'){
           $CS_ID=CS_Request("CS_ID");
           $CS_Name=CS_Request("CS_Name");
           $CS_Html=CS_Request("CS_Html");
           $CS_Js=CS_Request("CS_Js");
           if(empty($CS_Name)||empty($CS_Html)||empty($CS_Js)) exit("<script>document.getElementById('Msg').innerHTML='请把资料填写完整!';</script>");

           if(get_magic_quotes_gpc()){
               $CS_Js=$CS_Js;
           }else{
               $CS_Js=str_replace("\\", "\\\\",$CS_Js);
           }

           if(!empty($CS_ID)){  //修改
                  $title="修改";
                  $sql="update ".Getdbname('ads')." set CS_Name='".$CS_Name."',CS_Html='".$CS_Html."',CS_Js='".$CS_Js."' where CS_ID=".$CS_ID;
           }else{   //新增
                  $title="添加";
                  $sql="Insert ".Getdbname('ads')."  (CS_Name,CS_Html,CS_Js) values ('".$CS_Name."','".$CS_Html."','".$CS_Js."')";
           }

           if($db->query($sql)){
              if(empty($CS_ID)){
                  $row=$db->getrow("SELECT * FROM ".Getdbname('ads')." Where CS_Name='".$CS_Name."'");
                  $CS_ID=$row['CS_ID'];
              }
                write_file("../../ads/".$CS_ID.".js", html_entity_decode(stripslashes($CS_Js))); //生成广告JS  

               echo '<script>document.getElementById("Msg").className="tips_success";$("#Msg").text("恭喜,'.$title.'成功!").css("color","#026E12");</script>';
	       echo "<script>setTimeout(\"this.parent.$('.webox').css({display:'none'});this.parent.$('.background').css({display:'none'});window.location='ads.php';\",2000);</script>";
           }else{
               echo '<script>$("#Msg").text("抱歉,'.$title.'失败!").css("color","red");</script>';
               echo "<script>setTimeout(\"window.location='javascript:history.go(-1);'\",2000);</script>";
           }
    }

}else{

?>
<div class="table_list">
<table width="100%" id="dellist">
		<thead>
			<tr>
				<td><label>ID</label></td>
				<td>名称</td>
				<td><span>调用代码</span></td>
				<td><span>浏览</span></td>
				<td>编辑/删除</td>
			</tr>
		</thead>
<?php 
	global $db;
	$sql="select * from ".Getdbname('ads')."";
	$result=$db->query($sql);
	$tempcount=$db->num_rows($result);
        if($tempcount=="0"){
		echo '<tbody>
			<tr><td colspan="4" class="tac">啊哦，没有符合条件的内容！</td></tr>
		 </tbody>';

        }else{
                if($result){
                     while($row=$db->fetch_array($result)){
?>
                        <tr>
				<td>&nbsp;<?php echo $row['CS_ID']?></td>
				<td><a href="?ac=edit&id=<?php echo $row['CS_ID']?>"><?php echo $row['CS_Name']?></td>
				<td><a href="javascript:Popsboxif('广告JS代码调用',450,125,'?ac=ty&id=<?php echo $row['CS_ID']?>');"><font color=red>调用代码</font></a></td>
				<td><a href="?ac=look&id=<?php echo $row['CS_ID']?>">浏览</a></td>
				<td><a href="?ac=edit&id=<?php echo $row['CS_ID']?>">编辑</a> &nbsp;&nbsp;<a style="cursor:pointer;"  onclick="del(<?php echo $row['CS_ID']?>)">删除</a></td>
			</tr>
<?php		
                     }
                }
        }
?>
	</table>
</div>
<?php
   if($op=='del'){   //删除
       $CS_ID=CS_Request("id");		
                $sql="delete from ".Getdbname('ads')." where CS_ID=".$CS_ID;
		if($db->query($sql)){
		     if(file_exists("../../ads/".$CS_ID.".js")){
				@unlink("../../ads/".$CS_ID.".js");
		     }
                     echo "<script>window.location='ads.php';</script>";
                }else{
                     echo "<script>window.location='ads.php';</script>";
                }
   }
}

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

