<?php
/**
* FILE_NAME : navgation.php
*
* @copyright Copyright (c) 2012-2013 www.shuangyulin.com
* @author 双鱼林
* @package 导航菜单管理
* @subpackage
* @version QQ:287307421 或 254540457
*/

define('IN_WEB', TRUE);

require_once( str_replace('\\', '/', dirname(__FILE__)). '/include/global.php');

/* act操作项的初始化 */
if (empty($_REQUEST['act'])) {
    $_REQUEST['act'] = 'show';
} else {
    $_REQUEST['act'] = trim($_REQUEST['act']);
}

/*--  初始化变量  --*/
$sql = $query = '';
$nav = $navs = $row = $list = $catelist = array();
	
/*------------------------------------------------------ */
//-- 显示导航列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'show')
{
    $sql = "SELECT * FROM {$dbprefix}navgation ORDER BY displayorder";
    $query = $DB->query($sql);
    while($nav = $DB->fetch_array($query))
    {
    	$nav['target'] = $nav['target'] ? "新窗口" : "原窗口";
    	if ($nav['outurl']) {
    		$nav['url'] = $nav['outurl']."<span style='color:red;'>(自定义链接)</span>";
    	}
    	$navs[] = $nav;
    }

    $smarty->assign("css_root",$css_root);
    $smarty->assign("navs",$navs);
    
    $smarty->display("navgation.html");
}

/*------------------------------------------------------ */
//-- 编辑导航
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'edit')
{
	$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : '';
	
    $sql = $query = '';
    $sql = "SELECT * FROM {$dbprefix}navgation WHERE id = '".$id."'";
    $query = $DB->query($sql);
    $nav = $DB->fetch_array($query);
    
    /*--  查询单页分类表  --*/
	$sql = "SELECT id,title FROM {$dbprefix}singlepage";
	$query = $DB->query($sql);
	while ($row = $DB->fetch_array($query)) {
		$list['url'] = "about.php?id=".$row['id'];
		$list['name'] = $row['title'];
		$catelist[] = $list;
	}
	
	/*--  查询文章分类表  --*/
	$sql = "SELECT catid,name FROM {$dbprefix}articlecategories";
	$query = $DB->query($sql);
	while ($row = $DB->fetch_array($query)) {
		$list['url'] = "news.php?id=".$row['catid'];
		$list['name'] = $row['name'];
		$catelist[] = $list;
	}
	
	/*--  查询图片分类表  --*/
	$sql = "SELECT catid,name FROM {$dbprefix}picturecategories";
	$query = $DB->query($sql);
	while ($row = $DB->fetch_array($query)) {
		$list['url'] = "picture.php?id=".$row['catid'];
		$list['name'] = $row['name'];
		$catelist[] = $list;
	}
	
	/*--  查询商品分类表  --*/
	$sql = "SELECT catid,name FROM {$dbprefix}producecategories";
	$query = $DB->query($sql);
	while ($row = $DB->fetch_array($query)) {
		$list['url'] = "produce.php?id=".$row['catid'];
		$list['name'] = $row['name'];
		$catelist[] = $list;
	}
	
	/*--  查询下载分类表  --*/
	$sql = "SELECT catid,name FROM {$dbprefix}downcategories";
	$query = $DB->query($sql);
	while ($row = $DB->fetch_array($query)) {
		$list['url'] = "download.php?id=".$row['catid'];
		$list['name'] = $row['name'];
		$catelist[] = $list;
	}
    
    $smarty->assign("nav_id",$nav['id']);
    $smarty->assign("nav_name",$nav['name']);
    $smarty->assign("nav_url",$nav['url']);
    $smarty->assign("nav_outurl",$nav['outurl']);
    $smarty->assign("nav_target",$nav['target']);
    $smarty->assign("nav_displayorder",$nav['displayorder']);
    $smarty->assign("css_root",$css_root);
    $smarty->assign("catelist",$catelist);
    
    $smarty->display("navgation_edit.html");
}

