/**
  * Builds a match object containing the result information posted by the control
  *
  */
 public function BuildPostedDataObject()
 {
     $tournament = new Match($this->GetSettings());
     $tournament->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)) {
             $tournament->SetId($s_id);
         }
     }
     # Get the title
     $s_key = $this->GetNamingPrefix() . 'Title';
     if (isset($_POST[$s_key])) {
         $tournament->SetTitle(strip_tags($_POST[$s_key]));
     }
     # Get the start date, because it's part of the title that has to be recreated for an internal postback
     $s_key = $this->GetNamingPrefix() . 'Start';
     if (isset($_POST[$s_key])) {
         $tournament->SetStartTime($_POST[$s_key]);
     }
     # Matches - get from aggregated editor
     $this->EnsureAggregatedEditors();
     $matches = $this->matches_editor->DataObjects()->GetItems();
     foreach ($matches as $match) {
         $tournament->AddMatchInTournament($match);
     }
     $this->SetDataObject($tournament);
 }
 /**
  * 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);
     }
 }
 /**
  * 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]));
     }
     $s_key = $this->GetNamingPrefix() . 'Seasons';
     if (isset($_POST[$s_key]) and strlen($_POST[$s_key])) {
         $possibly_posted = explode(';', $_POST[$s_key]);
         foreach ($possibly_posted as $season_id) {
             $s_key = $this->GetNamingPrefix() . 'Season' . $season_id;
             if (isset($_POST[$s_key]) and $_POST[$s_key] == $season_id) {
                 $season = new Season($this->GetSettings());
                 $season->SetId($_POST[$s_key]);
                 $match->Seasons()->Add($season);
             }
         }
     }
     $this->SetDataObject($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);
 }
 /**
  * Helper to build basic info about a match from raw data
  *
  * @param Match $match
  * @param DataRow $row
  */
 private function BuildMatchSummary(Match $match, $row)
 {
     $match->SetId($row->match_id);
     if (isset($row->start_time)) {
         $match->SetStartTime($row->start_time);
     }
     if (isset($row->start_time_known)) {
         $match->SetIsStartTimeKnown($row->start_time_known);
     }
     if (isset($row->match_type)) {
         $match->SetMatchType($row->match_type);
     }
     if (isset($row->player_type_id)) {
         $match->SetPlayerType($row->player_type_id);
     }
     if (isset($row->qualification)) {
         $match->SetQualificationType($row->qualification);
     }
     if (isset($row->players_per_team)) {
         $match->SetMaximumPlayersPerTeam($row->players_per_team);
     }
     if (isset($row->max_tournament_teams)) {
         $match->SetMaximumTeamsInTournament($row->max_tournament_teams);
     }
     if (isset($row->tournament_spaces)) {
         $match->SetSpacesLeftInTournament($row->tournament_spaces);
     }
     if (isset($row->overs_per_innings)) {
         $match->SetOvers($row->overs_per_innings);
     }
     if (isset($row->match_title)) {
         $match->SetTitle($row->match_title);
     }
     if (isset($row->custom_title)) {
         $match->SetUseCustomTitle($row->custom_title);
     }
     if (isset($row->home_bat_first) and !is_null($row->home_bat_first)) {
         $match->Result()->SetHomeBattedFirst($row->home_bat_first);
     }
     if (isset($row->won_toss) and !is_null($row->won_toss)) {
         $match->Result()->SetTossWonBy($row->won_toss);
     }
     if (isset($row->home_runs)) {
         $match->Result()->SetHomeRuns($row->home_runs);
     }
     if (isset($row->home_wickets)) {
         $match->Result()->SetHomeWickets($row->home_wickets);
     }
     if (isset($row->away_runs)) {
         $match->Result()->SetAwayRuns($row->away_runs);
     }
     if (isset($row->away_wickets)) {
         $match->Result()->SetAwayWickets($row->away_wickets);
     }
     if (isset($row->home_points)) {
         $match->Result()->SetHomePoints($row->home_points);
     }
     if (isset($row->away_points)) {
         $match->Result()->SetAwayPoints($row->away_points);
     }
     if (isset($row->player_of_match_id)) {
         $player = new Player($this->GetSettings());
         $player->SetId($row->player_of_match_id);
         $player->SetName($row->player_of_match);
         $player->SetShortUrl($row->player_of_match_url);
         $player->Team()->SetId($row->player_of_match_team_id);
         $match->Result()->SetPlayerOfTheMatch($player);
     }
     if (isset($row->player_of_match_home_id)) {
         $player = new Player($this->GetSettings());
         $player->SetId($row->player_of_match_home_id);
         $player->SetName($row->player_of_match_home);
         $player->SetShortUrl($row->player_of_match_home_url);
         $player->Team()->SetId($row->player_of_match_home_team_id);
         $match->Result()->SetPlayerOfTheMatchHome($player);
     }
     if (isset($row->player_of_match_away_id)) {
         $player = new Player($this->GetSettings());
         $player->SetId($row->player_of_match_away_id);
         $player->SetName($row->player_of_match_away);
         $player->SetShortUrl($row->player_of_match_away_url);
         $player->Team()->SetId($row->player_of_match_away_team_id);
         $match->Result()->SetPlayerOfTheMatchAway($player);
     }
     if (isset($row->match_notes)) {
         $match->SetNotes($row->match_notes);
     }
     if (isset($row->short_url)) {
         $match->SetShortUrl($row->short_url);
     }
     if (isset($row->update_search) and $row->update_search == 1) {
         $match->SetSearchUpdateRequired();
     }
     if (isset($row->added_by) and is_numeric($row->added_by)) {
         $match->SetAddedBy(new User($row->added_by));
     }
     if (isset($row->match_result_id)) {
         $match->Result()->SetResultType($row->match_result_id);
     }
     if (isset($row->date_changed)) {
         $match->SetLastAudit(new AuditData($row->modified_by_id, $row->known_as, $row->date_changed));
     }
 }
 /**
  * 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;
 }