Translation update done using Pootle.
[phpmyadmin/ammar_yasir.git] / libraries / mult_submits.inc.php
blob665b76d74a63ffb1668eb2c85aab8f1468132464
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 require './tbl_printview.php';
29 } else {
30 $selected = $selected_tbl;
31 switch ($submit_mult) {
32 case 'drop_db':
33 case 'drop_tbl':
34 case 'empty_tbl':
35 $what = $submit_mult;
36 break;
37 case 'check_tbl':
38 case 'optimize_tbl':
39 case 'repair_tbl':
40 case 'analyze_tbl':
41 $query_type = $submit_mult;
42 unset($submit_mult);
43 $mult_btn = __('Yes');
44 break;
45 case 'export':
46 unset($submit_mult);
47 require('db_export.php');
48 exit;
49 break;
50 } // end switch
52 } elseif (isset($selected_fld) && !empty($selected_fld)) {
53 // coming from table structure view - do something with selected columns/fileds
54 $selected = $selected_fld;
55 switch ($submit_mult) {
56 case 'drop':
57 $what = 'drop_fld';
58 break;
59 case 'primary':
60 // Gets table primary key
61 PMA_DBI_select_db($db);
62 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
63 $primary = '';
64 while ($row = PMA_DBI_fetch_assoc($result)) {
65 // Backups the list of primary keys
66 if ($row['Key_name'] == 'PRIMARY') {
67 $primary .= $row['Column_name'] . ', ';
69 } // end while
70 PMA_DBI_free_result($result);
71 if (empty($primary)) {
72 // no primary key, so we can safely create new
73 unset($submit_mult);
74 $query_type = 'primary_fld';
75 $mult_btn = __('Yes');
76 } else {
77 // primary key exists, so lets as user
78 $what = 'primary_fld';
80 break;
81 case 'index':
82 unset($submit_mult);
83 $query_type = 'index_fld';
84 $mult_btn = __('Yes');
85 break;
86 case 'unique':
87 unset($submit_mult);
88 $query_type = 'unique_fld';
89 $mult_btn = __('Yes');
90 break;
91 case 'ftext':
92 unset($submit_mult);
93 $query_type = 'fulltext_fld';
94 $mult_btn = __('Yes');
95 break;
96 case 'change':
97 require './tbl_alter.php';
98 break;
99 case 'browse':
100 // this should already be handled by tbl_structure.php
102 } else {
103 // coming from browsing - do something with selected rows
104 $what = 'row_delete';
105 $selected = $rows_to_delete;
107 } // end if
111 * Displays the confirmation form if required
113 if (!empty($submit_mult) && !empty($what)) {
114 unset($message);
116 require_once './libraries/header.inc.php';
117 if (strlen($table)) {
118 require './libraries/tbl_common.php';
119 $url_query .= '&amp;goto=tbl_sql.php&amp;back=tbl_sql.php';
120 require './libraries/tbl_info.inc.php';
121 require_once './libraries/tbl_links.inc.php';
122 } elseif (strlen($db)) {
123 require './libraries/db_common.inc.php';
124 require './libraries/db_info.inc.php';
125 } else {
126 require_once './libraries/server_common.inc.php';
127 require_once './libraries/server_links.inc.php';
130 // Builds the query
131 $full_query = '';
132 if ($what == 'drop_tbl') {
133 $full_query_views = '';
135 $selected_cnt = count($selected);
136 $i = 0;
137 foreach ($selected AS $idx => $sval) {
138 switch ($what) {
139 case 'row_delete':
140 $full_query .= htmlspecialchars($sval)
141 . ';<br />';
142 break;
143 case 'drop_db':
144 $full_query .= 'DROP DATABASE '
145 . PMA_backquote(htmlspecialchars($sval))
146 . ';<br />';
147 $reload = 1;
148 break;
150 case 'drop_tbl':
151 $current = $sval;
152 if (!empty($views) && in_array($current, $views)) {
153 $full_query_views .= (empty($full_query_views) ? 'DROP VIEW ' : ', ')
154 . PMA_backquote(htmlspecialchars($current));
155 } else {
156 $full_query .= (empty($full_query) ? 'DROP TABLE ' : ', ')
157 . PMA_backquote(htmlspecialchars($current));
159 break;
161 case 'empty_tbl':
162 $full_query .= 'TRUNCATE ';
163 $full_query .= PMA_backquote(htmlspecialchars($sval))
164 . ';<br />';
165 break;
167 case 'primary_fld':
168 if ($full_query == '') {
169 $full_query .= 'ALTER TABLE '
170 . PMA_backquote(htmlspecialchars($table))
171 . '<br />&nbsp;&nbsp;DROP PRIMARY KEY,'
172 . '<br />&nbsp;&nbsp; ADD PRIMARY KEY('
173 . '<br />&nbsp;&nbsp;&nbsp;&nbsp; '
174 . PMA_backquote(htmlspecialchars($sval))
175 . ',';
176 } else {
177 $full_query .= '<br />&nbsp;&nbsp;&nbsp;&nbsp; '
178 . PMA_backquote(htmlspecialchars($sval))
179 . ',';
181 if ($i == $selected_cnt-1) {
182 $full_query = preg_replace('@,$@', ');<br />', $full_query);
184 break;
186 case 'drop_fld':
187 if ($full_query == '') {
188 $full_query .= 'ALTER TABLE '
189 . PMA_backquote(htmlspecialchars($table));
191 $full_query .= '<br />&nbsp;&nbsp;DROP '
192 . PMA_backquote(htmlspecialchars($sval))
193 . ',';
194 if ($i == $selected_cnt - 1) {
195 $full_query = preg_replace('@,$@', ';<br />', $full_query);
197 break;
198 } // end switch
199 $i++;
201 if ($what == 'drop_tbl') {
202 if (!empty($full_query)) {
203 $full_query .= ';<br />' . "\n";
205 if (!empty($full_query_views)) {
206 $full_query .= $full_query_views . ';<br />' . "\n";
208 unset($full_query_views);
211 // Displays the confirmation form
212 $_url_params = array(
213 'query_type' => $what,
214 'reload' => (! empty($reload) ? 1 : 0),
216 if (strpos(' ' . $action, 'db_') == 1) {
217 $_url_params['db']= $db;
218 } elseif (strpos(' ' . $action, 'tbl_') == 1 || $what == 'row_delete') {
219 $_url_params['db']= $db;
220 $_url_params['table']= $table;
222 foreach ($selected as $idx => $sval) {
223 $_url_params['selected'][] = $sval;
225 if ($what == 'drop_tbl' && !empty($views)) {
226 foreach ($views as $current) {
227 $_url_params['views'][] = $current;
230 if ($what == 'row_delete') {
231 $_url_params['original_sql_query'] = $original_sql_query;
232 if (! empty($original_url_query)) {
233 $_url_params['original_url_query'] = $original_url_query;
237 <form action="<?php echo $action; ?>" method="post">
238 <?php
239 echo PMA_generate_common_hidden_inputs($_url_params);
241 <fieldset class="confirmation">
242 <legend><?php echo ($what == 'drop_db' ? __('You are about to DESTROY a complete database!') . '&nbsp;' : '') . __('Do you really want to '); ?>:</legend>
243 <tt><?php echo $full_query; ?></tt>
244 </fieldset>
245 <fieldset class="tblFooters">
246 <input type="submit" name="mult_btn" value="<?php echo __('Yes'); ?>" id="buttonYes" />
247 <input type="submit" name="mult_btn" value="<?php echo __('No'); ?>" id="buttonNo" />
248 </fieldset>
249 <?php
250 require './libraries/footer.inc.php';
251 } // end if
255 * Executes the query - dropping rows, columns/fields, tables or dbs
257 elseif ($mult_btn == __('Yes')) {
259 if ($query_type == 'drop_db' || $query_type == 'drop_tbl' || $query_type == 'drop_fld') {
260 require_once './libraries/relation_cleanup.lib.php';
263 $sql_query = '';
264 if ($query_type == 'drop_tbl') {
265 $sql_query_views = '';
267 $selected_cnt = count($selected);
268 $run_parts = FALSE; // whether to run query after each pass
269 $use_sql = FALSE; // whether to include sql.php at the end (to display results)
271 if ($query_type == 'primary_fld') {
272 // Gets table primary key
273 PMA_DBI_select_db($db);
274 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
275 $primary = '';
276 while ($row = PMA_DBI_fetch_assoc($result)) {
277 // Backups the list of primary keys
278 if ($row['Key_name'] == 'PRIMARY') {
279 $primary .= $row['Column_name'] . ', ';
281 } // end while
282 PMA_DBI_free_result($result);
285 $rebuild_database_list = false;
287 for ($i = 0; $i < $selected_cnt; $i++) {
288 switch ($query_type) {
289 case 'row_delete':
290 $a_query = $selected[$i];
291 $run_parts = TRUE;
292 break;
294 case 'drop_db':
295 PMA_relationsCleanupDatabase($selected[$i]);
296 $a_query = 'DROP DATABASE '
297 . PMA_backquote($selected[$i]);
298 $reload = 1;
299 $run_parts = TRUE;
300 $rebuild_database_list = true;
301 break;
303 case 'drop_tbl':
304 PMA_relationsCleanupTable($db, $selected[$i]);
305 $current = $selected[$i];
306 if (!empty($views) && in_array($current, $views)) {
307 $sql_query_views .= (empty($sql_query_views) ? 'DROP VIEW ' : ', ')
308 . PMA_backquote($current);
309 } else {
310 $sql_query .= (empty($sql_query) ? 'DROP TABLE ' : ', ')
311 . PMA_backquote($current);
313 $reload = 1;
314 break;
316 case 'check_tbl':
317 $sql_query .= (empty($sql_query) ? 'CHECK TABLE ' : ', ')
318 . PMA_backquote($selected[$i]);
319 $use_sql = TRUE;
320 break;
322 case 'optimize_tbl':
323 $sql_query .= (empty($sql_query) ? 'OPTIMIZE TABLE ' : ', ')
324 . PMA_backquote($selected[$i]);
325 $use_sql = TRUE;
326 break;
328 case 'analyze_tbl':
329 $sql_query .= (empty($sql_query) ? 'ANALYZE TABLE ' : ', ')
330 . PMA_backquote($selected[$i]);
331 $use_sql = TRUE;
332 break;
334 case 'repair_tbl':
335 $sql_query .= (empty($sql_query) ? 'REPAIR TABLE ' : ', ')
336 . PMA_backquote($selected[$i]);
337 $use_sql = TRUE;
338 break;
340 case 'empty_tbl':
341 $a_query = 'TRUNCATE ';
342 $a_query .= PMA_backquote(htmlspecialchars($selected[$i]));
343 $run_parts = TRUE;
344 break;
346 case 'drop_fld':
347 PMA_relationsCleanupColumn($db, $table, $selected[$i]);
348 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) : ',')
349 . ' DROP ' . PMA_backquote($selected[$i])
350 . (($i == $selected_cnt-1) ? ';' : '');
351 break;
353 case 'primary_fld':
354 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . (empty($primary) ? '' : ' DROP PRIMARY KEY,') . ' ADD PRIMARY KEY( ' : ', ')
355 . PMA_backquote($selected[$i])
356 . (($i == $selected_cnt-1) ? ');' : '');
357 break;
359 case 'index_fld':
360 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX( ' : ', ')
361 . PMA_backquote($selected[$i])
362 . (($i == $selected_cnt-1) ? ');' : '');
363 break;
365 case 'unique_fld':
366 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE( ' : ', ')
367 . PMA_backquote($selected[$i])
368 . (($i == $selected_cnt-1) ? ');' : '');
369 break;
371 case 'fulltext_fld':
372 $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT( ' : ', ')
373 . PMA_backquote($selected[$i])
374 . (($i == $selected_cnt-1) ? ');' : '');
375 break;
376 } // end switch
378 // All "DROP TABLE", "DROP FIELD", "OPTIMIZE TABLE" and "REPAIR TABLE"
379 // statements will be run at once below
380 if ($run_parts) {
381 $sql_query .= $a_query . ';' . "\n";
382 if ($query_type != 'drop_db') {
383 PMA_DBI_select_db($db);
385 $result = PMA_DBI_query($a_query);
386 } // end if
387 } // end for
389 if ($query_type == 'drop_tbl') {
390 if (!empty($sql_query)) {
391 $sql_query .= ';';
392 } elseif (!empty($sql_query_views)) {
393 $sql_query = $sql_query_views . ';';
394 unset($sql_query_views);
398 if ($use_sql) {
399 require './sql.php';
400 } elseif (!$run_parts) {
401 PMA_DBI_select_db($db);
402 $result = PMA_DBI_try_query($sql_query);
403 if ($result && !empty($sql_query_views)) {
404 $sql_query .= ' ' . $sql_query_views . ';';
405 $result = PMA_DBI_try_query($sql_query_views);
406 unset($sql_query_views);
409 if (! $result) {
410 $message = PMA_Message::error(PMA_DBI_getError());
413 if ($rebuild_database_list) {
414 // avoid a problem with the database list navigator
415 // when dropping a db from server_databases
416 $GLOBALS['pma']->databases->build();
418 } else {
419 $message = PMA_Message::success(__('No change'));