<?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[1][0]);

$action = getForm("action", "get");
$topicPath="../pic/zt/";

switch ($action) {
	case "last" : moveToLast();break;
	case "next" : moveToNext();break;
	case "del" : delTopic();break;
	case "delall" : delAll();break;
	case "edit" : editTopic();break;
	case "editone" : editOneTopic();break;
	case "add" : addTopic();break;
	default : main();popTopicWin();break;
}
viewFoot();

function main() {
	global $menuList;
	$id = getForm("id","get");
	$sqlStr = "SELECT `m_id`,`m_name`,`m_sort`,`m_template`,`m_enname`,`m_pic`,`m_des` FROM ".DB::table('topic')." ORDER BY m_sort ASC,m_id";
	$allRecordset = db::getCount($sqlStr);
?>
<script type="text/javascript">
function upformone(i){
	$('id').value=i,
	$('m_name').value=$('m_name'+i).value,
	$('m_pic').value=$('m_pic'+i).value,
	//$('m_bigpic').value=$('m_bigpic'+i).value,
	$('m_template').value=$('m_template'+i).value,
	$('m_enname').value=$('m_enname'+i).value,
	$('m_sort').value=$('m_sort'+i).value;
	$('form1').submit()
}
var _1;
function onUpClick(tg,el){
	_1=el;openWindow2(101,320,25,0);
	var msgDiv=$("msg");
	var _t = tg.offsetTop;
    var _l = tg.offsetLeft;
    while (tg = tg.offsetParent){_t+=tg.offsetTop; _l+=tg.offsetLeft;}
    msgDiv.style.cssText+="border:1px solid #55BBFF;background: #C1E7FF;padding:3px 0px 3px 4px;"
	msgDiv.style.top = (_t-1)+"px";
    msgDiv.style.left = (_l-1)+"px";
	msgDiv.innerHTML='<button type="button" class="btn" style="margin:0 6px 0 0;height:23px;float:right;padding:0;" onclick="closeWin()">关闭</button><iframe src="fckeditor/upload.htm?iszt=1" scrolling="no" topmargin="0" width="270" height="25" marginwidth="0" marginheight="0" frameborder="0" align="center"></iframe>';
}

function onUploadBack(fn){
	if(!!_1) _1.value=fn;
	closeWin();
}
</script>
<div id="append_parent"></div>
<div class="container" id="cpcontainer">
<!--当前导航-->
<script type="text/JavaScript">if(parent.$('admincpnav')) parent.$('admincpnav').innerHTML='后台首页&nbsp;&raquo;&nbsp;<%=menuList(1,0)%>&nbsp;&raquo;&nbsp;专题管理';</script>
<?php
   if ($allRecordset > 0) {
?>
<form action="?action=editone" id="form1" name="form1" method="post">
<input type="hidden" name="m_id" id="id" value="" />
<input type="hidden" name="m_name" id="m_name" value="" />
<input type="hidden" name="m_pic" id="m_pic" value="" />
<input type="hidden" name="m_bigpic" id="m_bigpic" value="" />
<input type="hidden" name="m_template" id="m_template" value="" />
<input type="hidden" name="m_enname" id="m_enname" value="" />
<input type="hidden" name="m_sort" id="m_sort" value="" />
</form>
<form method="post" name="topicform">
<table class="tb">
<tr class="thead"><th colspan="9">专题管理</th></tr>
    <TR align="center">
		<TD width="7%">ID</TD>
		<TD width="17%">专题名称</TD>
		<TD width="13%">专题封面</TD>
		<!--<TD width="13%">专题大图</TD>-->
		<TD width="12%">专题模板</TD>
		<TD width="12%">专题别名</TD>
		<TD width="6%">描述</TD>
		<TD width="7%">排序</TD>
		<TD width="14%">操作</TD>
    </TR>
    <?php
    $query = DB::query($sqlStr);
    while($row = DB::fetch($query)) {
	    $tcount = DB::result_first("SELECT count(*) FROM ".DB::table('data')." WHERE m_topic=".$row['m_id']);
?>
    <TR<?php if($id == $row['m_id']){?>  class="editlast"<?php }?> name="topictr"  id="topictr<?=$row['m_id']?>" align="center">
		<td align="left"><input type="checkbox" value="<?=$row['m_id']?>" name="m_id[]"  checked="checked"  class="checkbox" /><?=$row['m_id']?></td>
		<td align="left"><input type="text" name="m_name<?=$row['m_id']?>" id="m_name<?=$row['m_id']?>" value="<?=$row['m_name']?>" size="15"/>(<font color="red"><?=$tcount?></font>)</td>
		<td><input type="text" name="m_pic<?=$row['m_id']?>" id="m_pic<?=$row['m_id']?>" value="<?=$row['m_pic']?>" size="10"/>&nbsp;<button type="button" class="btn" style="width:50px;height:22px;" onclick="onUpClick(this,this.form.elements['m_pic<?=$row['m_id']?>'])">浏览..</button></td>
		<!--<td><input type="text" name="m_bigpic<?=$row['m_id']?>" id="m_bigpic<?=$row['m_id']?>" value="<%=topicArray(8,i)%>" size="10"/>&nbsp;<button type="button" class="btn" style="width:50px;height:22px;" onclick="onUpClick(this,this.form.elements['m_bigpic<?=$row['m_id']?>'])">浏览..</button></td>-->
		<td><input type="text" name="m_template<?=$row['m_id']?>" id="m_template<?=$row['m_id']?>" value="<?=$row['m_template']?>" size="10"/></td>
		<td><input type="text" name="m_enname<?=$row['m_id']?>" id="m_enname<?=$row['m_id']?>" value="<?=$row['m_enname']?>" size="10"/></td>
		<td><img <?php if($row['m_des']){?>src='imgs/modify1.gif'<?php }else{?>src='imgs/modify.gif'<?php }?>  title='修改专题描述' style="cursor:pointer" onClick="viewCurrentTopicTr('<?=$row['m_id']?>');openTopicDesWin('topicdes','<?=$row['m_id']?>')" /></td>
		<td><input name="m_sort<?=$row['m_id']?>" id="m_sort<?=$row['m_id']?>" type="text" value="<?=$row['m_sort']?>"  size="3"/></td>
		<td><a href="?action=last&id=<?=$row['m_id']?>">上移</a> <a href="?action=next&id=<?=$row['m_id']?>">下移</a> <a href="javascript:upformone(<?=$row['m_id']?>)">修改</a> <a onclick="if(confirm('确定要删除吗')){return true;}else{return false;}" href="?action=del&id=<?=$row['m_id']?>">删除</a></td>
    </TR>
    <?php }?>
    <tr><td colspan="9">全选<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('确定要删除吗')){topicform.action='?action=delall';}else{return false}" class="btn"  />&nbsp;&nbsp;<input type="submit" value="批量修改选中专题" name="Submit" class="btn" onclick="topicform.action='?action=edit';" /></td></tr>
</TABLE>
</form>
<?php
} else {
?>
<table><tr><td>暂无专题</td></tr></table>
<?php	
}
?>
<form action="?action=add" method="post">
	<table class="tb mt20">
		<tr class="thead"><th colspan="9">添加专题</th></tr>
		<tr align="center">
			<TD width="7%">&nbsp;</TD>
		  <TD width="19%">名称<font color='red'>＊</font><br>
	      <input type="text" size="15" name="m_name" /></TD>
			<TD width="13%">封面<font color='red'>＊</font><br><input type="text" name="m_pic" size="10"/>&nbsp;<button type="button" class="btn" style="width:50px;height:22px;" onclick="onUpClick(this,this.form.elements['m_pic'])">浏览..</button></TD>
			<!--<TD width="13%">大图<font color='red'>＊</font><br><input type="text" name="m_bigpic" size="10"/>&nbsp;<button type="button" class="btn" style="width:50px;height:22px;" onclick="onUpClick(this,this.form.elements['m_bigpic'])">浏览..</button></TD>-->
			<TD width="12%">模板<br><input type="text" name="m_template" size="10"/></TD>
			<TD width="12%">别名<font color='red'>＊</font><br><input type="text" name="m_enname" size="10"/></TD>			
			<TD width="6%">&nbsp;</td>
			<TD width="7%">排序<br><input type="text" size="3" name="m_sort"/></TD>
			<TD width="10%">&nbsp;<br>
		  &nbsp;<input type="submit" value="添加专题" class="btn" /></TD>
	  </tr>
	</table>
