<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class PostAction extends MemberAction
{
    public function run()
    {
        $sort = empty($_GET['sort']) ? 'DESC' : 'ASC';
        $field = S::gp('f', 'G', 2) ? 'id' : 'lastpost';
        $uid = !empty($_GET['uid']) ? S::gp('uid', 'G', 2) : $this->uid();
        
        $orderBy = ' ORDER BY '.$field . ' ' . $sort;
        $count = $this->db()->getValue('SELECT COUNT(*) AS c FROM '.Threads::table().' WHERE uid = '.$uid);
        
        $pagination = new iPage();
        $pagination->setPageSize(10);
        $pagination->setTotalRecord($count);
        $sql = 'SELECT id,username,uid,subject,lastpost,lastpost_user,hits,replies,isdigest FROM '.Threads::table().' WHERE uid = '.$uid. ' '.$orderBy. ' LIMIT '.$pagination->getOffset().','.$pagination->getPageSize();
        $threads = $this->db()->setQueryString($sql)->find();
        $floor = $pagination->getFloor();
        $isSelf = $uid == $this->uid() ? true : false;
        
        $this->getController()->render('post',array(
                'pagination' => $pagination,
                'floor' => $floor,
                'threads' => $threads,
                'sort' => $sort,
                'field' => $field,
                'isSelf' => $isSelf,
                'uid' => $uid,
            )
        );
    }
}