<?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");
switch($action) {
	case 'del' : delNewsType();break;
	case 'delall' : delAll();break;
	case 'edit' : editNewsType();break;
	case 'hide' : hideNewsType();break;
	case 'nohide' : nohideNewsType();break;
	case 'add' : addNewsType();break;
	case 'merge' : mergeNewsType();break;
	case 'move' : moveNewsType();break;
	default : main();break;
}

viewFoot();

function main() {
	global $menuList;
?>
<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>
<form method="post"  name="editform">
<table class="tb2">
<tr class="thead"><th colspan="9">新闻分类管理(<font color='red'>＊</font>为必填,其它选填)</th></tr>	
<tr align="center">
			<td align="left" width="21%">ID.分类名称(文章数量)</td>
			<td width="10%">中文名</td>
			<td width="10%">英文名</td>
			<td width="10%">频道模板</td>
			<td width="10%">内容模板</td>
			<td width="10%">关键词</td>
			<td width="10%">描述</td>
			<td width="6%">排序</td>
			<td width="13%">&nbsp;操作</td>
</tr>
</table>			  
<?php
	newsTypeList(0)
?>
<table class="tb"><tr><td>全选<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('确定要删除吗')){editform.action='?action=delall';}else{return false}" class="btn"  /><input type="submit" value="批量修改选中分类" class="btn" onclick="editform.action='?action=edit';" />&nbsp;&nbsp;&nbsp;移动到
      <select name="m_upid_to"><option value="0">顶级分类</option>
    <?makeNewsTypeOption(0,"&nbsp;|&nbsp;&nbsp;")?>
	</select><input type="submit" value="批量移动分类" name="Submit" class="btn" onclick="editform.action='?action=move';"/></td></tr></table>
</form>

<table class="tb mt20">
<tr class="thead"><th colspan="9">添加分类</th></tr>	
<form method="post" action="?action=add"  name="addform" onSubmit="return validateform()">
<tr align="center">
    <td width="160" align=left>
    选择层级：
      <select name="m_upid"><option value="0">顶级分类</option>
    <?makeNewsTypeOption(0,"&nbsp;|&nbsp;&nbsp;")?>
	</select><font color='red'>＊</font>
    </td>
    <td width="160">中文名<font color='red'>＊</font><br />
      <input type="text" size="10" name="m_name"></td>
    <td width="160">英文名<font color='red'>＊</font><br />
      <input type="text" size="10" name="m_enname"></td>
    <td width="140">频道模板<br />
      <input type="text" size="10" name="m_template"></td>
    <td width="140">内容模板<br />
      <input type="text" size="10" name="m_subtemplate"></td>
    <td width="140">关键词<br />
      <input type="text" size="10" maxlength="255" name="m_keyword"></td>
    <td width="140">描述<br />
      <input type="text" size="10" maxlength="255" name="m_description"></td>
    <td width="136">排序<br />
      <input type="text" size="3" name="m_sort"></td>
    <td width="175"><br />
<input type="submit" value="添 加" name="Submit" class="btn" /></td>
</tr>
</form>
</table>
<table class="tb mt20">
<tr class="thead"><th>合并分类</th></tr>
<form method="post" action="?action=merge">
<tr><td width="100%">将分类：<?makeNewsTypeSelect("leftSelect")?>的数据合并到：<?makeNewsTypeSelect("rightSelect")?><input type="submit" value="确认合并" class="btn" /><br /><font color=#FF0000>注意，程序将前面的分类数据合并到后面分类后会删除之前分类生成的HTML,但不会删除该分类。</font></td></tr>
</form>
</table>
<?php
}

