<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class NewAction extends BaseAction
{
    public function run()
    {
        $pageSize = 20;
        $count = $this->db()->getValue('SELECT COUNT(*) FROM '.BookPostMonth::table().' WHERE isadvance = 1 ');

        $pageObj = new iPage();
        $pageObj->setPageSize($pageSize);
        $pageObj->setTotalRecord($count);

        $sql = 'SELECT bookpostsid,uid,bookid,message,createdate FROM '.BookPostMonth::table().' WHERE isadvance = 1 ORDER BY bookpostsid DESC '.S::sqlLimit($pageObj->getOffset(), $pageObj->getPageSize());
        $posts = $this->db()->setQueryString($sql)->find();

        if (!empty($posts))
        {
            foreach ($posts as &$post)
            {
                $memberInfo = Members::single()->getOneCache($post['uid']);
                if (!empty($memberInfo['medals']))
                {
                    $post['medals'] = iMedals::single()->getMedalsString($memberInfo['medals']);
                }
                $book = getBookInfo($post['bookid']);
                $post['message'] = iubb(iSubstr($post['message'], 0, 60));
                $post['name'] = $book['bookname'];
            }
        }
        $this->getController()->render('new', array('posts'=>$posts, 'pageObj'=>$pageObj));
    }
}
