/**
  * @return void
  * @desc Re-build from data posted by this control the data object this control is editing
  */
 public function BuildPostedDataObject()
 {
     $player = new Player($this->GetSettings());
     $key = $this->GetNamingPrefix() . 'item';
     if (isset($_POST[$key])) {
         $player->SetId($this->GetNamingPrefix() . $_POST[$key]);
     }
     $key = $this->GetNamingPrefix() . 'Name';
     if (isset($_POST[$key])) {
         $player->SetName($this->GetNamingPrefix() . $_POST[$key]);
     }
     $this->SetDataObject($player);
     $key = $this->GetNamingPrefix() . "MergeOptions";
     if (isset($_POST[$key]) and $_POST[$key] == "1") {
         $this->merge_requested = true;
     }
 }
 /**
  * (non-PHPdoc)
  * @see data/validation/DataValidator#Test($s_input, $a_keys)
  */
 public function Test($a_data, $a_keys)
 {
     /* Only way to be sure of testing name against what it will be matched against is to use the code that transforms it when saving */
     $player = new Player($this->GetSiteSettings());
     $player->SetName($a_data[$a_keys[0]]);
     $comparable_name = $player->GetComparableName();
     if ($comparable_name == "noballs") {
         return false;
     }
     if ($comparable_name == "wides") {
         return false;
     }
     if ($comparable_name == "byes") {
         return false;
     }
     if ($comparable_name == "bonusruns") {
         return false;
     }
     return true;
 }
 /**
  * @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);
 }
 /**
  * @access public
  * @return void
  * @param int[] $player_ids
  * @desc Delete from the database the players matching the supplied ids
  */
 public function Delete($player_ids)
 {
     # check parameter
     $this->ValidateNumericArray($player_ids);
     # Get tables
     $players = $this->GetSettings()->GetTable('Player');
     $matches = $this->GetSettings()->GetTable("Match");
     $batting = $this->GetSettings()->GetTable("Batting");
     $bowling = $this->GetSettings()->GetTable("Bowling");
     $stats = $this->GetSettings()->GetTable('PlayerMatch');
     $player_id_list = implode(', ', $player_ids);
     # delete from short URL cache
     require_once 'http/short-url-manager.class.php';
     $o_url_manager = new ShortUrlManager($this->GetSettings(), $this->GetDataConnection());
     $sql = "SELECT short_url FROM {$players} WHERE player_id IN ({$player_id_list})";
     $result = $this->GetDataConnection()->query($sql);
     while ($row = $result->fetch()) {
         $o_url_manager->Delete($row->short_url);
     }
     $result->closeCursor();
     unset($o_url_manager);
     # remove player of the match awards
     $sql = "UPDATE {$matches} SET player_of_match_id = NULL WHERE player_of_match_id IN ({$player_id_list})";
     $this->LoggedQuery($sql);
     $sql = "UPDATE {$matches} SET player_of_match_home_id = NULL WHERE player_of_match_home_id IN ({$player_id_list})";
     $this->LoggedQuery($sql);
     $sql = "UPDATE {$matches} SET player_of_match_away_id = NULL WHERE player_of_match_away_id IN ({$player_id_list})";
     $this->LoggedQuery($sql);
     # Reassign batting and bowling to 'unknown' player
     $unknown_player = new Player($this->GetSettings());
     $unknown_player->SetName("Unknown");
     foreach ($player_ids as $player_id) {
         $sql = "SELECT team_id FROM {$players} WHERE player_id  = " . Sql::ProtectNumeric($player_id);
         $result = $this->GetDataConnection()->query($sql);
         $row = $result->fetch();
         $unknown_player->Team()->SetId($row->team_id);
         $unknown_player->SetId($this->SaveOrMatchPlayer($unknown_player));
         $player = new Player($this->GetSettings());
         $player->SetId($player_id);
         $this->is_internal_delete = true;
         $this->MergePlayers($player, $unknown_player);
         $this->is_internal_delete = false;
     }
     # Delete statistics
     $sql = "DELETE FROM {$stats} WHERE player_id IN ({$player_id_list})";
     $this->LoggedQuery($sql);
     # delete the player
     $sql = "DELETE FROM {$players} WHERE player_id IN ({$player_id_list})";
     $this->LoggedQuery($sql);
 }
 /**
  * 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));
     }
 }
 /**
  * Read when and how often a player has played
  */
 public function ReadPlayerSummary()
 {
     $from = $this->FromFilteredPlayerStatistics() . " INNER JOIN nsa_team AS team ON nsa_player_match.team_id = team.team_id ";
     $where = "";
     $where = $this->ApplyFilters($where);
     if ($where) {
         $where = "WHERE " . substr($where, 3, strlen($where) - 3);
     }
     $sql = "SELECT player_id, player_name, player_role, player_url,\r\n\t\tteam.team_id, team.team_name, team.short_url AS team_short_url,\r\n\t\tCOUNT(nsa_player_match.match_id) AS total_matches, MIN(nsa_player_match.match_time) AS first_played, MAX(nsa_player_match.match_time) AS last_played\r\n\t\t{$from}\r\n\t\t{$where}\r\n\t\tGROUP BY nsa_player_match.player_id\r\n\t\tORDER BY player_name ASC";
     $data = array();
     $result = $this->GetDataConnection()->query($sql);
     if (!$this->GetDataConnection()->isError()) {
         while ($row = $result->fetch()) {
             $player = new Player($this->GetSettings());
             $player->SetId($row->player_id);
             $player->SetName($row->player_name);
             if (isset($row->total_matches)) {
                 $player->SetTotalMatches($row->total_matches);
             }
             if (isset($row->first_played)) {
                 $player->SetFirstPlayedDate($row->first_played);
             }
             if (isset($row->last_played)) {
                 $player->SetLastPlayedDate($row->last_played);
             }
             if (isset($row->player_role)) {
                 $player->SetPlayerRole($row->player_role);
             }
             if (isset($row->player_url)) {
                 $player->SetShortUrl($row->player_url);
             }
             $player->Team()->SetId($row->team_id);
             $player->Team()->SetName($row->team_name);
             if (isset($row->team_short_url)) {
                 $player->Team()->SetShortUrl($row->team_short_url);
             }
             $data[] = $player;
         }
     }
     $result->closeCursor();
     unset($result);
     return $data;
 }
 /**
  * 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;
 }