public function OnPostback()
 {
     # If there's no id, ensure no match object is created. Page will then display a "match not found" message.
     # There's a separate page for adding matches, even for admins.
     if (!$this->editor->GetDataObjectId()) {
         return;
     }
     # Get the submitted match
     $this->match = $this->editor->GetDataObject();
     # Because this is a new request, we need to reverify whether 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());
     # Don't wan't to edit tournaments on this page, so even as admin make sure we're not trying to save one.
     # Can't change the match type, so find out what that is from the db too.
     $this->b_is_tournament = $check_match->GetMatchType() == MatchType::TOURNAMENT;
     # Check whether cancel was clicked
     if ($this->editor->CancelClicked()) {
         # If so, get the match's short URL and redirect
         $this->match_manager->ExpandMatchUrl($this->match);
         $this->Redirect($this->match->GetNavigateUrl());
     }
     # save data if valid
     if ($this->IsValid() and !$this->b_is_tournament) {
         # Save player of match
         $this->match_manager->SaveHighlights($this->match);
         $this->match_manager->ExpandMatchUrl($this->match);
         # Add comment if provided
         if (trim($this->match->GetNewComment())) {
             require_once 'forums/topic-manager.class.php';
             require_once 'forums/review-item.class.php';
             require_once 'forums/subscription-manager.class.php';
             $topic_manager = new TopicManager($this->GetSettings(), $this->GetDataConnection());
             $item_to_comment_on = new ReviewItem($this->GetSettings());
             $item_to_comment_on->SetType(ContentType::STOOLBALL_MATCH);
             $item_to_comment_on->SetId($this->match->GetId());
             $item_to_comment_on->SetNavigateUrl("https://" . $this->GetSettings()->GetDomain() . $this->match->GetNavigateUrl());
             $message = $topic_manager->SaveComment($item_to_comment_on, $this->match->GetNewComment());
             # send subscription emails
             $subs_manager = new SubscriptionManager($this->GetSettings(), $this->GetDataConnection());
             $subs_manager->SendCommentsSubscriptions($item_to_comment_on, $message);
             unset($subs_manager);
         }
         # Match may have been updated so send an email
         $this->match_manager->NotifyMatchModerator($this->match->GetId());
         # Show user the match, so they can see update was applied
         $this->Redirect($this->match->GetNavigateUrl());
     }
 }
 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 OnPostback()
 {
     # get object
     $this->tournament = $this->editor->GetDataObject();
     if ($this->editor->GetDataObjectId()) {
         # Because this is a new request we need to reverify security details 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();
         if ($check_match instanceof Match) {
             # This page is only for tournaments, so check that against the db
             $this->b_is_tournament = $check_match->GetMatchType() == MatchType::TOURNAMENT;
             /* The editor requires the teams in the tournament to populate the dropdown and to come up with a
              * match title, and it doesn't have them when recreating the tournament from the postback data. 
              * Since we have $check_match available from the database anyway, get the teams from there. */
             foreach ($check_match->GetAwayTeams() as $team) {
                 $this->tournament->AddAwayTeam($team);
             }
         }
     } else {
         # Error for there being no match id on postback,
         $this->b_is_tournament = false;
     }
     # Check whether cancel was clicked
     if ($this->editor->CancelClicked()) {
         $this->match_manager->ExpandMatchUrl($this->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_is_tournament) {
             # Save the matches in the tournament
             $this->match_manager->SaveMatchesInTournament($this->tournament);
             $this->match_manager->NotifyMatchModerator($this->tournament->GetId());
             $this->match_manager->ExpandMatchUrl($this->tournament);
             http_response_code(303);
             $this->Redirect($this->tournament->GetNavigateUrl());
         }
     }
 }
 public function OnPostback()
 {
     # If there's no id, ensure no match object is created. Page will then display a "match not found" message.
     # There's a separate page for adding matches, even for admins.
     if (!$this->editor->GetDataObjectId()) {
         return;
     }
     # Get the submitted match
     $this->match = $this->editor->GetDataObject();
     # Because this is a new request, we need to reverify whether 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());
     # Don't wan't to edit tournaments on this page, so even as admin make sure we're not trying to save one.
     # Can't change the match type, so find out what that is from the db too.
     $this->b_is_tournament = $check_match->GetMatchType() == MatchType::TOURNAMENT;
     # Check whether cancel was clicked
     if ($this->editor->CancelClicked()) {
         # Match may have been updated so send an email
         $this->match_manager->NotifyMatchModerator($this->match->GetId());
         # If so, get the match's short URL and redirect
         $this->match_manager->ExpandMatchUrl($this->match);
         $this->Redirect($this->match->GetNavigateUrl());
     }
     # save data if valid
     if ($this->IsValid() and !$this->b_is_tournament) {
         switch ($this->editor->GetCurrentPage()) {
             case ScorecardEditControl::FIRST_INNINGS:
                 $this->match_manager->SaveScorecard($this->match, true, $this->SearchIndexer());
                 $this->editor->SetCurrentPage(ScorecardEditControl::SECOND_INNINGS);
                 break;
             case ScorecardEditControl::SECOND_INNINGS:
                 $this->match_manager->SaveScorecard($this->match, false, $this->SearchIndexer());
                 $this->match_manager->ExpandMatchUrl($this->match);
                 http_response_code(303);
                 $this->Redirect($this->match->EditHighlightsUrl());
                 break;
         }
     }
 }
 public function OnPostback()
 {
     # If there's no id, ensure no match object is created. Page will then display a "match not found" message.
     # There's a separate page for adding matches, even for admins.
     if (!$this->editor->GetDataObjectId()) {
         return;
     }
     # Get the submitted match
     $this->match = $this->editor->GetDataObject();
     $check_match = $this->match;
     # Because this is a new request, if the user isn't admin we need to reverify whether 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.
     if (!$this->b_user_is_match_admin) {
         $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());
         if ($this->b_user_is_match_owner) {
             # Set the owner of the match. This means the edit control knows who the owner is and therefore
             # whether to display the fixture editor on an invalid postback
             $this->match->SetAddedBy(AuthenticationManager::GetUser());
         } else {
             # If user is neither admin nor owner, they won't have the team info. Get it from the $check_match so
             # that the match title can be updated correctly with a changed result.
             $this->match->SetHomeTeam($check_match->GetHomeTeam());
             $this->match->SetAwayTeam($check_match->GetAwayTeam());
         }
     }
     # Don't wan't to edit tournaments on this page, so even as admin make sure we're not trying to save one.
     # If user's not admin, can't change the match type, so find out what that is from the db too. For admin,
     # $check_match is the submitted one as the match type might've been changed.
     $this->b_is_tournament = $check_match->GetMatchType() == MatchType::TOURNAMENT;
     # Check whether cancel was clicked
     if ($this->editor->CancelClicked()) {
         # If so, get the match's short URL and redirect
         $this->match_manager->ExpandMatchUrl($this->match);
         $this->Redirect($this->match->GetNavigateUrl());
     }
     # save data if valid
     if ($this->IsValid() and !$this->b_is_tournament) {
         # Check whether the user has permission to update the fixture as well as the result
         if ($this->b_user_is_match_admin or $this->b_user_is_match_owner) {
             # Get the ground name from the database. This is used when compiling an email about the updated match result.
             if ($this->match->GetGround() instanceof Ground and $this->match->GetGround()->GetId() and !$this->match->GetGround()->GetName()) {
                 require_once 'stoolball/ground-manager.class.php';
                 $ground_manager = new GroundManager($this->GetSettings(), $this->GetDataConnection());
                 $ground_manager->ReadById(array($this->match->GetGround()->GetId()));
                 if ($ground_manager->GetCount()) {
                     $this->match->SetGround($ground_manager->GetFirst());
                 }
                 unset($ground_manager);
             }
             $this->match_manager->SaveFixture($this->match);
             if ($this->b_user_is_match_admin) {
                 $this->match_manager->SaveSeasons($this->match, false);
             }
             $this->editor->SetNavigateUrl($this->match->GetEditNavigateUrl());
             # because edit URL may have changed
         }
         # Save the result
         $this->match_manager->SaveIfPlayed($this->match);
         $this->match_manager->SaveWhoWonTheToss($this->match);
         $this->match_manager->SaveWhoBattedFirst($this->match);
         # If match didn't happen or the teams aren't known yet, save and finish, otherwise go to next page
         $result = $this->match->Result()->GetResultType();
         if ($result == MatchResult::HOME_WIN_BY_FORFEIT or $result == MatchResult::AWAY_WIN_BY_FORFEIT or $result == MatchResult::CANCELLED or $result == MatchResult::POSTPONED or $check_match->GetStartTime() > gmdate('U') or $this->b_user_is_match_admin and (!$this->match->GetHomeTeamId() or !$this->match->GetAwayTeamId())) {
             # Match may have been updated so, first, send an email
             $this->match_manager->NotifyMatchModerator($this->match->GetId());
             http_response_code(303);
             $this->Redirect($this->match->GetNavigateUrl());
         } else {
             http_response_code(303);
             $this->Redirect($this->match->EditScorecardUrl());
         }
     }
 }
 function OnLoadPageData()
 {
     /* @var $o_match Match */
     /* @var $o_team Team */
     # new data manager
     $match_manager = new MatchManager($this->GetSettings(), $this->GetDataConnection());
     # create repeater control, and save any posted data
     $this->repeater = new DataEditRepeater($this, 'CreateEditControl');
     $this->repeater->SetCssClass('matchResults');
     $this->repeater->SetPersistedParameters(array('team', 'season', "tournament"));
     $this->repeater->SetButtonText('Save all results');
     $this->repeater->SetShowButtonsAtTop(true);
     if ($this->IsPostback() and !$this->IsRefresh() and $this->IsValid()) {
         require_once 'forums/topic-manager.class.php';
         require_once 'forums/review-item.class.php';
         require_once 'forums/subscription-manager.class.php';
         $topic_manager = new TopicManager($this->GetSettings(), $this->GetDataConnection());
         foreach ($this->repeater->GetDataObjects() as $o_current_match) {
             /* @var $o_current_match Match */
             $match_manager->SaveResult($o_current_match);
             $match_manager->ExpandMatchUrl($o_current_match);
             $match_manager->NotifyMatchModerator($o_current_match->GetId());
             if (trim($o_current_match->GetNewComment())) {
                 $item_to_comment_on = new ReviewItem($this->GetSettings());
                 $item_to_comment_on->SetType(ContentType::STOOLBALL_MATCH);
                 $item_to_comment_on->SetId($o_current_match->GetId());
                 $item_to_comment_on->SetNavigateUrl("https://" . $this->GetSettings()->GetDomain() . $o_current_match->GetNavigateUrl());
                 $message = $topic_manager->SaveComment($item_to_comment_on, $o_current_match->GetNewComment());
                 # send subscription emails - new object each time to reset list of who's already recieved an email
                 $subs_manager = new SubscriptionManager($this->GetSettings(), $this->GetDataConnection());
                 $subs_manager->SendCommentsSubscriptions($item_to_comment_on, $message);
                 unset($subs_manager);
             }
         }
         $this->b_saved = true;
     }
     # get matches
     if (!is_null($this->i_team_id)) {
         $a_season_times = Season::SeasonDates();
         $match_manager->FilterByTeam(array($this->i_team_id));
         $match_manager->FilterByDateStart($a_season_times[0]);
         $match_manager->FilterByDateEnd($a_season_times[1]);
         $match_manager->ReadMatchSummaries();
     } else {
         if (!is_null($this->i_season_id)) {
             $match_manager->ReadBySeasonId(array($this->i_season_id), true);
         } else {
             if (!is_null($this->tournament_id)) {
                 $match_manager->FilterByTournament($this->tournament_id);
                 $this->a_matches = $match_manager->ReadMatchSummaries();
             }
         }
     }
     $this->a_matches = $match_manager->GetItems();
     # Make sure we have some matches
     if (count($this->a_matches)) {
         # If it's matches for a team, get the team
         if (!is_null($this->i_team_id)) {
             # Should only need to look at the first match, because the team must be a
             # part of that match in order for the match to be in this list
             $o_match =& $this->a_matches[0];
             $o_team = $o_match->GetHomeTeam();
             if ($o_team instanceof Team and $o_team->GetId() == $this->i_team_id) {
                 $this->team = $o_team;
             } else {
                 foreach ($o_match->GetAwayTeams() as $o_team) {
                     if ($o_team->GetId() == $this->i_team_id) {
                         $this->team = $o_team;
                         break;
                     }
                 }
             }
             if (!is_object($this->team)) {
                 $this->Redirect();
             }
             # Now that we have short URL data, if data has just been saved for team, redirect back to team
             if ($this->b_saved) {
                 $this->Redirect($this->team->GetNavigateUrl());
             }
         } else {
             if (!is_null($this->i_season_id)) {
                 # get details of the season
                 require_once 'stoolball/competition-manager.class.php';
                 $o_comp_manager = new CompetitionManager($this->GetSettings(), $this->GetDataConnection());
                 $o_comp_manager->ReadById(null, array($this->i_season_id));
                 $o_competition = $o_comp_manager->GetFirst();
                 unset($o_comp_manager);
                 if ($o_competition instanceof Competition) {
                     $this->season = $o_competition->GetWorkingSeason();
                 }
                 if (!$this->season instanceof Season) {
                     $this->Redirect();
                 }
                 # Now that we have short URL data, if data has just been saved for season, redirect back to season
                 if ($this->b_saved) {
                     $this->Redirect($this->season->GetNavigateUrl());
                 }
             } else {
                 if (!is_null($this->tournament_id)) {
                     $match_manager->ReadByMatchId(array($this->tournament_id));
                     $this->tournament = $match_manager->GetFirst();
                     if (!is_null($this->tournament)) {
                         # If the tournament has just been saved, now we have its URL so redirect to the thanks page
                         if ($this->b_saved) {
                             $this->Redirect($this->tournament->GetNavigateUrl());
                         }
                     }
                 }
             }
         }
     } else {
         $this->Redirect();
     }
     unset($match_manager);
 }