undefined messages in Designer
[phpmyadmin/last10db.git] / libraries / mult_submits.inc.php
blobe473192c60b5dd37074ad8a391f114bbbf7a2884
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 view')) {
28 require './tbl_printview.php';
29 } else {
30 $selected = $selected_tbl;
31 switch ($submit_mult) {
32 case 'drop_db':
33 $what = 'drop_db';
34 break;
35 case __('Drop'):
36 $what = 'drop_tbl';
37 break;
38 case __('Empty'):
39 $what = 'empty_tbl';
40 break;
41 case __('Check table'):
42 unset($submit_mult);
43 $query_type = 'check_tbl';
44 $mult_btn = __('Yes');
45 break;
46 case __('Optimize table'):
47 unset($submit_mult);
48 $query_type = 'optimize_tbl';
49 $mult_btn = __('Yes');
50 break;
51 case __('Repair table'):
52 unset($submit_mult);
53 $query_type = 'repair_tbl';
54 $mult_btn = __('Yes');
55 break;
56 case __('Analyze table'):
57 unset($submit_mult);
58 $query_type = 'analyze_tbl';
59 $mult_btn = __('Yes');
60 break;
61 case __('Export'):
62 unset($submit_mult);
63 require('db_export.php');
64 exit;
65 break;
66 } // end switch
68 } elseif (isset($selected_fld) && !empty($selected_fld)) {
69 // coming from table structure view - do something with selected columns/fileds
70 $selected = $selected_fld;
71 switch ($submit_mult) {
72 case __('Drop'):
73 $what = 'drop_fld';
74 break;
75 case __('Primary'):
76 // Gets table primary key
77 PMA_DBI_select_db($db);
78 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
79 $primary = '';
80 while ($row = PMA_DBI_fetch_assoc($result)) {
81 // Backups the list of primary keys
82 if ($row['Key_name'] == 'PRIMARY') {
83 $primary .= $row['Column_name'] . ', ';
85 } // end while
86 PMA_DBI_free_result($result);
87 if (empty($primary)) {
88 // no primary key, so we can safely create new
89 unset($submit_mult);
90 $query_type = 'primary_fld';
91 $mult_btn = __('Yes');
92 } else {
93 // primary key exists, so lets as user
94 $what = 'primary_fld';
96 break;
97 case __('Index'):
98 unset($submit_mult);
99 $query_type = 'index_fld';
100 $mult_btn = __('Yes');
101 break;
102 case __('Unique'):
103 unset($submit_mult);
104 $query_type = 'unique_fld';
105 $mult_btn = __('Yes');
106 break;
107 case __('Fulltext'):
108 unset($submit_mult);
109 $query_type = 'fulltext_fld';
110 $mult_btn = __('Yes');
111 break;
112 case __('Change'):
113 require './tbl_alter.php';
114 break;
115 case __('Browse'):
116 // this should already be handled by tbl_structure.php
118 } else {
119 // coming from borwsing - do something with selected rows
120 $what = 'row_delete';
121 $selected = $rows_to_delete;
123 } // end if
127 * Displays the confirmation form if required
129 if (!empty($submit_mult) && !empty($what)) {
130 unset($message);
132 require_once './libraries/header.inc.php';
133 if (strlen($table)) {
134 require './libraries/tbl_common.php';
135 $url_query .= '&amp;goto=tbl_sql.php&amp;back=tbl_sql.php';
136 require './libraries/tbl_info.inc.php';
137 require_once './libraries/tbl_links.inc.php';
138 } elseif (strlen($db)) {
139 require './libraries/db_common.inc.php';
140 require './libraries/db_info.inc.php';
141 } else {
142 require_once './libraries/server_common.inc.php';
143 require_once './libraries/server_links.inc.php';
146 // Builds the query
147 $full_query = '';
148 if ($what == 'drop_tbl') {
149 $full_query_views = '';
151 $selected_cnt = count($selected);
152 $i = 0;
153 foreach ($selected AS $idx => $sval) {
154 switch ($what) {
155 case 'row_delete':
156 $full_query .= htmlspecialchars($sval)
157 . ';<br />';
158 break;
159 case 'drop_db':
160 $full_query .= 'DROP DATABASE '
161 . PMA_backquote(htmlspecialchars($sval))
162 . ';<br />';
163 $reload = 1;
164 break;
166 case 'drop_tbl':
167 $current = $sval;
168 if (!empty($views) && in_array($current, $views)) {
169 $full_query_views .= (empty($full_query_views) ? 'DROP VIEW ' : ', ')
170 . PMA_backquote(htmlspecialchars($current));
171 } else {
172 $full_query .= (empty($full_query) ? 'DROP TABLE ' : ', ')
173 . PMA_backquote(htmlspecialchars($current));
175 break;
177 case 'empty_tbl':
178 $full_query .= 'TRUNCATE ';
179 $full_query .= PMA_backquote(htmlspecialchars($sval))
180 . ';<br />';
181 break;
183 case 'primary_fld':
184 if ($full_query == '') {
185 $full_query .= 'ALTER TABLE '
186 . PMA_backquote(htmlspecialchars($table))
187 . '<br />&nbsp;&nbsp;DROP PRIMARY KEY,'
188 . '<br />&nbsp;&nbsp; ADD PRIMARY KEY('
189 . '<br />&nbsp;&nbsp;&nbsp;&nbsp; '
190 . PMA_backquote(htmlspecialchars($sval))
191 . ',';
192 } else {
193 $full_query .= '<br />&nbsp;&nbsp;&nbsp;&nbsp; '
194 . PMA_backquote(htmlspecialchars($sval))
195 . ',';
197 if ($i == $selected_cnt-1) {
198 $full_query = preg_replace('@,$@', ');<br />', $full_query);
200 break;
202 case 'drop_fld':
203 if ($full_query == '') {
204 $full_query .= 'ALTER TABLE '
205 . PMA_backquote(htmlspecialchars($table));
207 $full_query .= '<br />&nbsp;&nbsp;DROP '
208 . PMA_backquote(htmlspecialchars($sval))
209 . ',';
210 if ($i == $selected_cnt - 1) {
211 $full_query = preg_replace('@,$@', ';<br />', $full_query);
213 break;
214 } // end switch
215 $i++;
217 if ($what == 'drop_tbl') {
218 if (!empty($full_query)) {
219 $full_query .= ';<br />' . "\n";
221 if (!empty($full_query_views)) {
222 $full_query .= $full_query_views . ';<br />' . "\n";
224 unset($full_query_views);
227 // Displays the confirmation form
228 $_url_params = array(
229 'query_type' => $what,
230 'reload' => (! empty($reload) ? 1 : 0),
232 if (strpos(' ' . $action, 'db_') == 1) {
233 $_url_params['db']= $db;
234 } elseif (strpos(' ' . $action, 'tbl_') == 1 || $what == 'row_delete') {
235 $_url_params['db']= $db;
236 $_url_params['table']= $table;
238 foreach ($selected as $idx => $sval) {
239 $_url_params['selected'][] = $sval;
241 if ($what == 'drop_tbl' && !empty($views)) {
242 foreach ($views as $current) {
243 $_url_params['views'][] = $current;
246 if ($what == 'row_delete') {
247 $_url_params['original_sql_query'] = $original_sql_query;
248 if (! empty($original_url_query)) {
249 $_url_params['original_url_query'] = $original_url_query;
253 <form action="<?php echo $action; ?>" method="post">
254 <?php
255 echo PMA_generate_common_hidden_inputs($_url_params);
257 <fieldset class="confirmation">
258 <legend><?php echo ($what == 'drop_db' ? __('You are about to DESTROY a complete database!') . '&nbsp;' : '') . __('Do you really want to '); ?>:</legend>
259 <tt><?php echo $full_query; ?></tt>
260 </fieldset>
261 <fieldset class="tblFooters">
262 <input type="submit" name="mult_btn" value="<?php echo __('Yes'); ?>" id="buttonYes" />
263 <input type="submit" name="mult_btn" value="<?php echo __('No'); ?>" id="buttonNo" />
264 </fieldset>
265 <?php
266 require './libraries/footer.inc.php';
267 } // end if
271 * Executes the query - dropping rows, columns/fields, tables or dbs
273 elseif ($mult_btn == __('Yes')) {
275 if ($query_type == 'drop_db' || $query_type == 'drop_tbl' || $query_type == 'drop_fld') {
276 require_once './libraries/relation_cleanup.lib.php';
279 $sql_query = '';
280 if ($query_type == 'drop_tbl') {
281 $sql_query_views = '';
283 $selected_cnt = count($selected);
284 $run_parts = FALSE; // whether to run query after each pass
285 $use_sql = FALSE; // whether to include sql.php at the end (to display results)
287 if ($query_type == 'primary_fld') {
288 // Gets table primary key
289 PMA_DBI_select_db($db);
290 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
291 $primary = '';
292 while ($row = PMA_DBI_fetch_assoc($result)) {
293 // Backups the list of primary keys
294 if ($row['Key_name'] == 'PRIMARY') {
295 $primary .= $row['Column_name'] . ', ';
297 } // end while
298 PMA_DBI_free_result($result);
301 $rebuild_database_list = false;
303 for ($i = 0; $i < $selected_cnt; $i++) {
304 switch ($query_type) {
305 case 'row_delete':
306 $a_query = $selected[$i];
307 $run_parts = TRUE;
308 break;
310 case 'drop_db':
311 PMA_relationsCleanupDatabase($selected[$i]);
312 $a_query = 'DROP DATABASE '
313 . PMA_backquote($selected[$i]);
314 $reload = 1;
315 $run_parts = TRUE;
316 $rebuild_database_list = true;
317 break;
319 case 'drop_tbl':
320 PMA_relationsCleanupTable($db, $selected[$i]);
321 $current = $selected[$i];
322 if (!empty($views) && in_array($current, $views)) {
323 $sql_query_views .= (empty($sql_query_views) ? 'DROP VIEW ' : ', ')
324 . PMA_backquote($current);
325 } else {
326 $sql_query .= (empty($sql_query) ? 'DROP TABLE ' : ', ')
327 . PMA_backquote($current);
329 $reload = 1;
330 break;
332 case 'check_tbl':
333 $sql_query .= (empty($sql_query) ? 'CHECK TABLE ' : ', ')
334 . PMA_backquote($selected[$i]);
335 $use_sql = TRUE;
336 break;
338 case 'optimize_tbl':
339 $sql_query .= (empty($sql_query) ? 'OPTIMIZE TABLE ' : ', ')
340 . PMA_backquote($selected[$i]);
341 $use_sql = TRUE;
342 break;
344 case 'analyze_tbl':
345 $sql_query .= (empty($sql_query) ? 'ANALYZE TABLE ' : ', ')
346 . PMA_backquote($selected[$i]);
347 $use_sql = TRUE;
348 break;
350 case 'repair_tbl':
351 $sql_query .= (empty($sql_query) ? 'REPAIR TABLE ' : ', ')
352 . PMA_backquote($selected[$i]);
353 $use_sql = TRUE;
354 break;
356 case 'empty_tbl':
357 $a_query = 'TRUNCATE ';
358 $a_query .= PMA_backquote(htmlspecialchars($selected[$i]));
359 $run_parts = TRUE;
360 break;
362 case 'drop_fld':
363 PMA_relationsCleanupColumn($db, $table, $selected[$i]);
364 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) : ',')
365 . ' DROP ' . PMA_backquote($selected[$i])
366 . (($i == $selected_cnt-1) ? ';' : '');
367 break;
369 case 'primary_fld':
370 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . (empty($primary) ? '' : ' DROP PRIMARY KEY,') . ' ADD PRIMARY KEY( ' : ', ')
371 . PMA_backquote($selected[$i])
372 . (($i == $selected_cnt-1) ? ');' : '');
373 break;
375 case 'index_fld':
376 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX( ' : ', ')
377 . PMA_backquote($selected[$i])
378 . (($i == $selected_cnt-1) ? ');' : '');
379 break;
381 case 'unique_fld':
382 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE( ' : ', ')
383 . PMA_backquote($selected[$i])
384 . (($i == $selected_cnt-1) ? ');' : '');
385 break;
387 case 'fulltext_fld':
388 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT( ' : ', ')
389 . PMA_backquote($selected[$i])
390 . (($i == $selected_cnt-1) ? ');' : '');
391 break;
392 } // end switch
394 // All "DROP TABLE", "DROP FIELD", "OPTIMIZE TABLE" and "REPAIR TABLE"
395 // statements will be run at once below
396 if ($run_parts) {
397 $sql_query .= $a_query . ';' . "\n";
398 if ($query_type != 'drop_db') {
399 PMA_DBI_select_db($db);
401 $result = PMA_DBI_query($a_query);
402 } // end if
403 } // end for
405 if ($query_type == 'drop_tbl') {
406 if (!empty($sql_query)) {
407 $sql_query .= ';';
408 } elseif (!empty($sql_query_views)) {
409 $sql_query = $sql_query_views . ';';
410 unset($sql_query_views);
414 if ($use_sql) {
415 require './sql.php';
416 } elseif (!$run_parts) {
417 PMA_DBI_select_db($db);
418 $result = PMA_DBI_try_query($sql_query);
419 if ($result && !empty($sql_query_views)) {
420 $sql_query .= ' ' . $sql_query_views . ';';
421 $result = PMA_DBI_try_query($sql_query_views);
422 unset($sql_query_views);
425 if (! $result) {
426 $message = PMA_Message::error(PMA_DBI_getError());
429 if ($rebuild_database_list) {
430 // avoid a problem with the database list navigator
431 // when dropping a db from server_databases
432 $GLOBALS['pma']->databases->build();
434 } else {
435 $message = PMA_Message::success(__('No change'));