/**
  * 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);
 }