Beispiel #1
0
 public function save_game()
 {
     import('core.validation');
     import('trouble.game');
     $validator = \Core\Validator::validator('\\Trouble\\Game');
     $editing = $_POST['id'] > 0 ? True : False;
     try {
         if ($editing) {
             $this->_auth->check_admin('game', $_POST['id']);
             $game = \Trouble\Game::container()->get_by_id($_POST['id']);
             $validator->set_id($game->id);
             $game->overwrite($_POST, True);
         } else {
             $game = \Trouble\Game::mapper()->create_object($_POST);
             $game->creator = $this->_auth->user_id();
         }
         try {
             \Core\Auth::hash($game, 'password');
         } catch (\Core\AuthEmptyPasswordError $e) {
             $game->remove('password');
         }
         $validator->validate($_POST);
         \Core\Storage::container()->get_storage('Game')->save($game);
         if (!$editing) {
             $this->_auth->add_admin('game', $game->id, $game->creator);
         }
         if ($editing) {
             echo $this->_return_message("Success", "Saved.");
         } else {
             echo $this->_return_message("Success", "Created game.");
         }
     } catch (\Core\ValidationError $e) {
         echo $this->_return_message("Fail", "Validation error(s):", $e->get_errors());
     } catch (\Core\AuthNotLoggedInError $e) {
         $this->_not_logged_in();
     } catch (\Core\AuthDeniedError $e) {
         $this->_access_denied();
     } catch (\Exception $e) {
         $this->_unhandled_exception($e);
     }
 }