</form>
<?php
}

function popTopicWin() {
?>
<div id="topicdes" style="top:100px; display:none;" class="popdiv">
	<div class="poptitie"><img src="../pic/btn_close.gif" alt="关闭" onClick="$('topicdes').style.display='none'" />自定义标签路径 </div>
    <div class="popbody"><div>
 <table width="100%" border="0" cellspacing="0" cellpadding="0">
<form id="f_des"  action="admin_ajax.php?action=submittopicdes" method="post">
<tr>
  <td width="554" >专题简介：<br>
  <input type="hidden" name="f_m_id"   >
  <textarea cols="72"  rows="14"  name="f_m_des"></textarea></td><td></td>
</tr>
<tr><td><input type="button" onClick="submitTopicDes('topicdes')" value="修     改" class="btn" /></td></tr>
</form>
</table>
</div></div>
</div>
<script type="text/javascript" src="imgs/drag.js"></script>
<?php
}

function moveToLast() {
	$id=getForm("id","get");
	if (empty($id)) alertMsg("","admin_topic.php");
	$cur = DB::result_first("SELECT m_sort FROM ".DB::table('topic')." WHERE m_id=".$id);
	$cou = DB::result_first("SELECT count(*) FROM ".DB::table('topic')." WHERE m_sort<'$cur'");
	if ($cou > 0) {
	    $flag = DB::result_first("SELECT m_sort FROM ".DB::table('topic')." WHERE m_sort<'$cur' ORDER BY m_sort DESC");
		DB::update('topic', array('m_sort' => $flag),array('m_id' => $id));
	} else {
		DB::query("UPDATE ".DB::table('topic')." SET m_sort=m_sort-1 WHERE m_id='$id'");
	}
	alertMsg("", "admin_topic.php?id=$id");
}


