/**
  * 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);
 }
 /**
  * Builds a match object containing the result information posted by the control
  *
  */
 public function BuildPostedDataObject()
 {
     $match = new Match($this->GetSettings());
     $match->SetMatchType(MatchType::TOURNAMENT);
     # Get match id
     $s_key = $this->GetNamingPrefix() . 'item';
     if (isset($_POST[$s_key])) {
         $s_id = $_POST[$s_key];
         if (strlen($s_id)) {
             $match->SetId($s_id);
         }
     }
     # Get the title
     $s_key = $this->GetNamingPrefix() . 'Title';
     if (isset($_POST[$s_key])) {
         $match->SetTitle(strip_tags($_POST[$s_key]));
     }
     # Get the qualification type
     $s_key = $this->GetNamingPrefix() . 'Qualify';
     if (isset($_POST[$s_key])) {
         $match->SetQualificationType($_POST[$s_key]);
     }
     # Get the player type
     $s_key = $this->GetNamingPrefix() . 'PlayerType';
     if (isset($_POST[$s_key])) {
         $match->SetPlayerType($_POST[$s_key]);
     }
     $s_key = $this->GetNamingPrefix() . "Players";
     if (isset($_POST[$s_key]) and strlen($_POST[$s_key])) {
         $match->SetMaximumPlayersPerTeam($_POST[$s_key]);
     }
     # Get the number of overs
     $s_key = $this->GetNamingPrefix() . "Overs";
     if (isset($_POST[$s_key]) and strlen($_POST[$s_key])) {
         $match->SetOvers($_POST[$s_key]);
     }
     # Get the short URL
     $s_key = $this->GetNamingPrefix() . 'ShortUrl';
     if (isset($_POST[$s_key])) {
         $match->SetShortUrl($_POST[$s_key]);
     }
     # Get the start date
     $s_key = $this->GetNamingPrefix() . 'Start';
     $match->SetStartTime(DateControl::GetPostedTimestampUtc($s_key));
     $match->SetIsStartTimeKnown(DateControl::GetIsTimePosted($s_key));
     # Get the initial team
     $team = new Team($this->GetSettings());
     $s_key = $this->GetNamingPrefix() . 'ContextTeam';
     if (isset($_POST[$s_key]) and strlen($_POST[$s_key])) {
         $team->SetId($_POST[$s_key]);
         $match->AddAwayTeam($team);
     }
     # Get the ground
     $s_key = $this->GetNamingPrefix() . 'Ground';
     if (isset($_POST[$s_key]) and strlen($_POST[$s_key])) {
         $o_ground = new Ground($this->GetSettings());
         $o_ground->SetId($_POST[$s_key]);
         $match->SetGround($o_ground);
     }
     # Get the notes
     $s_key = $this->GetNamingPrefix() . 'Notes';
     if (isset($_POST[$s_key])) {
         $match->SetNotes($_POST[$s_key]);
     }
     $this->SetDataObject($match);
 }
 /**
  * Builds data posted on pages 2/3 back into a match object
  * @return Match
  */
 private function BuildPostedScorecard()
 {
     $match = new Match($this->GetSettings());
     $match->SetId($this->GetDataObjectId());
     # Must have data on which team is which, otherwise none of the rest makes sense
     # Team ids are essential for saving data, while team names and match title are
     # purely so they can be redisplayed if the page is invalid
     $key = "teams";
     if (!isset($_POST[$key]) or strpos($_POST[$key], ScorecardEditControl::DATA_SEPARATOR) === false) {
         return $match;
     }
     $teams = explode(ScorecardEditControl::DATA_SEPARATOR, $_POST[$key], 6);
     if (count($teams) != 6) {
         return $match;
     }
     switch ($teams[0]) {
         case "0":
             $match->Result()->SetHomeBattedFirst(false);
             $home_batting = $this->GetCurrentPage() == 3;
             break;
         case "1":
             $match->Result()->SetHomeBattedFirst(true);
             $home_batting = $this->GetCurrentPage() == 2;
             break;
         default:
             $home_batting = $this->GetCurrentPage() == 2;
     }
     $home_team = new Team($this->GetSettings());
     $home_team->SetId($teams[1]);
     $home_team->SetName($teams[2]);
     $match->SetHomeTeam($home_team);
     $away_team = new Team($this->GetSettings());
     $away_team->SetId($teams[3]);
     $away_team->SetName($teams[4]);
     $match->SetAwayTeam($away_team);
     $match->SetTitle($teams[5]);
     # Read posted batting data
     $key = "batRows";
     if (isset($_POST[$key])) {
         # This controls not only which fields are read, but also which are redisplayed on an invalid postback or for the next innings.
         $match->SetMaximumPlayersPerTeam(intval($_POST[$key]));
     }
     for ($i = 1; $i <= $match->GetMaximumPlayersPerTeam(); $i++) {
         $key = "batName{$i}";
         if (isset($_POST[$key])) {
             # The row exists - has it been filled in?
             if (trim($_POST[$key])) {
                 # Read the batter data in this row
                 $player = new Player($this->GetSettings());
                 $player->SetName($_POST[$key]);
                 $player->Team()->SetId($home_batting ? $home_team->GetId() : $away_team->GetId());
                 $key = "batHowOut{$i}";
                 $how_out = (isset($_POST[$key]) and is_numeric($_POST[$key])) ? (int) $_POST[$key] : null;
                 $key = "batOutBy{$i}";
                 $dismissed_by = null;
                 if (isset($_POST[$key]) and trim($_POST[$key])) {
                     $dismissed_by = new Player($this->GetSettings());
                     $dismissed_by->SetName($_POST[$key]);
                     $dismissed_by->Team()->SetId($home_batting ? $away_team->GetId() : $home_team->GetId());
                 }
                 $key = "batBowledBy{$i}";
                 $bowler = null;
                 if (isset($_POST[$key]) and trim($_POST[$key])) {
                     $bowler = new Player($this->GetSettings());
                     $bowler->SetName($_POST[$key]);
                     $bowler->Team()->SetId($home_batting ? $away_team->GetId() : $home_team->GetId());
                 }
                 # Correct caught and bowled if marked as caught
                 if ($how_out == Batting::CAUGHT and !is_null($dismissed_by) and !is_null($bowler) and trim($dismissed_by->GetName()) == trim($bowler->GetName())) {
                     $how_out = Batting::CAUGHT_AND_BOWLED;
                     $dismissed_by = null;
                 }
                 $key = "batRuns{$i}";
                 $runs = (isset($_POST[$key]) and is_numeric($_POST[$key])) ? (int) $_POST[$key] : null;
                 $key = "batBalls{$i}";
                 $balls = (isset($_POST[$key]) and is_numeric($_POST[$key])) ? (int) $_POST[$key] : null;
                 # Add that batting performance to the match result
                 $batting = new Batting($player, $how_out, $dismissed_by, $bowler, $runs, $balls);
                 if ($home_batting) {
                     $match->Result()->HomeBatting()->Add($batting);
                 } else {
                     $match->Result()->AwayBatting()->Add($batting);
                 }
             }
         }
     }
     $key = "batByes";
     if (isset($_POST[$key]) and is_numeric($_POST[$key])) {
         $player = new Player($this->GetSettings());
         $player->SetPlayerRole(Player::BYES);
         $player->Team()->SetId($home_batting ? $home_team->GetId() : $away_team->GetId());
         $batting = new Batting($player, null, null, null, (int) $_POST[$key]);
         if ($home_batting) {
             $match->Result()->HomeBatting()->Add($batting);
         } else {
             $match->Result()->AwayBatting()->Add($batting);
         }
     }
     $key = "batWides";
     if (isset($_POST[$key]) and is_numeric($_POST[$key])) {
         $player = new Player($this->GetSettings());
         $player->SetPlayerRole(Player::WIDES);
         $player->Team()->SetId($home_batting ? $home_team->GetId() : $away_team->GetId());
         $batting = new Batting($player, null, null, null, (int) $_POST[$key]);
         if ($home_batting) {
             $match->Result()->HomeBatting()->Add($batting);
         } else {
             $match->Result()->AwayBatting()->Add($batting);
         }
     }
     $key = "batNoBalls";
     if (isset($_POST[$key]) and is_numeric($_POST[$key])) {
         $player = new Player($this->GetSettings());
         $player->SetPlayerRole(Player::NO_BALLS);
         $player->Team()->SetId($home_batting ? $home_team->GetId() : $away_team->GetId());
         $batting = new Batting($player, null, null, null, (int) $_POST[$key]);
         if ($home_batting) {
             $match->Result()->HomeBatting()->Add($batting);
         } else {
             $match->Result()->AwayBatting()->Add($batting);
         }
     }
     $key = "batBonus";
     if (isset($_POST[$key]) and is_numeric($_POST[$key])) {
         $player = new Player($this->GetSettings());
         $player->SetPlayerRole(Player::BONUS_RUNS);
         $player->Team()->SetId($home_batting ? $home_team->GetId() : $away_team->GetId());
         $batting = new Batting($player, null, null, null, (int) $_POST[$key]);
         if ($home_batting) {
             $match->Result()->HomeBatting()->Add($batting);
         } else {
             $match->Result()->AwayBatting()->Add($batting);
         }
     }
     $key = "batTotal";
     if (isset($_POST[$key]) and is_numeric($_POST[$key])) {
         if ($home_batting) {
             $match->Result()->SetHomeRuns($_POST[$key]);
         } else {
             $match->Result()->SetAwayRuns($_POST[$key]);
         }
     }
     $key = "batWickets";
     if (isset($_POST[$key]) and is_numeric($_POST[$key])) {
         if ($home_batting) {
             $match->Result()->SetHomeWickets($_POST[$key]);
         } else {
             $match->Result()->SetAwayWickets($_POST[$key]);
         }
     }
     # Read posted bowling data
     $key = "bowlerRows";
     if (isset($_POST[$key])) {
         # This controls not only which fields are read, but also which are redisplayed on an invalid postback or for the next innings.
         $match->SetOvers(intval($_POST[$key]));
     }
     for ($i = 1; $i <= $match->GetOvers(); $i++) {
         $key = "bowlerName{$i}";
         if (isset($_POST[$key])) {
             # The row exists - has it been filled in?
             if (trim($_POST[$key])) {
                 # Read the bowler data in this row
                 # strlen test allows 0 but not empty string, because is_numeric allows empty string
                 $player = new Player($this->GetSettings());
                 $player->SetName($_POST[$key]);
                 $player->Team()->SetId($home_batting ? $away_team->GetId() : $home_team->GetId());
                 $key = "bowlerBalls{$i}";
                 $balls = (isset($_POST[$key]) and is_numeric($_POST[$key]) and strlen(trim($_POST[$key]))) ? (int) $_POST[$key] : null;
                 $key = "bowlerNoBalls{$i}";
                 $no_balls = (isset($_POST[$key]) and is_numeric($_POST[$key]) and strlen(trim($_POST[$key]))) ? (int) $_POST[$key] : null;
                 $key = "bowlerWides{$i}";
                 $wides = (isset($_POST[$key]) and is_numeric($_POST[$key]) and strlen(trim($_POST[$key]))) ? (int) $_POST[$key] : null;
                 $key = "bowlerRuns{$i}";
                 $runs = (isset($_POST[$key]) and is_numeric($_POST[$key]) and strlen(trim($_POST[$key]))) ? (int) $_POST[$key] : null;
                 # Add that over to the match result
                 $bowling = new Over($player);
                 $bowling->SetBalls($balls);
                 $bowling->SetNoBalls($no_balls);
                 $bowling->SetWides($wides);
                 $bowling->SetRunsInOver($runs);
                 if ($home_batting) {
                     $match->Result()->AwayOvers()->Add($bowling);
                 } else {
                     $match->Result()->HomeOvers()->Add($bowling);
                 }
             }
         }
     }
     return $match;
 }