private function IndexPlayers()
 {
     require_once "data/process-manager.class.php";
     $this->process = new ProcessManager("players", 500);
     if ($this->process->ReadyToDeleteAll()) {
         $this->SearchIndexer()->DeleteFromIndexByType("player");
     }
     # Get all players, but exclude unused extras
     $player = $this->GetSettings()->GetTable('Player');
     $player_batch = $this->GetDataConnection()->query("SELECT player_id FROM {$player} WHERE total_matches >= 1 ORDER BY player_id" . $this->process->GetQueryLimit());
     $player_ids = array();
     while ($row = $player_batch->fetch()) {
         $player_ids[] = $row->player_id;
     }
     if (count($player_ids)) {
         require_once 'stoolball/player-manager.class.php';
         require_once 'search/player-search-adapter.class.php';
         $manager = new PlayerManager($this->GetSettings(), $this->GetDataConnection());
         foreach ($player_ids as $player_id) {
             $manager->ReadPlayerById($player_id);
             $players = $manager->GetItems();
             foreach ($players as $player) {
                 $adapter = new PlayerSearchAdapter($player);
                 $this->SearchIndexer()->Index($adapter->GetSearchableItem());
                 $this->process->OneMoreDone();
             }
         }
         $this->SearchIndexer()->CommitChanges();
         unset($manager);
     }
 }
 public function OnLoadPageData()
 {
     # Always get the player's unfiltered profile, because it's needed for the page description
     require_once "stoolball/player-manager.class.php";
     $player_manager = new PlayerManager($this->GetSettings(), $this->GetDataConnection());
     $player_manager->ReadPlayerById($this->player->GetId());
     $this->player_unfiltered = $player_manager->GetFirst();
     if (!$this->player_unfiltered instanceof Player) {
         http_response_code(404);
         $this->not_found = true;
         return;
     }
     # Update search engine
     if ($this->player_unfiltered->GetSearchUpdateRequired()) {
         require_once "search/player-search-adapter.class.php";
         $this->SearchIndexer()->DeleteFromIndexById("player" . $this->player->GetId());
         $adapter = new PlayerSearchAdapter($this->player_unfiltered);
         $this->SearchIndexer()->Index($adapter->GetSearchableItem());
         $this->SearchIndexer()->CommitChanges();
         $player_manager->SearchUpdated($this->player->GetId());
     }
     unset($player_manager);
     # Check first for a player created using 'add player', who hasn't played yet
     if ($this->player_unfiltered->GetTotalMatches() == 0) {
         $this->player = $this->player_unfiltered;
     } else {
         # Now get statistics for the player
         $statistics_manager = new StatisticsManager($this->GetSettings(), $this->GetDataConnection());
         $statistics_manager->FilterByPlayer(array($this->player->GetId()));
         # Apply filters common to all statistics
         $this->filter_control = new StatisticsFilterControl();
         $filter_batting_position = StatisticsFilter::SupportBattingPositionFilter($statistics_manager);
         $this->filter_control->SupportBattingPositionFilter($filter_batting_position);
         $this->filter .= $filter_batting_position[2];
         $filter_match_type = StatisticsFilter::SupportMatchTypeFilter($statistics_manager);
         $this->filter_control->SupportMatchTypeFilter($filter_match_type);
         $this->filter .= $filter_match_type[2];
         $filter_opposition = StatisticsFilter::SupportOppositionFilter($statistics_manager);
         $this->filter_control->SupportOppositionFilter($filter_opposition);
         $this->filter .= $filter_opposition[2];
         $filter_competition = StatisticsFilter::SupportCompetitionFilter($statistics_manager);
         $this->filter_control->SupportCompetitionFilter($filter_competition);
         $this->filter .= $filter_competition[2];
         $this->filter .= StatisticsFilter::ApplySeasonFilter($this->GetSettings(), $this->GetDataConnection(), $statistics_manager);
         $filter_ground = StatisticsFilter::SupportGroundFilter($statistics_manager);
         $this->filter_control->SupportGroundFilter($filter_ground);
         $this->filter .= $filter_ground[2];
         $filter_date = StatisticsFilter::SupportDateFilter($statistics_manager);
         if (!is_null($filter_date[0])) {
             $this->filter_control->SupportAfterDateFilter($filter_date[0]);
         }
         if (!is_null($filter_date[1])) {
             $this->filter_control->SupportBeforeDateFilter($filter_date[1]);
         }
         $this->filter .= $filter_date[2];
         $filter_innings = StatisticsFilter::SupportInningsFilter($statistics_manager);
         $this->filter_control->SupportInningsFilter($filter_innings[1]);
         $this->filter .= $filter_innings[2];
         $filter_won_match = StatisticsFilter::SupportMatchResultFilter($statistics_manager);
         $this->filter_control->SupportMatchResultFilter($filter_won_match[1]);
         $this->filter .= $filter_won_match[2];
         # Now get the statistics for the player
         $data = $statistics_manager->ReadPlayerSummary();
         if (count($data)) {
             $this->player = $data[0];
         } else {
             if ($this->filter) {
                 # If no matches matched the filter, ensure we have the player's name and team
                 $this->player = $this->player_unfiltered;
                 $this->filter_matched_nothing = true;
             } else {
                 $this->regenerating = true;
             }
         }
         $data = $statistics_manager->ReadBestBattingPerformance(false);
         foreach ($data as $performance) {
             $batting = new Batting($this->player, $performance["how_out"], null, null, $performance["runs_scored"], $performance["balls_faced"]);
             $this->player->Batting()->Add($batting);
         }
         if ($this->player->GetPlayerRole() == Player::PLAYER) {
             $data = $statistics_manager->ReadBestPlayerAggregate("player_of_match");
             $this->player->SetTotalPlayerOfTheMatchNominations(count($data) ? $data[0]["statistic"] : 0);
         }
         unset($statistics_manager);
     }
 }
 /**
  * @return void
  * @param Match $match
  * @param bool $is_first_innings
  * @desc Saves the batting and bowling scorecards for one innings
  */
 public function SaveScorecard(Match $match, $is_first_innings, ISearchIndexProvider $search)
 {
     # To add a scorecard there must always already be a match to update
     if (!$match->GetId()) {
         return;
     }
     # This isn't for tournaments
     if ($match->GetMatchType() == MatchType::TOURNAMENT) {
         return;
     }
     # Get tables
     $batting_table = $this->GetSettings()->GetTable("Batting");
     $bowling_table = $this->GetSettings()->GetTable("Bowling");
     $match_table = $this->GetSettings()->GetTable('Match');
     $mt = $this->GetSettings()->GetTable('MatchTeam');
     # Is this scorecard for the home or the away innings?
     $sql = "SELECT home_bat_first FROM {$match_table} WHERE match_id = " . Sql::ProtectNumeric($match->GetId());
     $result = $this->GetDataConnection()->query($sql);
     $row = $result->fetch();
     if (is_null($row->home_bat_first) or (bool) $row->home_bat_first === true) {
         $is_home_innings = $is_first_innings;
     } else {
         $is_home_innings = !$is_first_innings;
     }
     $result->closeCursor();
     # Prepare data for query
     if ($is_home_innings) {
         $bowling_team_id = $match->GetAwayTeamId();
         $batting_team_id = $match->GetHomeTeamId();
         $team_bowling = $match->Result()->AwayOvers();
         $team_batting = $match->Result()->HomeBatting();
     } else {
         $bowling_team_id = $match->GetHomeTeamId();
         $batting_team_id = $match->GetAwayTeamId();
         $team_bowling = $match->Result()->HomeOvers();
         $team_batting = $match->Result()->AwayBatting();
     }
     # Find the match_team_id for the bowling
     $sql = "SELECT match_team_id FROM {$mt}\r\n\t\t\t\tWHERE match_id " . Sql::ProtectNumeric($match->GetId(), false, true) . "\r\n\t\t\t\tAND team_id " . Sql::ProtectNumeric($bowling_team_id, false, true) . "\r\n\t\t\t\tAND team_role = " . ($is_home_innings ? TeamRole::Away() : TeamRole::Home());
     $result = $this->GetDataConnection()->query($sql);
     $row = $result->fetch();
     $bowling_match_team_id = $row->match_team_id;
     $result->closeCursor();
     # Find the match_team_id for the batting
     $sql = "SELECT match_team_id FROM {$mt}\r\n\t\t\t\tWHERE match_id " . Sql::ProtectNumeric($match->GetId(), false, true) . "\r\n\t\t\t\tAND team_id " . Sql::ProtectNumeric($batting_team_id, false, true) . "\r\n\t\t\t\tAND team_role = " . ($is_home_innings ? TeamRole::Home() : TeamRole::Away());
     $result = $this->GetDataConnection()->query($sql);
     $row = $result->fetch();
     $batting_match_team_id = $row->match_team_id;
     $result->closeCursor();
     $affected_players = $this->SaveBattingScorecard($match, $is_home_innings, $batting_match_team_id, $team_batting);
     $affected_bowlers = $this->SaveBowlingScorecard($match, $is_home_innings, $batting_match_team_id, $bowling_match_team_id, $team_bowling);
     $affected_players = array_merge($affected_players, $affected_bowlers);
     $affected_players = array_unique($affected_players);
     if (count($affected_players)) {
         require_once 'stoolball/statistics/statistics-manager.class.php';
         $statistics_manager = new StatisticsManager($this->GetSettings(), $this->GetDataConnection());
         # generate player statistics from the data entered
         $statistics_manager->UpdateBattingStatistics($affected_players, array($batting_match_team_id));
         $statistics_manager->UpdateFieldingStatistics($affected_players, array($bowling_match_team_id));
         $statistics_manager->UpdateBowlingStatistics($affected_players, array($bowling_match_team_id));
         $statistics_manager->DeleteObsoleteStatistics($match->GetId());
         # update overall stats for players
         $statistics_manager->UpdatePlayerStatistics($affected_players);
         unset($statistics_manager);
         # update search engine
         require_once "stoolball/player-manager.class.php";
         require_once "search/player-search-adapter.class.php";
         $player_manager = new PlayerManager($this->GetSettings(), $this->GetDataConnection());
         foreach ($affected_players as $player_id) {
             $player_manager->ReadPlayerById($player_id);
             $player = $player_manager->GetFirst();
             $search->DeleteFromIndexById("player" . $player_id);
             if ($player instanceof Player) {
                 $adapter = new PlayerSearchAdapter($player);
                 $search->Index($adapter->GetSearchableItem());
             }
         }
         $search->CommitChanges();
         unset($player_manager);
     }
 }
 public function UpdatePlayer()
 {
     # If the player data is valid, save and redirect back to player
     $this->player = $this->editor->GetDataObject();
     if ($this->editor->CancelClicked()) {
         $this->player_manager->ReadPlayerById($this->player->GetId());
         $this->player = $this->player_manager->GetFirst();
         $this->Redirect($this->player->GetPlayerUrl());
     }
     if ($this->IsValid()) {
         # First check in the database that this is not an extras player that can't be renamed
         $this->player_manager->ReadPlayerById($this->player->GetId());
         $player_to_edit = $this->player_manager->GetFirst();
         if ($player_to_edit->GetPlayerRole() != Player::PLAYER) {
             http_response_code(401);
             return;
         }
         # Now check again, because it's a new request, that the user has permission
         $this->CheckForPermission($player_to_edit->Team());
         # Get the existing player for their team, change the name and see if the renamed player
         # already exists and is different from the one being edited
         $player_to_edit->SetName($this->player->GetName());
         $this->player_manager->MatchExistingPlayer($player_to_edit);
         if ($player_to_edit->GetId() and $player_to_edit->GetId() != $this->player->GetId()) {
             if ($this->editor->IsMergeRequested()) {
                 $this->player_manager->MergePlayers($this->player, $player_to_edit);
                 # Remove details of both players from the search engine.
                 $this->SearchIndexer()->DeleteFromIndexById("player" . $this->player->GetId());
                 $this->SearchIndexer()->DeleteFromIndexById("player" . $player_to_edit->GetId());
                 # Re-request player to get details for search engine, and player URL to redirect to
                 require_once "search/player-search-adapter.class.php";
                 $this->player_manager->ReadPlayerById($player_to_edit->GetId());
                 $this->player = $this->player_manager->GetFirst();
                 $adapter = new PlayerSearchAdapter($this->player);
                 $this->SearchIndexer()->Index($adapter->GetSearchableItem());
                 $this->SearchIndexer()->CommitChanges();
                 unset($player_manager);
                 $this->Redirect($this->player->GetPlayerUrl());
             } else {
                 $this->editor->SetCurrentPage(PlayerEditor::MERGE_PLAYER);
             }
         } else {
             # Set the team short URL so that it can be used to regenerate the player's short URL
             $this->player->Team()->SetShortUrl($player_to_edit->Team()->GetShortUrl());
             $this->player_manager->SavePlayer($this->player);
             unset($player_manager);
             $this->Redirect($this->player->GetPlayerUrl());
         }
     }
 }