<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class DeleteAction extends MemberAction
{
    public function run()
    {
        $id = S::gp('id','G',2);
        $diary = $this->db()->select()->from(Diary::table())->where('id='.S::sqlEscape($id))->find(true);
        if (empty($diary))
        {
            throw new iSystemException('不存在的日志');
        }
        if (!isGM() && $diary['uid'] != $this->uid())
        {
            throw new iSystemException('非法操作');
        }
        
        if (!$_GET['confirm'])
        {
            $message[] = '确定要删除吗？';
            $message[] = ilink('确定',url('system',array('s'=>'member','c'=>'diary','a'=>'delete','id'=>$id,'confirm'=>1))).'.'.ilink('取消',url('system',array('s'=>'member','c'=>'diary','a'=>'read','id'=>$id)));
            $this->showMessage($message);
        }
        
        $this->db()->delete(Diary::table())->where('id='.S::sqlEscape($id))->q();
        $this->db()->delete(DiaryData::table())->where('did='.S::sqlEscape($id))->q();
        $this->db()->update(Memberfields::table())->decrField(array('diaries'=>1))->where('uid='.S::sqlEscape($diary['uid']))->q();
        if ($diary['tid'])
        {
            $count = $this->db()->getValue('SELECT COUNT(*) FROM '.Diary::table().' WHERE uid='.S::sqlEscape($diary['uid']).' AND tid='.S::sqlEscape($diary['tid']));
            $this->db()->update(DiaryType::table())->value(array('num'=>$count))->where('id='.S::sqlEscape($diary['tid']));
        }
        
        iforward(url('system',array('s'=>'member','c'=>'diary')));
    }
}