<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class CommentDeleteAction extends MemberAction
{
    public function run()
    {
        $id = S::gp('id','G',2);
        $f = S::gp('f','G',2);
        $comment = $this->db()->select()->from(DiaryComment::table())->where('id='.S::sqlEscape($id))->find(true);
        if (empty($comment))
        {
            throw new iSystemException('不存在的评论');
        }
        $diary = $this->db()->select()->from(Diary::table())->where('id='.S::sqlEscape($comment['did']))->find(true);
        if (!isGM() && $diary['uid'] != $this->uid())
        {
            throw new iSystemException('非法操作');
        }
        
        $this->db()->delete(DiaryComment::table())->where('id='.S::sqlEscape($id))->q();
        $this->db()->update(Diary::table())->decrField(array('comments'=>1))->where('id='.S::sqlEscape($diary['id']))->q();
        $f ? $merge = array('a'=>'read') : $merge = array('a'=>'comments');
        $forward = url('system',array_merge(array('s'=>'member','c'=>'diary','id'=>$diary['id']),$merge));
        iforward($forward);
    }
}