<?php
require_once(dirname(__FILE__)."/admin_inc.php");
/*******************************************************************************************
' Software name: DJCMS Content Management System
' Version:3.0
' Web: http://www.djcms.net
' Author: 心情(qq:1130731)
' Copyright (C) 2009-2010 DJCMS官方 版权所有
' 法律申明：DJCMS程序所有代码100%原创、未引入任何网上代码,对一切抄袭行为、坚决严肃追究法律责任
'*******************************************************************************************/
checkPower();
viewHead("广告管理"."-" .$menuList[5][0]);

$errorInfo = "信息填写不完整，请检查";
$action = getForm("action", "get");
switch ($action) {
	case "add" : addAds();popHtmlToJS();break;
	case "del" : delAds();break;
	case "delall" : delAll();break;
	case "edit" : editAds();popHtmlToJS();break;
	case "editsave" : editSave();break;
	case "addsave" : addSave();break;
	case "single" : makeSingleAd();break;
	case "viewads" : viewAds();break;
	default : main();popDiv();break;
}
viewFoot();

function main() {
	global $menuList;
	$id = getForm("id","get");
	$page = getForm("page","get"); 
	$number = 30;
	$allRecordset = DB::result_first('SELECT count(*) FROM '.DB::table('ads').'');
	$tpagecount = ceil($allRecordset / $number);
	if ($page > $tpagecount) $page = $tpagecount;
?>
<div class="container" id="cpcontainer">
<!--当前导航-->
<script type="text/JavaScript">if(parent.$('admincpnav')) parent.$('admincpnav').innerHTML='后台首页&nbsp;&raquo;&nbsp;<?=$menuList[5][0]?>&nbsp;&raquo;&nbsp;广告管理';</script>
<!--
<table align="left"><tr><td><input type="button" value="添加广告" onClick="location.href='?action=add'" class="btn" /></td></tr></table>
-->
<?php
   if ($allRecordset > 0) {
?>
<form action="?action=delall" method="post">
<table class="tb nobdt">
    <tr class="thead"><td colspan="15">广告管理</td></tr>
    <TR>
      <TD width="10%"  align="left">ID</TD>
      <TD width="30%" align="left">名称</TD>
       <TD width="15%" align="left">标识(文件名)</TD>
       <TD width="10%" align="center">描述</TD>
       <TD width="8%"  align="center">文件存在?</TD>
       <TD width="12%"  align="center">添加时间</TD>
      <TD width="20%"   align="center">操作</TD>
    </TR>
<?php
    $page = max(intval($page), 1);
    $offset = $number*($page-1);
	$query = DB::query("SELECT m_id,m_name,m_enname,m_des,m_addtime FROM ".DB::table('ads')." ORDER BY m_id DESC LIMIT $offset,$number");
	while ($row = DB::fetch($query)) {
        $m_id = $row['m_id'];
?>
     <tr id="adtr<?=$row['m_id']?>" name="adtr" <?=$id == $row['m_id'] ? 'class=\"editlast\"' : ''?> >
      <td  align="left"><input type="checkbox" value="<?=$row['m_id']?>" name="m_id[]" class="checkbox" /><?=$row['m_id']?></TD>
      <td align="left"><?=$row['m_name']?></TD>
      <td  align="left"><?=$row['m_enname']?></TD>
      <td  align="center" ><?=$row['m_des']?></TD>
            <td  align="center"><?=isAdsFileExist($row['m_enname'], $row['m_id'])?></TD>
      <td  align="center"><?php echo isCurrentDay($row['m_addtime']);?></TD>
      <td   align="center"><a href="?action=viewads&id=<?=$row['m_id']?>">预览</a>&nbsp;&nbsp;<a href="javascript:" onClick="viewCurrentAdTr('<?=$row['m_id']?>');openAdWin('callad','../js/{djcms:adfolder}/<?=$row['m_enname']?>.js','/{djcms:sitepath}')">调用广告</a>&nbsp;&nbsp;<a href="?action=edit&page=<?=$page?>&id=<?php echo $m_id;?>">编辑</a>&nbsp;&nbsp;<a  href="?action=del&id=<?=$row['m_id']?>" onclick="if(confirm('确定要删除吗')){return true;}else{return false;}">删除</a></TD>
    </tr>
<?php
    }
?>
    <tr>
    <td colspan="7">全选<input type="checkbox" name="chkall" id="chkall" class="checkbox" onclick="checkAll(this.checked,'input','m_id[]')" />反选<input type="checkbox" name="chkothers" id="chkothers" class="checkbox" onclick="checkOthers('input','m_id[]')" /><input type="submit" value="批量删除" onclick="if(confirm('确定要批量删除吗')){return true}else{return false}" class="btn"  />&nbsp;&nbsp; <?=fPageCount($allRecordset, $number, $page)?></td></tr>
</TABLE>
</form>
</div>
<?php
  } else {
     echo "<table class='tb'><tr><td>没有广告</td></tr></table>";
  }
}

