<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class NewAction extends BbsAction
{
    public function run()
    {
        $fid = S::getGP('fid', 'G');
        $sqlAdd = !empty($fid) ? ' AND fid='.$fid : '';
        $pageSize = 20;
        $tFields = 'id,subject,createdate,uid,username,topped,replies,hits,islock,isdigest';
        $tTable = Threads::table();
        
        if ($fid)
        {
            $forum = Forums::single()->getOne($fid);
            empty($forum) && $this->showMessage('不存在的版块');
            $this->isAllowVisit($fid);
        }
        
        $count = $this->db()->getValue('SELECT COUNT(*) FROM '.Threads::table().' WHERE `date` = '.S::sqlEscape(date('Y-m-d')).' AND ischeck = 1 AND isdelete = 0'.$sqlAdd);
        $pageObj = new iPage();
        $pageObj->setPageSize($pageSize);
        $pageObj->setTotalRecord($count);
        
        $sql = 'SELECT '.$tFields.' FROM '.$tTable.' WHERE ischeck=1 AND isdelete = 0 AND `date`='.S::sqlEscape(date('Y-m-d')).$sqlAdd.' ORDER BY id DESC LIMIT '.$pageObj->getOffset().','.$pageObj->getPageSize();
        $threads = $this->db()->setQueryString($sql)->find();
        
        $isAllowPost = $this->isAllowPost($fid);
        $this->getController()->render('new', array(
                'fid' => $fid,
                'forum' => $forum,
                'threads' => $threads,
                'pageObj' => $pageObj,
                'isAllowPost' => $isAllowPost,
            )
        );
    }
}