/**
  * Re-build from data posted by this control a single data object which this control is editing
  *
  * @param int $i_counter
  * @param int $i_id
  */
 protected function BuildPostedItem($i_counter = null, $i_id = null)
 {
     $match = new Match($this->GetSettings());
     $match->SetMatchType(MatchType::TOURNAMENT_MATCH);
     $key = $this->GetNamingPrefix() . 'MatchId' . $i_counter;
     if (isset($_POST[$key]) and is_numeric($_POST[$key])) {
         $match->SetId($_POST[$key]);
     }
     $key = $this->GetNamingPrefix() . 'MatchOrder' . $i_counter;
     if (isset($_POST[$key]) and is_numeric($_POST[$key])) {
         $match->SetOrderInTournament($_POST[$key]);
     }
     $key = $this->GetNamingPrefix() . 'MatchIdValue' . $i_counter;
     if (isset($_POST[$key])) {
         $match->SetTitle($_POST[$key]);
     }
     $key = $this->GetNamingPrefix() . 'HomeTeamId' . $i_counter;
     if (isset($_POST[$key]) and $_POST[$key]) {
         $team = new Team($this->GetSettings());
         $team->SetId($_POST[$key]);
         $match->SetHomeTeam($team);
     }
     $key = $this->GetNamingPrefix() . 'AwayTeamId' . $i_counter;
     if (isset($_POST[$key]) and $_POST[$key]) {
         $team = new Team($this->GetSettings());
         $team->SetId($_POST[$key]);
         $match->SetAwayTeam($team);
     }
     if ($match->GetId() or $match->GetHomeTeamId() and $match->GetAwayTeamId()) {
         $this->DataObjects()->Add($match);
     } else {
         $this->IgnorePostedItem($i_counter);
     }
 }
 public function OnPageInit()
 {
     # create edit control
     if ($this->team instanceof Team) {
         $match = new Match($this->GetSettings());
         $match->SetHomeTeam($this->team);
         # Createa match specifying requested team as the home team, so that it gets selected by default
         $this->edit = new MatchFixtureEditControl($this->GetSettings(), $match);
     } else {
         $this->edit = new MatchFixtureEditControl($this->GetSettings());
     }
     $this->edit->SetMatchType($this->i_match_type);
     $this->RegisterControlForValidation($this->edit);
 }
 /**
  * @return void
  * @desc Re-build from data posted by this control the data object this control is editing
  */
 function BuildPostedDataObject()
 {
     $match = new Match($this->GetSettings());
     $match->SetId($this->GetDataObjectId());
     # Get match date
     $s_key = $this->GetNamingPrefix() . 'Date';
     if (isset($_POST[$s_key]) and strlen($_POST[$s_key]) and is_numeric($_POST[$s_key])) {
         $match->SetStartTime($_POST[$s_key]);
     }
     # Get team names
     $s_key = $this->GetNamingPrefix() . 'Home';
     if (isset($_POST[$s_key])) {
         $team_data = explode(MatchHighlightsEditControl::DATA_SEPARATOR, $_POST[$s_key], 2);
         if (count($team_data) == 2) {
             $o_home = new Team($this->GetSettings());
             $o_home->SetId($team_data[0]);
             $o_home->SetName($team_data[1]);
             $match->SetHomeTeam($o_home);
         }
     }
     $s_key = $this->GetNamingPrefix() . 'Away';
     if (isset($_POST[$s_key])) {
         $team_data = explode(MatchHighlightsEditControl::DATA_SEPARATOR, $_POST[$s_key], 2);
         if (count($team_data) == 2) {
             $o_away = new Team($this->GetSettings());
             $o_away->SetId($team_data[0]);
             $o_away->SetName($team_data[1]);
             $match->SetAwayTeam($o_away);
         }
     }
     # Get the result
     $s_key = $this->GetNamingPrefix() . 'Result';
     if (isset($_POST[$s_key])) {
         $s_result = $_POST[$s_key];
         if (strlen($s_result)) {
             $match->Result()->SetResultType($s_result);
         }
     }
     # Get players of the match. Fields to use depend on which radio button was selected.
     $s_key = $this->GetNamingPrefix() . 'POM';
     if (isset($_POST[$s_key])) {
         $pom_type = (int) $_POST[$s_key];
         if ($pom_type == MatchHighlightsEditControl::PLAYER_OF_THE_MATCH_OVERALL) {
             $s_key = $this->GetNamingPrefix() . 'Player';
             if (isset($_POST[$s_key]) and $_POST[$s_key]) {
                 $player = new Player($this->GetSettings());
                 $player->SetName($_POST[$s_key]);
                 $s_key = $this->GetNamingPrefix() . 'PlayerTeam';
                 if (isset($_POST[$s_key])) {
                     $player->Team()->SetId($_POST[$s_key]);
                 }
                 $match->Result()->SetPlayerOfTheMatch($player);
             }
         } else {
             if ($pom_type == MatchHighlightsEditControl::PLAYER_OF_THE_MATCH_HOME_AND_AWAY) {
                 $s_key = $this->GetNamingPrefix() . 'PlayerHome';
                 if (isset($_POST[$s_key]) and $_POST[$s_key]) {
                     $player = new Player($this->GetSettings());
                     $player->SetName($_POST[$s_key]);
                     $player->Team()->SetId($match->GetHomeTeamId());
                     $match->Result()->SetPlayerOfTheMatchHome($player);
                 }
                 $s_key = $this->GetNamingPrefix() . 'PlayerAway';
                 if (isset($_POST[$s_key]) and $_POST[$s_key]) {
                     $player = new Player($this->GetSettings());
                     $player->SetName($_POST[$s_key]);
                     $player->Team()->SetId($match->GetAwayTeamId());
                     $match->Result()->SetPlayerOfTheMatchAway($player);
                 }
             }
         }
     }
     $s_key = $this->GetNamingPrefix() . 'Comments';
     if (isset($_POST[$s_key])) {
         $match->SetNewComment($_POST[$s_key]);
     }
     $this->SetDataObject($match);
 }
 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());
         }
     }
 }
 /**
  * Builds a match object containing the result information posted by the control
  *
  */
 public function BuildPostedDataObject()
 {
     $o_match = new Match($this->GetSettings());
     $o_match->SetId($this->GetDataObjectId());
     # Get match date
     $s_key = $this->GetNamingPrefix() . 'Date';
     if (isset($_POST[$s_key]) and strlen($_POST[$s_key]) and is_numeric($_POST[$s_key])) {
         $o_match->SetStartTime($_POST[$s_key]);
     }
     # Get team names
     $s_key = $this->GetNamingPrefix() . 'Home';
     if (isset($_POST[$s_key])) {
         $team_data = explode(";", $_POST[$s_key], 2);
         if (count($team_data) == 2) {
             $o_home = new Team($this->GetSettings());
             $o_home->SetId($team_data[0]);
             $o_home->SetName($team_data[1]);
             $o_match->SetHomeTeam($o_home);
         }
     }
     $s_key = $this->GetNamingPrefix() . 'Away';
     if (isset($_POST[$s_key])) {
         $team_data = explode(";", $_POST[$s_key], 2);
         if (count($team_data) == 2) {
             $o_away = new Team($this->GetSettings());
             $o_away->SetId($team_data[0]);
             $o_away->SetName($team_data[1]);
             $o_match->SetAwayTeam($o_away);
         }
     }
     # Get who batted first
     $s_key = $this->GetNamingPrefix() . 'BatFirst';
     if (isset($_POST[$s_key])) {
         $s_batted = $_POST[$s_key];
         if ($s_batted == 'home') {
             $o_match->Result()->SetHomeBattedFirst(true);
         } else {
             if ($s_batted == 'away') {
                 $o_match->Result()->SetHomeBattedFirst(false);
             }
         }
     }
     # Get the result
     $s_key = $this->GetNamingPrefix() . 'Result';
     if (isset($_POST[$s_key])) {
         $s_result = $_POST[$s_key];
         if (strlen($s_result)) {
             $o_match->Result()->SetResultType($s_result);
         }
     }
     # Get the home score
     $s_key = $this->GetNamingPrefix() . 'HomeRuns';
     if (isset($_POST[$s_key])) {
         $s_home_runs = $_POST[$s_key];
         if (strlen($s_home_runs)) {
             $o_match->Result()->SetHomeRuns($s_home_runs);
         }
     }
     $s_key = $this->GetNamingPrefix() . 'HomeWickets';
     if (isset($_POST[$s_key])) {
         $s_home_wickets = $_POST[$s_key];
         if (strlen($s_home_wickets)) {
             $o_match->Result()->SetHomeWickets($s_home_wickets);
         }
     }
     # Get the away score
     $s_key = $this->GetNamingPrefix() . 'AwayRuns';
     if (isset($_POST[$s_key])) {
         $s_away_runs = $_POST[$s_key];
         if (strlen($s_away_runs)) {
             $o_match->Result()->SetAwayRuns($s_away_runs);
         }
     }
     $s_key = $this->GetNamingPrefix() . 'AwayWickets';
     if (isset($_POST[$s_key])) {
         $s_away_wickets = $_POST[$s_key];
         if (strlen($s_away_wickets)) {
             $o_match->Result()->SetAwayWickets($s_away_wickets);
         }
     }
     $s_key = $this->GetNamingPrefix() . 'Comments';
     if (isset($_POST[$s_key])) {
         $o_match->SetNewComment($_POST[$s_key]);
     }
     $this->SetDataObject($o_match);
 }
 /**
  * Builds a match object containing the result information posted by the control
  *
  */
 public function BuildPostedDataObject()
 {
     $o_match = new Match($this->GetSettings());
     # Get match id
     $s_key = $this->GetNamingPrefix() . 'item';
     if (isset($_POST[$s_key])) {
         $s_id = $_POST[$s_key];
         if (strlen($s_id)) {
             $o_match->SetId($s_id);
         }
     }
     # Get the short URL
     $s_key = $this->GetNamingPrefix() . 'ShortUrl';
     if (isset($_POST[$s_key])) {
         $o_match->SetShortUrl($_POST[$s_key]);
     }
     # Get the start date
     $s_key = $this->GetNamingPrefix() . 'Start';
     $o_match->SetStartTime(DateControl::GetPostedTimestampUtc($s_key));
     $o_match->SetIsStartTimeKnown(DateControl::GetIsTimePosted($s_key));
     # Get the home team
     # Test for (int)$_POST[$s_key] deliberately excludes "Not known" value, which is 0
     $o_home = new Team($this->GetSettings());
     $s_key = $this->GetNamingPrefix() . 'Home';
     if (isset($_POST[$s_key]) and strlen($_POST[$s_key]) and (int) $_POST[$s_key]) {
         $o_home->SetId($_POST[$s_key]);
         $o_match->SetHomeTeam($o_home);
     }
     # Get the away team
     # Test for (int)$_POST[$s_key] deliberately excludes "Not known" value, which is 0
     $o_away = new Team($this->GetSettings());
     $s_key = $this->GetNamingPrefix() . 'Away';
     if (isset($_POST[$s_key]) and strlen($_POST[$s_key]) and (int) $_POST[$s_key]) {
         $o_away->SetId($_POST[$s_key]);
         $o_match->SetAwayTeam($o_away);
     }
     # 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]);
         $o_match->SetGround($o_ground);
     }
     # Get the notes
     $s_key = $this->GetNamingPrefix() . 'Notes';
     if (isset($_POST[$s_key])) {
         $o_match->SetNotes($_POST[$s_key]);
     }
     # Get the match type
     $s_key = $this->GetNamingPrefix() . 'MatchType';
     if (isset($_POST[$s_key]) and is_numeric($_POST[$s_key])) {
         $o_match->SetMatchType($_POST[$s_key]);
     }
     # Get the tournament
     if ($o_match->GetMatchType() == MatchType::TOURNAMENT_MATCH) {
         $s_key = $this->GetNamingPrefix() . 'Tournament';
         if (isset($_POST[$s_key]) and is_numeric($_POST[$s_key])) {
             $tournament = new Match($this->GetSettings());
             $tournament->SetMatchType(MatchType::TOURNAMENT);
             $tournament->SetId($_POST[$s_key]);
             $o_match->SetTournament($tournament);
         }
     }
     # Get the season
     $s_key = $this->GetNamingPrefix() . 'Season';
     if (isset($_POST[$s_key]) and strlen($_POST[$s_key])) {
         $o_season = new Season($this->GetSettings());
         $o_season->SetId($_POST[$s_key]);
         $o_match->Seasons()->Add($o_season);
     }
     $this->SetDataObject($o_match);
 }
 /**
  * Populates the collection of business objects from raw data
  *
  * @return bool
  * @param MySqlRawData $o_result
  */
 protected function BuildItems(MySqlRawData $o_result)
 {
     $this->Clear();
     $o_match_builder = new CollectionBuilder();
     $o_team_builder = new CollectionBuilder();
     while ($o_row = $o_result->fetch()) {
         if (!$o_match_builder->IsDone($o_row->match_id)) {
             if (isset($o_match)) {
                 $this->Add($o_match);
                 $o_team_builder->Reset();
             }
             # create new
             $o_match = new Match($this->GetSettings());
             $o_match->SetId($o_row->match_id);
             if (isset($o_row->start_time)) {
                 $o_match->SetStartTime($o_row->start_time);
             }
             if (isset($o_row->home_runs)) {
                 $o_match->Result()->SetHomeRuns($o_row->home_runs);
             }
             if (isset($o_row->away_runs)) {
                 $o_match->Result()->SetAwayRuns($o_row->away_runs);
             }
             if (isset($o_row->match_result_id)) {
                 $o_match->Result()->SetResultType($o_row->match_result_id);
             }
             if (isset($o_row->home_team_id) and !is_null($o_row->home_team_id)) {
                 $o_home = new Team($this->o_settings);
                 $o_home->SetId($o_row->home_team_id);
                 if (isset($o_row->home_team_name)) {
                     $o_home->SetName($o_row->home_team_name);
                 }
                 if (isset($o_row->home_short_url)) {
                     $o_home->SetShortUrl($o_row->home_short_url);
                 }
                 $o_match->SetHomeTeam($o_home);
                 unset($o_home);
             }
             if (isset($o_row->ground_id)) {
                 $o_ground = new Ground($this->GetSettings());
                 $o_ground->SetId($o_row->ground_id);
                 $o_match->SetGround($o_ground);
                 unset($o_ground);
             }
         }
         # Add away teams
         if (isset($o_row->away_team_id) && !$o_team_builder->IsDone($o_row->away_team_id)) {
             $o_away = new Team($this->o_settings);
             $o_away->SetId($o_row->away_team_id);
             if (isset($o_row->away_team_name)) {
                 $o_away->SetName($o_row->away_team_name);
             }
             if (isset($o_row->away_short_url)) {
                 $o_away->SetShortUrl($o_row->away_short_url);
             }
             $o_match->AddAwayTeam($o_away);
             unset($o_away);
         }
     }
     # Add final match
     if (isset($o_match)) {
         $this->Add($o_match);
     }
     return true;
 }
 /**
  * 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;
 }