function popDiv() {
?>
<div id="callad" style="top:100px; display:none;" class="popdiv">
	<div class="poptitie"><img src="../pic/btn_close.gif" alt="关闭" onClick="$('callad').style.display='none'" />广告调用路径 </div>
    <div class="popbody"><div>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td  width="60" valign="top">调用代码</td>
          <td><textarea cols="75" rows="4" id="adpath" onfocus="this.select();" ></textarea></td>
        </tr>
       <tr><td align="center" colspan="2"><font color="red">请手动复制以上文本框中的广告调用代码，粘贴到模板文件相应位置即可</font></td></tr>
        </table>
    </div></div>
</div>
<script type="text/javascript" src="imgs/drag.js"></script>
<?php
}

function popHtmlToJS() {
?>
<div id="htmltojs" style="top:100px; display:none;" class="popdiv">
	<div class="poptitie"><img src="../pic/btn_close.gif" alt="关闭" onClick="$('htmltojs').style.display='none'" />HTML转JS </div>
    <div class="popbody"><div>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td  width="60" valign="top">HTML代码</td>
          <td><textarea cols="75" rows="8" id="htmlstr" onkeyup="htmlToJs('htmlstr','jsstr')" onfocus="htmlToJs('htmlstr','jsstr')"></textarea></td>
        </tr>
         <tr>
          <td  width="60" valign="top">JS代码</td>
          <td><textarea cols="75" rows="8" id="jsstr" ></textarea></td>
        </tr>
       <tr><td align="center" colspan="2"><input type="button" value="插  入"  class="btn" onclick="insertHtmlToJsWin('htmltojs','m_content','jsstr')" /></td></tr>
        </table>
    </div></div>
</div>
<div id="jstohtml" style="top:100px; display:none;" class="popdiv">
	<div class="poptitie"><img src="../pic/btn_close.gif" alt="关闭" onClick="$('jstohtml').style.display='none'" />JS转HTML</div>
    <div class="popbody"><div>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td  width="60" valign="top">JS代码</td>
          <td><textarea cols="75" rows="8" id="jsstr2" onkeyup="jstohtml('jsstr2','htmlstr2')" onfocus="jstohtml('jsstr2','htmlstr2')"></textarea></td>
        </tr>
         <tr>
          <td  width="60" valign="top">HTML代码</td>
          <td><textarea cols="75" rows="8" id="htmlstr2" ></textarea></td>
        </tr>
       <tr><td align="center" colspan="2"><input type="button" value="插  入"  class="btn" onclick="insertHtmlToJsWin('jstohtml','m_content','jsstr2')" /></td></tr>
        </table>
    </div></div>
</div>
<script type="text/javascript" src="imgs/drag.js"></script>
<?php
}

function addAds() {
?>
<div class="container" id="cpcontainer">
<form action="?action=addsave" method="post" >
<table class="tb nobdt">
<tr class="thead"><th colspan="2">添加广告</th></tr>
    <TR>
      <TD vAlign=center width="11%" >广告名称：</TD>
      <TD width="89%" ><input type="text" size="50" name="m_name" /><font color="red">＊</font></TD>
    </TR>
    <TR>
      <TD >广告标识：</TD>
      <TD ><input type="text" size="50" name="m_enname" /><font color="red">＊</font></TD>
    </TR>
    <TR>
      <TD >广告描述：</TD>
      <TD ><input type="text" size="100" name="m_des" /></TD>
    </TR>
    <TR>
       <TD >广告内容：<br />(<font color="red">填写js代码</font>)<br /><input type="button" value="HTML转JS"  onclick="openHtmlToJsWin('htmltojs')" class="btn"/><br/><input type="button" value="JS转HTML"  onclick="openHtmlToJsWin('jstohtml')" class="btn"/></TD>
      <TD ><textarea name="m_content" id="m_content"  style="width:98%;font-family: Arial, Helvetica, sans-serif;font-size: 14px;" rows="20"  ></textarea><font color="red">＊</font></TD>
    </TR>
    <TR>
      <td></td><TD ><input type="submit" value="添 加 广 告" class="btn" />
      &nbsp;<input type="button" value="返   回"  class="btn" onClick="javascript:history.go(-1)" /></TD>
    </TR>
</td></tr></table>
</form>
</div>
<?php
}

function viewAds() {
?>
<div class="container" id="cpcontainer">
<form action="?action=addsave" method="post" >
<table class="tb nobdt">
<tr class="thead"><th colspan="2">预览广告</th></tr>
<tr><td style="text-align:center">
<table  style="border:#BBDDFF solid 2px;"><tr><td>
<?php
    $cid = getForm("id","get");
	$row = DB::fetch_first('SELECT m_content FROM '.DB::table('ads').' WHERE m_id=$cid');
	echo ("<script>");
	echo replacestr(decodeHtml($row['m_content']), "+sitePath+", "+'".sitePath."'+");
	echo ("</script>");
?>
</td></tr></table>
</td></tr>
<tr><td><input type="button" value="返   回"  class="btn" onClick="javascript:history.go(-1)" /></td></tr></table>
</form>
</div>
<?php
}

