protected function _edit() { $t = $this->_template; $t->add('_jsapps', 'game_form'); $storage = \Core\Storage::container()->get_storage('Game'); $mapper = \Trouble\Game::mapper()->attach_storage($storage); try { if ($this->_args['game_id']) { $t->title = "Edit Game"; $game = $this->_game; $this->_auth->check_admin('game', $game->id); $game->form_start_date = $game->start_date->format('Y-m-d'); $game->form_end_date = $game->end_date->format('Y-m-d'); $t->administration = $this->_administration($game); $t->vouchers = $this->_vouchers($game); } else { $t->title = "Game Creation"; $t->new = True; $game = \Trouble\Game::create($_POST, True); } $t->game = $game; return $t->render('forms/game.php'); } catch (\Core\AuthDeniedError $e) { throw new \Core\HTTPError(401, "Editing Game"); } catch (\Core\AuthNotLoggedInError $e) { throw new \Core\HTTPError(401, "Editing Game"); } }
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); } }