Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / sql_query_form.lib.php
blobbf2e81cca083718a74d210491c682dfe274bf1eb
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.lib.php'; // used for file listing
22 require_once './libraries/bookmark.lib.php'; // used for bookmarks
24 /**
25 * Prints the sql query boxes
27 * @param boolean|string $query query to display in the textarea
28 * or true to display last executed
29 * @param boolean|string $display_tab sql|files|history|full|false
30 * what part to display
31 * false if not inside querywindow
32 * @param string $delimiter delimeter
34 * @return void
36 * @usedby server_sql.php
37 * @usedby db_sql.php
38 * @usedby tbl_sql.php
39 * @usedby tbl_structure.php
40 * @usedby tbl_tracking.php
41 * @usedby querywindow.php
43 function PMA_sqlQueryForm($query = true, $display_tab = false, $delimiter = ';')
45 // check tab to display if inside querywindow
46 if (! $display_tab) {
47 $display_tab = 'full';
48 $is_querywindow = false;
49 } else {
50 $is_querywindow = true;
53 // query to show
54 if (true === $query) {
55 $query = $GLOBALS['sql_query'];
58 // set enctype to multipart for file uploads
59 if ($GLOBALS['is_upload']) {
60 $enctype = ' enctype="multipart/form-data"';
61 } else {
62 $enctype = '';
65 $table = '';
66 $db = '';
67 if (! strlen($GLOBALS['db'])) {
68 // prepare for server related
69 $goto = empty($GLOBALS['goto']) ?
70 'server_sql.php' : $GLOBALS['goto'];
71 } elseif (! strlen($GLOBALS['table'])) {
72 // prepare for db related
73 $db = $GLOBALS['db'];
74 $goto = empty($GLOBALS['goto']) ?
75 'db_sql.php' : $GLOBALS['goto'];
76 } else {
77 $table = $GLOBALS['table'];
78 $db = $GLOBALS['db'];
79 $goto = empty($GLOBALS['goto']) ?
80 'tbl_sql.php' : $GLOBALS['goto'];
84 // start output
85 if ($is_querywindow) {
87 <form method="post" id="sqlqueryform" target="frame_content"
88 action="import.php"<?php echo $enctype; ?> name="sqlform"
89 onsubmit="var save_name = window.opener.parent.frame_content.name;
90 window.opener.parent.frame_content.name = save_name + '<?php echo time(); ?>';
91 this.target = window.opener.parent.frame_content.name;
92 return checkSqlQuery(this)">
93 <?php
94 } else {
95 echo '<form method="post" action="import.php" ' . $enctype;
96 echo ' class="ajax"';
97 echo ' id="sqlqueryform" name="sqlform">' . "\n";
100 if ($is_querywindow) {
101 echo '<input type="hidden" name="focus_querywindow" value="true" />'
102 ."\n";
103 if ($display_tab != 'sql' && $display_tab != 'full') {
104 echo '<input type="hidden" name="sql_query" value="" />' . "\n";
105 echo '<input type="hidden" name="show_query" value="1" />' . "\n";
108 echo '<input type="hidden" name="is_js_confirmed" value="0" />' . "\n"
109 .PMA_generate_common_hidden_inputs($db, $table) . "\n"
110 .'<input type="hidden" name="pos" value="0" />' . "\n"
111 .'<input type="hidden" name="goto" value="'
112 .htmlspecialchars($goto) . '" />' . "\n"
113 .'<input type="hidden" name="message_to_show" value="'
114 . __('Your SQL query has been executed successfully') . '" />' . "\n"
115 .'<input type="hidden" name="prev_sql_query" value="'
116 . htmlspecialchars($query) . '" />' . "\n";
118 // display querybox
119 if ($display_tab === 'full' || $display_tab === 'sql') {
120 PMA_sqlQueryFormInsert($query, $is_querywindow, $delimiter);
123 // display uploads
124 if ($display_tab === 'files' && $GLOBALS['is_upload']) {
125 PMA_sqlQueryFormUpload();
128 // Bookmark Support
129 if ($display_tab === 'full' || $display_tab === 'history') {
130 if (! empty($GLOBALS['cfg']['Bookmark'])) {
131 PMA_sqlQueryFormBookmark();
135 // Encoding setting form appended by Y.Kawada
136 if (function_exists('PMA_set_enc_form')) {
137 echo PMA_set_enc_form(' ');
140 echo '</form>' . "\n";
142 // print an empty div, which will be later filled with
143 // the sql query results by ajax
144 echo '<div id="sqlqueryresults"></div>';
148 * Prints querybox fieldset
150 * @param string $query query to display in the textarea
151 * @param boolean $is_querywindow if inside querywindow or not
152 * @param string $delimiter default delimiter to use
154 * @return void
156 * @usedby PMA_sqlQueryForm()
158 function PMA_sqlQueryFormInsert(
159 $query = '', $is_querywindow = false, $delimiter = ';'
161 // enable auto select text in textarea
162 if ($GLOBALS['cfg']['TextareaAutoSelect']) {
163 $auto_sel = ' onclick="selectContent(this, sql_box_locked, true)"';
164 } else {
165 $auto_sel = '';
168 // enable locking if inside query window
169 if ($is_querywindow) {
170 $locking = ' onkeypress="document.sqlform.elements[\'LockFromUpdate\'].'
171 .'checked = true;"';
172 $height = $GLOBALS['cfg']['TextareaRows'] * 1.25;
173 } else {
174 $locking = '';
175 $height = $GLOBALS['cfg']['TextareaRows'] * 2;
178 $table = '';
179 $db = '';
180 $fields_list = array();
181 if (! strlen($GLOBALS['db'])) {
182 // prepare for server related
183 $legend = sprintf(
184 __('Run SQL query/queries on server %s'),
185 '&quot;' . htmlspecialchars(
186 ! empty($GLOBALS['cfg']['Servers'][$GLOBALS['server']]['verbose'])
187 ? $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['verbose']
188 : $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['host']
189 ) . '&quot;'
191 } elseif (! strlen($GLOBALS['table'])) {
192 // prepare for db related
193 $db = $GLOBALS['db'];
194 // if you want navigation:
195 $tmp_db_link = '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase']
196 . '?' . PMA_generate_common_url($db) . '"';
197 if ($is_querywindow) {
198 $tmp_db_link .= ' target="_self"'
199 . ' onclick="this.target=window.opener.frame_content.name"';
201 $tmp_db_link .= '>'
202 . htmlspecialchars($db) . '</a>';
203 // else use
204 // $tmp_db_link = htmlspecialchars($db);
205 $legend = sprintf(__('Run SQL query/queries on database %s'), $tmp_db_link);
206 if (empty($query)) {
207 $query = PMA_Util::expandUserString(
208 $GLOBALS['cfg']['DefaultQueryDatabase'], 'backquote'
211 } else {
212 $table = $GLOBALS['table'];
213 $db = $GLOBALS['db'];
214 // Get the list and number of fields
215 // we do a try_query here, because we could be in the query window,
216 // trying to synchonize and the table has not yet been created
217 $fields_list = PMA_DBI_get_columns($db, $GLOBALS['table'], null, true);
219 $tmp_db_link = '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase']
220 . '?' . PMA_generate_common_url($db) . '"';
221 if ($is_querywindow) {
222 $tmp_db_link .= ' target="_self"'
223 . ' onclick="this.target=window.opener.frame_content.name"';
225 $tmp_db_link .= '>'
226 . htmlspecialchars($db) . '</a>';
227 // else use
228 // $tmp_db_link = htmlspecialchars($db);
229 $legend = sprintf(__('Run SQL query/queries on database %s'), $tmp_db_link);
230 if (empty($query)) {
231 $query = PMA_Util::expandUserString(
232 $GLOBALS['cfg']['DefaultQueryTable'], 'backquote'
236 $legend .= ': ' . PMA_Util::showMySQLDocu('SQL-Syntax', 'SELECT');
238 if (count($fields_list)) {
239 $sqlquerycontainer_id = 'sqlquerycontainer';
240 } else {
241 $sqlquerycontainer_id = 'sqlquerycontainerfull';
244 echo '<a id="querybox"></a>' . "\n"
245 .'<div id="queryboxcontainer">' . "\n"
246 .'<fieldset id="queryboxf">' . "\n";
247 echo '<legend>' . $legend . '</legend>' . "\n";
248 echo '<div id="queryfieldscontainer">' . "\n";
249 echo '<div id="' . $sqlquerycontainer_id . '">' . "\n"
250 .'<textarea tabindex="100" name="sql_query" id="sqlquery"'
251 .' cols="' . $GLOBALS['cfg']['TextareaCols'] . '"'
252 .' rows="' . $height . '"'
253 .' dir="' . $GLOBALS['text_dir'] . '"'
254 .$auto_sel . $locking . '>'
255 . htmlspecialchars($query)
256 . '</textarea>' . "\n";
257 // Add buttons to generate query easily for
258 // select all, single select, insert, update and delete
259 if (count($fields_list)) {
260 echo '<input type="button" value="SELECT *" id="selectall" class="button sqlbutton" />';
261 echo '<input type="button" value="SELECT" id="select" class="button sqlbutton" />';
262 echo '<input type="button" value="INSERT" id="insert" class="button sqlbutton" />';
263 echo '<input type="button" value="UPDATE" id="update" class="button sqlbutton" />';
264 echo '<input type="button" value="DELETE" id="delete" class="button sqlbutton" />';
266 echo '<input type="button" value="' . __('Clear') . '" id="clear" class="button sqlbutton" />';
267 echo '</div>' . "\n";
269 if (count($fields_list)) {
270 echo '<div id="tablefieldscontainer">' . "\n"
271 .'<label>' . __('Columns') . '</label>' . "\n"
272 .'<select id="tablefields" name="dummy" '
273 .'size="' . ($GLOBALS['cfg']['TextareaRows'] - 2) . '" '
274 .'multiple="multiple" ondblclick="insertValueQuery()">' . "\n";
275 foreach ($fields_list as $field) {
276 echo '<option value="'
277 .PMA_Util::backquote(htmlspecialchars($field['Field'])) . '"';
278 if (isset($field['Field'])
279 && strlen($field['Field'])
280 && isset($field['Comment'])
282 echo ' title="' . htmlspecialchars($field['Comment']) . '"';
284 echo '>' . htmlspecialchars($field['Field']) . '</option>' . "\n";
286 echo '</select>' . "\n"
287 .'<div id="tablefieldinsertbuttoncontainer">' . "\n";
288 if ($GLOBALS['cfg']['PropertiesIconic']) {
289 echo '<input type="button" class="button" name="insert" value="&lt;&lt;"'
290 .' onclick="insertValueQuery()"'
291 .' title="' . __('Insert') . '" />' . "\n";
292 } else {
293 echo '<input type="button" class="button" name="insert"'
294 .' value="' . __('Insert') . '"'
295 .' onclick="insertValueQuery()" />' . "\n";
297 echo '</div>' . "\n"
298 .'</div>' . "\n";
301 echo '<div class="clearfloat"></div>' . "\n";
302 echo '</div>' . "\n";
304 if (! empty($GLOBALS['cfg']['Bookmark'])) {
306 <div id="bookmarkoptions">
307 <div class="formelement">
308 <label for="bkm_label">
309 <?php echo __('Bookmark this SQL query'); ?>:</label>
310 <input type="text" name="bkm_label" id="bkm_label" tabindex="110" value="" />
311 </div>
312 <div class="formelement">
313 <input type="checkbox" name="bkm_all_users" tabindex="111" id="id_bkm_all_users" value="true" />
314 <label for="id_bkm_all_users">
315 <?php echo __('Let every user access this bookmark'); ?></label>
316 </div>
317 <div class="formelement">
318 <input type="checkbox" name="bkm_replace" tabindex="112" id="id_bkm_replace"
319 value="true" />
320 <label for="id_bkm_replace">
321 <?php echo __('Replace existing bookmark of same name'); ?></label>
322 </div>
323 </div>
324 <?php
327 echo '<div class="clearfloat"></div>' . "\n";
328 echo '</fieldset>' . "\n"
329 .'</div>' . "\n";
331 echo '<fieldset id="queryboxfooter" class="tblFooters">' . "\n";
332 echo '<div class="formelement">' . "\n";
333 if ($is_querywindow) {
335 <script type="text/javascript">
336 //<![CDATA[
337 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> ');
338 //]]>
339 </script>
340 <?php
342 echo '</div>' . "\n";
343 echo '<div class="formelement">' . "\n";
344 echo '<label for="id_sql_delimiter">[ ' . __('Delimiter')
345 .'</label>' . "\n";
346 echo '<input type="text" name="sql_delimiter" tabindex="131" size="3" '
347 .'value="' . $delimiter . '" '
348 .'id="id_sql_delimiter" /> ]' . "\n";
350 echo '<input type="checkbox" name="show_query" value="1" '
351 .'id="checkbox_show_query" tabindex="132" checked="checked" />' . "\n"
352 .'<label for="checkbox_show_query">' . __('Show this query here again')
353 .'</label>' . "\n";
355 if (! $is_querywindow) {
356 echo '<input type="checkbox" name="retain_query_box" value="1" '
357 . 'id="retain_query_box" tabindex="133" '
358 . ($GLOBALS['cfg']['RetainQueryBox'] === false
359 ? '' : ' checked="checked"')
360 . ' />'
361 . '<label for="retain_query_box">' . __('Retain query box')
362 . '</label>';
364 echo '</div>' . "\n";
365 echo '<input type="submit" id="button_submit_query" name="SQL" tabindex="200" value="' . __('Go') . '" />'
366 ."\n";
367 echo '<div class="clearfloat"></div>' . "\n";
368 echo '</fieldset>' . "\n";
372 * Prints bookmark fieldset
374 * @return void
376 * @usedby PMA_sqlQueryForm()
378 function PMA_sqlQueryFormBookmark()
380 $bookmark_list = PMA_Bookmark_getList($GLOBALS['db']);
381 if (! $bookmark_list || count($bookmark_list) < 1) {
382 return;
385 echo '<fieldset id="bookmarkoptions">';
386 echo '<legend>';
387 echo __('Bookmarked SQL query') . '</legend>' . "\n";
388 echo '<div class="formelement">';
389 echo '<select name="id_bookmark" id="id_bookmark">' . "\n";
390 echo '<option value="">&nbsp;</option>' . "\n";
391 foreach ($bookmark_list as $key => $value) {
392 echo '<option value="' . htmlspecialchars($key) . '">'
393 .htmlspecialchars($value) . '</option>' . "\n";
395 // &nbsp; is required for correct display with styles/line height
396 echo '</select>&nbsp;' . "\n";
397 echo '</div>' . "\n";
398 echo '<div class="formelement">' . "\n";
399 echo __('Variable');
400 echo PMA_Util::showDocu('faq', 'faqbookmark');
401 echo '<input type="text" name="bookmark_variable" class="textfield"'
402 .' size="10" />' . "\n";
403 echo '</div>' . "\n";
404 echo '<div class="formelement">' . "\n";
405 echo '<input type="radio" name="action_bookmark" value="0"'
406 .' id="radio_bookmark_exe" checked="checked" />'
407 .'<label for="radio_bookmark_exe">' . __('Submit')
408 .'</label>' . "\n";
409 echo '<input type="radio" name="action_bookmark" value="1"'
410 .' id="radio_bookmark_view" />'
411 .'<label for="radio_bookmark_view">' . __('View only')
412 .'</label>' . "\n";
413 echo '<input type="radio" name="action_bookmark" value="2"'
414 .' id="radio_bookmark_del" />'
415 .'<label for="radio_bookmark_del">' . __('Delete')
416 .'</label>' . "\n";
417 echo '</div>' . "\n";
418 echo '<div class="clearfloat"></div>' . "\n";
419 echo '</fieldset>' . "\n";
421 echo '<fieldset id="bookmarkoptionsfooter" class="tblFooters">' . "\n";
422 echo '<input type="submit" name="SQL" id="button_submit_bookmark" value="' . __('Go') . '" />';
423 echo '<div class="clearfloat"></div>' . "\n";
424 echo '</fieldset>' . "\n";
428 * Prints bookmark fieldset
430 * @return void
432 * @usedby PMA_sqlQueryForm()
434 function PMA_sqlQueryFormUpload()
436 global $timeout_passed, $local_import_file;
438 $errors = array();
440 // we allow only SQL here
441 $matcher = '@\.sql(\.(' . PMA_supportedDecompressions() . '))?$@';
443 if (!empty($GLOBALS['cfg']['UploadDir'])) {
444 $files = PMA_getFileSelectOptions(
445 PMA_Util::userDir($GLOBALS['cfg']['UploadDir']), $matcher,
446 (isset($timeout_passed) && $timeout_passed && isset($local_import_file))
447 ? $local_import_file
448 : ''
450 } else {
451 $files = '';
454 // start output
455 echo '<fieldset id="">';
456 echo '<legend>';
457 echo __('Browse your computer:') . '</legend>';
458 echo '<div class="formelement">';
459 echo '<input type="file" name="sql_file" class="textfield" /> ';
460 echo PMA_Util::getFormattedMaximumUploadSize($GLOBALS['max_upload_size']);
461 // some browsers should respect this :)
462 echo PMA_Util::generateHiddenMaxFileSize($GLOBALS['max_upload_size']) . "\n";
463 echo '</div>';
465 if ($files === false) {
466 $errors[] = PMA_Message::error(__('The directory you set for upload work cannot be reached'));
467 } elseif (!empty($files)) {
468 echo '<div class="formelement">';
469 echo '<strong>' . __('web server upload directory') .':</strong>' . "\n";
470 echo '<select size="1" name="sql_localfile">' . "\n";
471 echo '<option value="" selected="selected"></option>' . "\n";
472 echo $files;
473 echo '</select>' . "\n";
474 echo '</div>';
477 echo '<div class="clearfloat"></div>' . "\n";
478 echo '</fieldset>';
481 echo '<fieldset id="" class="tblFooters">';
482 echo __('Character set of the file:') . "\n";
483 echo PMA_generateCharsetDropdownBox(
484 PMA_CSDROPDOWN_CHARSET,
485 'charset_of_file', null, 'utf8', false
487 echo '<input type="submit" name="SQL" value="' . __('Go')
488 .'" />' . "\n";
489 echo '<div class="clearfloat"></div>' . "\n";
490 echo '</fieldset>';
492 foreach ($errors as $error) {
493 $error->display();