<?php
/**
 * 新建用户组
 * @author yzxh24
 */
class CreateAction extends BaseAction
{
    public function run()
    {
        if (Yii::app()->request->isPostRequest)
        {
            $newGroups = $this->getController()->arrayFlipKeys($_POST['add']);
            $oldGroups = $_POST['group'];
            foreach($newGroups as $k => $v)
            {
				if(!$v['title'] || !$v['title1'] || !$v['title2'])
				{
					unset($newGroups[$k]);
				}
            }
            
            foreach ($oldGroups as $key => $value)
            {
                $group = UserGroup::model()->findByPk($key);
                if (!empty($group))
                {
                    $group->grouptitle = $value['title'];
                    $group->grouptitle1 = $value['title1'];
                    $group->grouptitle2 = $value['title2'];
                    $group->save();
                }
            }
            
            foreach ($newGroups as $group)
            {
                $item = new UserGroup();
                $item->grouptitle = $group['title'];
                $item->grouptitle1 = $group['title1'];
                $item->grouptitle2 = $group['title2'];
                $item->groupimg = 5;
                $item->gptype = Yii::app()->request->getQuery('type');
                $item->isdefault = 0;
                $item->save();
            }
            
            $this->getController()->redirect(array('index'));
        }
        else
        {
            throw new CHttpException(500,'非法操作');
        }
    }
}