<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class ReadAction extends ForumAction
{
    public function run()
    {
        $bookPostReply = BookPostReply::single();
        $postId = S::gp('cid','G',2);
        $this->db()->update(BookPostReply::table())->incrField(array('viewcount'=>1))->where(array('postid'=>$postId,'isfirst'=>1))->q();
        $thread = $bookPostReply->getThread($postId);

        if ($bookPostReply->mustBuy($thread))
        {
            $bookPostReply->halt($thread);
        }
    
        $good = (!empty($thread['good']) ? count(array_diff(explode(',', $thread['good']),array(''))) : 0);
        $bad = (!empty($thread['bad']) ? count(array_diff(explode(',', $thread['bad']),array(''))) : 0);
        $complaints = (!empty($thread['complaints']) ? count(array_diff(explode(',', $thread['complaints']),array(''))) : 0);
        $replyCount = $this->db()->getValue('SELECT COUNT(1) AS c FROM '.BookPostReply::table().' WHERE postid='.$postId.' AND isfirst=0');
        
        $sql = 'SELECT uid,username,content,createdate FROM '.BookPostReply::table().' WHERE postid='.$postId.' AND isfirst = 0 ORDER BY id DESC LIMIT 5';
        $replies = $this->db()->setQueryString($sql)->find();
        
        $this->getController()->render('read', array(
                'bookid' => $this->id(),
                'thread' => $thread,
                'postId' => $postId,
                'good' => $good,
                'bad' => $bad,
                'complaints' => $complaints,
                'replyCount' => $replyCount,
                'replies' => $replies,
            )
        );
    }
}