TableIndexesController.php 5.0 KB

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