Exemple #1
0
 /**
  * 
  * 活动报名
  * @param $id
  */
 public function user_apply($id = NULL)
 {
     if ($this->get_method() != 'POST') {
         $this->send_response(405, NULL, '请求的方法不存在');
     }
     if (empty($id)) {
         $this->send_response(400, NULL, '400501:活动ID为空');
     }
     $data = $this->get_data();
     if (!$data) {
         $this->send_response(400, NULL, '400412:活动信息非法');
     }
     $event_info = $this->model->get($id);
     if (!$event_info) {
         $this->send_response(400, NULL, '400506:活动不存在');
     }
     if ($post['apply_type'] == Kohana::config('event.apply_type.joined') && empty($data['user'])) {
         $this->send_response(400, NULL, '400508:活动报名信息为空');
     }
     if ($event_info['organizer'] == $this->user_id && $post['apply_type'] == Kohana::config('event.apply_type.refused')) {
         $this->send_response(400, NULL, '400507:活动创建者不能不参加活动');
     }
     $update_apply_type = false;
     $post = new Validation($data);
     $post->add_rules('apply_type', 'required', 'numeric', array($this, '_check_type_validation'));
     if ($post->validate()) {
         $form = $post->as_array();
         $user_info = sns::getuser($this->user_id);
         $apply_info = $this->model->getApplyInfo(array('eid' => $id, 'pid' => 0, 'uid' => $this->user_id));
         if ($apply_info) {
             $update_apply_type = true;
         } else {
             if (time() > $event_info['deadline']) {
                 $this->send_response(400, NULL, '400509:活动报名已截止');
             }
         }
         $eventUser = array('eid' => $id, 'pid' => 0, 'uid' => $this->user_id, 'name' => $user_info['realname'], 'mobile' => $user_info['mobile'], 'apply_type' => $post['apply_type'], 'apply_time' => time(), 'grade' => Kohana::config('event.grade.normal'));
         if (!empty($post['apply_doc'])) {
             if (!$this->_check_user_apply_doc_valid($id, $post['apply_doc'])) {
                 $this->send_response(400, NULL, '400531:报名信息非法');
             }
             $this->model->addUserApplyDoc($id, $this->user_id, 0, $user_info['realname'], $post['apply_doc']);
         }
         $this->model->applyEvent($eventUser, $update_apply_type);
         //家属报名
         $this->_dependent_apply_event($post['dependent'], $id, $this->user_id, $post['apply_type']);
         if ($post['apply_type'] == Kohana::config('event.apply_type.joined') || $post['apply_type'] == Kohana::config('event.apply_type.interested')) {
             if ($post['apply_type'] == Kohana::config('event.apply_type.joined')) {
                 $content = $user_info['realname'] . '参加了活动"' . $event_info['title'] . '"';
             } else {
                 $content = $user_info['realname'] . '对活动"' . $event_info['title'] . '"表示感兴趣';
             }
             $opt = array('event' => array('id' => $id, 'name' => $event_info['title'], 'cover' => ''), 'no_sign' => 1);
             $this->send_event_mq(Kohana::config('uap.xiaomo'), $event_info['organizer'], $content, $opt);
             $this->_add_group_user($event_info['gid'], $this->user_id, Kohana::config('group.grade.normal'));
         } elseif ($post['apply_type'] == Kohana::config('event.apply_type.refused')) {
             $this->_del_group_user($event_info['gid'], $this->user_id);
         }
         $this->send_response(200);
     }
     $errors = $post->errors();
     foreach ($errors as $key => $value) {
         switch ($key) {
             case 'apply_type':
                 $this->send_response(400, NULL, '400405:活动类型为空');
                 break;
             case 'user_name_empty':
                 $this->send_response(400, NULL, '400502:名字为空');
                 break;
             case 'user_mobile_empty':
                 $this->send_response(400, NULL, '400503:手机号为空');
                 break;
             case 'user_mobile_format':
                 $this->send_response(400, NULL, '400504:手机号格式不正确');
                 break;
         }
     }
 }