<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class NextAction extends MemberAction
{
    public function run()
    {
        $bookId = S::gp('bid','G',2);
        $chapterId = S::gp('cid','G',2);
        
        $sql = "SELECT `bid`, `cid`, `sp` FROM `".Favorites::table()."`
        	 	WHERE `memberid` = '{$this->uid()}'
        	 	AND `bid` = '$bookId'
        	 	AND (`cid` IS NOT NULL OR `sp` IS NOT NULL)";
        $progress = $this->db()->setQueryString($sql)->find(true);
        if (empty($progress))
        {
            throw new iSystemException('您还没有保存过本书的阅读记录');
        }
        $chapters = getChapters($bookId, $chapterId);
        if (empty($chapters['next']))
        {
            throw new iSystemException('该书暂时没有更新章节，请耐心等待更新');
        }
        
        iforward(url('read',array('bid'=>$bookId, 'cid'=>$chapters['next']['chapterid'])));
    }
}