<?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%原创、未引入任何网上代码,对一切抄袭行为、坚决严肃追究法律责任
'*******************************************************************************************/
viewHead("自定义标签管理"."-" .$menuList[2][0]);
$errorInfo="信息填写不完整，请检查";$errorSelflabel="已经存在此标签，请更换名称";
$action = getForm("action", "get");
switch ($action) {
	case "add" : addSelfLabel();break;
	case "del" : delSelfLabel();break;
	case "delall" : delAll();break;
	case "edit" : editSelfLabel();break;
	case "editsave" : editSave();break;
	case "addsave" : addSave();break;
	default : main();popDiv();break;
} 
viewFoot();

function main() {
	global $menuList;
	$id = getForm("id","get");
	$page = getForm("page","get") ;
    $number = 30;
    $page = max(intval($page), 1);
	$offset = $number*($page-1);
	$allRecordset = DB::result_first('SELECT count(*) FROM '.DB::table('selflabel').'');
?>
<div class="container" id="cpcontainer">
<!--当前导航-->
<script type="text/JavaScript">if(parent.$('admincpnav')) parent.$('admincpnav').innerHTML='后台首页&nbsp;&raquo;&nbsp;<?php echo $menuList[2][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 name="selflabelform" method="post">
<table class="tb">
<tr class="thead"><th colspan="6">自定义标签管理</th></tr>
    <TR align="center">
      <TD>ID</TD>
      <TD>名称</TD>
       <TD>标签代码</TD>
       <TD>描述</TD>
       <TD>添加时间</TD>
      <TD>操作</TD>
    </TR>
<?php
    $query = DB::query("SELECT m_id,m_name,m_content,m_des,m_addtime FROM ".DB::table('selflabel')."  ORDER BY m_id DESC LIMIT $offset,$number");
	while ($row = DB::fetch($query)) {
?>
     <TR id="adtr<?=$row['m_id']?>" name="adtr" <?php if($id == $row['m_id']) echo " class=\"editlast\"";?> align="center">
      <TD><input type="checkbox" value="<?=$row['m_id']?>" name="m_id[]"  class="checkbox" /><?=$row['m_id']?></TD>
      <TD><?=$row['m_name']?></TD>
      <TD>{self:<?=$row['m_name']?>}</TD>
      <TD><?=$row['m_des']?></TD>
      <TD><?=isCurrentDay($row['m_addtime'])?></TD>
      <TD><a href="#" onclick="viewCurrentAdTr('<?=$row['m_id']?>');copyToClipboard('{self:<?=$row['m_name']?>}');">调用标签</a> <a href="#" onClick="viewCurrentAdTr('<?=$row['m_id']?>');openSelfLabelWin('selflabel','<?=$row['m_id']?>')">查看内容</a>&nbsp;&nbsp;<a href="?action=edit&page=<?=$page?>&id=<?=$row['m_id']?>">编辑</a>&nbsp;&nbsp;<a  onclick="if(confirm('确定要删除吗')){return true;}else{return false;}" href="?action=del&id=<?=$row['m_id']?>">删除</a></TD>
    </TR>
<?php
   }
?>
    <tr><td  colspan="6">全选<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('确定要删除吗')){selflabelform.action='?action=delall';}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 addSelfLabel(){
?>
<div class="container" id="cpcontainer">
<form action="?action=addsave" method="post" >
<table class="tb">
<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="100" name="m_des" /></TD>
    </TR>
    <TR>
      <TD >自定义标签内容： </TD>
      <TD ><textarea name="m_content"  cols="100" rows="20" ></textarea><font color="red">＊</font><br />
      如果希望同时多个内容随机显示，请使用$$$隔开 
      </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 editSelfLabel() {
	$id = getForm("id","get") ; $page = getForm("page","get");
	$row = DB::fetch_first("SELECT * FROM `".DB::table('selflabel')."` WHERE m_id=".$id);
?>
<div class="container" id="cpcontainer">
<form action="?action=editsave&id=<?=$id?>&page=<?=$page?>" method="post" >
<table class="tb">
	<tr class="thead"><th colspan="2">编辑自定义标签</th></tr>
	<tr>
      <TD width="15%">自定义标签名称：</TD>
      <TD><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="100" name="m_des" value="<?=$row['m_des']?>" /></TD>
    </TR>
    <TR>
      <TD >自定义标签内容：</TD>
      <TD ><textarea name="m_content"  cols="100" rows="20" ><?=decodeHtml($row['m_content'])?></textarea><font color="red">＊</font><br />
      如果希望同时多个内容随机显示，请使用$$$隔开 </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></table>
</form></div>
<?php
}

function addSave() {
	global $errorInfo,$errorSelflabel;
	$m_name = getForm("m_name","post");$m_des = getForm("m_des","post");$m_content = getForm("m_content","post");
	$m_content = encodeHtml($m_content);
	if (empty($m_content)) die($errorInfo);
	$num = DB::result_first("SELECT count(*) FROM `".DB::table('selflabel')."` WHERE m_name='$m_name'");
	if ($num > 0) die($errorSelflabel);
	$feedarr = array(
	'm_name' => $m_name,
	'm_des' => $m_des,
	'm_content' => $m_content,
	'm_addtime' => time()
	);
	DB::insert('selflabel', $feedarr);
	alertMsg("添加成功", "admin_selflabel.php");
}

function delAll() {
	$ids = getForm("m_id","post");
	DB::delete('selflabel', 'm_id IN ('.dimplode($ids).')');
	alertMsg("", "admin_selflabel.php");
}

function delSelfLabel() {
	$id = getForm("id","get");
	DB::delete('selflabel', array('m_id' => $id));
	alertMsg("", "admin_selflabel.php");
}

function editSave() {
	global $errorInfo,$errorSelflabel;
	$id = getForm("id","get");$page = getForm("page","get");$m_name = getForm("m_name","post");$m_des = getForm("m_des","post");$m_content = getForm("m_content","post");
	$m_content = encodeHtml($m_content);
	if (empty($m_content)) die($errorInfo);
	$num = DB::result_first("SELECT count(*) FROM `".DB::table('selflabel')."` WHERE m_name='$m_name' AND m_id<>$id");
	if ($num > 0) die($errorSelflabel);
	$feedarr = array(
	'm_name' => $m_name,
	'm_des' => $m_des,
	'm_content' => $m_content,
	'm_addtime' => time()
	);
	DB::update('selflabel', $feedarr, array('m_id' => $id));
	alertMsg("修改成功", "admin_selflabel.php?id=$id&page=$page");
}

function popDiv(){
?>
	<div id="selflabel" style="top:100px; display:none;" class="popdiv">
	<div class="poptitie"><img src="../pic/btn_close.gif" alt="关闭" onClick="$('selflabel').style.display='none'" />自定义标签路径 </div>
    <div class="popbody"><div><textarea cols="80" rows="14" id="labelcontent" style="width:516px"></textarea></div></div>
</div>
<script type="text/javascript" src="imgs/drag.js"></script>
<?php
}
?>
