Ejemplo n.º 1
0
 public function sign()
 {
     $isAllowed = Util\Auth::isAuthorized($this->signedUser, 'member');
     $json = [];
     $json['result'] = \Rebond\Core\ResultType::ERROR;
     if (!$isAllowed) {
         $json['message'] = Util\Lang::lang('accessNonAuthorized');
         return json_encode($json);
     }
     $tournamentId = Util\Converter::toInt('tournamentId', 'post');
     $action = Util\Converter::toString('action', 'post');
     if ($tournamentId == 0 || !in_array($action, ['up', 'out'])) {
         $json['message'] = 'Invalid options';
         return json_encode($json);
     }
     $tournament = \Own\Bus\Tournament\Data::loadById($tournamentId);
     $playerId = $this->player->getId();
     if ($tournament->getClassification() == Classification::AMATEUR && $this->player->getTourPoint() >= 10) {
         $json['message'] = Util\Lang::lang('tooManyPointsForAmateur');
         return json_encode($json);
     }
     if ($tournament->getClassification() != Classification::AMATEUR && $this->player->getTourPoint() < 10) {
         $json['message'] = Util\Lang::lang('notEnoughPointForTournament');
         return json_encode($json);
     }
     if ($tournament->getClassification() <= Classification::ATP_250 && $this->player->getTourPoint() <= 100) {
         $json['message'] = Util\Lang::lang('needMorePointsForATP');
         return json_encode($json);
     }
     $options = [];
     $options['where'][] = 'tournament_player.tournament_id = ' . $tournamentId;
     $tp = \Own\Bus\TournamentPlayer\Data::loadAllByPlayerId($playerId, $options);
     if ($action == 'up') {
         if (count($tp) > 0) {
             $json['message'] = Util\Lang::lang('alreadySignedUp');
             return json_encode($json);
         }
         if ($this->player->getIsInTournament() || $this->player->getIsRegistered()) {
             $json['message'] = Util\Lang::lang('alreadySignedUpInOtherTournament');
             return json_encode($json);
         }
         $tp = new \Own\Bus\TournamentPlayer\Model();
         $tp->setTournamentId($tournamentId);
         $tp->setPlayerId($playerId);
         $tp->save();
         $this->player->setIsRegistered(true);
         $this->player->save();
         $json['result'] = \Rebond\Core\ResultType::SUCCESS;
         $json['message'] = Util\Lang::lang('signedUp') . '!';
         $json['newAction'] = 'out';
         $json['html'] = Util\Lang::lang('signOut');
         return json_encode($json);
     }
     if ($action == 'out') {
         if (count($tp) == 0) {
             $json['message'] = Util\Lang::lang('notRegistered');
             return json_encode($json);
         }
         \Own\Bus\TournamentPlayer\Data::deleteById($tp[0]->getId());
         $this->player->setIsRegistered(false);
         $this->player->save();
         $json['result'] = \Rebond\Core\ResultType::SUCCESS;
         $json['message'] = Util\Lang::lang('signedOut') . '!';
         $json['newAction'] = 'up';
         $json['html'] = Util\Lang::lang('signUp');
         return json_encode($json);
     }
     $json['message'] = 'nothing to do';
     return json_encode($json);
 }