Minor improvement to prior commit.
[openemr.git] / phpmyadmin / libraries / controllers / TableIndexesController.class.php
blob6d29877ee039b4ea6dc30e5d265e669b72a0c4e5
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 /**
5 * Holds the PMA\TableIndexesController
7 * @package PMA
8 */
10 namespace PMA\Controllers\Table;
12 use PMA\Controllers\TableController;
13 use PMA_Index;
14 use PMA_Message;
15 use PMA_Response;
16 use PMA\Template;
17 use PMA_Util;
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';
26 /**
27 * Class TableIndexesController
29 * @package PMA\Controllers\Table
31 class TableIndexesController extends TableController
33 /**
34 * @var PMA_Index $index
36 protected $index;
38 /**
39 * Constructor
41 * @param PMA_Index $index Index
43 public function __construct($index)
45 parent::__construct();
47 $this->index = $index;
50 /**
51 * Index
53 * @return void
55 public function indexAction()
57 if (isset($_REQUEST['do_save_data'])) {
58 $this->doSaveDataAction();
59 return;
60 } // end builds the new index
62 $this->displayFormAction();
65 /**
66 * Display the form to edit/create an index
68 * @return void
70 public function displayFormAction()
72 include_once 'libraries/tbl_info.inc.php';
74 $add_fields = 0;
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')
93 ? '0' : '1',
95 $this->index->set($index_params);
96 $add_fields = count($fields);
97 } else {
98 $fields = $this->dbi->getTable($this->db, $this->table)
99 ->getNameAndTypeOfTheColumns();
102 $form_params = array(
103 'db' => $this->db,
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(
119 array(
120 'fields' => $fields,
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"
134 * @return void
136 public function doSaveDataAction()
138 $error = false;
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(
147 'sql_data',
148 Template::get('preview_sql')
149 ->render(
150 array(
151 'query_data' => $sql_query
155 } elseif (!$error) {
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(
167 'index_table',
168 PMA_Index::getHtmlForIndexes(
169 $this->table, $this->db
172 } else {
173 include 'tbl_structure.php';
175 } else {
176 $this->response->isSuccess(false);
177 $this->response->addJSON('message', $error);