Exemplo n.º 1
0
 /**
  * 创建群
  */
 public function create()
 {
     if ($this->get_method() != 'POST') {
         $this->send_response(405, NULL, '请求的方法不存在');
     }
     $data = $this->get_data();
     $post = new Validation($data);
     $post->add_rules('name', 'required', 'length[1, 70]');
     $post->add_rules('introduction', 'length[0, 255]');
     $post->add_rules('notice', 'length[0, 255]');
     if ($post->validate()) {
         $groupInfo = $post->as_array();
         $groupInfo['gname'] = $groupInfo['name'];
         if (!isset($groupInfo['introduction'])) {
             unset($groupInfo['introduction']);
         }
         if (!isset($groupInfo['notice'])) {
             unset($groupInfo['notice']);
         }
         unset($groupInfo['name']);
         $groupInfo['type'] = isset($groupInfo['type']) ? intval($groupInfo['type']) : 1;
         $groupInfo['privacy'] = isset($groupInfo['privacy']) ? intval($groupInfo['privacy']) : 1;
         $groupNum = $this->model->getCreateGroupNum($this->user_id);
         $userModel = User_Model::instance();
         //$result = $userModel->get_user_info($this->user_id);
         if ($groupInfo['privacy'] == Kohana::config('group.privacy.public')) {
             $groupLimit = Kohana::config('group.limit.public');
         } else {
             $groupLimit = Kohana::config('group.limit.private');
         }
         if ($groupNum >= $groupLimit) {
             $this->send_response(400, NULL, '400409:群可创建数已用完');
         }
         $nowTime = time();
         $groupInfo['create_time'] = $nowTime;
         $groupInfo['modify_time'] = $nowTime;
         $groupInfo['creator_id'] = $this->user_id;
         $groupInfo['master_id'] = $this->user_id;
         $groupInfo['member_number'] = 1;
         $group_id = $this->model->add($groupInfo);
         if (!$group_id) {
             $this->send_response(400, NULL, '创建群失败');
         }
         $result = $this->model->addGroupMember($group_id, $this->user_id, Kohana::config('group.grade.master'));
         if (!$result) {
             $this->send_response(400, NULL, '添加群成员失败');
         }
         Tab_Model::instance()->create($this->user_id, Kohana::config('group.type.group'), $group_id);
         $this->send_response(200, array('id' => floatval($group_id)));
     }
     $errors = $post->errors();
     foreach ($errors as $key => $value) {
         switch ($key) {
             case 'type':
                 $this->send_response(400, NULL, '400405:群类型非法');
                 break;
             case 'name':
                 if ('required' == $value) {
                     $this->send_response(400, NULL, '400404:群名称为空');
                 } elseif ('length' == $value) {
                     $this->send_response(400, NULL, '400406:群名称超出长度限制');
                 }
                 break;
             case 'introduction':
                 $this->send_response(400, NULL, '400407:群介绍超出长度限制');
                 break;
             case 'notice':
                 $this->send_response(400, NULL, '400408:群公告超出长度限制');
                 break;
             default:
                 $this->send_response(400, NULL, '400413:群数据非法');
                 break;
         }
     }
     $this->send_response(400, NULL, '400413:群数据非法');
 }