<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class TypeDeleteAction extends MemberAction
{
    public function run()
    {
        $tid = S::gp('tid','G',2);
        $type = $this->db()->select()->from(DiaryType::table())->where('id='.$tid)->find(true);
        if (empty($type))
        {
            throw new iSystemException('不存在的分类');
        }
        if ($this->uid() != $type['uid'])
        {
            throw new iSystemException('此分类不属于您');
        }
        
        if (!$_GET['confirm'])
        {
            $message[] = '您确定要删除该分类吗？删除后该分类下的文章将不受影响';
            $message[] = ilink('确定',url('system',array('s'=>'member','c'=>'diary','a'=>'td','tid'=>$tid,'confirm'=>1))).'|'.ilink('取消',url('system',array('s'=>'member','c'=>'diary','a'=>'type')));
            $this->showMessage($message);
        }
        $this->db()->delete(DiaryType::table())->where('id='.S::sqlEscape($tid))->q();
        // 重置掉此分类日志的分类ID
        $this->db()->update(Diary::table())->value(array('tid'=>0))->where('uid='.S::sqlEscape($this->uid()).' AND tid='.S::sqlEscape($tid))->q();
        
        iforward(url('system',array('s'=>'member','c'=>'diary','a'=>'type')));
    }
}