<?php
class EditNameAction extends BaseAction
{
    public function run()
    {
        if (Yii::app()->request->isAjaxRequest)
        {
            $fid = intval(Yii::app()->request->getQuery('fid'));
            $id = intval(Yii::app()->request->getQuery('id'));
            $name = trim(Yii::app()->request->getPost('name'));
            
            if (empty($name))
            {
                echo '分类名不能为空';
                Yii::app()->end();
            }
            $type = TopicTypes::model()->findByPk($id);
            if (empty($type))
            {
                echo '不存在的分类';
                Yii::app()->end();
            }
            if ($type->fid != $fid)
            {
                echo '此分类不属于本版块，不能修改';
                Yii::app()->end();
            }
            if ($type->name == $name)
            {
                echo $name.'已经存在，请换一个';
                Yii::app()->end();
            }
            
            $type->name = $name;
            $type->save();
            Yii::app()->end();
        }
    }
}