<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class CancelAction extends ForumAction
{
    public function run()
    {
        if (!IS_SUPER)
        {
            $this->NoPermission();
        }

        $cid = S::gp('cid','G',2);
        $row = BookPost::single()->getOne($cid, $this->id());
    	if (empty($row))
    	{
    		throw new iSystemException('不存在的评论');
    	}
    	if (!$row['digest'])
    	{
    		throw new iSystemException('不是精华评论，不能取消');
    	}

	    $this->db()->update(BookPost::table($this->id()))->value(array('digest'=>0))->where('bookpostsid='.$cid)->q();
	    $this->db()->update(BookForum::table())->decrField(array('income'=>20))->where(array('bookid'=>$this->id()))->q();

        $this->db()->delete(BookPostDigest::table())->where(array('postid'=>$cid))->q();
        updateDigestComment($this->id(), 3);


        $bookInfo = $this->bookInfo();
        $credit = 2 == $bookInfo['type'] ? 100 : setting('comment_digest') * 2;
        iCredits::single($row['uid'], userName($row['uid']))->consume($credit);

        $logData = array(
            'uid' => $row['uid'],
            'ouid' => $this->uid(),
            'bookid' => $this->id(),
            'type' => 'CancelDigest',
            'message' => '书友[uid='.$row['uid'].']的精评被[uid='.$this->uid().']取消',
        );
        BookManageLog::single()->create($logData);

	    $message[] = '精华评论取消成功';
        $message[] = '返回' . ilink('管理', $this->getController()->url('system', array('s'=>'forum','c'=>'manage','a'=>'lite','id'=>$this->id()))) . '.' . ilink('书会', $this->getController()->url('system',array('s'=>'forum','id'=>$this->id()))) . '.' . ilink('书页', url('bookshow', array('id'=>$this->id())));
        $this->showMessage($message);
    }
}