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

if (URL_REWRITE)
{
    $pathInfo = iRequest::getPathInfo();
    if (!empty($pathInfo))
    {
        preg_match('/new-(\d+)/s', $pathInfo, $match);
        $_GET['page'] = $match[1];
    }
}

if (isset($_GET['action']) && $_GET['action'] == 'more')
{
    $sql = 'SELECT B.`bookid`, B.`bookname`, B.bookinfo, B.size, B.`typeid`, T.`typename` FROM `'.Book::table().'` AS B
        	INNER JOIN `'.Types::table().'` AS T ON T.`typeid` = B.`typeid`
        	WHERE B.`view` = 1';
    $countSql = 'SELECT COUNT(*) FROM `'.Book::table().'` AS B
        		 INNER JOIN `'.Types::table().'` AS T ON T.`typeid` = B.`typeid`
        		 WHERE B.`view` = 1
        		 ORDER BY B.`bookid` DESC';
}
else
{
    $condition = 'B.view = 1';
    if (isProxyRequest())
    {
        $timestr = date('Y-m-d H:i:s', $time - 1800);
        $condition .= " AND updatedate < '{$timestr}' ";
    }
    
    $countSql = 'SELECT COUNT(*) FROM `'.Book::table().'` AS B
     	 		 INNER JOIN `'.Types::table().'` AS T ON T.`typeid` = B.`typeid`
    			 WHERE '.$condition;
    
    $sql = 'SELECT B.`bookname`, B.`bookid`, B.`author`, B.size, B.bookinfo, B.`status`, B.`wlastchapterid`, B.`wlastchaptername`,B.`lastchapterid`, B.`lastchaptername`, B.`updatedate`, T.`typeid`, T.`typename`
 		 	FROM `'.Book::table().'` AS B
     	 	INNER JOIN `'.Types::table().'` AS T ON T.`typeid` = B.`typeid`
    		WHERE ' . $condition . '
    		ORDER BY B.`updatedate` DESC';
}

$pageSize = isWebRequest() ? 30 : 10;
$count = $db->getValue($countSql);
$pageObj = new iPage();
$pageObj->setPageSize($pageSize);
$pageObj->setTotalRecord($count);
$sql .= ' LIMIT '.$pageObj->getOffset().','.$pageObj->getPageSize();

$key = md5($sql);
$new = $cache->get($key);
if (false === $new)
{
    $new = $db->setQueryString($sql)->find();
    $cache->set($key, $new, 10 * 60);
}

if (!empty($new))
{
    foreach ($new as &$book)
    {
        $value['bookinfo'] = preg_replace('/\s/i', '', preg_replace('/\n/i', '', preg_replace('/　/i', '', $book['bookinfo'])));
        if (mb_strlen($book['bookinfo'], 'utf-8') > 30)
        {
            $book['bookinfo'] = iSubstr($book['bookinfo'], 0, 32, '...');
        }
    }
}

include_once tpl("new");