<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class FavoriteAction extends BcartAction
{
    public function run()
    {
        $this->getController()->isLogin();
        $bcart = $this->getCart();

        if ($this->uid() == $bcart['uid'])
        {
            throw new iSystemException('不能收藏自己的书单');
        }
        $permissions = Members::single()->getPermissions($this->uid());
        $fLimit = $permissions['bcartfavoritelimit'];

        $check = $this->db()->getValue('SELECT COUNT(*) FROM '.Ufavorites::table().' WHERE uid='.S::sqlEscape($this->uid()).' AND `type`='.S::sqlEscape('bcart').' AND id='.S::sqlEscape($bcart['id']));
        if ($check)
        {
            throw new iSystemException('您已经收藏过这个书单');
        }
        
        $count = $this->db()->getValue('SELECT COUNT(*) FROM '.Ufavorites::table().' WHERE uid='.S::sqlEscape($this->uid()).' AND `type`='.S::sqlEscape('bcart'));
        if ($fLimit <= $count)
        {
            throw new iSystemException('您当前等级只能收藏'.$fLimit.'个书单');
        }

        $this->db()->insert(Ufavorites::table())->value(array('id'=>$bcart['id'],'type'=>'bcart','createdate'=>CTIME,'uid'=>$this->uid()))->q();
        $this->db()->update(Bcarts::table())->incrField(array('favorite'=>1))->where(array('id'=>$bcart['id']))->q();

        $feed = '{{'.$this->uid().'}}收藏了书单[url='.staticUrl('system',array('s'=>'bcart','a'=>'view','id'=>$bcart['id'])).']《'.$this->getController()->filter($bcart['subject']).'》[/url]';
        iFeed::single()->create($feed, $this->uid());
        
        $message[] = '书单收藏成功';
        $message[] = '返回'.ilink('书单', url('system', array('s'=>'bcart','a'=>'view','id'=>$bcart['id']))).'.'.ilink('书单市场', url('system', array('s'=>'bcart'))).'.'.memberLink();
        $this->showMessage($message);
    }
}
