<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class IndexAction extends MemberAction
{
    public function run()
    {
        $bsTable = BookShelf::table();
        $bTable = Book::table();
        $fTable = Favorites::table();
        $cTable = Chapter::table();
        
        $id = S::gp('id', 'G', 2);
        $myBookShelfs = $this->getBookShelfs($id);
        
        $count = $this->db()->getValue('SELECT COUNT(*) FROM '.$bsTable.' AS bs INNER JOIN '.$bTable.' AS b ON b.bookid = bs.bid WHERE uid='.$this->uid().' AND sid='.$id.' AND b.view = 1');
        $pageObj = new iPage();
        $pageObj->setPageSize(10);
        $pageObj->setTotalRecord($count);
        
        $sql = 'SELECT bookid AS bid,typeid,bookname,lastchapterid,lastchaptername,updatedate,author,status,f.cid,f.sp
                FROM '.$bTable.' AS b
                INNER JOIN '.$bsTable.' AS bs ON bs.bid = b.bookid
                INNER JOIN '.$fTable.' AS f ON f.bid = bs.bid AND f.memberid = bs.uid
                WHERE bs.uid = '.$this->uid()." AND bs.sid = $id ORDER BY updatedate DESC LIMIT {$pageObj->getOffset()},{$pageObj->getPageSize()}";
        $progresss = $this->db()->setQueryString($sql)->find();
        if (!empty($progresss))
        {
            foreach ($progresss as &$row)
            {
                $ar = '';
                $allowParam = array('tid','bid','cid','sp');
                foreach ($row as $key => $value)
                {
                    if (!is_null($value) && in_array($key, $allowParam))
                    {
                        $ar[$key] = $value;
                    }
                }
                $noRead = Chapter::single()->getNotReads($row['bid'], $row['cid']);
                $row['c'] = false === $noRead ? 0 : $noRead;
                $progressurl = url('read', $ar);
                $row['progressurl'] = $progressurl;
                $cids[] = $row['cid'];
            }
    
            $sql = "SELECT chapterid,chaptername FROM {$cTable} WHERE chapterid IN (".implode(',', $cids).')';
            $chapters = $this->db()->setQueryString($sql)->find();
            /**/
            foreach ($progresss as &$v)
            {
                foreach ($chapters as $key => $chapter)
                {
                    if ($v['cid'] == $chapter['chapterid'])
                    {
                        $v['progresschapter'] = $chapter['chaptername'];
                        unset($chapters[$key]);
                        break;
                    }
                }
            }
        }
    
        $current = $myBookShelfs[$id];
        $floor = $pageObj->getFloor();
        
        $this->getController()->render('index',array(
                'id' => $id,
                'progresss' => $progresss,
                'pageObj' => $pageObj,
                'floor' => $floor,
                'current' => $current,
                'myBookShelfs' => $myBookShelfs,
            )
        );
    }
}