function OnPostback()
 {
     if ($this->editor->GetDataObjectId()) {
         # Because this is a new request we need to reverify that this is the match owner before letting anything happen.
         # Can't trust that info from a postback so MUST go to the database to check it.
         $this->match_manager->ReadByMatchId(array($this->editor->GetDataObjectId()));
         $check_match = $this->match_manager->GetFirst();
         $this->b_user_is_match_owner = ($check_match instanceof Match and AuthenticationManager::GetUser()->GetId() == $check_match->GetAddedBy()->GetId());
         # This page is only for tournaments, so check that against the db too
         $this->b_is_tournament = $check_match->GetMatchType() == MatchType::TOURNAMENT;
     } else {
         # Not an entirely correct error for there being no match id on postback,
         # but no match = not the owner, and most importantly it will prevent the
         # match being saved or the edit form being shown.
         $this->b_user_is_match_owner = false;
     }
     # get object
     $this->tournament = $this->editor->GetDataObject();
     # Check whether cancel was clicked
     if ($this->editor->CancelClicked()) {
         $this->match_manager->ExpandMatchUrl($this->tournament);
         if ($this->adding) {
             http_response_code(303);
             $this->Redirect($this->tournament->GetDeleteNavigateUrl());
         } else {
             # Show the tournament
             $this->Redirect($this->tournament->GetNavigateUrl());
         }
     }
     # save data if valid
     if ($this->IsValid()) {
         # Confirm match is being saved as a tournament
         $this->b_is_tournament = $this->b_is_tournament and $this->tournament->GetMatchType() == MatchType::TOURNAMENT;
         # Check that the requester has permission to update this match
         if (($this->b_user_is_match_admin or $this->b_user_is_match_owner) and $this->b_is_tournament) {
             # Save the teams in the tournament
             $this->match_manager->SaveTeams($this->tournament);
             $this->match_manager->NotifyMatchModerator($this->tournament->GetId());
             $this->match_manager->ExpandMatchUrl($this->tournament);
             http_response_code(303);
             if ($this->adding) {
                 $this->Redirect($this->tournament->AddTournamentCompetitionsUrl());
             } else {
                 $this->Redirect($this->tournament->GetNavigateUrl());
             }
         }
     }
 }
 function OnLoadPageData()
 {
     /* @var $o_last_match Match */
     /* @var $season Season */
     /* @var $team Team */
     # First best guess at where user came from is the page field posted back,
     # second best is tournaments page. Either can be tampered with, so there will be
     # a check later before they're used for redirection. If there's a context
     # season or team its URL will be read from the db and overwrite this later.
     if (isset($_POST['page'])) {
         $this->destination_url_if_cancelled = $_POST['page'];
     } else {
         $this->destination_url_if_cancelled = "/tournaments";
     }
     # new data manager
     $match_manager = new MatchManager($this->GetSettings(), $this->GetDataConnection());
     $match_manager->FilterByMatchType(array(MatchType::TOURNAMENT));
     # Check whether cancel was clicked
     if ($this->IsPostback() and $this->editor->CancelClicked()) {
         # new tournament, nothing saved yet, so just send user back where they came from
         $this->Cancel();
     }
     # Save match
     if ($this->IsPostback() and $this->IsValid()) {
         # Get posted match
         $this->tournament = $this->editor->GetDataObject();
         # Save match
         $match_manager->SaveFixture($this->tournament);
         if (count($this->tournament->GetAwayTeams())) {
             $match_manager->SaveTeams($this->tournament);
         }
         if ($this->season instanceof Season) {
             $this->tournament->Seasons()->Add($this->season);
             $match_manager->SaveSeasons($this->tournament, true);
         }
         http_response_code(303);
         $this->Redirect($this->tournament->AddTournamentTeamsUrl());
     }
     if (isset($this->season)) {
         $season_manager = new SeasonManager($this->GetSettings(), $this->GetDataConnection());
         $season_manager->ReadById(array($this->season->GetId()));
         $this->season = $season_manager->GetFirst();
         $this->editor->SetContextSeason($this->season);
         $this->destination_url_if_cancelled = $this->season->GetNavigateUrl();
         unset($season_manager);
         # If we're adding a match to a season, get last game in season for its date
         $match_manager->ReadLastInSeason($this->season->GetId());
     }
     if (isset($this->team)) {
         # Get more information about the team itself
         require_once 'stoolball/team-manager.class.php';
         $team_manager = new TeamManager($this->GetSettings(), $this->GetDataConnection());
         $team_manager->ReadById(array($this->team->GetId()));
         $this->team = $team_manager->GetFirst();
         $this->editor->SetContextTeam($this->team);
         $this->destination_url_if_cancelled = $this->team->GetNavigateUrl();
         # Get the last game already scheduled for the team to use its date
         $match_manager->ReadLastForTeam($this->team->GetId());
         # Read teams played in the last year in order to get their grounds, which will be put at the top of the select list
         $team_manager->ReadRecentOpponents(array($this->team->GetId()), 12);
         $this->editor->ProbableTeams()->SetItems($team_manager->GetItems());
         unset($team_manager);
     }
     # Use the date of the most recent tournament if it was this year, otherwise just use the default of today
     $o_last_match = $match_manager->GetFirst();
     if (is_object($o_last_match) and gmdate('Y', $o_last_match->GetStartTime()) == gmdate('Y')) {
         if ($o_last_match->GetIsStartTimeKnown()) {
             # If the last match has a time, use it
             $this->editor->SetDefaultTime($o_last_match->GetStartTime());
         } else {
             # If the last match has no time, use 11am BST
             $this->editor->SetDefaultTime(gmmktime(10, 0, 0, gmdate('m', $o_last_match->GetStartTime()), gmdate('d', $o_last_match->GetStartTime()), gmdate('Y', $o_last_match->GetStartTime())));
         }
     }
     unset($match_manager);
     # Get grounds
     $o_ground_manager = new GroundManager($this->GetSettings(), $this->GetDataConnection());
     $o_ground_manager->ReadAll();
     $a_grounds = $o_ground_manager->GetItems();
     $this->editor->Grounds()->SetItems($a_grounds);
     unset($o_ground_manager);
 }