function editAds() {
	$id = getForm("id","get");$page = getForm("page","get");
	$row = DB::fetch_first('SELECT * FROM '.DB::table('ads').' WHERE m_id='.$id);
?>
<div class="container" id="cpcontainer">
<form action="?action=editsave&id=<?=$id?>&page=<?=$page?>" method="post" >
<table class="tb nobdt">
<tr class="thead"><th colspan="2">修改广告</th></tr>
    <TR>
      <TD vAlign=center width="11%" >广告名称：</TD>
      <TD width="89%" ><input type="text" size="50" name="m_name" value="<?=$row['m_name']?>" /><font color="red">＊</font></TD>
    </TR>
    <TR>
      <TD >广告标识：</TD>
      <TD ><input type="text" size="50" name="m_enname" value="<?=$row['m_enname']?>" /><font color="red">＊</font></TD>
    </TR>
    <TR>
      <TD >广告描述：</TD>
      <TD ><input type="text" size="100" name="m_des" value="<?=$row['m_des']?>" /></TD>
    </TR>
    <TR>
        <TD >广告内容：<br />(<font color="red">填写js代码</font>)<br /><input type="button" value="HTML转JS"  onclick="openHtmlToJsWin('htmltojs')" class="btn"/><br/><input type="button" value="JS转HTML"  onclick="openHtmlToJsWin('jstohtml')" class="btn"/></TD>
      <TD ><textarea name="m_content" style="width:98%;font-family: Arial, Helvetica, sans-serif;font-size: 14px;" rows="20" ><?=decodeHtml($row['m_content'])?></textarea><font color="red">＊</font></TD>
    </TR>
    <TR  >
      <td></td><TD ><input type="submit" value="修 改 广 告" class="btn" />
      &nbsp;<input type="button" value="返   回"  class="btn" onClick="javascript:history.go(-1)" /></TD>
    </TR>
</td></tr></table>
</form>
</div>
<?php
}

function addSave() {
	global $errorInfo;
	$m_name = getForm("m_name","post");$m_enname = getForm("m_enname","post");$m_des = getForm("m_des","post");$m_content = getForm("m_content","post");
	$m_encontent=encodeHtml($m_content);
	if (empty($m_name) || empty($m_enname))  die($errorInfo);
	$feedarr = array(
	'm_name' => $m_name,
	'm_enname' => $m_enname,
	'm_des' => $m_des,
	'm_content' => $m_content,
	'm_addtime' => time()
	);
	DB::insert('ads', $feedarr);
	createTextFile($m_content, "../js/".siteAd."/".$m_enname.".js");
	alertMsg("添加成功", "admin_ads.php");
}

function delAll() {
	$ids = getForm("m_id","post");
	$query = DB::query("SELECT m_id,m_enname FROM ".DB::table('ads')." WHERE m_id IN (".dimplode($ids).")");
	while ($row = DB::fetch($query)) {
		delFile("../js/".siteAd."/".$row['m_enname'].".js");
		DB::delete('ads', array('m_id' => $row['m_id']));
	}
	alertMsg("", "admin_ads.php");
}

function delAds(){
	$id = getForm("id","get");
	$filename = DB::result_first("SELECT m_enname FROM ".DB::table('ads')." WHERE m_id='$id'");
	delFile("../js/".siteAd."/".$filename.".js");
	DB::delete('ads', array('m_id' => $id));
	alertMsg("删除成功", "admin_ads.php");
}


function editSave() {
	global $errorInfo;
	$id = getForm("id","get");$page=getForm("page","get");$m_name = getForm("m_name","post");$m_enname = getForm("m_enname","post");$m_des = getForm("m_des","post");$m_content=getForm("m_content","post");
	$m_encontent=encodeHtml($m_content);
	if (empty($m_name) || empty($m_enname))  die($errorInfo);
	$feedarr = array(
	'm_name' => $m_name,
	'm_enname' => $m_enname,
	'm_des' => $m_des,
	'm_content' => $m_content,
	'm_addtime' => time()
	);
	DB::update('ads', $feedarr, array('m_id' => $id));
	createTextFile($m_content, "../js/".siteAd."/$m_enname.js");
	alertMsg("修改成功", "admin_ads.php?id=$id&page=$page");
}

function isAdsFileExist($filename, $id) {
	$adFile="../js/".siteAd."/$filename.js";
	if (isExistFile($adFile)) echo "<a href='".$adFile."'><img src='imgs/yes.gif' border='0'/></a>"; else  echo "<a href='?action=single&id=".$id."'><img src='imgs/no.gif' border='0'/></a>";
}

function makeSingleAd() {
	$id = getForm("id","get");
	$row = DB::fetch_first("SELECT m_enname,m_content FROM ".DB::table('ads')." WHERE m_id='$id'");
	createTextFile(decodeHtml($row['m_content']), "../js/".siteAd."/".$row['m_enname'].".js");
	alertMsg("", "admin_ads.php");
}
?>
