TableIndexesController.class.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Holds the PMA\TableIndexesController
  5. *
  6. * @package PMA
  7. */
  8. namespace PMA\Controllers\Table;
  9. use PMA\Controllers\TableController;
  10. use PMA_Index;
  11. use PMA_Message;
  12. use PMA_Response;
  13. use PMA\Template;
  14. use PMA_Util;
  15. require_once 'libraries/Index.class.php';
  16. require_once 'libraries/Message.class.php';
  17. require_once 'libraries/Util.class.php';
  18. require_once 'libraries/Index.class.php';
  19. require_once 'libraries/controllers/TableController.class.php';
  20. require_once 'libraries/Template.class.php';
  21. /**
  22. * Class TableIndexesController
  23. *
  24. * @package PMA\Controllers\Table
  25. */
  26. class TableIndexesController extends TableController
  27. {
  28. /**
  29. * @var PMA_Index $index
  30. */
  31. protected $index;
  32. /**
  33. * Constructor
  34. *
  35. * @param PMA_Index $index Index
  36. */
  37. public function __construct($index)
  38. {
  39. parent::__construct();
  40. $this->index = $index;
  41. }
  42. /**
  43. * Index
  44. *
  45. * @return void
  46. */
  47. public function indexAction()
  48. {
  49. if (isset($_REQUEST['do_save_data'])) {
  50. $this->doSaveDataAction();
  51. return;
  52. } // end builds the new index
  53. $this->displayFormAction();
  54. }
  55. /**
  56. * Display the form to edit/create an index
  57. *
  58. * @return void
  59. */
  60. public function displayFormAction()
  61. {
  62. include_once 'libraries/tbl_info.inc.php';
  63. $add_fields = 0;
  64. if (isset($_REQUEST['index']) && is_array($_REQUEST['index'])) {
  65. // coming already from form
  66. if (isset($_REQUEST['index']['columns']['names'])) {
  67. $add_fields = count($_REQUEST['index']['columns']['names'])
  68. - $this->index->getColumnCount();
  69. }
  70. if (isset($_REQUEST['add_fields'])) {
  71. $add_fields += $_REQUEST['added_fields'];
  72. }
  73. } elseif (isset($_REQUEST['create_index'])) {
  74. $add_fields = $_REQUEST['added_fields'];
  75. } // end preparing form values
  76. // Get fields and stores their name/type
  77. if (isset($_REQUEST['create_edit_table'])) {
  78. $fields = json_decode($_REQUEST['columns'], true);
  79. $index_params = array(
  80. 'Non_unique' => ($_REQUEST['index']['Index_choice'] == 'UNIQUE')
  81. ? '0' : '1',
  82. );
  83. $this->index->set($index_params);
  84. $add_fields = count($fields);
  85. } else {
  86. $fields = $this->dbi->getTable($this->db, $this->table)
  87. ->getNameAndTypeOfTheColumns();
  88. }
  89. $form_params = array(
  90. 'db' => $this->db,
  91. 'table' => $this->table,
  92. );
  93. if (isset($_REQUEST['create_index'])) {
  94. $form_params['create_index'] = 1;
  95. } elseif (isset($_REQUEST['old_index'])) {
  96. $form_params['old_index'] = $_REQUEST['old_index'];
  97. } elseif (isset($_REQUEST['index'])) {
  98. $form_params['old_index'] = $_REQUEST['index'];
  99. }
  100. $this->response->getHeader()->getScripts()->addFile('indexes.js');
  101. $this->response->addHTML(
  102. Template::get('table/index_form')->render(
  103. array(
  104. 'fields' => $fields,
  105. 'index' => $this->index,
  106. 'form_params' => $form_params,
  107. 'add_fields' => $add_fields
  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($_REQUEST['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. if ($GLOBALS['is_ajax_request'] == true) {
  138. $message = PMA_Message::success(
  139. __('Table %1$s has been altered successfully.')
  140. );
  141. $message->addParam($this->table);
  142. $this->response->addJSON(
  143. 'message', PMA_Util::getMessage($message, $sql_query, 'success')
  144. );
  145. $this->response->addJSON(
  146. 'index_table',
  147. PMA_Index::getHtmlForIndexes(
  148. $this->table, $this->db
  149. )
  150. );
  151. } else {
  152. include 'tbl_structure.php';
  153. }
  154. } else {
  155. $this->response->isSuccess(false);
  156. $this->response->addJSON('message', $error);
  157. }
  158. }
  159. }