<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class FavoriteAction extends MemberAction
{
    public function run()
    {
        $bookid = intval(S::gp('bid','G'));
        $book = fetchBookInfo($bookid);
        empty($book) && $this->showMessage('不存在的小说');
        
        $check = $this->db()->getValue("SELECT COUNT(*) FROM `".Favorites::table()."` WHERE `memberid` = ".$this->uid()." AND `bid` = '$bookid'");            
        if (!$check)
        {
            $chapters = Chapter::single()->getChapterList($bookid);
            $this->db()->insert(Favorites::table())->value(array('createdate'=>CDATETIME,'memberid'=>$this->uid(),'bid'=>$bookid,'cid'=>$chapters[0]['chapterid']))->q();
            Favorites::single()->updateBnum($this->uid());
            Book::single()->updateFavorites($bookid);
            $message[] = "恭喜您,收藏成功,登录以后可在会员中心看到,以后此书有更新我们将第一时间通知您";
            
            $feed = '{{'.$this->uid().'}}收藏了[url='.staticUrl('bookshow',array('bid'=>$bookid)).']《'.$book['bookname'].'》[/url]';
            iFeed::single()->create($feed, $this->uid(), 'book');
        }
        else
        {
            $message[] = "您已经收藏过本书了";
        }
        $message[] = '返回'.ilink('书页',url('bookshow',array('bid'=>$bookid))).'.'.memberLink().'.'.indexLink();
        $this->showMessage($message);
    }
}