<?php
/**
* FILE_NAME : download.php
*
* @copyright Copyright (c) 2012-2013 www.shuangyulin.com
* @author 双鱼林
* @package 文件下载管理
* @subpackage
* @version QQ287307421或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']);
}

/*------------------------------------------------------ */
//-- 显示下载列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'show')
{
	$sql = $query = $downlist = '';
	
	$sql = "SELECT * FROM {$dbprefix}downloads ORDER BY displayorder";
	$query = $DB->query($sql);
	while ($row = $DB->fetch_array($query))
	{
		$row['note'] = $row['note'] ? cutstr($row['note'],40) : "无文件说明";
		$downlist[] = $row;
	}
	
	$smarty->assign("css_root", $css_root);
	$smarty->assign("web_url", $web_url);
	
	$smarty->assign("downlist", $downlist);
	
	$smarty->display("download.html");
}

/*------------------------------------------------------ */
//-- 增加文件下载
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'add')
{
	$sql = $query = $downcatelist = '';
//	$name = $_REQUEST['name'] ? $_REQUEST['name'] : '';
	
	/*--  查询分类信息  --*/
	$sql = "SELECT catid,name FROM {$dbprefix}downcategories";
	$query = $DB->query($sql);
	while ($row = $DB->fetch_array($query)) {
		$downcatelist[] = $row;
	}
	
	$smarty->assign("css_root", $css_root);
	$smarty->assign("web_url", $web_url);
//	$smarty->assign("name", $name);
	
	$smarty->assign("downcatelist",$downcatelist);
	$smarty->display("download_add.html");
}

/*------------------------------------------------------ */
//-- 增加文件下载成功
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'addok')
{
	$down = $_REQUEST['down'];
	$downfile = $_REQUEST['downfile'];
	$sql = $query = '';
	
	/*--  插入信息  --*/
	$sql = "INSERT INTO {$dbprefix}downloads (id,catid,title,downfile,note,postdate,displayorder) VALUE ('','".$down['catid']."','".$down['title']."','".$downfile."','".$down['note']."','".$timestamp."','".$down['displayorder']."')";
	$query = $DB->query($sql);
	
	if ($query) {
		showmsg("文件下载增加成功","download.php?act=show");
	} else {
		showmsg("文件下载增加失败","download.php?act=show");
	}
}

/*------------------------------------------------------ */
//-- 编辑文件下载
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'edit')
{
	$id = $_REQUEST['id'];
	
	$sql = $query = $row = '';
	$downcatelist = $list = array();
	
	/*--  查询分类信息  --*/
	$sql = "SELECT catid,name FROM {$dbprefix}downcategories";
	$query = $DB->query($sql);
	while ($row = $DB->fetch_array($query))
	{
		$downcatelist[] = $row;
	}
	
	$sql = "SELECT * FROM {$dbprefix}downloads WHERE id = $id";
	$query = $DB->query($sql);
	$list = $DB->fetch_array($query);
	
	$smarty->assign("css_root", $css_root);
	$smarty->assign("web_url", $web_url);
	
	$smarty->assign("down_id",$list['id']);
	$smarty->assign("down_catid",$list['catid']);
	$smarty->assign("down_title",$list['title']);
	$smarty->assign("down_file",$list['downfile']);
	$smarty->assign("down_note",$list['note']);
	$smarty->assign("down_disp",$list['displayorder']);
	
	$smarty->assign("downcatelist",$downcatelist);
	
	$smarty->display("download_edit.html");
}

/*------------------------------------------------------ */
//-- 编辑文件下载成功
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'editok')
{
	$down = $_REQUEST['down'];
	$downfile = $_REQUEST['downfile'];
	$sql = $query = $singcatelist = '';
	
	/*--  修改信息  --*/
	$sql = "UPDATE {$dbprefix}downloads SET catid = '".$down['catid']."',title = '".$down['title']."',downfile = '".$downfile."',note = '".$down['note']."',displayorder = '".$down['displayorder']."' WHERE id = '".$down['id']."'";
	$query = $DB->query($sql);
	
	if ($query) {
		showmsg("文件下载更新成功","download.php?act=show");
	}
}

/*------------------------------------------------------ */
//-- 删除下载
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'remove')
{
	$id = $_REQUEST['id'];
	$sql = $query = $downcatelist = '';
	
	/*--  删除信息  --*/
	$sql = "DELETE FROM {$dbprefix}downloads WHERE id = $id";
	$query = $DB->query($sql);
	
	if ($query) {
		showmsg("文件删除成功","download.php?act=show");
	} else {
		showmsg("文件删除失败");
	}
}
?>