Add OK and Cancel button for grid editing
[phpmyadmin/arisferyanto.git] / libraries / mult_submits.inc.php
blob6c67b2730536332299ec0b2c6bf227057073678b
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @package PhpMyAdmin
6 */
7 if (! defined('PHPMYADMIN')) {
8 exit;
11 /**
12 * Prepares the work and runs some other scripts if required
14 if (! empty($submit_mult)
15 && $submit_mult != __('With selected:')
16 && (! empty($selected_db)
17 || ! empty($selected_tbl)
18 || ! empty($selected_fld)
19 || ! empty($rows_to_delete))) {
20 define('PMA_SUBMIT_MULT', 1);
21 if (isset($selected_db) && !empty($selected_db)) {
22 // coming from server database view - do something with selected databases
23 $selected = $selected_db;
24 $what = 'drop_db';
25 } elseif (isset($selected_tbl) && !empty($selected_tbl)) {
26 // coming from database structure view - do something with selected tables
27 if ($submit_mult == 'print') {
28 include './tbl_printview.php';
29 } else {
30 $selected = $selected_tbl;
31 switch ($submit_mult) {
32 case 'add_prefix_tbl':
33 case 'replace_prefix_tbl':
34 case 'copy_tbl_change_prefix':
35 case 'drop_db':
36 case 'drop_tbl':
37 case 'empty_tbl':
38 $what = $submit_mult;
39 break;
40 case 'check_tbl':
41 case 'optimize_tbl':
42 case 'repair_tbl':
43 case 'analyze_tbl':
44 $query_type = $submit_mult;
45 unset($submit_mult);
46 $mult_btn = __('Yes');
47 break;
48 case 'export':
49 unset($submit_mult);
50 include 'db_export.php';
51 exit;
52 break;
53 } // end switch
55 } elseif (isset($selected_fld) && !empty($selected_fld)) {
56 // coming from table structure view - do something with selected columns/fileds
57 $selected = $selected_fld;
58 switch ($submit_mult) {
59 case 'drop':
60 $what = 'drop_fld';
61 break;
62 case 'primary':
63 // Gets table primary key
64 PMA_DBI_select_db($db);
65 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
66 $primary = '';
67 while ($row = PMA_DBI_fetch_assoc($result)) {
68 // Backups the list of primary keys
69 if ($row['Key_name'] == 'PRIMARY') {
70 $primary .= $row['Column_name'] . ', ';
72 } // end while
73 PMA_DBI_free_result($result);
74 if (empty($primary)) {
75 // no primary key, so we can safely create new
76 unset($submit_mult);
77 $query_type = 'primary_fld';
78 $mult_btn = __('Yes');
79 } else {
80 // primary key exists, so lets as user
81 $what = 'primary_fld';
83 break;
84 case 'index':
85 unset($submit_mult);
86 $query_type = 'index_fld';
87 $mult_btn = __('Yes');
88 break;
89 case 'unique':
90 unset($submit_mult);
91 $query_type = 'unique_fld';
92 $mult_btn = __('Yes');
93 break;
94 case 'spatial':
95 unset($submit_mult);
96 $query_type = 'spatial_fld';
97 $mult_btn = __('Yes');
98 break;
99 case 'ftext':
100 unset($submit_mult);
101 $query_type = 'fulltext_fld';
102 $mult_btn = __('Yes');
103 break;
104 case 'change':
105 include './tbl_alter.php';
106 break;
107 case 'browse':
108 // this should already be handled by tbl_structure.php
110 } else {
111 // coming from browsing - do something with selected rows
112 $what = 'row_delete';
113 $selected = $rows_to_delete;
115 } // end if
119 * Displays the confirmation form if required
121 if (!empty($submit_mult) && !empty($what)) {
122 unset($message);
124 include_once './libraries/header.inc.php';
125 if (strlen($table)) {
126 include './libraries/tbl_common.php';
127 $url_query .= '&amp;goto=tbl_sql.php&amp;back=tbl_sql.php';
128 include './libraries/tbl_info.inc.php';
129 include_once './libraries/tbl_links.inc.php';
130 } elseif (strlen($db)) {
131 include './libraries/db_common.inc.php';
132 include './libraries/db_info.inc.php';
133 } else {
134 include_once './libraries/server_common.inc.php';
135 include_once './libraries/server_links.inc.php';
138 // Builds the query
139 $full_query = '';
140 if ($what == 'drop_tbl') {
141 $full_query_views = '';
143 $selected_cnt = count($selected);
144 $i = 0;
145 foreach ($selected AS $idx => $sval) {
146 switch ($what) {
147 case 'row_delete':
148 $full_query .= htmlspecialchars($sval)
149 . ';<br />';
150 break;
151 case 'drop_db':
152 $full_query .= 'DROP DATABASE '
153 . PMA_backquote(htmlspecialchars($sval))
154 . ';<br />';
155 $reload = 1;
156 break;
158 case 'drop_tbl':
159 $current = $sval;
160 if (!empty($views) && in_array($current, $views)) {
161 $full_query_views .= (empty($full_query_views) ? 'DROP VIEW ' : ', ')
162 . PMA_backquote(htmlspecialchars($current));
163 } else {
164 $full_query .= (empty($full_query) ? 'DROP TABLE ' : ', ')
165 . PMA_backquote(htmlspecialchars($current));
167 break;
169 case 'empty_tbl':
170 $full_query .= 'TRUNCATE ';
171 $full_query .= PMA_backquote(htmlspecialchars($sval))
172 . ';<br />';
173 break;
175 case 'primary_fld':
176 if ($full_query == '') {
177 $full_query .= 'ALTER TABLE '
178 . PMA_backquote(htmlspecialchars($table))
179 . '<br />&nbsp;&nbsp;DROP PRIMARY KEY,'
180 . '<br />&nbsp;&nbsp; ADD PRIMARY KEY('
181 . '<br />&nbsp;&nbsp;&nbsp;&nbsp; '
182 . PMA_backquote(htmlspecialchars($sval))
183 . ',';
184 } else {
185 $full_query .= '<br />&nbsp;&nbsp;&nbsp;&nbsp; '
186 . PMA_backquote(htmlspecialchars($sval))
187 . ',';
189 if ($i == $selected_cnt-1) {
190 $full_query = preg_replace('@,$@', ');<br />', $full_query);
192 break;
194 case 'drop_fld':
195 if ($full_query == '') {
196 $full_query .= 'ALTER TABLE '
197 . PMA_backquote(htmlspecialchars($table));
199 $full_query .= '<br />&nbsp;&nbsp;DROP '
200 . PMA_backquote(htmlspecialchars($sval))
201 . ',';
202 if ($i == $selected_cnt - 1) {
203 $full_query = preg_replace('@,$@', ';<br />', $full_query);
205 break;
206 } // end switch
207 $i++;
209 if ($what == 'drop_tbl') {
210 if (!empty($full_query)) {
211 $full_query .= ';<br />' . "\n";
213 if (!empty($full_query_views)) {
214 $full_query .= $full_query_views . ';<br />' . "\n";
216 unset($full_query_views);
219 // Displays the confirmation form
220 $_url_params = array(
221 'query_type' => $what,
222 'reload' => (! empty($reload) ? 1 : 0),
224 if (strpos(' ' . $action, 'db_') == 1) {
225 $_url_params['db']= $db;
226 } elseif (strpos(' ' . $action, 'tbl_') == 1 || $what == 'row_delete') {
227 $_url_params['db']= $db;
228 $_url_params['table']= $table;
230 foreach ($selected as $idx => $sval) {
231 $_url_params['selected'][] = $sval;
233 if ($what == 'drop_tbl' && !empty($views)) {
234 foreach ($views as $current) {
235 $_url_params['views'][] = $current;
238 if ($what == 'row_delete') {
239 $_url_params['original_sql_query'] = $original_sql_query;
240 if (! empty($original_url_query)) {
241 $_url_params['original_url_query'] = $original_url_query;
245 <form action="<?php echo $action; ?>" method="post">
246 <?php
247 echo PMA_generate_common_hidden_inputs($_url_params);
249 <?php if ($what == 'replace_prefix_tbl' || $what == 'copy_tbl_change_prefix') { ?>
250 <fieldset class = "input">
251 <legend><?php echo ($what == 'replace_prefix_tbl' ? __('Replace table prefix') : __('Copy table with prefix')) ?>:</legend>
252 <table>
253 <tr>
254 <td><?php echo __('From'); ?></td><td><input type="text" name="from_prefix" id="initialPrefix"</td>
255 </tr>
256 <tr>
257 <td><?php echo __('To'); ?> </td><td><input type="text" name="to_prefix" id="newPrefix"</td>
258 </tr>
259 </table>
260 </fieldset>
261 <fieldset class="tblFooters">
262 <button type="submit" name="mult_btn" value="<?php echo __('Yes'); ?>" id="buttonYes"><?php echo __('Submit'); ?></button>
263 </fieldset>
264 <?php
266 elseif ($what == 'add_prefix_tbl') { ?>
267 <fieldset class = "input">
268 <legend><?php echo __('Add table prefix') ?>:</legend>
269 <table>
270 <tr>
271 <td><?php echo __('Add prefix'); ?></td> <td><input type="text" name="add_prefix" id="txtPrefix"</td>
272 </tr>
273 </table>
274 </fieldset>
275 <fieldset class="tblFooters">
276 <button type="submit" name="mult_btn" value="<?php echo __('Yes'); ?>" id="buttonYes"><?php echo __('Submit'); ?></button>
277 </fieldset>
278 <?php
280 else { ?>
281 <fieldset class="confirmation">
282 <legend><?php echo ($what == 'drop_db' ? __('You are about to DESTROY a complete database!') . '&nbsp;' : '') . __('Do you really want to '); ?>:</legend>
283 <tt><?php echo $full_query; ?></tt>
284 </fieldset>
285 <fieldset class="tblFooters">
286 <input type="submit" name="mult_btn" value="<?php echo __('Yes'); ?>" id="buttonYes" />
287 <input type="submit" name="mult_btn" value="<?php echo __('No'); ?>" id="buttonNo" />
288 </fieldset>
289 <?php
291 include './libraries/footer.inc.php';
293 } // end if
297 * Executes the query - dropping rows, columns/fields, tables or dbs
299 elseif ($mult_btn == __('Yes')) {
301 if ($query_type == 'drop_db' || $query_type == 'drop_tbl' || $query_type == 'drop_fld') {
302 include_once './libraries/relation_cleanup.lib.php';
305 $sql_query = '';
306 if ($query_type == 'drop_tbl') {
307 $sql_query_views = '';
309 $selected_cnt = count($selected);
310 $run_parts = false; // whether to run query after each pass
311 $use_sql = false; // whether to include sql.php at the end (to display results)
313 if ($query_type == 'primary_fld') {
314 // Gets table primary key
315 PMA_DBI_select_db($db);
316 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
317 $primary = '';
318 while ($row = PMA_DBI_fetch_assoc($result)) {
319 // Backups the list of primary keys
320 if ($row['Key_name'] == 'PRIMARY') {
321 $primary .= $row['Column_name'] . ', ';
323 } // end while
324 PMA_DBI_free_result($result);
327 $rebuild_database_list = false;
329 for ($i = 0; $i < $selected_cnt; $i++) {
330 switch ($query_type) {
331 case 'row_delete':
332 $a_query = $selected[$i];
333 $run_parts = true;
334 break;
336 case 'drop_db':
337 PMA_relationsCleanupDatabase($selected[$i]);
338 $a_query = 'DROP DATABASE '
339 . PMA_backquote($selected[$i]);
340 $reload = 1;
341 $run_parts = true;
342 $rebuild_database_list = true;
343 break;
345 case 'drop_tbl':
346 PMA_relationsCleanupTable($db, $selected[$i]);
347 $current = $selected[$i];
348 if (!empty($views) && in_array($current, $views)) {
349 $sql_query_views .= (empty($sql_query_views) ? 'DROP VIEW ' : ', ')
350 . PMA_backquote($current);
351 } else {
352 $sql_query .= (empty($sql_query) ? 'DROP TABLE ' : ', ')
353 . PMA_backquote($current);
355 $reload = 1;
356 break;
358 case 'check_tbl':
359 $sql_query .= (empty($sql_query) ? 'CHECK TABLE ' : ', ')
360 . PMA_backquote($selected[$i]);
361 $use_sql = true;
362 break;
364 case 'optimize_tbl':
365 $sql_query .= (empty($sql_query) ? 'OPTIMIZE TABLE ' : ', ')
366 . PMA_backquote($selected[$i]);
367 $use_sql = true;
368 break;
370 case 'analyze_tbl':
371 $sql_query .= (empty($sql_query) ? 'ANALYZE TABLE ' : ', ')
372 . PMA_backquote($selected[$i]);
373 $use_sql = true;
374 break;
376 case 'repair_tbl':
377 $sql_query .= (empty($sql_query) ? 'REPAIR TABLE ' : ', ')
378 . PMA_backquote($selected[$i]);
379 $use_sql = true;
380 break;
382 case 'empty_tbl':
383 $a_query = 'TRUNCATE ';
384 $a_query .= PMA_backquote($selected[$i]);
385 $run_parts = true;
386 break;
388 case 'drop_fld':
389 PMA_relationsCleanupColumn($db, $table, $selected[$i]);
390 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) : ',')
391 . ' DROP ' . PMA_backquote($selected[$i])
392 . (($i == $selected_cnt-1) ? ';' : '');
393 break;
395 case 'primary_fld':
396 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . (empty($primary) ? '' : ' DROP PRIMARY KEY,') . ' ADD PRIMARY KEY( ' : ', ')
397 . PMA_backquote($selected[$i])
398 . (($i == $selected_cnt-1) ? ');' : '');
399 break;
401 case 'index_fld':
402 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX( ' : ', ')
403 . PMA_backquote($selected[$i])
404 . (($i == $selected_cnt-1) ? ');' : '');
405 break;
407 case 'unique_fld':
408 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE( ' : ', ')
409 . PMA_backquote($selected[$i])
410 . (($i == $selected_cnt-1) ? ');' : '');
411 break;
413 case 'spatial_fld':
414 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD SPATIAL( ' : ', ')
415 . PMA_backquote($selected[$i])
416 . (($i == $selected_cnt-1) ? ');' : '');
417 break;
419 case 'fulltext_fld':
420 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT( ' : ', ')
421 . PMA_backquote($selected[$i])
422 . (($i == $selected_cnt-1) ? ');' : '');
423 break;
425 case 'add_prefix_tbl':
426 $newtablename = $add_prefix . $selected[$i];
427 $a_query = 'ALTER TABLE ' . PMA_backquote($selected[$i]) . ' RENAME ' . PMA_backquote($newtablename) ; // ADD PREFIX TO TABLE NAME
428 $run_parts = true;
429 break;
431 case 'replace_prefix_tbl':
432 $current = $selected[$i];
433 $newtablename = preg_replace("/^" . $from_prefix . "/", $to_prefix, $current);
434 $a_query = 'ALTER TABLE ' . PMA_backquote($selected[$i]) . ' RENAME ' . PMA_backquote($newtablename) ; // CHANGE PREFIX PATTERN
435 $run_parts = true;
436 break;
438 case 'copy_tbl_change_prefix':
439 $current = $selected[$i];
440 $newtablename = preg_replace("/^" . $from_prefix . "/", $to_prefix, $current);
441 $a_query = 'CREATE TABLE ' . PMA_backquote($newtablename) . ' SELECT * FROM ' . PMA_backquote($selected[$i]) ; // COPY TABLE AND CHANGE PREFIX PATTERN
442 $run_parts = true;
443 break;
445 } // end switch
447 // All "DROP TABLE", "DROP FIELD", "OPTIMIZE TABLE" and "REPAIR TABLE"
448 // statements will be run at once below
449 if ($run_parts) {
450 $sql_query .= $a_query . ';' . "\n";
451 if ($query_type != 'drop_db') {
452 PMA_DBI_select_db($db);
454 $result = PMA_DBI_query($a_query);
455 } // end if
456 } // end for
458 if ($query_type == 'drop_tbl') {
459 if (!empty($sql_query)) {
460 $sql_query .= ';';
461 } elseif (!empty($sql_query_views)) {
462 $sql_query = $sql_query_views . ';';
463 unset($sql_query_views);
467 if ($use_sql) {
468 include './sql.php';
469 } elseif (!$run_parts) {
470 PMA_DBI_select_db($db);
471 $result = PMA_DBI_try_query($sql_query);
472 if ($result && !empty($sql_query_views)) {
473 $sql_query .= ' ' . $sql_query_views . ';';
474 $result = PMA_DBI_try_query($sql_query_views);
475 unset($sql_query_views);
478 if (! $result) {
479 $message = PMA_Message::error(PMA_DBI_getError());
482 if ($rebuild_database_list) {
483 // avoid a problem with the database list navigator
484 // when dropping a db from server_databases
485 $GLOBALS['pma']->databases->build();
487 } else {
488 $message = PMA_Message::success(__('No change'));