<?php
/**
* FILE_NAME : download.php
*
* @copyright Copyright (c) www.shuangyulin.com
* @author 双鱼林设计
* @package 前台文件下载
* @subpackage
* @version QQ:287307421或254540457
*/

require_once './include/common.inc.php';

/*--  对一些系统变量赋值  --*/
$template->assign("css_root",$css_root);
$template->assign("siteurl",$_CFG['siteurl']);
$template->assign("sitename",$_CFG['sitename']);

/*--  初始化 url 传值  --*/
$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 1;

/*--  查询下载信息  --*/
$sql = $allfile = '';
$down = $cate = array();

$sql = "SELECT * FROM {$dbprefix}downcategories WHERE catid = ".$id;
$down = $DB->fetch_one_array($sql);
if (!$down) {
	showmessage("找不到您查找的页面，请点击导航栏浏览！","index.php");
}

/*--  查询分类  --*/
$sql = "SELECT * FROM {$dbprefix}downcategories WHERE parentid = '".$down['parentid']."' ORDER BY displayorder";
$query = $DB->query($sql);
while ($row = $DB->fetch_array($query)) {
	$row['url'] = "download.php?id=".$row['catid'];
	$cate[] = $row;
}

/*--  处理分页数据  --*/
$sql = "SELECT * FROM {$dbprefix}downloads WHERE catid = '".$id."'";
$query = $DB->query($sql);

$allnum = $DB->num_rows($query);
$perpage = $_CFG['article_num'] ? intval($_CFG['article_num']) : 15;
//$perpage = 1;
$curpage = (int)$_GET['page'];
$php_self = "download.php?id=".$id;

/*--  创建分页  --*/
$pages = multi($allnum, $perpage, $curpage, $php_self, 5);

/*--  根据分类 id 和 分页信息 查询该分类下的所有下载  --*/
if (($curpage-1) >= 0) {
	$step = $curpage-1;
} else {
	$step = 0;
}
$startpage = $perpage * $step;

$sql = "SELECT d.title,d.downfile,d.note,d.postdate,a.id,a.filename,a.temname,a.size,a.downloads FROM {$dbprefix}downloads d LEFT JOIN {$dbprefix}attachments a ON (d.downfile = a.temname) WHERE d.catid = '".$down['catid']."' ORDER BY d.displayorder LIMIT $startpage , $perpage";
$query = $DB->query($sql);
while ($row = $DB->fetch_array($query)) {
	$row['downurl'] = "attachments.php?id=".$row['id'];
	$row['size'] = formatbyte($row['size'],2);
	$row['postdate'] = formatdate($_CFG['format_type'],$row['postdate']);
	$row['note'] = cutstr($row['note'],200);
	$allfile[] = $row;
}

/*--  选择模板  --*/
$pagetpl = '';
if (!$cate['template']) {
	$pagetpl = "download.html";
} else {
	$pagetpl = $cate['template'];
}

require_once 'header.php';

/*--  对模板进行赋值  --*/
$template->assign("pagename",$down['name']);
$template->assign("pageurl",$php_self);
$template->assign("pages",$pages);
$template->assign("cates",$cate);
$template->assign("allfile",$allfile);

$template->display($pagetpl);

require_once 'footer.php';
?>