<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class ListAction extends BbsAction
{
    public function run()
    {
        $sqlAdd = '';
        $tid = $this->getTid();
        $thread = $this->getExistsThread($tid);
        $count = $thread['replies'];
        $sort = isset($_GET['sort']) ? 'ASC' : 'DESC';
        
        $uid = S::gp('uid','G',2);
        if ($uid)
        {
            $sqlAdd = ' AND uid='.$uid;
            $count = $this->db()->getValue('SELECT COUNT(*) FROM '.Posts::table().' WHERE tid='.$tid.' AND isdelete = 0 '.$sqlAdd);
        }
        
        $pageObj = new iPage();
        $pageSize = 10;
        $pageObj->setPageSize($pageSize);
        $pageObj->setTotalRecord($count);

        $hasTop = false;
        $sql = 'SELECT id,tid,uid,username,content,createdate,floor,toped FROM '.Posts::table().' WHERE tid='.$tid.' AND isdelete = 0 '.$sqlAdd.' ORDER BY id '.$sort.' '.S::sqlLimit($pageObj->getOffset(),$pageObj->getPageSize());
        $query = $this->db()->query($sql);
        while ($post = $this->db()->getArr($query))
        {
            $post['toped'] && $hasTop = true;
            $posts[] = $this->analyzeRead($post, 'reply');
        }
        
        $forum = $this->getController()->getForumCache($thread['fid']);
        $allows = $this->getAllows($thread);
        
        $this->getController()->render('list', array(
                'tid' => $tid,
                'pageObj' => $pageObj,
                'thread' => $thread,
                'posts' => $posts,
                'hasTop' => $hasTop,
                'forum' => $forum,
                'allows' => $allows,
                'sort' => $sort,
            )
        );
    }
}