IndexRenameController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Controllers\Table;
  4. use PhpMyAdmin\DatabaseInterface;
  5. use PhpMyAdmin\DbTableExists;
  6. use PhpMyAdmin\Index;
  7. use PhpMyAdmin\ResponseRenderer;
  8. use PhpMyAdmin\Table\Indexes;
  9. use PhpMyAdmin\Template;
  10. use PhpMyAdmin\Url;
  11. use PhpMyAdmin\Util;
  12. use function is_array;
  13. final class IndexRenameController extends AbstractController
  14. {
  15. /** @var DatabaseInterface */
  16. private $dbi;
  17. /** @var Indexes */
  18. private $indexes;
  19. public function __construct(
  20. ResponseRenderer $response,
  21. Template $template,
  22. string $db,
  23. string $table,
  24. DatabaseInterface $dbi,
  25. Indexes $indexes
  26. ) {
  27. parent::__construct($response, $template, $db, $table);
  28. $this->dbi = $dbi;
  29. $this->indexes = $indexes;
  30. }
  31. public function __invoke(): void
  32. {
  33. global $db, $table, $urlParams, $cfg, $errorUrl;
  34. if (! isset($_POST['create_edit_table'])) {
  35. Util::checkParameters(['db', 'table']);
  36. $urlParams = ['db' => $db, 'table' => $table];
  37. $errorUrl = Util::getScriptNameForOption($cfg['DefaultTabTable'], 'table');
  38. $errorUrl .= Url::getCommon($urlParams, '&');
  39. DbTableExists::check();
  40. }
  41. if (isset($_POST['index'])) {
  42. if (is_array($_POST['index'])) {
  43. // coming already from form
  44. $oldIndex = is_array($_POST['old_index']) ? $_POST['old_index']['Key_name'] : $_POST['old_index'];
  45. $index = clone $this->dbi->getTable($this->db, $this->table)->getIndex($oldIndex);
  46. $index->setName($_POST['index']['Key_name']);
  47. } else {
  48. $index = $this->dbi->getTable($this->db, $this->table)->getIndex($_POST['index']);
  49. }
  50. } else {
  51. $index = new Index();
  52. }
  53. if (isset($_POST['do_save_data'])) {
  54. $this->indexes->doSaveData($index, true, $this->db, $this->table);
  55. return;
  56. }
  57. $this->displayRenameForm($index);
  58. }
  59. /**
  60. * Display the rename form to rename an index
  61. *
  62. * @param Index $index An Index instance.
  63. */
  64. private function displayRenameForm(Index $index): void
  65. {
  66. $this->dbi->selectDb($GLOBALS['db']);
  67. $formParams = [
  68. 'db' => $this->db,
  69. 'table' => $this->table,
  70. ];
  71. if (isset($_POST['old_index'])) {
  72. $formParams['old_index'] = $_POST['old_index'];
  73. } elseif (isset($_POST['index'])) {
  74. $formParams['old_index'] = $_POST['index'];
  75. }
  76. $this->addScriptFiles(['indexes.js']);
  77. $this->render('table/index_rename_form', [
  78. 'index' => $index,
  79. 'form_params' => $formParams,
  80. ]);
  81. }
  82. }