2 /* vim: set expandtab sw=4 ts=4 sts=4: */
5 * Holds the PMA\TableIndexesController
10 namespace PMA\Controllers\Table
;
12 use PMA\Controllers\TableController
;
19 require_once 'libraries/Index.class.php';
20 require_once 'libraries/Message.class.php';
21 require_once 'libraries/Util.class.php';
22 require_once 'libraries/Index.class.php';
23 require_once 'libraries/controllers/TableController.class.php';
24 require_once 'libraries/Template.class.php';
27 * Class TableIndexesController
29 * @package PMA\Controllers\Table
31 class TableIndexesController
extends TableController
34 * @var PMA_Index $index
41 * @param PMA_Index $index Index
43 public function __construct($index)
45 parent
::__construct();
47 $this->index
= $index;
55 public function indexAction()
57 if (isset($_REQUEST['do_save_data'])) {
58 $this->doSaveDataAction();
60 } // end builds the new index
62 $this->displayFormAction();
66 * Display the form to edit/create an index
70 public function displayFormAction()
72 include_once 'libraries/tbl_info.inc.php';
75 if (isset($_REQUEST['index']) && is_array($_REQUEST['index'])) {
76 // coming already from form
77 if (isset($_REQUEST['index']['columns']['names'])) {
78 $add_fields = count($_REQUEST['index']['columns']['names'])
79 - $this->index
->getColumnCount();
81 if (isset($_REQUEST['add_fields'])) {
82 $add_fields +
= $_REQUEST['added_fields'];
84 } elseif (isset($_REQUEST['create_index'])) {
85 $add_fields = $_REQUEST['added_fields'];
86 } // end preparing form values
88 // Get fields and stores their name/type
89 if (isset($_REQUEST['create_edit_table'])) {
90 $fields = json_decode($_REQUEST['columns'], true);
91 $index_params = array(
92 'Non_unique' => ($_REQUEST['index']['Index_choice'] == 'UNIQUE')
95 $this->index
->set($index_params);
96 $add_fields = count($fields);
98 $fields = $this->dbi
->getTable($this->db
, $this->table
)
99 ->getNameAndTypeOfTheColumns();
102 $form_params = array(
104 'table' => $this->table
,
107 if (isset($_REQUEST['create_index'])) {
108 $form_params['create_index'] = 1;
109 } elseif (isset($_REQUEST['old_index'])) {
110 $form_params['old_index'] = $_REQUEST['old_index'];
111 } elseif (isset($_REQUEST['index'])) {
112 $form_params['old_index'] = $_REQUEST['index'];
115 $this->response
->getHeader()->getScripts()->addFile('indexes.js');
117 $this->response
->addHTML(
118 Template
::get('table/index_form')->render(
121 'index' => $this->index
,
122 'form_params' => $form_params,
123 'add_fields' => $add_fields
130 * Process the data from the edit/create index form,
131 * run the query to build the new index
132 * and moves back to "tbl_sql.php"
136 public function doSaveDataAction()
140 $sql_query = $this->dbi
->getTable($this->db
, $this->table
)
141 ->getSqlQueryForIndexCreateOrEdit($this->index
, $error);
143 // If there is a request for SQL previewing.
144 if (isset($_REQUEST['preview_sql'])) {
146 $this->response
->addJSON(
148 Template
::get('preview_sql')
151 'query_data' => $sql_query
157 $this->dbi
->query($sql_query);
158 if ($GLOBALS['is_ajax_request'] == true) {
159 $message = PMA_Message
::success(
160 __('Table %1$s has been altered successfully.')
162 $message->addParam($this->table
);
163 $this->response
->addJSON(
164 'message', PMA_Util
::getMessage($message, $sql_query, 'success')
166 $this->response
->addJSON(
168 PMA_Index
::getHtmlForIndexes(
169 $this->table
, $this->db
173 include 'tbl_structure.php';
176 $this->response
->isSuccess(false);
177 $this->response
->addJSON('message', $error);