<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class ListAction extends ForumAction
{
    public function run()
    {
        $sort = $_GET['sort'] ? 'ASC' : 'DESC';
        $postId = S::gp('cid','G',2);

        $replyCount = $this->db()->getValue('SELECT COUNT(*) AS c FROM '.BookPostReply::table().' WHERE postid='.$postId.' AND isfirst=0');

        $pageSize = 10;
        $pagination = new iPage();
        $pagination->setPageSize($pageSize);
        $pagination->setTotalRecord($replyCount);
        $offset = $pagination->getOffset();
        
        $sql = 'SELECT uid,username,content,createdate FROM '.BookPostReply::table().' WHERE postid='.$postId.' AND isfirst=0 ORDER BY createdate '.$sort." LIMIT $offset,$pageSize";
        $replies = $this->db()->setQueryString($sql)->find();
    
        $thread = BookPostReply::single()->getThread($postId);
        $floor = $pagination->getFloor();
        
        $this->getController()->render('list',array(
                'bookid' => $this->id(),
                'thread' => $thread,
                'replies' => $replies,
                'postId' => $postId,
                'pagination' => $pagination,
                'floor' => $floor,
            )
        );
    }
}