<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class ScoreAction extends BcartAction
{
    public function run()
    {
        $scoreArray = array(1, 2, 3, 4, 5);
        $this->getController()->isLogin();
        $bcart = $this->getCart();
        $t = S::gp('t', 'G', 2);
        if (!in_array($t, $scoreArray))
        {
            throw new iSystemException('不存在的评分选项');
        }

        $check = $this->db()->getValue('SELECT COUNT(*) FROM '.BcartScore::table().' WHERE uid='.S::sqlEscape($this->uid()).' AND bcid='.S::sqlEscape($bcart['id']));
        if ($check)
        {
            throw new iSystemException('您已经给本书单评过分,每个书单只能评一次!');

        }
        $this->db()->update(Bcarts::table())->incrField(array('scores'=>1, 'score'=>$t))->where(array('id'=>$bcart['id']))->q();
        $this->db()->insert(BcartScore::table())->value(array('uid'=>$this->uid(),'bcid'=>$bcart['id'],'ip'=>ONLINE_IP,'createdate'=>CDATETIME,'score'=>$t,'valid'=>u('verify') ? 1 : 0))->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')));
        $this->showMessage($message);
    }
}