<?php
/**
* FILE_NAME : picture.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;

/*--  查询自己，判断url提交是否正确  --*/
$sql = $allpic = '';
$oneself = $cates = array();

$sql = "SELECT catid,name,parentid,template,covertemplate FROM {$dbprefix}picturecategories WHERE catid = $id";
$oneself = $DB->fetch_one_array($sql);
if (!$oneself) {
	showmessage("您查看的页面不存在，2秒后跳到主页","inde.php");
}

/*--  查询父级分类，方便小导航显示  --*/
$sql = "SELECT catid,name FROM {$dbprefix}picturecategories WHERE catid = '".$oneself['parentid']."'";
$parcate = $DB->fetch_one_array($sql);
$parcateurl = "picture.php?id=".$parcate['catid'];
$parcatename = $parcate['name'];

/*--  查询子分类列表  --*/
$sql = "SELECT catid,name,covertemplate,template FROM {$dbprefix}picturecategories WHERE parentid = $id ORDER BY displayorder";
$query = $DB->query($sql);
if ($query) {
	while ($row = $DB->fetch_array($query)) {
		$row['sysurl'] = "picture.php?id=".$row['catid'];
		$cates[] = $row;
	}
}

/*--  查询最新图片列表  --*/
$sql = "SELECT prid,catid,title FROM {$dbprefix}picturecontent ORDER BY dateline DESC LIMIT 10";
$query = $DB->query($sql);
while ($row = $DB->fetch_array($query)) {
	$row['sysurl'] = "pictures.php?id=".$row['prid'];
	$newpic[] = $row;
}

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

$allnum = $DB->num_rows($query);
$perpage = $_CFG['article_num'] ? intval($_CFG['article_num']) : 15;
//$perpage = 12;
$curpage = (int)$_GET['page'];
$php_self = "picture.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 * FROM {$dbprefix}picturecontent WHERE catid = '".$oneself['catid']."' ORDER BY dateline DESC LIMIT $startpage , $perpage";
$query = $DB->query($sql);
while ($row = $DB->fetch_array($query)) {
	$row['sysurl'] = "pictures.php?id=".$row['prid'];
	$row['dateline'] = formatdate($_CFG['format_type'],$row['dateline']);
	/*--  处理封面图片  --*/
	$row['coverpic'] = unserialize($row['coverpic']);
	//处理封面图片的路径
	$row['coverpic'] = $web_url.substr($row['coverpic']['cover_dir'],3).$row['coverpic']['cover_name'];
	$allpic[] = $row;
}

/*--  当该分类的自定义模板为空，使用默认模板  --*/
$pagetpl = '';
if (!$oneself['template'] && !$oneself['covertemplate']) {	
	$pagetpl = "picture.html";	
}
elseif ($oneself['covertemplate']) {
	$pagetpl = $oneself['covertemplate'];
}
elseif ($oneself['template']) {
	$pagetpl = $oneself['template'];
}

require_once 'header.php';

/*--  对模板进行赋值  --*/
$template->assign("oneselfname",$oneself['name']);
$template->assign("oneselurl",$php_self);

/*--  对父级分类赋值  --*/
$template->assign("parcateurl",$parcateurl);
$template->assign("parcatename",$parcatename);

$template->assign("pages",$pages);

/*--  对左边栏的子分类赋值  --*/
$template->assign("cates",$cates);
/*--  对左边栏的最新图片赋值  --*/
$template->assign("newpic",$newpic);
/*--  对图片组赋值  --*/
$template->assign("allpic",$allpic);

$template->display($pagetpl);

require_once 'footer.php';
?>