<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class TypeAddAction extends MemberAction
{
    public function run()
    {
        if (isset($_POST['name']))
        {
            $name = S::gp('name','P');
            $privacy = S::gp('privacy','P',2);
            
            // 检查分类是否已经存在
            $check = $this->db()->getValue('SELECT COUNT(*) FROM '.DiaryType::table().' WHERE uid='.S::sqlEscape($this->uid()).' AND name='.S::sqlEscape($name));
            if ($check)
            {
                throw new iSystemException('分类'.$name.'已经存在');
            }
            
            if (empty($name) || 2 > mb_strlen($name, 'utf-8') || mb_strlen($name, 'utf-8') > 6)
            {
                throw new iSystemException('分类名长度必须大于等于2或者小于等于6个字');
            }
            $this->db()->insert(DiaryType::table())->value(array('uid'=>$this->uid(),'name'=>$name,'privacy'=>$privacy))->q();

            iforward(url('system',array('s'=>'member','c'=>'diary','a'=>'type')));
        }
        $this->getController()->render('typeadd');
    }
}