<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
define('IN_IORANGE', true);
require_once 'include/init.php';

if (URL_REWRITE)
{
    $pathInfo = iRequest::getPathInfo();
    if (!empty($pathInfo))
    {
        preg_match('/(\d+)/s', $pathInfo, $match);
        $_GET['bid'] = $match[1];
    }
}
$bookid = S::gp('bid','G',2);
$book = fetchBookInfo($bookid);
Book::isCanRead($book['view']);

$bookname = $book['bookname'];
$author = $book['author'];
$lastchaptername = $book['lastchaptername'];

$addCondition = isWebRequest() ? '' : ' AND isimage = 0';
$pagesize = 10;
$sort = $_GET['sort'] ? 'DESC' : 'ASC';
$totalRecord = $db->getValue("SELECT COUNT(*) AS c FROM `".Chapter::table()."` WHERE `bookid` = $bookid AND display = 1 {$addCondition}");

$pageObj = new iPage();
$pageObj->setTotalRecord($totalRecord);
$pageObj->setPageSize($pagesize);

$limit = 'LIMIT '.$pageObj->getOffset().','.$pageObj->getPageSize();
$sql = "SELECT chapterid,chaptername,bookid,size,createdate,isvip FROM `".Chapter::table()."` WHERE `bookid` = $bookid AND display = 1 {$addCondition} ORDER BY `sort` $sort {$limit}";
$chapters = $db->setQueryString($sql)->find();

include_once tpl('chapter');