IndexesController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Holds the PhpMyAdmin\Controllers\Table\IndexesController
  5. *
  6. * @package PhpMyAdmin\Controllers
  7. */
  8. declare(strict_types=1);
  9. namespace PhpMyAdmin\Controllers\Table;
  10. use PhpMyAdmin\DatabaseInterface;
  11. use PhpMyAdmin\Index;
  12. use PhpMyAdmin\Message;
  13. use PhpMyAdmin\Response;
  14. use PhpMyAdmin\Template;
  15. use PhpMyAdmin\Util;
  16. /**
  17. * Class IndexesController
  18. *
  19. * @package PhpMyAdmin\Controllers
  20. */
  21. class IndexesController extends AbstractController
  22. {
  23. /**
  24. * @var Index
  25. */
  26. protected $index;
  27. /**
  28. * Constructor
  29. *
  30. * @param Response $response Response object
  31. * @param DatabaseInterface $dbi DatabaseInterface object
  32. * @param Template $template Template object
  33. * @param string $db Database name
  34. * @param string $table Table name
  35. * @param Index $index Index object
  36. */
  37. public function __construct(
  38. $response,
  39. $dbi,
  40. Template $template,
  41. $db,
  42. $table,
  43. $index
  44. ) {
  45. parent::__construct($response, $dbi, $template, $db, $table);
  46. $this->index = $index;
  47. }
  48. /**
  49. * Index
  50. *
  51. * @return void
  52. */
  53. public function indexAction()
  54. {
  55. if (isset($_POST['do_save_data'])) {
  56. $this->doSaveDataAction();
  57. return;
  58. } // end builds the new index
  59. $this->displayFormAction();
  60. }
  61. /**
  62. * Display the form to edit/create an index
  63. *
  64. * @return void
  65. */
  66. public function displayFormAction()
  67. {
  68. $this->dbi->selectDb($GLOBALS['db']);
  69. $add_fields = 0;
  70. if (isset($_POST['index']) && is_array($_POST['index'])) {
  71. // coming already from form
  72. if (isset($_POST['index']['columns']['names'])) {
  73. $add_fields = count($_POST['index']['columns']['names'])
  74. - $this->index->getColumnCount();
  75. }
  76. if (isset($_POST['add_fields'])) {
  77. $add_fields += $_POST['added_fields'];
  78. }
  79. } elseif (isset($_POST['create_index'])) {
  80. $add_fields = $_POST['added_fields'];
  81. } // end preparing form values
  82. // Get fields and stores their name/type
  83. if (isset($_POST['create_edit_table'])) {
  84. $fields = json_decode($_POST['columns'], true);
  85. $index_params = [
  86. 'Non_unique' => $_POST['index']['Index_choice'] == 'UNIQUE'
  87. ? '0' : '1',
  88. ];
  89. $this->index->set($index_params);
  90. $add_fields = count($fields);
  91. } else {
  92. $fields = $this->dbi->getTable($this->db, $this->table)
  93. ->getNameAndTypeOfTheColumns();
  94. }
  95. $form_params = [
  96. 'db' => $this->db,
  97. 'table' => $this->table,
  98. ];
  99. if (isset($_POST['create_index'])) {
  100. $form_params['create_index'] = 1;
  101. } elseif (isset($_POST['old_index'])) {
  102. $form_params['old_index'] = $_POST['old_index'];
  103. } elseif (isset($_POST['index'])) {
  104. $form_params['old_index'] = $_POST['index'];
  105. }
  106. $this->response->getHeader()->getScripts()->addFile('indexes.js');
  107. $this->response->addHTML(
  108. $this->template->render('table/index_form', [
  109. 'fields' => $fields,
  110. 'index' => $this->index,
  111. 'form_params' => $form_params,
  112. 'add_fields' => $add_fields,
  113. 'create_edit_table' => isset($_POST['create_edit_table']),
  114. 'default_sliders_state' => $GLOBALS['cfg']['InitialSlidersState'],
  115. ])
  116. );
  117. }
  118. /**
  119. * Process the data from the edit/create index form,
  120. * run the query to build the new index
  121. * and moves back to "tbl_sql.php"
  122. *
  123. * @return void
  124. */
  125. public function doSaveDataAction()
  126. {
  127. $error = false;
  128. $sql_query = $this->dbi->getTable($this->db, $this->table)
  129. ->getSqlQueryForIndexCreateOrEdit($this->index, $error);
  130. // If there is a request for SQL previewing.
  131. if (isset($_POST['preview_sql'])) {
  132. $this->response->addJSON(
  133. 'sql_data',
  134. $this->template->render('preview_sql', ['query_data' => $sql_query])
  135. );
  136. } elseif (! $error) {
  137. $this->dbi->query($sql_query);
  138. $response = Response::getInstance();
  139. if ($response->isAjax()) {
  140. $message = Message::success(
  141. __('Table %1$s has been altered successfully.')
  142. );
  143. $message->addParam($this->table);
  144. $this->response->addJSON(
  145. 'message',
  146. Util::getMessage($message, $sql_query, 'success')
  147. );
  148. $this->response->addJSON(
  149. 'index_table',
  150. Index::getHtmlForIndexes(
  151. $this->table,
  152. $this->db
  153. )
  154. );
  155. } else {
  156. include ROOT_PATH . 'tbl_structure.php';
  157. }
  158. } else {
  159. $this->response->setRequestStatus(false);
  160. $this->response->addJSON('message', $error);
  161. }
  162. }
  163. }