<?php
/*******************************************************************
 * 版权协议：2009-2019
 * 版权所有: PHP-北京金南曦网络技术有限公司旗下PHPWMS，PHP商城系统技术支持中心http://www.php188.net，并保留所有权利。 
* PHP商城系统网址: http://www.php188.com
 * PHPWMS系统网址：http://www.phpwms.com
 * 销售电话：010-87875088
 * 销售电话：010-87875188 
*------------------------------------------------------------------
 * 这不是一个完全免费软件！您只能在不用于商业目的的前提下对程序代码进
 * 行修改和使用；不允许对程序代码以任何形式任何目的的再发布。
 *------------------------------------------------------------------
 * 文件名称：admin_category.php
 * 摘　　要：分类
 * 适用范围：PHPWMS 1.0
 * 版　　本：1.0
 *------------------------------------------------------------------
 * Script Written By phpwms项目组
 * 最后更新： 2009-09-24 phpwms 由PHP金南曦网络技术有限公司整理发布
 *******************************************************************/
if(!defined('IN_SYS'))exit('access die');
$do			  = trim($_REQUEST['do']);
$action		  = trim($_REQUEST['action']);
$category_id  = to_int($_REQUEST['category_id']);
$channel_type = to_int($_REQUEST['channel_type']);
$tpl -> assign("do",$do);
$tpl -> assign("action",$action);
$tpl -> assign("category_id",$category_id);
$channel_type = to_int($channel_type);
$tpl -> assign("channel_type",$channel_type);
$tpl -> assign("channel_type",empty($channel_type)?'':$channel_type);
if(empty($channel_type))exit('crack die');
if (!getAdminPri('3333')) exit(NO_AUTH_LOGIN);
if ($channel_type==1) {
   if (!getAdminPri('3102')) { //判断是否有权限2008-6-27
	  	exit(NO_AUTH_LOGIN);
      }
} else if ($channel_type==2) {
   if (!getAdminPri('3202')) {
	   exit(NO_AUTH_LOGIN);
   }
} else if ($channel_type==3) {
   if (!getAdminPri('3302')) {
	   exit(NO_AUTH_LOGIN);
   }
}else if ($channel_type==4) {
   if (!getAdminPri('4402')) {
	   exit(NO_AUTH_LOGIN);
   }
}
if(is_post()){
	$task = $_POST['task'];
	$_SESSION['post_category_data'] = $_POST;
	switch ($task){
		case 'add':
		case 'edit':
			$add = $task=='add'?TRUE:FALSE;
			$cate_name = $_POST['category_name'];
			if(empty($cate_name))exit($sp->bgmsg('请输入分类名称'));
			$channel_id = to_int($_POST['channel_id']);
			$channel_type = to_int($_POST['channel_type']);
			if(empty($channel_type))exit($sp->bgmsg('参数错误'));
			if(empty($channel_id))exit($sp->bgmsg('请选择所属频道'));
			$cate_sign = $_POST['cate_sign'];
			if($add && !empty($cate_sign)){
				$c = $db->fetch("SELECT category_id FROM ##__category WHERE `cate_sign` = '$cate_sign'");
				if(!empty($c))exit($sp->bgmsg('存在相同的分类标志位.请重新输入'));
			}
			if($task=='edit'){
				$cate_id = $_POST['category_id'];
				if(empty($category_id))exit($sp->bgmsg('参数错误'));
				if(!empty($cate_sign)){
					$c = $db->fetch("SELECT category_id FROM ##__category WHERE `cate_sign` = '$cate_sign' AND `category_id` <> '$cate_id'");
					if(!empty($c))exit($sp->bgmsg('存在相同的分类标志位.请重新输入'));
				}
			}
			if(!class_exists('WMSTree'))require SYS_ROOT.'pub/phpwms.tree.class.php';
			$wms_tree = new WMSTree();
			$parent_id = to_int($_POST['parent_id']);
		if($parent_id<=0){//顶级分类
			//检查分类名是否重复
			$and = $add?'':" AND `category_id` <> '$cate_id' ";//
			$exist_name = $db->fetch("SELECT `category_id` FROM `##__category` WHERE `category_name` = '$cate_name' AND `parent_id`='0' AND `channel_model` = '$channel_type' $and ;");
			if(!empty($exist_name))die($sp->bgmsg('存在相同分类'));
		}else{
			//检查父分类是否存在
			$exist = $db->fetch("SELECT `category_id` FROM `##__category` WHERE `category_id` = '$parent_id'  AND `channel_model` = '$channel_type' ; ");
			if(empty($exist))exit('不存在该父分类');//不存在该父分类
			//检查同级是否相同
			if($add){
				$children = $db->fetch("SELECT `category_name`,`parent_id` FROM `##__category` WHERE `parent_id` = '$parent_id' AND `category_name` = '$cate_name' AND `channel_model` = '$channel_type';");
				if(!empty($children)){//若有检查
					if(in_array($cate_name,mulitarray_to_array($children)))die($sp->bgmsg('已存在相同分类'));
				}
			}
			//编辑模式
			if(!$add){
				$children = $db->fetch("SELECT `category_name`,`category_id` FROM `##__category` WHERE `parent_id` = '$parent_id' AND `channel_model` = '$channel_type';");
				if(!empty($children)){
					$names = array();
					foreach ($children as $k=>$c){
						if($c['category_id']==$cate_id)unset($children[$k]);
						$names[] = $c;
					}
					if(!empty($children)){
						foreach ($children as $v){
							if($v['category_name']==$cate_name){
								die($sp->bgmsg('已存在相同分类'));
							}
						}
					}
				}
				unset($children);
			}
		}
		//检查是否把父分类放在子分类里
		if(!$add){
			//先获得子分类 
			$channel_tress = $db->fetch("SELECT * FROM ##__category WHERE `channel_model` = '$channel_type';");
			$curent_childern = $wms_tree->get_son($channel_tress,'category_id','parent_id',$cate_id,'category_id');
			if(in_array($parent_id,$curent_childern))die($sp->bgmsg('父分类不能放在子分类下'));//父分类不能放在子分类下
			if($cate_id==$parent_id)die($sp->bgmsg('操作错误,不能自己做自己的父类'));
		}
			$user_group = to_int($_POST['user_group']);
			$user_group = empty($user_group)?'1':$user_group;
			$sql_data = array(
				'category_name'=>$cate_name,
				'channel_id'=>$channel_id,
				'parent_id'=>$parent_id,
				'category_desc'=>$_POST['category_desc'],
				'channel_model'=>$channel_type,
				'seo_info'=>serialize_deep($_POST['seo']),
				'user_group'=>$user_group,
				'cfg'=>serialize_deep($_POST['cfg']),
				'in_nav'=>!empty($_POST['in_nav'])?1:0,
				'category_pass'=>$_POST['category_pass'],
				'allow_post'=>to_int($_POST['allow_post']),
				'cate_sign'=>$_POST['cate_sign'],
			);
			$tpl_name = $_POST['tpl_name'];
			$sql_data['tpl_name'] = !empty($tpl_name)?$tpl_name:'';
			if($task =='add'){
				$db->insert($sql_data,'category');
				$msg = '添加分类成功';
			}else{
				$db->update($sql_data,'category',array('category_id'=>$cate_id));
				$msg = '编辑分类成功';
			}
			$_SESSION['post_category_data'] = '';
			php188StaticCache::getInstance()->remove('phpwms_channel_categorys');
			$sp->bgmsg($msg,'admincp.php?action=category&channel_type='.$channel_type);
			 php188StaticCache::getInstance()->remove('phpwms_channel_categorys');
		break;
		default:exit('crack die');
	}
exit();
}
$model_list = get_models();
foreach ($model_list as $list){
	if(trim($list['id']) == trim($channel_type)){
		$channel_type_name = $list['cn_name'];
		break;
	}else{
		$channel_type_name  = '';
	}
}
$pn = !empty($channel_type_name)?'['.$channel_type_name.']':'';
$tpl->assign('model_list',$model_list);
$tpl -> assign("channel_type_name",$channel_type_name);
$tpl->assign('channel_list');
$channel_list = get_channels($channel_type);
$tpl->assign('user_group_list',get_user_group());
if(empty($channel_list))exit($sp->bgmsg('无可用频道,请先添加频道','admincp.php?action=channel&do=add&channel_type='.$channel_type));
$pn  = '分类管理';
switch ($do){
	case 'ajax_call_category'://根据频道选择分类
		$channel_id = to_int($_GET['id']);
		if(empty($channel_id))exit(json_encodes(array('error'=>'1','msg'=>'非法操作')));
		$channel_type = to_int($_GET['channel_type']);
		if(empty($channel_type))exit(json_encodes(array('error'=>'1','msg'=>'参数错误')));
		$list_category_list = array_group_by(building_tree($channel_type),'channel_id');
		$end_data = isset($list_category_list[$channel_id])?$list_category_list[$channel_id]:array();
		exit(json_encodes(array('error'=>'2','msg'=>$end_data)));
		break;
	case 'add':
	case 'edit':
		$se_data = $_SESSION['post_category_data'];
		if($do=='edit'){
			if(empty($category_id))exit($sp->bgmsg('参数错误','admincp.php?action=category&channel_type='.$channel_type));
			$check = $db->fetch("SELECT * FROM ##__category AS C ,##__channel AS D 
				 WHERE C.`category_id` = '$category_id' AND C.channel_id = D.channel_id ;",TRUE);
			$tpl->assign('channel_names',$check['channel_name']);
			if(empty($check))exit($sp->bgmsg('无可用分类','admincp.php?action=category&channel_type='.$channel_type));
			$check['seo'] = unserialize_deep($check['seo_info']);
			$check['cfg'] = unserialize_deep($check['cfg']);
			$channel_id = $check['channel_id'];
			$channel_type = $check['channel_model'];
			//注意位置
			$check = !empty($se_data)?$se_data:$check;
			$tpl->assign('data',$check);
			$list_category_list = array_group_by(building_tree($channel_type),'channel_id');
			$category_list = isset($list_category_list[$channel_id])?$list_category_list[$channel_id]:array();
			$tpl -> assign("category_list",$category_list);
		}
		if($do=='add' && !empty($se_data)){
			unset($se_data['channel_id']);
			$tpl->assign('data',$se_data);
		}
		$show_nav = $do=='add'?'添加分类':'编辑分类';
		$tpl->assign('channel_list',$channel_list);
		break;
	case 'delete':
		$channel_id = to_int($_GET['channel_id']);
		$channel_type = to_int($_GET['channel_type']);
		$category_id = to_int($_GET['category_id']);
		$check_children = $db->fetch("SELECT `category_id` FROM ##__category WHERE `parent_id` = '$category_id';");
		if(!empty($check_children))exit($sp->bgmsg('该分类下还有子分类,请先删除子分类.'));
		//检查是否有文章
		$c = $db->fetch("SELECT `c_id` FROM ##__contents WHERE `category_id` in($category_id) limit 0,1");
		if(!empty($c))exit($sp->bgmsg('该分类下还有数据,请先删除后再删分类.'));
		$db->query("DELETE FROM ##__category WHERE `category_id` = '$category_id';");
		php188StaticCache::getInstance()->remove('phpwms_channel_categorys');
		exit($sp->bgmsg('删除分类成功'));
		break;
	default:
		$channel_id = to_int($_GET['channel_id']);
		$category_list = building_tree($channel_type);
		if(!empty($channel_id)){
			$list_category_list = array_group_by($category_list,'channel_id');
			$category_list = isset($list_category_list[$channel_id])?$list_category_list[$channel_id]:array();
		}
		if(!empty($category_list)){
			foreach ($category_list as &$c)$c['cfg'] = unserialize_deep($c['cfg']);
		}
		$tpl->assign(array(
			'model'=>get_action('',$channel_type),
		));
		$tpl -> assign("category_list",$category_list);
	$show_nav = '<b>'.$channel_type_name.'模型分类列表</b>';
}
$tpl -> assign("admin_nav",shownav("<a href=\"?action=$action&channel_type=$channel_type\">$pn</a>&nbsp;&raquo;&nbsp;$show_nav"));
$sp  -> outPut('category_list.tpl',$pn);
?>