<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class BookShelfAction extends MemberAction
{
    public function run()
    {
        $mTable = Members::table();
        $mfTable = Memberfields::table();
        $bTable = Book::table();
        $fTable = Favorites::table();
        
        $uid = intval(S::gp('uid','G'));
        empty($uid) && $this->showMessage('非法操作，请返回');
        
        $sql = 'SELECT m.uid,m.username,mf.bookshelf FROM '.$mTable.' AS m INNER JOIN '.$mfTable.' AS mf ON mf.uid=m.uid WHERE m.uid='.$uid;
        $member = $this->db()->setQueryString($sql)->find(true);
        empty($member) && $this->showMessage('不存在的会员');
        
        if (!$member['bookshelf'])
        {
            throw new iSystemException('对方没有公开书架');
        }
        
        $count = $this->db()->getValue("SELECT COUNT(*) FROM {$fTable} WHERE memberid = {$uid}");
        $pageObj = new iPage();
        $pageObj->setPageSize(10);
        $pageObj->setTotalRecord($count);
        
        $sql = "SELECT T.`typename`, B.`bookid` AS bookid, B.`typeid`, B.`bookname`, B.`lastchapterid`, B.`lastchaptername`, B.`updatedate`, B.`author`, B.`status` 
        		FROM `{$fTable}` AS F
			    INNER JOIN `{$bTable}` AS B ON B.`bookid`=F.`bid`
			    INNER JOIN ".Types::table()." AS T ON T.`typeid`=B.`typeid`
			    WHERE F.`memberid` = '$uid'
			    ORDER BY F.favoritesid DESC LIMIT {$pageObj->getOffset()},{$pageObj->getPageSize()}";
        $bookshelfs = $this->db()->setQueryString($sql)->find();
        
        $this->getController()->render('bookshelf',array(
                'bookshelfs' => $bookshelfs,
                'member' => $member,
                'pageObj' => $pageObj,
            )
        );
    }
}