function newsTypeList($topId) {
    $tlist = getNewsTypeLists();
	foreach ($tlist as $row) {
		if ($row['m_upid'] == $topId) {
?>
<table class="tb2">
	<tr align="center">
		<td  align="left" width="21%"><?php if($topId!=0){?>&nbsp;&nbsp;&nbsp;&nbsp;<?php }?><input type="checkbox" name="m_id[]" value="<?=$row['m_id']?>"  class="checkbox" /><?=$row['m_id']?>.<a href="admin_news.php?type=<?=$row['m_id']?>"><?=$row['m_name']?></a>(<font color="red"><?=getNumPerNewsType($row['m_id'])?></font>)</td>
		<td width="10%"><input type="text" size="10" name="m_name<?=$row['m_id']?>" value="<?=$row['m_name']?>"></td>
		<td width="10%"><input type="text" size="10" name="m_enname<?=$row['m_id']?>" value="<?=$row['m_enname']?>"></td>
		<td width="10%"><input type="text" size="10" name="m_template<?=$row['m_id']?>" value="<?=$row['m_template']?>"></td>
		<td width="10%"><input type="text" size="10" name="m_subtemplate<?=$row['m_id']?>" id="m_subtemplate<?=$row['m_id']?>" value="<?=$row['m_subtemplate']?>"></td>
		<td width="10%"><input type="text" size="10" maxlength="255" name="m_keyword<?=$row['m_id']?>" value="<?=$row['m_keyword']?>"></td>
		<td width="10%"><input type="text" size="10" maxlength="255" name="m_description<?=$row['m_id']?>" value="<?=$row['m_description']?>"></td>
		<td width="6%"><input type="text" size="3" name="m_sort<?=$row['m_id']?>" value="<?=$row['m_sort']?>"></td>
		<td width="13%"><input type="button" value="删除" onClick="javascript:location.href='?action=del&m_id=<?=$row['m_id']?>';" name="Input" class="btn" /><?php if($row['m_hide']==1){?><input type="button" value="取消隐藏" onClick="javascript:location.href='?action=nohide&m_id=<?=$row['m_id']?>';" name="Input2" class="btn" style="width:60px"/><?php }elseif($row['m_hide']==0){?><input type="button" value="隐藏" onClick="location.href='?action=hide&m_id=<?=$row['m_id']?>';" name="Input2" class="btn" style="width:60px"/> <?php }?></td>
  	</tr>
</table>
<?php
      newsTypeList($row['m_id']);
		}
	}
}

function mergeNewsType() {
	$leftSelect = getForm("leftSelect","post") ; if (empty($leftSelect)) die("请选择分类");
	$rightSelect = getForm("rightSelect","post") ; if (empty($rightSelect)) die("请选择分类");
	$typePath = getTypePathOnCache($leftSelect,"news");
	DB::update('news', array('m_type' => $rightSelect),array('m_type' => $leftSelect));clearNewsTypeCache();
	if (newsMakeMode != "dir2"){
	    if (!empty($typePath)){ delfolder("../".$typePath); }else{  alertMsg("分类别名丢失，以仿程序删除根目录，此请求不允许操作","admin_newstype.php");}
    }
	alertMsg("", "admin_newstype.php");
}

function moveNewsType() {
	$topid = getForm("m_upid_to","post");
	$ids = getForm("m_id","post");
	$back = getRefer();
	if (empty($ids)) {
		alertMsg("请选择需要移动的分类", "admin_newstype.php");last();die();
	}
	foreach ($ids as $id) {
	    $subTypeList=",,".getNewsTypeIdOnCache($id).",,";
		if (!isExistStr($subTypeList,",".$topid.",")) DB::update('type', array('m_upid' => $topid),array('m_id' => $id));
	}
	clearNewsTypeCache();alertMsg("已经成功移动分类！", "admin_newstype.php");
}

function addNewsType() {
	$m_name = getForm("m_name","post");
	$m_upid = getForm("m_upid","post");
	$m_enname = getForm("m_enname","post");
	$m_template = getForm("m_template","post") ; if (empty($m_template)) $m_template = "newspage.html";
	$m_subtemplate = getForm("m_subtemplate","post") ; if (empty($m_subtemplate)) $m_subtemplate = "news.html";
	if (empty($m_name) || empty($m_enname)){ alertMsg("信息填写不完整，请检查",getRefer());die("");}
	$m_sort = getForm("m_sort","post");
	if (empty($m_sort)) {
		$m_sort = DB::result_first('select max(m_sort)+1 from '.DB::table('type').'');
	}
	if (!is_numeric($m_sort)) $m_sort = 1;
	$m_keyword = getStrByLen(getForm("m_keyword","post"),255);
	$m_description = getStrByLen(getForm("m_description","post"),255);
	$typearr = array(
	'm_name' => $m_name,
	'm_enname' => $m_enname,
	'm_sort' => $m_sort,
	'm_template' => $m_template,
    'm_upid' => $m_upid,
	'm_keyword' => $m_keyword,
	'm_description' => $m_description,
	'm_type' => 1,
	'm_subtemplate' => $m_subtemplate,
	);
	DB::insert('type', $typearr);
	clearNewsTypeCache();alertMsg("", "admin_newstype.php");
}

