acknowledgments update
[openemr.git] / phpmyadmin / tbl_indexes.php
bloba778814c32102aeb3573a0a7570625e8c5367ff4
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Displays index edit/creation form and handles it
6 * @package PhpMyAdmin
7 */
9 /**
10 * Gets some core libraries
12 require_once 'libraries/common.inc.php';
13 require_once 'libraries/Index.class.php';
14 require_once 'libraries/tbl_common.inc.php';
16 // Get fields and stores their name/type
17 $fields = array();
18 foreach (PMA_DBI_get_columns_full($db, $table) as $row) {
19 if (preg_match('@^(set|enum)\((.+)\)$@i', $row['Type'], $tmp)) {
20 $tmp[2] = substr(
21 preg_replace('@([^,])\'\'@', '\\1\\\'', ',' . $tmp[2]), 1
23 $fields[$row['Field']] = $tmp[1] . '('
24 . str_replace(',', ', ', $tmp[2]) . ')';
25 } else {
26 $fields[$row['Field']] = $row['Type'];
28 } // end while
30 // Prepares the form values
31 if (isset($_REQUEST['index'])) {
32 if (is_array($_REQUEST['index'])) {
33 // coming already from form
34 $index = new PMA_Index($_REQUEST['index']);
35 } else {
36 $index = PMA_Index::singleton($db, $table, $_REQUEST['index']);
38 } else {
39 $index = new PMA_Index;
42 /**
43 * Process the data from the edit/create index form,
44 * run the query to build the new index
45 * and moves back to "tbl_sql.php"
47 if (isset($_REQUEST['do_save_data'])) {
48 $error = false;
50 // $sql_query is the one displayed in the query box
51 $sql_query = 'ALTER TABLE ' . PMA_Util::backquote($db)
52 . '.' . PMA_Util::backquote($table);
54 // Drops the old index
55 if (! empty($_REQUEST['old_index'])) {
56 if ($_REQUEST['old_index'] == 'PRIMARY') {
57 $sql_query .= ' DROP PRIMARY KEY,';
58 } else {
59 $sql_query .= ' DROP INDEX '
60 . PMA_Util::backquote($_REQUEST['old_index']) . ',';
62 } // end if
64 // Builds the new one
65 switch ($index->getType()) {
66 case 'PRIMARY':
67 if ($index->getName() == '') {
68 $index->setName('PRIMARY');
69 } elseif ($index->getName() != 'PRIMARY') {
70 $error = PMA_Message::error(
71 __('The name of the primary key must be "PRIMARY"!')
74 $sql_query .= ' ADD PRIMARY KEY';
75 break;
76 case 'FULLTEXT':
77 case 'UNIQUE':
78 case 'INDEX':
79 case 'SPATIAL':
80 if ($index->getName() == 'PRIMARY') {
81 $error = PMA_Message::error(__('Can\'t rename index to PRIMARY!'));
83 $sql_query .= ' ADD ' . $index->getType() . ' '
84 . ($index->getName() ? PMA_Util::backquote($index->getName()) : '');
85 break;
86 } // end switch
88 $index_fields = array();
89 foreach ($index->getColumns() as $key => $column) {
90 $index_fields[$key] = PMA_Util::backquote($column->getName());
91 if ($column->getSubPart()) {
92 $index_fields[$key] .= '(' . $column->getSubPart() . ')';
94 } // end while
96 if (empty($index_fields)) {
97 $error = PMA_Message::error(__('No index parts defined!'));
98 } else {
99 $sql_query .= ' (' . implode(', ', $index_fields) . ')';
102 if (PMA_MYSQL_INT_VERSION > 50500) {
103 $sql_query .= "COMMENT '"
104 . PMA_Util::sqlAddSlashes($index->getComment())
105 . "'";
107 $sql_query .= ';';
109 if (! $error) {
110 PMA_DBI_query($sql_query);
111 $message = PMA_Message::success(
112 __('Table %1$s has been altered successfully')
114 $message->addParam($table);
116 if ($GLOBALS['is_ajax_request'] == true) {
117 $response = PMA_Response::getInstance();
118 $response->addJSON('message', $message);
119 $response->addJSON('index_table', PMA_Index::getView($table, $db));
120 $response->addJSON(
121 'sql_query',
122 PMA_Util::getMessage(null, $sql_query)
124 } else {
125 $active_page = 'tbl_structure.php';
126 include 'tbl_structure.php';
128 exit;
129 } else {
130 if ($GLOBALS['is_ajax_request'] == true) {
131 $response = PMA_Response::getInstance();
132 $response->isSuccess(false);
133 $response->addJSON('message', $error);
134 exit;
135 } else {
136 $error->display();
139 } // end builds the new index
143 * Display the form to edit/create an index
146 // Displays headers (if needed)
147 $response = PMA_Response::getInstance();
148 $header = $response->getHeader();
149 $scripts = $header->getScripts();
150 $scripts->addFile('indexes.js');
151 require_once 'libraries/tbl_info.inc.php';
153 if (isset($_REQUEST['index']) && is_array($_REQUEST['index'])) {
154 // coming already from form
155 $add_fields
156 = count($_REQUEST['index']['columns']['names']) - $index->getColumnCount();
157 if (isset($_REQUEST['add_fields'])) {
158 $add_fields += $_REQUEST['added_fields'];
160 } elseif (isset($_REQUEST['create_index'])) {
161 $add_fields = $_REQUEST['added_fields'];
162 } else {
163 $add_fields = 1;
166 // end preparing form values
169 <form action="tbl_indexes.php" method="post" name="index_frm" id="index_frm" class="ajax"
170 onsubmit="if (typeof(this.elements['index[Key_name]'].disabled) != 'undefined') {
171 this.elements['index[Key_name]'].disabled = false}">
172 <?php
173 $form_params = array(
174 'db' => $db,
175 'table' => $table,
178 if (isset($_REQUEST['create_index'])) {
179 $form_params['create_index'] = 1;
180 } elseif (isset($_REQUEST['old_index'])) {
181 $form_params['old_index'] = $_REQUEST['old_index'];
182 } elseif (isset($_REQUEST['index'])) {
183 $form_params['old_index'] = $_REQUEST['index'];
186 echo PMA_generate_common_hidden_inputs($form_params);
188 <fieldset id="index_edit_fields">
189 <?php
190 if ($GLOBALS['is_ajax_request'] != true) {
192 <legend>
193 <?php
194 if (isset($_REQUEST['create_index'])) {
195 echo __('Add index');
196 } else {
197 echo __('Edit index');
200 </legend>
201 <?php
204 <div class='index_info'>
205 <div>
206 <div class="label">
207 <strong>
208 <label for="input_index_name">
209 <?php echo __('Index name:'); ?>
210 <?php
211 echo PMA_Util::showHint(
212 PMA_Message::notice(
214 '("PRIMARY" <b>must</b> be the name of'
215 . ' and <b>only of</b> a primary key!)'
220 </label>
221 </strong>
222 </div>
223 <input type="text" name="index[Key_name]" id="input_index_name" size="25"
224 value="<?php echo htmlspecialchars($index->getName()); ?>"
225 onfocus="this.select()" />
226 </div>
227 <?php
228 if (PMA_MYSQL_INT_VERSION > 50500) {
230 <div>
231 <div class="label">
232 <strong>
233 <label for="input_index_comment">
234 <?php echo __('Comment:'); ?>
235 </label>
236 </strong>
237 </div>
238 <input type="text" name="index[Index_comment]" id="input_index_comment" size="30"
239 value="<?php echo htmlspecialchars($index->getComment()); ?>"
240 onfocus="this.select()" />
241 </div>
242 <?php
245 <div>
246 <div class="label">
247 <strong>
248 <label for="select_index_type">
249 <?php echo __('Index type:'); ?>
250 <?php echo PMA_Util::showMySQLDocu('SQL-Syntax', 'ALTER_TABLE'); ?>
251 </label>
252 </strong>
253 </div>
254 <select name="index[Index_type]" id="select_index_type" >
255 <?php echo $index->generateIndexSelector(); ?>
256 </select>
257 </div>
258 <div class="clearfloat"></div>
259 </div>
261 <table id="index_columns">
262 <thead>
263 <tr><th><?php echo __('Column'); ?></th>
264 <th><?php echo __('Size'); ?></th>
265 </tr>
266 </thead>
267 <tbody>
268 <?php
269 $odd_row = true;
270 $spatial_types = array(
271 'geometry', 'point', 'linestring', 'polygon', 'multipoint',
272 'multilinestring', 'multipolygon', 'geomtrycollection'
274 foreach ($index->getColumns() as $column) {
276 <tr class="<?php echo $odd_row ? 'odd' : 'even'; ?> noclick">
277 <td>
278 <select name="index[columns][names][]">
279 <option value="">-- <?php echo __('Ignore'); ?> --</option>
280 <?php
281 foreach ($fields as $field_name => $field_type) {
282 if (($index->getType() != 'FULLTEXT'
283 || preg_match('/(char|text)/i', $field_type))
284 && ($index->getType() != 'SPATIAL'
285 || in_array($field_type, $spatial_types))
287 echo '<option value="' . htmlspecialchars($field_name) . '"'
288 . (($field_name == $column->getName())
289 ? ' selected="selected"'
290 : '') . '>'
291 . htmlspecialchars($field_name) . ' ['
292 . htmlspecialchars($field_type) . ']'
293 . '</option>' . "\n";
295 } // end foreach $fields
297 </select>
298 </td>
299 <td>
300 <input type="text" size="5" onfocus="this.select()"
301 name="index[columns][sub_parts][]"
302 value="<?php
303 if ($index->getType() != 'SPATIAL') {
304 echo $column->getSubPart();
306 ?>"/>
307 </td>
308 </tr>
309 <?php
310 $odd_row = !$odd_row;
311 } // end foreach $edited_index_info['Sequences']
312 for ($i = 0; $i < $add_fields; $i++) {
314 <tr class="<?php echo $odd_row ? 'odd' : 'even'; ?> noclick">
315 <td>
316 <select name="index[columns][names][]">
317 <option value="">-- <?php echo __('Ignore'); ?> --</option>
318 <?php
319 foreach ($fields as $field_name => $field_type) {
320 echo '<option value="' . htmlspecialchars($field_name) . '">'
321 . htmlspecialchars($field_name) . ' ['
322 . htmlspecialchars($field_type) . ']'
323 . '</option>' . "\n";
324 } // end foreach $fields
326 </select>
327 </td>
328 <td>
329 <input type="text" size="5" onfocus="this.select()"
330 name="index[columns][sub_parts][]" value="" />
331 </td>
332 </tr>
333 <?php
334 $odd_row = !$odd_row;
335 } // end foreach $edited_index_info['Sequences']
337 </tbody>
338 </table>
339 </fieldset>
340 <fieldset class="tblFooters">
341 <?php
342 if ($GLOBALS['is_ajax_request'] != true || ! empty($_REQUEST['ajax_page_request'])) {
344 <input type="submit" name="do_save_data" value="<?php echo __('Save'); ?>" />
345 <span id="addMoreColumns">
346 <?php
347 echo __('Or') . ' ';
348 printf(
349 __('Add %s column(s) to index') . "\n",
350 '<input type="text" name="added_fields" size="2" value="1" />'
352 echo '<input type="submit" name="add_fields" value="' . __('Go') . '" />' . "\n";
354 </span>
355 <?php
356 } else {
357 $btn_value = sprintf(__('Add %s column(s) to index'), 1);
358 echo '<div class="slider"></div>';
359 echo '<div class="add_fields">';
360 echo '<input type="submit" value="' . $btn_value . '" />';
361 echo '</div>';
364 </fieldset>
365 </form>