/*------------------------------------------------------ */
//-- 更新导航
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'editok')
{
	$nav = isset($_REQUEST['nav']) ? $_REQUEST['nav'] : '';
	
	/*--  判断是否是自定义链接  --*/
	if ($nav['url'] == 'custom') {
		if ( $nav['outurl'] != '') {
			$nav['url'] = $nav['outurl'];
		} else {
			showmsg("自定义链接为空");
		}	
	}
	
    $sql = "UPDATE {$dbprefix}navgation SET name='".$nav['name']."', url= '".$nav['url']."', outurl= '".$nav['outurl']."', target = ".$nav['target'].", displayorder = ".$nav['displayorder']." WHERE id = '".$nav['id']."'";
    $query = $DB->query($sql);
    
	if ($query) {
    	showmsg("导航更新成功","navgation.php?act=show");
    } else {
    	showmsg("导航更新失败","navgation.php?act=show");
    }
}

/*------------------------------------------------------ */
//-- 删除导航
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'remove')
{
	$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : '';
	
    $sql = "DELETE FROM {$dbprefix}navgation WHERE id = '".$id."'";
    $query = $DB->query($sql);
    
    if ($query) {
    	showmsg("导航删除成功","navgation.php?act=show");
    } else {
    	showmsg("导航删除失败","navgation.php?act=show");
    }
}

/*------------------------------------------------------ */
//-- 添加导航
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'add')
{
	/*--  查询单页分类表  --*/
	$sql = "SELECT id,title FROM {$dbprefix}singlepage";
	$query = $DB->query($sql);
	while ($row = $DB->fetch_array($query)) {
		$list['url'] = "about.php?id=".$row['id'];
		$list['name'] = $row['title'];
		$catelist[] = $list;
	}
	
	/*--  查询文章分类表  --*/
	$sql = "SELECT catid,name FROM {$dbprefix}articlecategories";
	$query = $DB->query($sql);
	while ($row = $DB->fetch_array($query)) {
		$list['url'] = "news.php?id=".$row['catid'];
		$list['name'] = $row['name'];
		$catelist[] = $list;
	}
	
	/*--  查询图片分类表  --*/
	$sql = "SELECT catid,name FROM {$dbprefix}picturecategories";
	$query = $DB->query($sql);
	while ($row = $DB->fetch_array($query)) {
		$list['url'] = "picture.php?id=".$row['catid'];
		$list['name'] = $row['name'];
		$catelist[] = $list;
	}
	
	/*--  查询商品分类表  --*/
	$sql = "SELECT catid,name FROM {$dbprefix}producecategories";
	$query = $DB->query($sql);
	while ($row = $DB->fetch_array($query)) {
		$list['url'] = "produce.php?id=".$row['catid'];
		$list['name'] = $row['name'];
		$catelist[] = $list;
	}
	
	/*--  查询下载分类表  --*/
	$sql = "SELECT catid,name FROM {$dbprefix}downcategories";
	$query = $DB->query($sql);
	while ($row = $DB->fetch_array($query)) {
		$list['url'] = "download.php?id=".$row['catid'];
		$list['name'] = $row['name'];
		$catelist[] = $list;
	}
	
	$smarty->assign("css_root",$css_root);
    $smarty->assign("catelist",$catelist);
    
    $smarty->display("navgation_add.html");
}

/*------------------------------------------------------ */
//-- 添加导航成功
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'addok')
{
	$nav = isset($_REQUEST['nav']) ? $_REQUEST['nav'] : '';
	
	/*--  判断是否是自定义链接  --*/
	if ($nav['url'] == 'custom') {
		if ( $nav['outurl'] != '') {
			$nav['url'] = $nav['outurl'];
		} else {
			showmsg("自定义链接为空");
		}	
	}
	
	/*--  添加导航  --*/
    $sql = "INSERT INTO {$dbprefix}navgation (id, name, url, outurl, target, displayorder) VALUES ('','".$nav['name']."','".$nav['url']."','".$nav['outurl']."','".$nav['target']."','".$nav['displayorder']."')";
    $query = $DB->query($sql);
    
    if ($query) {
    	showmsg("导航添加成功","navgation.php?act=show");
    } else {
    	showmsg("导航添加失败","navgation.php?act=add");
    }
}


unset($sql,$query,$nav,$navs,$row,$list,$catelist);
?>