<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class RatingAction extends BbsAction
{
    public function run()
    {
        $tid = $this->getTid();
        $thread = $this->getExistsThread($tid);
        $pid = S::gp('pid','G',2);
        $merge = array();
        
        $where = 'tid='.$tid;
        if ($pid)
        {
            $where = 'pid='.$pid;
            $merge = array_merge($merge, array('pid'=>$pid));
        }
        
        $count = $this->db()->getValue('SELECT COUNT(*) FROM '.ThreadsRating::table().' WHERE '.$where);
        $pageObj = new iPage();
        $pageObj->setPageSize(10);
        $pageObj->setTotalRecord($count);
        
        $sql = 'SELECT * FROM '.ThreadsRating::table().' WHERE '.$where.' ORDER BY id DESC '.S::sqlLimit($pageObj->getOffset(),$pageObj->getPageSize());
        $ratings = $this->db()->setQueryString($sql)->find();
        
        if (!empty($ratings))
        {
            foreach ($ratings as &$rating)
            {
                $rating['username'] = Members::single()->getNameByUid($rating['uid']);
            }
        }
        //print_r($ratings);die;
        $this->getController()->render('rating', array(
                'tid' => $tid,
                'thread' => $thread,
                'merge' => $merge,
                'pageObj' => $pageObj,
                'ratings' => $ratings,
            )
        );
    }
}