<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class DeleteCommentAction extends BcartAction
{
    public function run()
    {
        $this->getController()->isLogin();
        $cid = S::gp('cid', 'G', 2);
        $comment = $this->db()->select(array('id', 'uid', 'bcid'))->from(BcartComment::table())->where(array('id'=>$cid))->find(true);
        if (empty($comment))
        {
            throw new iSystemException('不存在的评论');
        }

        $bcart = $this->db()->select(array('id', 'uid'))->from(Bcarts::table())->where(array('id'=>$comment['bcid']))->find(true);
        if (empty($bcart))
        {
            throw new iSystemException('不存在的书单');
        }
        if ($bcart['uid'] != $this->uid() && !isGM($this->uid()))
        {
            throw new iSystemException('权限不足,无法删除此评论');
        }

        $this->db()->delete(BcartComment::table())->where(array('id'=>$cid))->q();
        $this->db()->update(Bcarts::table())->decrField(array('comment'=>1))->where(array('id'=>$bcart['id']))->q();

        $direct = !empty($_GET['direct']) ? urldecode(S::gp('direct', 'G')) : url('system', array('s'=>'bcart', 'a'=>'view', 'id' => $bcart['id']));
        iforward($direct);
    }
}
