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());
         }
     }
 }