Translation update done using Pootle.
[phpmyadmin-themes.git] / libraries / sql_query_form.lib.php
blob0435325936e9c9fb76710ae5a313eb612942e894
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * functions for displaying the sql query form
6 * @usedby server_sql.php
7 * @usedby db_sql.php
8 * @usedby tbl_sql.php
9 * @usedby tbl_structure.php
10 * @usedby tbl_tracking.php
11 * @usedby querywindow.php
12 * @package phpMyAdmin
14 if (! defined('PHPMYADMIN')) {
15 exit;
18 /**
21 require_once './libraries/file_listing.php'; // used for file listing
22 require_once './libraries/bookmark.lib.php'; // used for file listing
24 /**
25 * prints the sql query boxes
27 * @usedby server_sql.php
28 * @usedby db_sql.php
29 * @usedby tbl_sql.php
30 * @usedby tbl_structure.php
31 * @usedby tbl_tracking.php
32 * @usedby querywindow.php
33 * @uses $GLOBALS['table']
34 * @uses $GLOBALS['db']
35 * @uses $GLOBALS['server']
36 * @uses $GLOBALS['goto']
37 * @uses $GLOBALS['is_upload'] from common.inc.php
38 * @uses $GLOBALS['sql_query'] from grab_globals.lib.php
39 * @uses $GLOBALS['cfg']['DefaultQueryTable']
40 * @uses $GLOBALS['cfg']['DefaultQueryDatabase']
41 * @uses $GLOBALS['cfg']['Servers']
42 * @uses $GLOBALS['cfg']['DefaultTabDatabase']
43 * @uses $GLOBALS['cfg']['Bookmark']
44 * @uses PMA_generate_common_url()
45 * @uses PMA_backquote()
46 * @uses PMA_DBI_fetch_result()
47 * @uses PMA_showMySQLDocu()
48 * @uses PMA_generate_common_hidden_inputs()
49 * @uses PMA_sqlQueryFormBookmark()
50 * @uses PMA_sqlQueryFormInsert()
51 * @uses PMA_sqlQueryFormUpload()
52 * @uses PMA_DBI_QUERY_STORE
53 * @uses PMA_set_enc_form()
54 * @uses sprintf()
55 * @uses htmlspecialchars()
56 * @uses str_replace()
57 * @uses md5()
58 * @uses function_exists()
59 * @param boolean|string $query query to display in the textarea
60 * or true to display last executed
61 * @param boolean|string $display_tab sql|files|history|full|FALSE
62 * what part to display
63 * false if not inside querywindow
64 * @param string $delimiter
66 function PMA_sqlQueryForm($query = true, $display_tab = false, $delimiter = ';')
68 // check tab to display if inside querywindow
69 if (! $display_tab) {
70 $display_tab = 'full';
71 $is_querywindow = false;
72 } else {
73 $is_querywindow = true;
76 // query to show
77 if (true === $query) {
78 $query = $GLOBALS['sql_query'];
81 // set enctype to multipart for file uploads
82 if ($GLOBALS['is_upload']) {
83 $enctype = ' enctype="multipart/form-data"';
84 } else {
85 $enctype = '';
88 $table = '';
89 $db = '';
90 if (! strlen($GLOBALS['db'])) {
91 // prepare for server related
92 $goto = empty($GLOBALS['goto']) ?
93 'server_sql.php' : $GLOBALS['goto'];
94 } elseif (! strlen($GLOBALS['table'])) {
95 // prepare for db related
96 $db = $GLOBALS['db'];
97 $goto = empty($GLOBALS['goto']) ?
98 'db_sql.php' : $GLOBALS['goto'];
99 } else {
100 $table = $GLOBALS['table'];
101 $db = $GLOBALS['db'];
102 $goto = empty($GLOBALS['goto']) ?
103 'tbl_sql.php' : $GLOBALS['goto'];
107 // start output
108 if ($is_querywindow) {
110 <form method="post" id="sqlqueryform" target="frame_content"
111 action="import.php"<?php echo $enctype; ?> name="sqlform"
112 onsubmit="var save_name = window.opener.parent.frame_content.name;
113 window.opener.parent.frame_content.name = save_name + '<?php echo time(); ?>';
114 this.target = window.opener.parent.frame_content.name;
115 return checkSqlQuery(this)">
116 <?php
117 } else {
118 echo '<form method="post" action="import.php" ' . $enctype;
119 if ($GLOBALS['cfg']['AjaxEnable']) {
120 echo ' class="ajax"';
122 echo ' id="sqlqueryform"'
123 .' onsubmit="return checkSqlQuery(this)" name="sqlform">' . "\n";
126 if ($is_querywindow) {
127 echo '<input type="hidden" name="focus_querywindow" value="true" />'
128 ."\n";
129 if ($display_tab != 'sql' && $display_tab != 'full') {
130 echo '<input type="hidden" name="sql_query" value="" />' . "\n";
131 echo '<input type="hidden" name="show_query" value="1" />' . "\n";
134 echo '<input type="hidden" name="is_js_confirmed" value="0" />' . "\n"
135 .PMA_generate_common_hidden_inputs($db, $table) . "\n"
136 .'<input type="hidden" name="pos" value="0" />' . "\n"
137 .'<input type="hidden" name="goto" value="'
138 .htmlspecialchars($goto) . '" />' . "\n"
139 .'<input type="hidden" name="message_to_show" value="'
140 . htmlspecialchars(__('Your SQL query has been executed successfully')) . '" />' . "\n"
141 .'<input type="hidden" name="prev_sql_query" value="'
142 . htmlspecialchars($query) . '" />' . "\n";
144 // display querybox
145 if ($display_tab === 'full' || $display_tab === 'sql') {
146 PMA_sqlQueryFormInsert($query, $is_querywindow, $delimiter);
149 // display uploads
150 if ($display_tab === 'files' && $GLOBALS['is_upload']) {
151 PMA_sqlQueryFormUpload();
154 // Bookmark Support
155 if ($display_tab === 'full' || $display_tab === 'history') {
156 if (! empty($GLOBALS['cfg']['Bookmark'])) {
157 PMA_sqlQueryFormBookmark();
161 // Encoding setting form appended by Y.Kawada
162 if (function_exists('PMA_set_enc_form')) {
163 echo PMA_set_enc_form(' ');
166 echo '</form>' . "\n";
167 if ($is_querywindow) {
169 <script type="text/javascript">
170 //<![CDATA[
171 if (window.opener) {
172 window.opener.parent.insertQuery();
174 //]]>
175 </script>
176 <?php
179 // print an empty div, which will be later filled with the sql query results by ajax
180 echo '<div id="sqlqueryresults"></div>';
184 * prints querybox fieldset
186 * @usedby PMA_sqlQueryForm()
187 * @uses $GLOBALS['text_dir']
188 * @uses $GLOBALS['cfg']['TextareaAutoSelect']
189 * @uses $GLOBALS['cfg']['TextareaCols']
190 * @uses $GLOBALS['cfg']['TextareaRows']
191 * @uses PMA_USR_OS
192 * @uses PMA_USR_BROWSER_AGENT
193 * @uses PMA_USR_BROWSER_VER
194 * @uses htmlspecialchars()
195 * @param string $query query to display in the textarea
196 * @param boolean $is_querywindow if inside querywindow or not
197 * @param string $delimiter default delimiter to use
199 function PMA_sqlQueryFormInsert($query = '', $is_querywindow = false, $delimiter = ';')
202 // enable auto select text in textarea
203 if ($GLOBALS['cfg']['TextareaAutoSelect']) {
204 $auto_sel = ' onfocus="selectContent(this, sql_box_locked, true)"';
205 } else {
206 $auto_sel = '';
209 // enable locking if inside query window
210 if ($is_querywindow) {
211 $locking = ' onkeypress="document.sqlform.elements[\'LockFromUpdate\'].'
212 .'checked = true;"';
213 $height = $GLOBALS['cfg']['TextareaRows'] * 1.25;
214 } else {
215 $locking = '';
216 $height = $GLOBALS['cfg']['TextareaRows'] * 2;
219 $table = '';
220 $db = '';
221 $fields_list = array();
222 if (! strlen($GLOBALS['db'])) {
223 // prepare for server related
224 $legend = sprintf(__('Run SQL query/queries on server %s'),
225 '&quot;' . htmlspecialchars(
226 ! empty($GLOBALS['cfg']['Servers'][$GLOBALS['server']]['verbose']) ? $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['verbose'] : $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['host']) . '&quot;');
227 } elseif (! strlen($GLOBALS['table'])) {
228 // prepare for db related
229 $db = $GLOBALS['db'];
230 // if you want navigation:
231 $tmp_db_link = '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase']
232 . '?' . PMA_generate_common_url($db) . '"';
233 if ($is_querywindow) {
234 $tmp_db_link .= ' target="_self"'
235 . ' onclick="this.target=window.opener.frame_content.name"';
237 $tmp_db_link .= '>'
238 . htmlspecialchars($db) . '</a>';
239 // else use
240 // $tmp_db_link = htmlspecialchars($db);
241 $legend = sprintf(__('Run SQL query/queries on database %s'), $tmp_db_link);
242 if (empty($query)) {
243 $query = PMA_expandUserString($GLOBALS['cfg']['DefaultQueryDatabase'], 'PMA_backquote');
245 } else {
246 $table = $GLOBALS['table'];
247 $db = $GLOBALS['db'];
248 // Get the list and number of fields
249 // we do a try_query here, because we could be in the query window,
250 // trying to synchonize and the table has not yet been created
251 $fields_list = PMA_DBI_fetch_result(
252 'SHOW FULL COLUMNS FROM ' . PMA_backquote($db)
253 . '.' . PMA_backquote($GLOBALS['table']));
255 $tmp_db_link = '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase']
256 . '?' . PMA_generate_common_url($db) . '"';
257 if ($is_querywindow) {
258 $tmp_db_link .= ' target="_self"'
259 . ' onclick="this.target=window.opener.frame_content.name"';
261 $tmp_db_link .= '>'
262 . htmlspecialchars($db) . '</a>';
263 // else use
264 // $tmp_db_link = htmlspecialchars($db);
265 $legend = sprintf(__('Run SQL query/queries on database %s'), $tmp_db_link);
266 if (empty($query)) {
267 $query = PMA_expandUserString($GLOBALS['cfg']['DefaultQueryTable'], 'PMA_backquote');
270 $legend .= ': ' . PMA_showMySQLDocu('SQL-Syntax', 'SELECT');
272 if (count($fields_list)) {
273 $sqlquerycontainer_id = 'sqlquerycontainer';
274 } else {
275 $sqlquerycontainer_id = 'sqlquerycontainerfull';
278 echo '<a name="querybox"></a>' . "\n"
279 .'<div id="queryboxcontainer">' . "\n"
280 .'<fieldset id="querybox">' . "\n";
281 echo '<legend>' . $legend . '</legend>' . "\n";
282 echo '<div id="queryfieldscontainer">' . "\n";
283 echo '<div id="' . $sqlquerycontainer_id . '">' . "\n"
284 .'<textarea tabindex="100" name="sql_query" id="sqlquery"'
285 .' cols="' . $GLOBALS['cfg']['TextareaCols'] . '"'
286 .' rows="' . $height . '"'
287 .' dir="' . $GLOBALS['text_dir'] . '"'
288 .$auto_sel . $locking . '>' . htmlspecialchars($query) . '</textarea>' . "\n";
289 // Add buttons to generate query easily for select all,single select,insert,update and delete
290 if(count($fields_list)) {
291 echo '<input type="button" value="SELECT *" id="selectall" class="sqlbutton" />';
292 echo '<input type="button" value="SELECT" id="select" class="sqlbutton" />';
293 echo '<input type="button" value="INSERT" id="insert" class="sqlbutton" />';
294 echo '<input type="button" value="UPDATE" id="update" class="sqlbutton" />';
295 echo '<input type="button" value="DELETE" id="delete" class="sqlbutton" />';
297 echo '</div>' . "\n";
299 if (count($fields_list)) {
300 echo '<div id="tablefieldscontainer">' . "\n"
301 .'<label>' . __('Columns') . '</label>' . "\n"
302 .'<select id="tablefields" name="dummy" '
303 .'size="' . ($GLOBALS['cfg']['TextareaRows'] - 2) . '" '
304 .'multiple="multiple" ondblclick="insertValueQuery()">' . "\n";
305 foreach ($fields_list as $field) {
306 echo '<option value="'
307 .PMA_backquote(htmlspecialchars($field['Field'])) . '"';
308 if (isset($field['Field']) && strlen($field['Field']) && isset($field['Comment'])) {
309 echo ' title="' . htmlspecialchars($field['Comment']) . '"';
311 echo '>' . htmlspecialchars($field['Field']) . '</option>' . "\n";
313 echo '</select>' . "\n"
314 .'<div id="tablefieldinsertbuttoncontainer">' . "\n";
315 if ($GLOBALS['cfg']['PropertiesIconic']) {
316 echo '<input type="button" name="insert" value="&lt;&lt;"'
317 .' onclick="insertValueQuery()"'
318 .' title="' . __('Insert') . '" />' . "\n";
319 } else {
320 echo '<input type="button" name="insert"'
321 .' value="' . __('Insert') . '"'
322 .' onclick="insertValueQuery()" />' . "\n";
324 echo '</div>' . "\n"
325 .'</div>' . "\n";
328 echo '<div class="clearfloat"></div>' . "\n";
329 echo '</div>' . "\n";
331 if (! empty($GLOBALS['cfg']['Bookmark'])) {
333 <div id="bookmarkoptions">
334 <div class="formelement">
335 <label for="bkm_label">
336 <?php echo __('Bookmark this SQL query'); ?>:</label>
337 <input type="text" name="bkm_label" id="bkm_label" tabindex="110" value="" />
338 </div>
339 <div class="formelement">
340 <input type="checkbox" name="bkm_all_users" tabindex="111" id="id_bkm_all_users"
341 value="true" />
342 <label for="id_bkm_all_users">
343 <?php echo __('Let every user access this bookmark'); ?></label>
344 </div>
345 <div class="formelement">
346 <input type="checkbox" name="bkm_replace" tabindex="112" id="id_bkm_replace"
347 value="true" />
348 <label for="id_bkm_replace">
349 <?php echo __('Replace existing bookmark of same name'); ?></label>
350 </div>
351 </div>
352 <?php
355 echo '<div class="clearfloat"></div>' . "\n";
356 echo '</fieldset>' . "\n"
357 .'</div>' . "\n";
359 echo '<fieldset id="queryboxfooter" class="tblFooters">' . "\n";
360 echo '<div class="formelement">' . "\n";
361 if ($is_querywindow) {
363 <script type="text/javascript">
364 //<![CDATA[
365 document.writeln(' <input type="checkbox" name="LockFromUpdate" checked="checked" tabindex="120" id="checkbox_lock" /> <label for="checkbox_lock"><?php echo __('Do not overwrite this query from outside the window'); ?></label> ');
366 //]]>
367 </script>
368 <?php
370 echo '</div>' . "\n";
371 echo '<div class="formelement">' . "\n";
372 echo '<label for="id_sql_delimiter">[ ' . __('Delimiter')
373 .'</label>' . "\n";
374 echo '<input type="text" name="sql_delimiter" tabindex="131" size="3" '
375 .'value="' . $delimiter . '" '
376 .'id="id_sql_delimiter" /> ]' . "\n";
378 echo '<input type="checkbox" name="show_query" value="1" '
379 .'id="checkbox_show_query" tabindex="132" checked="checked" />' . "\n"
380 .'<label for="checkbox_show_query">' . __(' Show this query here again ')
381 .'</label>' . "\n";
383 echo '</div>' . "\n";
384 echo '<input type="submit" name="SQL" tabindex="200" value="' . __('Go') . '" />'
385 ."\n";
386 echo '<div class="clearfloat"></div>' . "\n";
387 echo '</fieldset>' . "\n";
391 * prints bookmark fieldset
393 * @usedby PMA_sqlQueryForm()
394 * @uses PMA_Bookmark_getList()
395 * @uses $GLOBALS['db']
396 * @uses $GLOBALS['pmaThemeImage']
397 * @uses $GLOBALS['cfg']['ReplaceHelpImg']
398 * @uses count()
399 * @uses htmlspecialchars()
401 function PMA_sqlQueryFormBookmark()
403 $bookmark_list = PMA_Bookmark_getList($GLOBALS['db']);
404 if (! $bookmark_list || count($bookmark_list) < 1) {
405 return;
408 echo '<fieldset id="bookmarkoptions">';
409 echo '<legend>';
410 echo __('Bookmarked SQL query') . '</legend>' . "\n";
411 echo '<div class="formelement">';
412 echo '<select name="id_bookmark">' . "\n";
413 echo '<option value="">&nbsp;</option>' . "\n";
414 foreach ($bookmark_list as $key => $value) {
415 echo '<option value="' . htmlspecialchars($key) . '">'
416 .htmlspecialchars($value) . '</option>' . "\n";
418 // &nbsp; is required for correct display with styles/line height
419 echo '</select>&nbsp;' . "\n";
420 echo '</div>' . "\n";
421 echo '<div class="formelement">' . "\n";
422 echo __('Variable');
423 if ($GLOBALS['cfg']['ReplaceHelpImg']) {
424 echo ' <a href="./Documentation.html#faqbookmark"'
425 .' target="documentation">'
426 .'<img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 'b_help.png"'
427 .' border="0" width="11" height="11" align="middle"'
428 .' alt="' . __('Documentation') . '" /></a> ';
429 } else {
430 echo ' (<a href="./Documentation.html#faqbookmark"'
431 .' target="documentation">' . __('Documentation') . '</a>): ';
433 echo '<input type="text" name="bookmark_variable" class="textfield"'
434 .' size="10" />' . "\n";
435 echo '</div>' . "\n";
436 echo '<div class="formelement">' . "\n";
437 echo '<input type="radio" name="action_bookmark" value="0"'
438 .' id="radio_bookmark_exe" checked="checked" />'
439 .'<label for="radio_bookmark_exe">' . __('Submit')
440 .'</label>' . "\n";
441 echo '<input type="radio" name="action_bookmark" value="1"'
442 .' id="radio_bookmark_view" />'
443 .'<label for="radio_bookmark_view">' . __('View only')
444 .'</label>' . "\n";
445 echo '<input type="radio" name="action_bookmark" value="2"'
446 .' id="radio_bookmark_del" />'
447 .'<label for="radio_bookmark_del">' . __('Delete')
448 .'</label>' . "\n";
449 echo '</div>' . "\n";
450 echo '<div class="clearfloat"></div>' . "\n";
451 echo '</fieldset>' . "\n";
453 echo '<fieldset id="bookmarkoptionsfooter" class="tblFooters">' . "\n";
454 echo '<input type="submit" name="SQL" value="' . __('Go') . '" />';
455 echo '<div class="clearfloat"></div>' . "\n";
456 echo '</fieldset>' . "\n";
460 * prints bookmark fieldset
462 * @usedby PMA_sqlQueryForm()
463 * @uses $GLOBALS['cfg']['GZipDump']
464 * @uses $GLOBALS['cfg']['BZipDump']
465 * @uses $GLOBALS['cfg']['UploadDir']
466 * @uses $GLOBALS['cfg']['AvailableCharsets']
467 * @uses $GLOBALS['charset']
468 * @uses $GLOBALS['max_upload_size']
469 * @uses PMA_supportedDecompressions()
470 * @uses PMA_getFileSelectOptions()
471 * @uses PMA_displayMaximumUploadSize()
472 * @uses PMA_generateCharsetDropdownBox()
473 * @uses PMA_generateHiddenMaxFileSize()
474 * @uses PMA_CSDROPDOWN_CHARSET
475 * @uses empty()
477 function PMA_sqlQueryFormUpload(){
478 $errors = array ();
480 $matcher = '@\.sql(\.(' . PMA_supportedDecompressions() . '))?$@'; // we allow only SQL here
482 if (!empty($GLOBALS['cfg']['UploadDir'])) {
483 $files = PMA_getFileSelectOptions(PMA_userDir($GLOBALS['cfg']['UploadDir']), $matcher, (isset($timeout_passed) && $timeout_passed && isset($local_import_file)) ? $local_import_file : '');
484 } else {
485 $files = '';
488 // start output
489 echo '<fieldset id="">';
490 echo '<legend>';
491 echo __('Location of the text file') . '</legend>';
492 echo '<div class="formelement">';
493 echo '<input type="file" name="sql_file" class="textfield" /> ';
494 echo PMA_displayMaximumUploadSize($GLOBALS['max_upload_size']);
495 // some browsers should respect this :)
496 echo PMA_generateHiddenMaxFileSize($GLOBALS['max_upload_size']) . "\n";
497 echo '</div>';
499 if ($files === FALSE) {
500 $errors[] = PMA_Message::error( __('The directory you set for upload work cannot be reached'));
501 } elseif (!empty($files)) {
502 echo '<div class="formelement">';
503 echo '<strong>' . __('web server upload directory') .':</strong>' . "\n";
504 echo '<select size="1" name="sql_localfile">' . "\n";
505 echo '<option value="" selected="selected"></option>' . "\n";
506 echo $files;
507 echo '</select>' . "\n";
508 echo '</div>';
511 echo '<div class="clearfloat"></div>' . "\n";
512 echo '</fieldset>';
515 echo '<fieldset id="" class="tblFooters">';
516 echo __('Character set of the file:') . "\n";
517 echo PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_CHARSET,
518 'charset_of_file', null, 'utf8', FALSE);
519 echo '<input type="submit" name="SQL" value="' . __('Go')
520 .'" />' . "\n";
521 echo '<div class="clearfloat"></div>' . "\n";
522 echo '</fieldset>';
524 foreach ($errors as $error) {
525 $error->display();