<?php

//禁止直接访问
!defined('IN_SYSTEM') && exit('Access Denied');

//对比InputKey
if(isset($_POST['NavInputKey'])){
	$NavInputKey = strip_tags(DeleteHtml(trimall(ToUtf8($_POST['NavInputKey']))));
	if($NavInputKey == ""){
		exit('Access Denied');
	}
	else{
		if(CheckPostInputKey($NavInputKey) == false){
			exit('Access Denied');
		}
	}
}
else{
	exit('Access Denied');
}

if($AdminLogin->CheckValue($NavInputKey, $_SESSION['NavInputKey']) == true && $AdminLogin->CheckHttpReferer(WEB_URL.'/admin/index.php?location=nav') == true){

//连接到MYSQL并打开系统数据库                                                         
$DataBase->open_db();

//查询导航栏目信息 
$DataBase->sel('nav', "ORDER BY `order` ASC");

//操作返回信息
$return['del'] = true;
$return['upnav'] = true;
$return['addnav'] = true;

//删除操作
if($AdminControl->IsPost(array('sel'), 'SaveSiteInfo')){
	//循环删除选中的栏目
	foreach ($AdminControl->Post['sel'] as $k => $v){
		//定义过滤方法
		$AdminControl->CheckPostFun = array(
				'sel' => array(array('ToUtf8', 'trimall', 'DeleteHtml', 'strip_tags'), array('CheckNull', 'CheckNavCheckbox'))
		);
		//过滤提交的信息
		if($AdminControl->CheckPostValue()){
			if(check_zzs($AdminControl->Post['sel'][$k])){
				if($DataBase->Del('nav', "WHERE `id` = '".$AdminControl->Post['sel'][$k]."'") == false){
					$return['del'] = false;
				}
				//待改进，$k是否要过滤？
			}
		}
		else{
			$return['del'] = false;
		}
		//echo $k.'=>'.$v.'<br/>';
	}
}

//如果存在导航栏目，则进行更新
if(count($DataBase->Result) !== 0){
	//循环接收提交的值并进行过滤，然后更新数据库
	foreach ($DataBase->Result as $value){
		if($AdminControl->IsPost(array('order_'.$value['id'], 'name_'.$value['id'], 'url_'.$value['id'], 'target_'.$value['id']), 'SaveSiteInfo')){
			//定义过滤方法
			$AdminControl->CheckPostFun = array(
					'order_'.$value['id'] => array(array('ToUtf8', 'trimall', 'DeleteHtml', 'strip_tags'), 'CheckNull'),
					'name_'.$value['id'] => array(array('ToUtf8', 'DeleteHtml', 'strip_tags'), 'CheckNull'),
					'url_'.$value['id'] => array(array('ToUtf8', 'trimall', 'DeleteHtml', 'strip_tags'), 'CheckOK'),
					'target_'.$value['id'] => array(array('ToUtf8', 'trimall', 'DeleteHtml', 'strip_tags'), array('CheckNull', 'CheckUrlTarget'))
			);
			//过滤提交的信息
			if($AdminControl->CheckPostValue()){
				//获取过滤后的值
				$PostMenu[1] = $AdminControl->Post['order_'.$value['id']];
				$PostMenu[2] = $AdminControl->Post['name_'.$value['id']];
				$PostMenu[3] = $AdminControl->Post['url_'.$value['id']];
				$PostMenu[4] = $AdminControl->Post['target_'.$value['id']];
				
				//如果没有更新则不执行updata语句
				if($PostMenu[1] !== $value['order'] || $PostMenu[2] !== $value['name'] || $PostMenu[3] !== $value['url'] || $PostMenu[4] !== $value['target']){
					//更新导航
					if($DataBase->up('nav', "`order` = '$PostMenu[1]', `name` = '$PostMenu[2]', `url` = '$PostMenu[3]', `target` = '$PostMenu[4]'", "WHERE `id` = '$value[id]'") == false){
						$return['upnav'] = false;	//更新失败
					}
				}						
			}
			else{
				$return['upnav'] = false; //验证未通过
			}
		}
	}
}
else{
	$return['upnav'] = false; //没有查询到栏目信息
}

//新增栏目
$i = 1;
while(true){
	$k = $i++;
	if($AdminControl->IsPost(array('NewOrder_'.$k, 'NewName_'.$k, 'NewUrl_'.$k, 'Newtarget_'.$k), 'SaveSiteInfo')){

		//定义过滤方法
		$AdminControl->CheckPostFun = array(
				'NewOrder_'.$k => array(array('ToUtf8', 'trimall', 'DeleteHtml', 'strip_tags'), 'CheckNull'),
				'NewName_'.$k => array(array('ToUtf8', 'DeleteHtml', 'strip_tags'), 'CheckNull'),
				'NewUrl_'.$k => array(array('ToUtf8', 'trimall', 'DeleteHtml', 'strip_tags'), 'CheckOK'),
				'Newtarget_'.$k => array(array('ToUtf8', 'trimall', 'DeleteHtml', 'strip_tags'), array('CheckNull', 'CheckUrlTarget'))
		);
		//过滤提交的信息
		if($AdminControl->CheckPostValue()){
			if($AdminControl->Post['NewName_'.$k] !== ''){
				//添加新栏目
				if($DataBase->Add('nav', array('order' => $AdminControl->Post['NewOrder_'.$k], 'name' => $AdminControl->Post['NewName_'.$k], 'url' => $AdminControl->Post['NewUrl_'.$k], 'target' => $AdminControl->Post['Newtarget_'.$k])) == false){
					$return['addnav'] = false;
				}
			}
		}
	}
	else{
		break;
	}
}

if($return['del'] == false || $return['upnav'] == false || $return['addnav'] == false){
	$AdminShowMessage = AdminShowMessage('更新失败！请检查原因！', WEB_URL.'/admin/index.php?location=nav', 0, '#F00');
}
else{
	$AdminShowMessage = AdminShowMessage('导航栏目更新成功！', WEB_URL.'/admin/index.php?location=nav', 1);
}

//关闭数据库连接
$DataBase->close_dblink();

}
else{
	$AdminShowMessage = AdminShowMessage('来路不正确！', WEB_URL.'/admin/index.php?location=nav', 0, '#F00');
}

?>