<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class CommentAction extends BaseAction
{
    public function run()
    {
        if (isset($_POST['message']))
        {
            $this->getController()->isLogin();
            $message = trim(S::gp('message', 'P'));
            if (empty($message))
            {
                throw new iSystemException('内容不能为空');
            }
            $data = array(
                'uid' => $this->uid(),
                'username' => $this->userName(),
                'message' => BookPost::single()->filter($message),
                'createdate' => date('Y-m-d H:i:s'),
            );
            $this->db()->insert(Wacomment::table())->value($data)->q();
            $forward = url('system',array('s'=>'props','c'=>'wa','a'=>'comment'));
            iforward($forward);
        }
        
        $pagesize = 10;
        $pageObj = new iPage();
        $totalRecord = $this->db()->getValue('SELECT COUNT(*) AS c FROM `'.Wacomment::table().'`');

        $pageObj = new iPage();
        $pageObj->setTotalRecord($totalRecord);
        $pageObj->setPageSize($pagesize);
        $offset = ($pageObj->getCurrentPage() - 1) * $pagesize;
        $sql = "SELECT * FROM `".Wacomment::table()."` ORDER BY `createdate` DESC LIMIT $offset,$pagesize";
        $comments = $this->db()->setQueryString($sql)->find();

        $floor = $totalRecord - ($pageObj->getCurrentPage() - 1) * $pagesize;
        
        $this->getController()->render('comment',array(
                'comments' => $comments,
                'pageObj' => $pageObj,
                'floor' => $floor,        
            )
        );
    }
}