function OnLoadPageData()
 {
     /* @var $competition Competition */
     # check parameter
     if (isset($_GET['competition']) and is_numeric($_GET['competition'])) {
         $comp_manager = new CompetitionManager($this->GetSettings(), $this->GetDataConnection());
         $comp_manager->ReadById(array($_GET['competition']), null);
         $this->competition = $comp_manager->GetFirst();
         $this->season = $this->competition->GetWorkingSeason();
         unset($comp_manager);
     } else {
         $this->Redirect("/competitions/");
     }
     # must have found a competition
     if (!$this->competition instanceof Competition) {
         $this->Redirect("/competitions/");
     }
     # Get seasons in the competition
     $a_comp_ids = array($this->competition->GetId());
     $o_season_manager = new SeasonManager($this->GetSettings(), $this->GetDataConnection());
     $o_season_manager->ReadByCompetitionId($a_comp_ids);
     $a_seasons = $o_season_manager->GetItems();
     $this->competition->SetSeasons($a_seasons);
     unset($o_season_manager);
     # Get stats highlights
     $this->statistics["querystring"] = "?competition=" . $this->competition->GetId();
     require_once 'stoolball/statistics/statistics-manager.class.php';
     $statistics_manager = new StatisticsManager($this->GetSettings(), $this->GetDataConnection());
     $statistics_manager->FilterByCompetition(array($this->competition->GetId()));
     require_once "_summary-data-query.php";
     unset($statistics_manager);
 }
 public function OnLoadPageData()
 {
     require_once 'stoolball/competition-manager.class.php';
     $manager = new CompetitionManager($this->GetSettings(), $this->GetDataConnection());
     if ($this->season_id) {
         $manager->ReadById(null, array($this->season_id));
     } else {
         $manager->ReadById(array($this->competition_id));
     }
     $this->competition = $manager->GetFirst();
     $this->season = $this->competition->GetWorkingSeason();
     unset($manager);
     # If the competition was requested, redirect to the current season
     if ($this->competition_id) {
         http_response_code(303);
         header("Location: " . $this->season->GetMapUrl());
         return;
     }
     $this->has_map = count($this->season->GetTeams());
     # Get other seasons
     require_once 'stoolball/season-manager.class.php';
     $a_comp_ids = array($this->competition->GetId());
     $o_season_manager = new SeasonManager($this->GetSettings(), $this->GetDataConnection());
     $o_season_manager->ReadByCompetitionId($a_comp_ids);
     $a_other_seasons = $o_season_manager->GetItems();
     $this->competition->SetSeasons(array());
     foreach ($a_other_seasons as $season) {
         if ($season->GetId() == $this->season->GetId()) {
             $this->competition->AddSeason($this->season, true);
         } else {
             $this->competition->AddSeason($season, false);
         }
     }
 }
 function OnLoadPageData()
 {
     /* @var $o_competition Competition */
     # check parameter
     if (!isset($_GET['season']) or !is_numeric($_GET['season'])) {
         http_response_code(400);
         exit;
     }
     if (isset($_GET['season']) and is_numeric($_GET['season'])) {
         $comp_manager = new CompetitionManager($this->GetSettings(), $this->GetDataConnection());
         $comp_manager->ReadById(null, array($_GET['season']));
         $this->competition = $comp_manager->GetFirst();
         unset($comp_manager);
     }
     # must have found a competition
     if (!$this->competition instanceof Competition) {
         http_response_code(404);
         exit;
     }
     $this->season = $this->competition->GetWorkingSeason();
     if (is_object($this->season)) {
         # get matches
         $o_match_manager = new MatchManager($this->GetSettings(), $this->GetDataConnection());
         $o_match_manager->ReadBySeasonId(array($this->season->GetId()));
         $a_matches = $o_match_manager->GetItems();
         $this->season->SetMatches($a_matches);
         # Get other seasons
         $a_comp_ids = array($this->competition->GetId());
         $o_season_manager = new SeasonManager($this->GetSettings(), $this->GetDataConnection());
         $o_season_manager->ReadByCompetitionId($a_comp_ids);
         $a_other_seasons = $o_season_manager->GetItems();
         $this->competition->SetSeasons(array());
         foreach ($a_other_seasons as $season) {
             if ($season->GetId() == $this->season->GetId()) {
                 $this->competition->AddSeason($this->season, true);
             } else {
                 $this->competition->AddSeason($season, false);
             }
         }
         unset($o_season_manager);
     } else {
         # Must have a season
         http_response_code(404);
         exit;
     }
 }
 function OnLoadPageData()
 {
     # get id of season
     $i_id = $this->season_manager->GetItemId($this->season);
     # read season data when first asked to edit a season
     if ($i_id and !$this->IsPostback()) {
         $this->season_manager->ReadById(array($i_id));
         $this->season = $this->season_manager->GetFirst();
     }
     # get all teams
     $this->team_manager->SetGroupInactiveLast(false);
     $this->team_manager->ReadAll();
     $this->edit->SetTeams($this->team_manager->GetItems());
     # tidy up
     unset($this->season_manager);
     unset($this->team_manager);
 }
 function OnLoadPageData()
 {
     # get item to be deleted
     if (!is_object($this->data_object)) {
         $id = $this->manager->GetItemId($this->data_object);
         $this->manager->ReadById(array($id));
         $this->data_object = $this->manager->GetFirst();
         # How many seasons in this competition?
         if (is_object($this->data_object) and $this->data_object->GetCompetition() instanceof Competition and $this->data_object->GetCompetition()->GetId()) {
             $this->manager->Clear();
             $this->manager->ReadByCompetitionId(array($this->data_object->GetCompetition()->GetId()));
             $this->seasons_in_competition = $this->manager->GetCount();
         }
     }
     # tidy up
     unset($this->manager);
 }
 /**
  * If the season parameter is in the query string apply season filter
  * @param SiteSettings $settings
  * @param MySqlConnection $connection
  * @param StatisticsManager $statistics_manager
  */
 public static function ApplySeasonFilter(SiteSettings $settings, MySqlConnection $connection, StatisticsManager $statistics_manager)
 {
     $filter = "";
     if (isset($_GET['season']) and is_numeric($_GET['season'])) {
         require_once 'stoolball/season-manager.class.php';
         $season_manager = new SeasonManager($settings, $connection);
         $season_manager->ReadById(array($_GET['season']));
         $season = $season_manager->GetFirst();
         unset($season_manager);
         if (!is_null($season)) {
             $statistics_manager->FilterBySeason(array($season->GetId()));
             $filter = "in the " . $season->GetCompetitionName() . " ";
         }
     }
     return $filter;
 }
 function OnLoadPageData()
 {
     /* @var $match_manager MatchManager */
     /* @var $editor TournamentEditControl */
     # get id of Match
     $i_id = $this->match_manager->GetItemId();
     if (!$i_id) {
         return;
     }
     # no need to read if redisplaying invalid form
     if ($this->IsValid()) {
         $this->match_manager->ReadByMatchId(array($i_id));
         $this->tournament = $this->match_manager->GetFirst();
         if ($this->tournament instanceof Match) {
             $this->b_user_is_match_owner = AuthenticationManager::GetUser()->GetId() == $this->tournament->GetAddedBy()->GetId();
             $this->b_is_tournament = $this->tournament->GetMatchType() == MatchType::TOURNAMENT;
         } else {
             return;
         }
     }
     if ($this->b_user_is_match_admin or $this->b_user_is_match_owner) {
         # Get all seasons
         # Don't filter by match type at this point because we want to know what other types of match the seasons support.
         # If it's tournaments only, they'll be treated differently.
         require_once 'stoolball/season-manager.class.php';
         $season_manager = new SeasonManager($this->GetSettings(), $this->GetDataConnection());
         $season_dates = Season::SeasonDates($this->tournament->GetStartTime());
         $season_manager->FilterByDateStart($season_dates[0]);
         $season_manager->FilterByDateEnd($season_dates[1]);
         $season_manager->ReadSeasonSummaries(array($this->tournament->GetPlayerType()));
         $this->editor->Seasons()->SetItems($season_manager->GetItems());
         unset($season_manager);
     }
 }
 /**
  * Saves the seasons a match is in
  *
  * @param Match $match
  * @param bool $b_is_new_match
  */
 public function SaveSeasons(Match $match, $b_is_new_match)
 {
     $s_match = $this->GetSettings()->GetTable('Match');
     $s_season_match = $this->GetSettings()->GetTable('SeasonMatch');
     $season_table = $this->GetSettings()->GetTable('Season');
     $comp_table = $this->GetSettings()->GetTable('Competition');
     # Get ids of the tournament matches as well as this match, because they must necessarily be in the same season as their tournament
     # so we'll update the tournament matches whenever we update the season
     $a_matches_in_seasons = array();
     # Check GetId() rather than $b_is_new_match because match is being added as a multi-part process. Even though it's
     # new, by this point in the process it has an id that we need to use.
     if ($match->GetId()) {
         $a_matches_in_seasons[] = $match->GetId();
     }
     if (!$b_is_new_match and $match->GetMatchType() == MatchType::TOURNAMENT) {
         $s_sql = "SELECT match_id FROM {$s_match} WHERE tournament_match_id = " . Sql::ProtectNumeric($match->GetId());
         $o_result = $this->GetDataConnection()->query($s_sql);
         while ($row = $o_result->fetch()) {
             $a_matches_in_seasons[] = $row->match_id;
         }
         $o_result->closeCursor();
     }
     # All changes to master data from here are logged, because this method can be called from the public interface
     # Clear out seasons for this match and its tournament matches, ready to re-insert
     if (count($a_matches_in_seasons)) {
         $sql = "DELETE FROM {$s_season_match} WHERE match_id IN (" . join(', ', $a_matches_in_seasons) . ')';
         $this->LoggedQuery($sql);
     }
     # Add seasons again with new data
     foreach ($match->Seasons() as $season) {
         /* @var $season Season */
         foreach ($a_matches_in_seasons as $i_match_id) {
             $sql = "INSERT INTO {$s_season_match} (season_id, match_id) VALUES (" . Sql::ProtectNumeric($season->GetId()) . ', ' . Sql::ProtectNumeric($i_match_id) . ') ';
             $this->LoggedQuery($sql);
         }
         # If participation in this match implies the team is part of the whole season (ie not practice or tournament or friendly),
         # make sure the team is in the season
         if ($match->GetMatchType() == MatchType::CUP or $match->GetMatchType() == MatchType::LEAGUE) {
             require_once 'stoolball/season-manager.class.php';
             $season_manager = new SeasonManager($this->GetSettings(), $this->GetDataConnection());
             if ($match->GetHomeTeamId()) {
                 $season_manager->EnsureTeamIsInSeason($match->GetHomeTeamId(), $season->GetId());
             }
             $a_away = $match->GetAwayTeams();
             if (is_array($a_away) and count($a_away)) {
                 foreach ($a_away as $o_away_team) {
                     if (is_null($o_away_team)) {
                         continue;
                     }
                     $season_manager->EnsureTeamIsInSeason($o_away_team->GetId(), $season->GetId());
                 }
             }
             unset($season_manager);
         }
     }
     # The number of players in the match is derived from the competitions it's in. It's never entered directly even
     # by admins or displayed. Done to save extra queries when rendering scorecard editing interfaces. If people want to
     # display the number of players per match they can enter the results with the players' names.
     # Get the max number of players who may play based on the competitions the match is in, so long as this isn't a tournament or friendly.
     # For a tournament we'll ask the user instead, so just ignore this code and keep the existing value. Even in a season different
     # tournaments may have different rules, so really can't automate. Friendlies are just as flexible so, again, can't predict.
     if ($match->GetId() and $match->GetMatchType() != MatchType::TOURNAMENT and $match->GetMatchType() != MatchType::TOURNAMENT_MATCH and $match->GetMatchType() != MatchType::FRIENDLY) {
         $season_ids = array();
         foreach ($match->Seasons() as $season) {
             $season_ids[] = $season->GetId();
         }
         if (count($season_ids)) {
             $s_sql = "SELECT MAX({$comp_table}.players_per_team) AS players_per_team, MAX({$comp_table}.overs) AS overs\r\n\t\t\t\tFROM {$season_table} INNER JOIN {$comp_table} ON {$season_table}.competition_id = {$comp_table}.competition_id\r\n\t\t\t\tWHERE {$season_table}.season_id IN (" . implode(',', $season_ids) . ")";
             $result = $this->GetDataConnection()->query($s_sql);
             if (!$this->GetDataConnection()->isError()) {
                 $row = $result->fetch();
                 $match->SetMaximumPlayersPerTeam($row->players_per_team);
                 $match->SetOvers($row->overs);
             }
             $result->closeCursor();
         }
         # Update the match. Using the GetMaximumPlayersPerTeam property because it will give us the existing value
         # (possibly the default value if the code above didn't run because there were no seasons).
         $sql = "UPDATE {$s_match} SET\r\n\t\t\t\t\t\tplayers_per_team = " . Sql::ProtectNumeric($match->GetMaximumPlayersPerTeam()) . ",\r\n\t\t\t\t\t\tovers = " . Sql::ProtectNumeric($match->GetOvers()) . "\r\n\t\t\t\t\t\tWHERE match_id = " . Sql::ProtectNumeric($match->GetId());
         $this->LoggedQuery($sql);
     }
     # This season is mentioned in search results for a match, so request an update,
     # and note for auditing that the match has been changed
     $sql = "UPDATE nsa_match SET \r\n\t    update_search = 1, \r\n\t    date_changed = " . gmdate("U") . ", \r\n        modified_by_id = " . Sql::ProtectNumeric(AuthenticationManager::GetUser()->GetId()) . "\r\n\t    WHERE match_id = " . Sql::ProtectNumeric($match->GetId(), false);
     $this->LoggedQuery($sql);
     # Match data has changed so notify moderator
     $this->QueueForNotification($match->GetId(), $b_is_new_match);
 }
 function OnLoadPageData()
 {
     /* @var $o_competition Competition */
     # check parameter
     if (!isset($_GET['item']) or !is_numeric($_GET['item'])) {
         http_response_code(400);
         exit;
     }
     # new data managers
     $o_comp_manager = new CompetitionManager($this->GetSettings(), $this->GetDataConnection());
     # get competition
     $latest = (isset($_GET['latest']) and $_GET['latest'] == '1');
     if ($latest) {
         $o_comp_manager->ReadById(array($_GET['item']), null);
     } else {
         $o_comp_manager->ReadById(null, array($_GET['item']));
     }
     $this->competition = $o_comp_manager->GetFirst();
     $this->season = $this->competition->GetWorkingSeason();
     # must have found a competition
     if (!$this->competition instanceof Competition or !$this->season instanceof Season) {
         http_response_code(404);
         exit;
     }
     # If the competition was requested, redirect to the current season
     if ($latest) {
         http_response_code(303);
         header("Location: " . $this->season->GetNavigateUrl());
         return;
     }
     # Update search engine. Only do this for the latest season as then we have the right teams already.
     if ($this->competition->GetSearchUpdateRequired() and $latest) {
         $this->SearchIndexer()->DeleteFromIndexById("competition" . $this->competition->GetId());
         require_once "search/competition-search-adapter.class.php";
         $adapter = new CompetitionSearchAdapter($this->competition);
         $this->SearchIndexer()->Index($adapter->GetSearchableItem());
         $this->SearchIndexer()->CommitChanges();
         $o_comp_manager->SearchUpdated($this->competition->GetId());
     }
     unset($o_comp_manager);
     # get matches
     $o_match_manager = new MatchManager($this->GetSettings(), $this->GetDataConnection());
     $o_match_manager->ReadBySeasonId(array($this->season->GetId()));
     $a_matches = $o_match_manager->GetItems();
     $this->season->SetMatches($a_matches);
     # While we're here, check if there are any outstanding notifications to be sent
     $o_match_manager->NotifyMatchModerator();
     unset($o_match_manager);
     # Get stats highlights
     require_once 'stoolball/statistics/statistics-manager.class.php';
     $statistics_manager = new StatisticsManager($this->GetSettings(), $this->GetDataConnection());
     $statistics_manager->FilterBySeason(array($this->season->GetId()));
     $statistics_manager->FilterMaxResults(1);
     $this->best_batting = $statistics_manager->ReadBestBattingPerformance();
     $this->best_bowling = $statistics_manager->ReadBestBowlingPerformance();
     $this->most_runs = $statistics_manager->ReadBestPlayerAggregate("runs_scored");
     $this->most_wickets = $statistics_manager->ReadBestPlayerAggregate("wickets");
     $this->most_catches = $statistics_manager->ReadBestPlayerAggregate("catches");
     # See what stats we've got available
     $best_batting_count = count($this->best_batting);
     $best_bowling_count = count($this->best_bowling);
     $best_batters = count($this->most_runs);
     $best_bowlers = count($this->most_wickets);
     $best_catchers = count($this->most_catches);
     $this->has_player_stats = ($best_batting_count or $best_batters or $best_bowling_count or $best_bowlers or $best_catchers);
     if (!$this->has_player_stats) {
         $player_of_match = $statistics_manager->ReadBestPlayerAggregate("player_of_match");
         $this->has_player_stats = (bool) count($player_of_match);
     }
     unset($statistics_manager);
     # Get other seasons
     $a_comp_ids = array($this->competition->GetId());
     $o_season_manager = new SeasonManager($this->GetSettings(), $this->GetDataConnection());
     $o_season_manager->ReadByCompetitionId($a_comp_ids);
     $a_other_seasons = $o_season_manager->GetItems();
     $this->competition->SetSeasons(array());
     foreach ($a_other_seasons as $season) {
         if ($season->GetId() == $this->season->GetId()) {
             $this->competition->AddSeason($this->season, true);
         } else {
             $this->competition->AddSeason($season, false);
         }
     }
     unset($o_season_manager);
 }
 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);
 }
 function OnLoadPageData()
 {
     /* @var Team $team */
     # check parameter
     if (!isset($_GET['item']) or !is_numeric($_GET['item'])) {
         $this->Redirect();
     }
     # new data manager
     $team_manager = new TeamManager($this->GetSettings(), $this->GetDataConnection());
     $match_manager = new MatchManager($this->GetSettings(), $this->GetDataConnection());
     # get teams
     $team_manager->FilterByTeamType(array());
     $team_manager->ReadById(array($_GET['item']));
     $this->team = $team_manager->GetFirst();
     # must have found a team
     if (!$this->team instanceof Team) {
         $this->Redirect('/teams/');
     }
     # Update search engine
     if ($this->team->GetSearchUpdateRequired()) {
         require_once "search/team-search-adapter.class.php";
         $this->SearchIndexer()->DeleteFromIndexById("team" . $this->team->GetId());
         $adapter = new TeamSearchAdapter($this->team);
         $this->SearchIndexer()->Index($adapter->GetSearchableItem());
         $this->SearchIndexer()->CommitChanges();
         $team_manager->SearchUpdated($this->team->GetId());
     }
     unset($team_manager);
     $this->is_one_time_team = $this->team->GetTeamType() == Team::ONCE;
     # get matches and match stats
     if (!$this->is_one_time_team) {
         $a_season_dates = Season::SeasonDates();
         $this->season_key = date('Y', $a_season_dates[0]);
         if ($this->season_key != date('Y', $a_season_dates[1])) {
             $this->season_key .= "/" . date('y', $a_season_dates[1]);
         }
         $match_manager->FilterByDateStart($a_season_dates[0]);
     }
     $match_manager->FilterByTeam(array($this->team->GetId()));
     $match_manager->ReadMatchSummaries();
     $this->a_matches = $match_manager->GetItems();
     unset($match_manager);
     $club = $this->team->GetClub();
     $this->has_facebook_group_url = ($club->GetFacebookUrl() and strpos($club->GetFacebookUrl(), '/groups/') !== false);
     $this->has_facebook_page_url = ($club->GetFacebookUrl() and !$this->has_facebook_group_url);
     if (!$this->has_facebook_page_url) {
         require_once 'stoolball/statistics/statistics-manager.class.php';
         $statistics_manager = new StatisticsManager($this->GetSettings(), $this->GetDataConnection());
         $statistics_manager->FilterByTeam(array($this->team->GetId()));
         $statistics_manager->FilterMaxResults(1);
         $this->best_batting = $statistics_manager->ReadBestBattingPerformance();
         $this->best_bowling = $statistics_manager->ReadBestBowlingPerformance();
         $this->most_runs = $statistics_manager->ReadBestPlayerAggregate("runs_scored");
         $this->most_wickets = $statistics_manager->ReadBestPlayerAggregate("wickets");
         $this->most_catches = $statistics_manager->ReadBestPlayerAggregate("catches");
         # See what stats we've got available
         $best_batting_count = count($this->best_batting);
         $best_bowling_count = count($this->best_bowling);
         $best_batters = count($this->most_runs);
         $best_bowlers = count($this->most_wickets);
         $best_catchers = count($this->most_catches);
         $this->has_player_stats = ($best_batting_count or $best_batters or $best_bowling_count or $best_bowlers or $best_catchers);
         if (!$this->has_player_stats) {
             $player_of_match = $statistics_manager->ReadBestPlayerAggregate("player_of_match");
             $this->has_player_stats = (bool) count($player_of_match);
         }
         unset($statistics_manager);
     }
     # Get whether to show add league/cup links
     $season_manager = new SeasonManager($this->GetSettings(), $this->GetDataConnection());
     $season_manager->ReadCurrentSeasonsByTeamId(array($this->team->GetId()), array(MatchType::CUP, MatchType::LEAGUE));
     $this->seasons = $season_manager->GetItems();
     unset($season_manager);
 }
 function OnLoadPageData()
 {
     /* @var $o_last_match Match */
     /* @var $season Season */
     /* @var $team Team */
     # new data manager
     $o_match_manager = new MatchManager($this->GetSettings(), $this->GetDataConnection());
     # Collect season to add this match to, starting with the URL
     # get season and teams (was at this stage because editor needed teams to build its
     # posted data object, but that's no longer the case so probably could be later if needed)
     if (isset($this->i_season_id)) {
         $season_manager = new SeasonManager($this->GetSettings(), $this->GetDataConnection());
         $season_manager->ReadById(array($this->i_season_id));
         $this->season = $season_manager->GetFirst();
         unset($season_manager);
         $this->edit->Seasons()->Add($this->season);
         # If there are at least 2 teams in the season, show only those teams, otherwise show all teams of the relevant player type
         if (count($this->season->GetTeams()) > 1) {
             $this->edit->SetTeams(array($this->season->GetTeams()));
         } else {
             require_once 'stoolball/team-manager.class.php';
             $team_manager = new TeamManager($this->GetSettings(), $this->GetDataConnection());
             $team_manager->FilterByPlayerType(array($this->season->GetCompetition()->GetPlayerType()));
             $team_manager->ReadTeamSummaries();
             $this->edit->SetTeams(array($team_manager->GetItems()));
             unset($team_manager);
         }
     }
     # Not elseif, because when you've added a match there's a season, but we still need this to run to populate
     # the choices for the next match to be added
     if ($this->team instanceof Team) {
         # Otherwise it should be a 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();
         if (!is_null($this->team)) {
             $this->edit->SetContextTeam($this->team);
             $season_ids = array();
             $team_groups = array();
             $a_exclude_team_ids = array();
             $team_manager->FilterByActive(true);
             # Add the home team first
             $team_groups[] = array($this->team);
             $a_exclude_team_ids[] = $this->team->GetId();
             # Get the seasons this team is in...
             $season_manager = new SeasonManager($this->GetSettings(), $this->GetDataConnection());
             if ($this->i_match_type == MatchType::FRIENDLY) {
                 # For a friendly, any group of teams they play with is fine
                 $season_manager->ReadCurrentSeasonsByTeamId(array($this->team->GetId()));
             } else {
                 # For anything else, get the seasons *for this type of match*
                 $season_manager->ReadCurrentSeasonsByTeamId(array($this->team->GetId()), array($this->i_match_type));
             }
             $seasons = $season_manager->GetItems();
             unset($season_manager);
             $this->edit->Seasons()->Clear();
             # on postback, the season just added is already there, so clear to prevent a duplicate
             foreach ($seasons as $season) {
                 $this->edit->Seasons()->Add($season);
                 $season_ids[] = $season->GetId();
             }
             #... and their opponent teams in those seasons
             if (count($season_ids)) {
                 $team_manager->FilterExceptTeams($a_exclude_team_ids);
                 $team_manager->ReadBySeasonId($season_ids);
                 $season_teams = $team_manager->GetItems();
                 if (count($season_teams)) {
                     $team_groups['This season\'s teams'] = $season_teams;
                 }
                 foreach ($season_teams as $team) {
                     $a_exclude_team_ids[] = $team->GetId();
                 }
             }
             # ...and if this is a friendly it could be any other team
             if ($this->i_match_type == MatchType::FRIENDLY) {
                 # get any other teams they played in the last 2 years, and combine with existing results
                 $team_manager->FilterExceptTeams($a_exclude_team_ids);
                 $team_manager->ReadRecentOpponents(array($this->team->GetId()), 24);
                 $recent_opponents = $team_manager->GetItems();
                 if (count($recent_opponents)) {
                     $team_groups['Recent opponents'] = $recent_opponents;
                 }
                 foreach ($recent_opponents as $team) {
                     $a_exclude_team_ids[] = $team->GetId();
                 }
                 # get any other teams they might play, and combine with existing results
                 $team_manager->FilterExceptTeams($a_exclude_team_ids);
                 $team_manager->ReadAll();
                 $team_groups['Other teams'] = $team_manager->GetItems();
             }
             # What if there are still no opponents to choose from? In that case select all teams.
             if (count($team_groups) == 1) {
                 $team_manager->FilterExceptTeams($a_exclude_team_ids);
                 $team_manager->ReadAll();
                 $team_groups[] = $team_manager->GetItems();
             }
             # Offer those teams to select from
             if ($total_groups = count($team_groups)) {
                 # If only two groups (home team + 1 group), don't group teams. Remove the only key from the array.
                 if ($total_groups == 2) {
                     $keys = array_keys($team_groups);
                     $team_groups = array($team_groups[$keys[0]], $team_groups[$keys[1]]);
                 }
                 $this->edit->SetTeams($team_groups);
             }
         }
         unset($team_manager);
     }
     # Save match
     if ($this->IsPostback() and $this->IsValid()) {
         # Get posted match
         $this->match = $this->edit->GetDataObject();
         if (!$this->IsRefresh()) {
             # Save match
             $o_match_manager->SaveFixture($this->match);
             $o_match_manager->SaveSeasons($this->match, true);
             $o_match_manager->NotifyMatchModerator($this->match->GetId());
             # Update 'next 5 matches'
             $o_match_manager->ReadNext();
             $this->a_next_matches = $o_match_manager->GetItems();
         }
         # Reset control for new match
         $this->edit->SetDataObject(new Match($this->GetSettings()));
     }
     $o_match_manager->FilterByMatchType(array($this->i_match_type));
     if (isset($this->i_season_id)) {
         # If we're adding a match to a season, get last game in season for its date
         $o_match_manager->ReadLastInSeason($this->season->GetId());
     } else {
         if ($this->team instanceof Team) {
             # Get the last game already scheduled for the team to use its date
             $o_match_manager->ReadLastForTeam($this->team->GetId());
         }
     }
     $o_last_match = $o_match_manager->GetFirst();
     if (is_object($o_last_match)) {
         $current_season = Season::SeasonDates();
         if (gmdate('Y', $o_last_match->GetStartTime()) < gmdate('Y', $current_season[0])) {
             # If the last match this team played was last season, use the time but not the date
             $this->edit->SetDefaultTime(gmmktime(gmdate('H', $o_last_match->GetStartTime()), gmdate('i', $o_last_match->GetStartTime()), 0, gmdate('m'), gmdate('d'), gmdate('Y')));
         } else {
             # If the last match was this season and has a time, use it
             if ($o_last_match->GetIsStartTimeKnown()) {
                 $this->edit->SetDefaultTime($o_last_match->GetStartTime());
             } else {
                 # If the last match has no time, use 6.30pm BST
                 $this->edit->SetDefaultTime(gmmktime(17, 30, 0, gmdate('m', $o_last_match->GetStartTime()), gmdate('d', $o_last_match->GetStartTime()), gmdate('Y', $o_last_match->GetStartTime())));
             }
         }
     }
     unset($o_match_manager);
     # Get grounds
     $o_ground_manager = new GroundManager($this->GetSettings(), $this->GetDataConnection());
     $o_ground_manager->ReadAll();
     $a_grounds = $o_ground_manager->GetItems();
     $this->edit->SetGrounds($a_grounds);
     unset($o_ground_manager);
 }
 /**
  * @access public
  * @return void
  * @param int[] $a_ids
  * @desc Delete from the db the Competitions matching the supplied ids
  */
 function Delete($a_ids)
 {
     # check paramter
     if (!is_array($a_ids)) {
         die('No Competitions to delete');
     }
     # build query
     $s_comp = $this->o_settings->GetTable('Competition');
     $s_season = $this->o_settings->GetTable('Season');
     $s_ids = join(', ', $a_ids);
     # get the seasons and use SeasonManager to deal with them
     $season_ids = array();
     $s_sql = 'SELECT season_id FROM ' . $s_season . ' WHERE competition_id IN (' . $s_ids . ') ';
     $result = $this->GetDataConnection()->query($s_sql);
     while ($row = $result->fetch()) {
         $season_ids[] = $row->season_id;
     }
     $result->closeCursor();
     require_once 'stoolball/season-manager.class.php';
     $season_manager = new SeasonManager($this->GetSettings(), $this->GetDataConnection());
     $season_manager->Delete($season_ids);
     unset($season_manager);
     # delete from short URL cache
     require_once 'http/short-url-manager.class.php';
     $o_url_manager = new ShortUrlManager($this->GetSettings(), $this->GetDataConnection());
     $s_sql = "SELECT short_url FROM {$s_comp} WHERE competition_id IN ({$s_ids})";
     $result = $this->GetDataConnection()->query($s_sql);
     while ($row = $result->fetch()) {
         $o_url_manager->Delete($row->short_url);
     }
     $result->closeCursor();
     unset($o_url_manager);
     # delete competition(s)
     $s_sql = 'DELETE FROM ' . $s_comp . ' WHERE competition_id IN (' . $s_ids . ') ';
     $o_result = $this->GetDataConnection()->query($s_sql);
     return $this->GetDataConnection()->GetAffectedRows();
 }