Example #1
0
 /**
  * 报名活动
  */
 public function create($id = 0)
 {
     $data = $this->get_data();
     $aid = (int) $id;
     $apply_type = (int) $data['type'];
     $webRequest = (int) $data['web'];
     if ($webRequest != 2) {
         $webRequest = 0;
     }
     if ($this->get_method() != 'POST') {
         $this->send_response(405, NULL, '请求的方法不存在');
     }
     if ($id == 0) {
         $this->send_response(400, NULL, '400501:活动id为空');
     }
     if ($apply_type < Kohana::config('activity.apply_type.join') || $apply_type > Kohana::config('activity.apply_type.interest')) {
         $this->send_response(400, NULL, '400508:活动报名类型非法');
     }
     $activity = $this->model->getActivityInfo($aid);
     if (!$activity) {
         $this->send_response(400, NULL, '400502:活动不存在');
     }
     if ($activity['end_time'] < time()) {
         $this->send_response(400, NULL, '活动已结束');
     }
     $permit = $this->_check_activity_view_permission($activity, $this->user_id);
     if (!$permit) {
         $this->send_response(400, NULL, '400510:无权限报名活动');
     }
     if ($webRequest > 0) {
         $types = array_flip(Kohana::config('activity.type'));
         $typeId = $types[$activity['type']];
         $typeNames = Kohana::config('activity.typeName');
         $type = $typeNames[$typeId];
         $view = new View('activity/details');
         $view->webRequest = $webRequest;
         $view->type = $type;
         $user = array();
         $user['id'] = floatval($activity['creator_id']);
         $userInfo = sns::getuser($activity['creator_id']);
         $user['name'] = $userInfo['realname'];
         $user['mobile'] = $userInfo['mobile'];
         $activity['user'] = $user;
         unset($user);
         $organizer = array();
         $organizerList = $this->model->getActivityOrganizer($aid);
         $organizerIdList = array();
         foreach ($organizerList as $value) {
             $user = array();
             $user['id'] = floatval($value['uid']);
             $userInfo = sns::getuser($value['uid']);
             $user['name'] = $userInfo['realname'];
             $user['avatar'] = sns::getavatar($value['uid']);
             $user['mobile'] = $userInfo['mobile'];
             $organizer[] = $user;
             $organizerIdList[] = $user['id'];
             unset($userInfo);
             unset($user);
         }
         $activity['organizer'] = $organizer;
         $view->activity = $activity;
         $view->apply_type = $apply_type;
     }
     $userModel = new User_Model();
     $nowTime = time();
     $feedModel = new Feed_Model();
     //获取用户是否已经参与了报名
     $applyResult = $this->model->getActivityApplyType($aid, $this->user_id);
     if ($applyResult) {
         $tab = $userModel->getTag($this->user_id, 15, $aid);
         if ($applyResult == $apply_type) {
             if ($webRequest > 0) {
                 $view->render(true);
                 exit;
             }
             $this->send_response(200);
         } else {
             if ($applyResult == Kohana::config('activity.apply_type.not_join') && !$tab) {
                 $userModel->insertTag($this->user_id, 15, $aid);
             } else {
                 if ($apply_type == Kohana::config('activity.apply_type.not_join')) {
                     //$userModel->deleteTag($this->user_id, 15, $aid);
                 }
             }
         }
         $activityMember = array('apply_type' => $apply_type, 'apply_time' => $nowTime);
         $grade = $this->model->getMemberGrade($aid, $this->user_id);
         if ($grade == Kohana::config('activity.grade.manager') && $apply_type != Kohana::config('activity.apply_type.join')) {
             $activityMember['grade'] = Kohana::config('activity.group.normal');
         }
         $result = $this->model->updateApplyActivity($aid, $this->user_id, $activityMember);
         $this->_add_feed_comment($activity, $applyResult, $apply_type, $this->user_id);
         $feedModel->addTab($aid, $activity['title'], 15, $this->user_id);
         if ($webRequest > 0) {
             $view->render(true);
             exit;
         }
         $this->send_response(200);
     }
     $activityMember['aid'] = $aid;
     $activityMember['uid'] = $this->user_id;
     $activityMember['apply_type'] = $apply_type;
     $activityMember['apply_time'] = $nowTime;
     $activityMember['grade'] = Kohana::config('activity.grade.normal');
     $this->model->applyActivity($activityMember);
     $userModel->insertTag($this->user_id, 15, $aid);
     $messageModel = new Message_Model();
     $appid = Kohana::config('uap.app.action');
     $tplid = Kohana::config('noticetpl.actionInvite.id');
     while (true) {
         $inviteMsg = $messageModel->getNoticeInfo(array('uid' => $this->user_id, 'appid' => $appid, 'tplid' => $tplid, 'objid' => $aid, 'status' => 0));
         if ($inviteMsg) {
             //修改系统消息模板
             $invite_uid = $inviteMsg['authorid'];
             $nid = $inviteMsg['id'];
             $messageModel->putChangeTplByid($this->user_id, $nid, $apply_type);
             $this->model->setInviteStatus($aid, $invite_uid, $this->user_id);
         } else {
             break;
         }
     }
     if ($apply_type == Kohana::config('activity.apply_type.not_join')) {
         if ($webRequest > 0) {
             $view->render(true);
             exit;
         }
         $this->send_response(200);
     }
     $this->_add_feed_comment($activity, 0, $apply_type, $this->user_id);
     $feedModel->addTab($aid, $activity['title'], 15, $this->user_id);
     if ($webRequest > 0) {
         $view->render(true);
         exit;
     }
     $this->send_response(200);
 }