<?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_types.php
 * 摘    要：扩展属性处理
 * 适用范围：PHPWMS 1.0
 * 版    本：1.0
 *------------------------------------------------------------------
 * Script Written By phpwms项目组
 * 最后更新： 2009-09-24 phpwms 由PHP金南曦网络技术有限公司整理发布
 *******************************************************************/
if(!defined('IN_SYS'))exit('access die');
$ta = $_REQUEST['task'];
switch ($ta){
	case 'list':
		if (!getAdminPri('3408')) { //判断是否有权限2008-6-27
	  		exit(NO_AUTH_LOGIN);
     	 }
		break;
	case 'add':
		if (!getAdminPri('3409')) { //判断是否有权限2008-6-27
	  		exit(NO_AUTH_LOGIN);
     	 }
		break;
}
if(is_post()){
	$task = $_POST['task'];
	switch ($task){
		case 'delete_attr':
			$attr_ids = $_POST['attr_ids'];
			if(!$ids = formart_ids($attr_ids))exit($sp->bgmsg('请选择要操作的对象'));
			$db->query("DELETE FROM ##__extend_attr WHERE `attr_id` in ($ids);");
			php188StaticCache::getInstance()->remove('phpwms_extend_types');
			exit($sp->bgmsg('操作成功.'));
			break;
		case 'add_attr':
		case 'edit_attr':
			$_SESSION['post_attr_data'] = $_POST;
			$edit = $task=='edit_attr'?TRUE:FALSE;
			$attr_name = $_POST['attr_name'];
			$attr_id = to_int($_POST['attr_id']);
			$type_id = to_int($_POST['attr_type_id']);
			if(empty($type_id))exit($sp->bgmsg('请选择所属类型.'));
			if(empty($attr_name))exit($sp->bgmsg('请输入参数组名称.'));
			if(!$edit){
				$c = $db->fetch("SELECT * FROM ##__extend_attr WHERE `attr_name` = '$attr_name' AND `attr_type_id` = '$type_id' ");
				if(!empty($c))exit($sp->bgmsg('存在相同的属性值.'));
			}
			if($edit){
				if(empty($attr_id))exit($sp->bgmsg('参数错误'));
				$c = $db->fetch("SELECT * FROM ##__extend_attr WHERE `attr_name` = '$attr_name' AND `attr_type_id` = '$type_id' AND `attr_id` <> '$attr_id' ");
				if(!empty($c))exit($sp->bgmsg('存在相同的属性值.'));
				$id = to_int($_POST['attr_id']);
				if(empty($id))exit($sp->bgmsg('参数错误.'));
			}
			$sql_data = array(
				'attr_name'=>$attr_name,
				'attr_value'=>$_POST['attr_value'],
				'attr_type_id'=>$type_id,
				'input_model'=>to_int($_POST['input_model']),
			);
			php188StaticCache::getInstance()->remove('phpwms_extend_types');
			if($edit){
				$db->update($sql_data,'extend_attr',array('attr_id'=>$attr_id));
				$_SESSION['post_attr_data'] = '';
				exit($sp->bgmsg('编辑成功','admincp.php?action=types&task=list_attr&type_id='.$type_id));
			}else{
				$db->insert($sql_data,'extend_attr');
				$_SESSION['post_attr_data'] = '';
				exit($sp->bgmsg('添加成功,还可以继续添加.'));
			}
			
			break;
		case 'edit_types':
		case 'add_types':
			$type_name = $_POST['type_name'];
			if(empty($type_name))exit($sp->bgmsg('请输入参数组名称'));
			$type_id = to_int($_POST['type_id']);
			if($task=='add_types'){
				$check = $db->fetch("SELECT type_id FROM ##__extend_type WHERE `type_name` = '$type_name';");
				if(!empty($check))exit($sp->bgmsg('已存在相同的类型,请重新添加.'));
			}
			if($task=='edit_types'){
				if(empty($type_id))exit($sp->bgmsg('参数错误.'));
				$check = $db->fetch("SELECT type_id FROM ##__extend_type WHERE `type_name` = '$type_name' AND `type_id` <> '$type_id';");
				if(!empty($check))exit($sp->bgmsg('已存在相同的参数组,请重新添加.'));
			}
			$sql_data = array(
				'type_name'=>$type_name,
				'type_desc'=>$_POST['type_desc'],
			);
			php188StaticCache::getInstance()->remove('phpwms_extend_types');
			if($task=='edit_types'){
				$db->update($sql_data,'extend_type',array('type_id'=>$type_id));
				exit($sp->bgmsg('编辑成功,页面将跳转','admincp.php?action=types&task=list'));
			}else{
				$db->insert($sql_data,'extend_type');
				exit($sp->bgmsg('操作成功,您还可以继续添加.'));
			}
			break;
		default:exit('invaild post data');
	}
}