function delTypeFirstPage($typeid) {
	$channelFirstPage = getNewsPartLink($typeid,1);
	if (!empty($channelFirstPage)) delFile('..'.$channelFirstPage);
}

function hideNewsType() {
	$id = getForm("m_id","get");
	$typePath = getTypePathOnCache($id,"news");
	delTypeFirstPage($id);
	DB::update('type', array('m_hide' => 1),array('m_id' => $id));
	if (!empty($typePath)) {
		delFolder("../".$typePath); 
	} else {  
		alertMsg("分类别名丢失，以防程序删除根目录，此请求不允许操作","admin_newstype.php");die("");
	}
	clearNewsTypeCache();alertMsg("", "admin_newstype.php");
}

function nohideNewsType() { 
    $id = getForm("m_id","get");
	DB::update('type', array('m_hide' => 0),array('m_id' => $id));
	clearNewsTypeCache();alertMsg("", "admin_newstype.php");
} 

function delAll() {
	$ids = getForm("m_id","post");
	if(empty($ids)) { alertMsg("", getRefer());exit();}
	foreach ($ids as $id) {
		delNewsTypeById($id);
    }
	clearNewsTypeCache();alertMsg("", "admin_type.php");
}

function delNewsType() { 
    $id = getForm("m_id","get");
	delNewsTypeById($id);
	alertMsg("", "admin_newstype.php");
}

function delNewsTypeById($id) { 
	$typePath = getTypePathOnCache($id,"news");
	if (isExistSub($id, "type")) {
		alertMsg("请先删除该分类的子类","admin_newstype.php"); 
	} else {
		if (isExistSub($id, "news")) { alertMsg("请先删除该子类下的所有视频","admin_newstype.php"); exit();}
		DB::delete('type', array('m_id' => $id));clearNewsTypeCache();
		if (!empty($typePath)) {
			if (isExistFolder("../".$typePath))  delFolder("../".$typePath);
		} else {
			alertMsg("分类别名丢失，以防程序删除根目录，此请求不允许操作","admin_newstype.php"); 
		}
	}	
}

function editNewsType() { 
	$ids = getForm("m_id","post");
	if(empty($ids)) { alertMsg("", $back);exit();}
	foreach ($ids as $id) {
		$m_name = getForm("m_name".$id,"post");
		$m_enname = getForm("m_enname".$id,"post");
		$m_sort = getForm("m_sort".$id,"post");
		if (empty($m_sort)) { 
	        $m_sort = DB::result_first('select max(m_sort)+1 from '.DB::table('type').' where m_type=1');
	    }
		$m_template = getForm("m_template".$id,"post") ; if (empty($m_template)) $m_template = "newspage.html";
		$m_subtemplate = getForm("m_subtemplate".$id,"post") ; if (empty($m_subtemplate)) $m_subtemplate = "news.html";
		$m_keyword = getStrByLen(getForm("m_keyword".$id,"post"),255);
		$m_description = getStrByLen(getForm("m_description".$id,"post"),255);
		if ((empty($id)) || (empty($m_name)) || (empty($m_enname))){ alertMsg("信息填写不完整，请检查",getRefer());die("");}
		$typearr = array(
	    'm_name' => $m_name,
	    'm_enname' => $m_enname,
	    'm_sort' => $m_sort,
	    'm_template' => $m_template,
        'm_upid' => $m_upid,
	    'm_keyword' => $m_keyword,
	    'm_description' => $m_description,
	    'm_type' => 1,
	    'm_subtemplate' => $m_subtemplate,
	    );
		DB::update('type', $typearr, array('m_id' => $id));clearNewsTypeCache();
	}
	alertMsg("", "admin_newstype.php");
}

function isExistSub($bigType, $operFlag) {
	switch ($operFlag) {
		case "type":
		    $tcount = DB::result_first("SELECT count(*) FROM `".DB::table('type')."` where m_upid=".$bigType);break;
		case "video":
		    $tcount = DB::result_first("SELECT count(*) FROM `".DB::table('news')."` where m_type=".$bigType);break;
	}
	if ($tcount > 0) return true;else return false;
}

function clearNewsTypeCache(){
	if (cacheStart==1) memory('rm','array_newstype_lists_all');
}
?>
