<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class SelectAction extends MemberAction
{
    public function run()
    {
        $myBookShelfs = BookShelf::single()->getAll($this->uid());
        $bookid = S::gp('bid', 'G', 2);
        $bookinfo = getBookInfo($bookid);
        
        $check = $this->db()->getValue('SELECT bid FROM `'.Favorites::table().'` WHERE memberid = '.$this->uid().' AND bid = '.$bookid);
        if (empty($check))
        {
            throw new iSystemException('此书不在你的阅读记录里，不能分组');
        }
        
        if (isset($_POST['bookshelf']))
        {
            $bookshelf = S::gp('bookshelf', 'P', 2);
            if (!array_key_exists($bookshelf, $myBookShelfs))
            {
                throw new iSystemException('不存在的书架');
            }
    
            $this->db()->query('REPLACE INTO '.BookShelf::table()." SET bid=$bookid,uid=".$this->uid().",sid=$bookshelf");
            $message[] = '保存成功';
            $message[] = '返回'.ilink('进度', url('system',array('s'=>'member','c'=>'my','a'=>'progress'))).'.'.memberLink().'.'.indexLink();
            
            $this->showMessage($message);
        }
        
        $this->getController()->render('select',array(
                'bookinfo' => $bookinfo,
                'bookid' => $bookid,
                'myBookShelfs' => $myBookShelfs,
            )
        );
    }
}