function moveToNext() {
	$id=getForm("id","get");
	if (empty($id)) alertMsg("","admin_topic.php");
	$cur = DB::result_first("SELECT m_sort FROM ".DB::table('topic')." WHERE m_id=".$id);
	$cou = DB::result_first("SELECT count(*) FROM ".DB::table('topic')." WHERE m_sort>'$cur'");
	if ($cou > 0) {
		$flag = DB::result_first("SELECT m_sort FROM ".DB::table('topic')." WHERE m_sort>'$cur' ORDER BY m_sort DESC");
		DB::update('topic', array('m_sort' => $flag),array('m_id' => $id));
	} else {
		DB::query("UPDATE ".DB::table('topic')." SET m_sort=m_sort+1 WHERE m_id='$id'");
	}
	alertMsg("", "admin_topic.php?id=$id");
}

function delAll() {
	$ids = getForm("m_id","post");
	DB::delete('topic', 'm_id IN ('.dimplode($ids).')');
	DB::update('data', array('m_topic' => 0),'m_topic IN ('.dimplode($ids).')');
	alertMsg("", "admin_topic.php");
}

function delTopic() {
	$id = getForm("id","get");
	if (empty($id)) alertMsg("","admin_topic.php");
	DB::delete('topic', array('m_id' => $id));
	DB::update('data', array('m_topic' => 0),array('m_topic' => $id));
	alertMsg("", "admin_topic.php");
}

function editOneTopic() {
	$id = getForm("m_id","post");
	$m_name = getForm("m_name","post");$m_enname = getForm("m_enname","post");$m_pic = getForm("m_pic","post");$m_bigpic=getForm("m_bigpic","post");
	if (empty($m_name) || empty($m_enname) || empty($m_pic))   die("信息填写不完整，请检查");
	$m_template = getForm("m_template","post") ; if (empty($m_template)) $m_template = "topic.html";
	$m_sort = getForm("m_sort","post");
	if (empty($m_sort)) {
		$m_sort = DB::result_first("SELECT max(m_sort)+1 FROM ".DB::table('topic')."");
	}
	if (!is_numeric($m_sort)) die("排序必须为数字");
	$typearr = array(
	'm_name' => $m_name,
	'm_sort' => $m_sort,
	'm_template' => $m_template,
	'm_enname' => $m_enname,
	'm_pic' => $m_pic,
	);
	DB::update('topic', $typearr, array('m_id' => $id));
	alertMsg("","admin_topic.php");
}

function editTopic() {
	$ids = getForm("m_id","post");
	if(empty($ids)) {
		alertMsg("", "admin_topic.php");
		exit();
	}
	foreach ($ids as $id) {
		$m_name = getForm("m_name".$id,"post") ; $m_enname = getForm("m_enname".$id,"post") ; $m_pic = getForm("m_pic".$id,"post");
		if (empty($m_name) || empty($m_enname) || empty($m_pic))   die("信息填写不完整，请检查");
		$m_template = getForm("m_template".$id,"post") ; if (empty($m_template)) $m_template = "topic.html";
		$m_sort = getForm("m_sort".$id,"post");
		if (empty($m_sort)) {
		    $m_sort = DB::result_first("SELECT max(m_sort)+1 FROM ".DB::table('topic')."");
	    }
		if (!is_numeric($m_sort)) die("排序必须为数字");
		$typearr = array(
	    'm_name' => $m_name,
	    'm_sort' => $m_sort,
	    'm_template' => $m_template,
	    'm_enname' => $m_enname,
        'm_pic' => $m_pic,
	    );
	    DB::update('topic', $typearr, array('m_id' => $id));
	}
	alertMsg("", "admin_topic.php");
}

function addTopic() {
	$m_name = getForm("m_name","post") ; $m_enname = getForm("m_enname","post") ; $m_pic = getForm("m_pic","post");
	if (empty($m_name) || empty($m_enname) || empty($m_pic))   die("信息填写不完整，请检查");
	$m_template = getForm("m_template","post") ; if (empty($m_template)) $m_template = "topic.html";
	$m_sort = getForm("m_sort","post") ;
	if (empty($m_sort)) {
		$m_sort = DB::result_first("SELECT max(m_sort)+1 FROM ".DB::table('topic')."");
	}
	if (!is_numeric($m_sort)) $m_sort = 1;
	$typearr = array(
	    'm_name' => $m_name,
	    'm_sort' => $m_sort,
	    'm_template' => $m_template,
	    'm_enname' => $m_enname,
        'm_pic' => $m_pic,
	);
	DB::insert('topic', $typearr);
	alertMsg("","admin_topic.php");
}
?>