$id = to_int($_GET['id']);
$do = $_GET['task'];
$tpl->assign('task',$do);
$navs = '<a href="admincp.php?action=types">扩展属性管理</a>&nbsp;&raquo;&nbsp;';
switch ($do){
	case 'list_attr':
			$type_id = to_int($_GET['type_id']);
			$where = !empty($type_id)?" WHERE `attr_type_id` = '$type_id' ":" ";
			$total = $db->fetch("SELECT count(*) AS T FROM ##__extend_attr $where",TRUE);
			$total = $total['T'];
			$W = !empty($type_id)?" WHERE A.`attr_type_id` = '$type_id' ":" ";
			$sql = "SELECT * FROM ##__extend_attr AS A LEFT JOIN ##__extend_type AS E ON A.attr_type_id = type_id $W";
			$u  = !empty($type_id)?'&type_id='.$type_id:'';
			$url = "admincp.php?action=types&task=list_attr".$u;
			$attr_data = page_for_db_extend($total,$sql,$url,20);
			$navs .= '属性列表';
			$tpl->assign('type_data',get_types());
			$tpl->assign('attr_data',$attr_data);
		break;
	case 'add_attr':
	case 'edit_attr':
		$edit = $task=='edit_attr'?TRUE:FALSE;
		$navs .= $edit?'编辑属性':'添加属性'; 
			$tpl->assign('type_data',get_types());
			if($edit){
				$data = $db->fetch("SELECT * FROM ##__extend_attr WHERE `attr_id` = '$id';",TRUE);
				if(empty($data))exit($sp->bgmsg('无可用数据','admincp.php?action=types&task=list_attr'));
			}
			$s_data = $_SESSION['post_attr_data'];
			if($s_data)$data = $s_data;
			$tpl->assign('data',$data);
		break;
	case 'delete_type'://删参数组
		if(empty($id))exit('crack die');
		$db->query("DELETE FROM ##__extend_type WHERE `type_id` = '$id'");
		$db->query("DELETE FROM ##__extend_attr WHERE `attr_type_id` = '$id';");
		$db->query("DELETE FROM ##__extend_type_attr_bind WHERE `type_id` = '$id'");
		php188StaticCache::getInstance()->remove('phpwms_extend_types');
		exit($sp->bgmsg('操作成功.'));
		break;
	case 'add'://增加参数组
	case 'edit'://编辑参数组
		if($do=='edit'){
			$nav = '编辑类型';
			if(empty($id))exit($sp->bgmsg('参数错误'));
			$data = get_types($id);
			if(empty($data))exit($sp->bgmsg('无可用数据','admincp.php?action=types&task=list'));
			$tpl->assign('data',$data);
		}else{
			$nav = '添加参数组';
		}
		$navs  .= $nav;
		break;
	default://列表
	$data = get_types();
	$navs .= '参数组列表';
	$attrs = $db->fetch("SELECT `attr_type_id` FROM ##__extend_attr;");
	$attrs = array_group_by($attrs,'attr_type_id');
	$keys = array_keys($attrs);
	if(!empty($attrs) && !empty($data)){
		foreach ($data as &$d){
			$d['total'] = in_array($d['type_id'],$keys)?count($attrs[$d['type_id']]):0;
		}
	}
	$tpl->assign('type_data',$data);
	$tpl->assign('task','list');
}
$tpl->assign('admin_nav',shownav($navs));
$tpl->display('types.tpl');
?>