UPDATE 4.4.0.0
[phpmyadmin.git] / libraries / sql_query_form.lib.php
blob99dad8d89c2a1864f00d3330f84fff3a9747ff2a
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 * @package PhpMyAdmin
13 if (! defined('PHPMYADMIN')) {
14 exit;
17 /**
20 require_once './libraries/file_listing.lib.php'; // used for file listing
21 require_once './libraries/bookmark.lib.php'; // used for bookmarks
23 /**
24 * return HTML for the sql query boxes
26 * @param boolean|string $query query to display in the textarea
27 * or true to display last executed
28 * @param boolean|string $display_tab sql|files|history|full|false
29 * what part to display
30 * false if not inside querywindow
31 * @param string $delimiter delimiter
33 * @return string
35 * @usedby server_sql.php
36 * @usedby db_sql.php
37 * @usedby tbl_sql.php
38 * @usedby tbl_structure.php
39 * @usedby tbl_tracking.php
41 function PMA_getHtmlForSqlQueryForm(
42 $query = true, $display_tab = false, $delimiter = ';'
43 ) {
44 $html = '';
45 if (! $display_tab) {
46 $display_tab = 'full';
48 // query to show
49 if (true === $query) {
50 $query = $GLOBALS['sql_query'];
53 // set enctype to multipart for file uploads
54 if ($GLOBALS['is_upload']) {
55 $enctype = ' enctype="multipart/form-data"';
56 } else {
57 $enctype = '';
60 $table = '';
61 $db = '';
62 if (! /*overload*/mb_strlen($GLOBALS['db'])) {
63 // prepare for server related
64 $goto = empty($GLOBALS['goto']) ?
65 'server_sql.php' : $GLOBALS['goto'];
66 } elseif (! /*overload*/mb_strlen($GLOBALS['table'])) {
67 // prepare for db related
68 $db = $GLOBALS['db'];
69 $goto = empty($GLOBALS['goto']) ?
70 'db_sql.php' : $GLOBALS['goto'];
71 } else {
72 $table = $GLOBALS['table'];
73 $db = $GLOBALS['db'];
74 $goto = empty($GLOBALS['goto']) ?
75 'tbl_sql.php' : $GLOBALS['goto'];
78 // start output
79 $html .= '<form method="post" action="import.php" ' . $enctype;
80 $html .= ' class="ajax lock-page"';
81 $html .= ' id="sqlqueryform" name="sqlform">' . "\n";
83 $html .= '<input type="hidden" name="is_js_confirmed" value="0" />'
84 . "\n" . PMA_URL_getHiddenInputs($db, $table) . "\n"
85 . '<input type="hidden" name="pos" value="0" />' . "\n"
86 . '<input type="hidden" name="goto" value="'
87 . htmlspecialchars($goto) . '" />' . "\n"
88 . '<input type="hidden" name="message_to_show" value="'
89 . __('Your SQL query has been executed successfully.') . '" />'
90 . "\n" . '<input type="hidden" name="prev_sql_query" value="'
91 . htmlspecialchars($query) . '" />' . "\n";
93 // display querybox
94 if ($display_tab === 'full' || $display_tab === 'sql') {
95 $html .= PMA_getHtmlForSqlQueryFormInsert(
96 $query, $delimiter
100 // display uploads
101 if ($display_tab === 'files' && $GLOBALS['is_upload']) {
102 $html .= PMA_getHtmlForSqlQueryFormUpload();
105 // Bookmark Support
106 if ($display_tab === 'full' || $display_tab === 'history') {
107 $cfgBookmark = PMA_Bookmark_getParams();
108 if ($cfgBookmark) {
109 $html .= PMA_getHtmlForSqlQueryFormBookmark();
113 // Encoding setting form appended by Y.Kawada
114 if (function_exists('PMA_Kanji_encodingForm')) {
115 $html .= PMA_Kanji_encodingForm();
118 $html .= '</form>' . "\n";
119 // print an empty div, which will be later filled with
120 // the sql query results by ajax
121 $html .= '<div id="sqlqueryresultsouter"></div>';
123 return $html;
127 * return HTML for Sql Query Form Insert
129 * @param string $query query to display in the textarea
130 * @param string $delimiter default delimiter to use
132 * @return string
134 * @usedby PMA_getHtmlForSqlQueryForm()
136 function PMA_getHtmlForSqlQueryFormInsert(
137 $query = '', $delimiter = ';'
139 // enable auto select text in textarea
140 if ($GLOBALS['cfg']['TextareaAutoSelect']) {
141 $auto_sel = ' onclick="selectContent(this, sql_box_locked, true);"';
142 } else {
143 $auto_sel = '';
146 $locking = '';
147 $height = $GLOBALS['cfg']['TextareaRows'] * 2;
149 $fields_list = array();
150 if (! /*overload*/mb_strlen($GLOBALS['db'])) {
151 // prepare for server related
152 $legend = sprintf(
153 __('Run SQL query/queries on server %s'),
154 '&quot;' . htmlspecialchars(
155 ! empty($GLOBALS['cfg']['Servers'][$GLOBALS['server']]['verbose'])
156 ? $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['verbose']
157 : $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['host']
158 ) . '&quot;'
160 } elseif (! /*overload*/mb_strlen($GLOBALS['table'])) {
161 // prepare for db related
162 $db = $GLOBALS['db'];
163 // if you want navigation:
164 $tmp_db_link = '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase']
165 . PMA_URL_getCommon(array('db' => $db)) . '"';
166 $tmp_db_link .= '>'
167 . htmlspecialchars($db) . '</a>';
168 // else use
169 // $tmp_db_link = htmlspecialchars($db);
170 $legend = sprintf(__('Run SQL query/queries on database %s'), $tmp_db_link);
171 if (empty($query)) {
172 $query = PMA_Util::expandUserString(
173 $GLOBALS['cfg']['DefaultQueryDatabase'], 'backquote'
176 } else {
177 $db = $GLOBALS['db'];
178 // Get the list and number of fields
179 // we do a try_query here, because we could be in the query window,
180 // trying to synchronize and the table has not yet been created
181 $fields_list = $GLOBALS['dbi']->getColumns(
182 $db, $GLOBALS['table'], null, true
185 $tmp_db_link = '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase']
186 . PMA_URL_getCommon(array('db' => $db)) . '"';
187 $tmp_db_link .= '>'
188 . htmlspecialchars($db) . '</a>';
189 // else use
190 // $tmp_db_link = htmlspecialchars($db);
191 $legend = sprintf(__('Run SQL query/queries on database %s'), $tmp_db_link);
192 if (empty($query)) {
193 $query = PMA_Util::expandUserString(
194 $GLOBALS['cfg']['DefaultQueryTable'], 'backquote'
198 $legend .= ': ' . PMA_Util::showMySQLDocu('SELECT');
200 if (count($fields_list)) {
201 $sqlquerycontainer_id = 'sqlquerycontainer';
202 } else {
203 $sqlquerycontainer_id = 'sqlquerycontainerfull';
206 $html = '<a id="querybox"></a>'
207 . '<div id="queryboxcontainer">'
208 . '<fieldset id="queryboxf">';
209 $html .= '<legend>' . $legend . '</legend>';
210 $html .= '<div id="queryfieldscontainer">';
211 $html .= '<div id="' . $sqlquerycontainer_id . '">'
212 . '<textarea tabindex="100" name="sql_query" id="sqlquery"'
213 . ' cols="' . $GLOBALS['cfg']['TextareaCols'] . '"'
214 . ' rows="' . $height . '"'
215 . ' dir="' . $GLOBALS['text_dir'] . '"'
216 . $auto_sel . $locking . '>'
217 . htmlspecialchars($query)
218 . '</textarea>';
219 $html .= '<div id="querymessage"></div>';
220 // Add buttons to generate query easily for
221 // select all, single select, insert, update and delete
222 if (count($fields_list)) {
223 $html .= '<input type="button" value="SELECT *" id="selectall"'
224 . ' class="button sqlbutton" />';
225 $html .= '<input type="button" value="SELECT" id="select"'
226 . ' class="button sqlbutton" />';
227 $html .= '<input type="button" value="INSERT" id="insert"'
228 . ' class="button sqlbutton" />';
229 $html .= '<input type="button" value="UPDATE" id="update"'
230 . ' class="button sqlbutton" />';
231 $html .= '<input type="button" value="DELETE" id="delete"'
232 . ' class="button sqlbutton" />';
234 $html .= '<input type="button" value="' . __('Clear') . '" id="clear"'
235 . ' class="button sqlbutton" />';
236 if ($GLOBALS['cfg']['CodemirrorEnable']) {
237 $html .= '<input type="button" value="' . __('Format') . '" id="format"'
238 . ' class="button sqlbutton" />';
240 $html .= '<input type="button" value="' . __('Get auto-saved query') . '" id="saved"'
241 . ' class="button sqlbutton" />';
242 $html .= '</div>' . "\n";
244 if (count($fields_list)) {
245 $html .= '<div id="tablefieldscontainer">'
246 . '<label>' . __('Columns') . '</label>'
247 . '<select id="tablefields" name="dummy" '
248 . 'size="' . ($GLOBALS['cfg']['TextareaRows'] - 2) . '" '
249 . 'multiple="multiple" ondblclick="insertValueQuery()">';
250 foreach ($fields_list as $field) {
251 $html .= '<option value="'
252 . PMA_Util::backquote(htmlspecialchars($field['Field'])) . '"';
253 if (isset($field['Field'])
254 && /*overload*/mb_strlen($field['Field'])
255 && isset($field['Comment'])
257 $html .= ' title="' . htmlspecialchars($field['Comment']) . '"';
259 $html .= '>' . htmlspecialchars($field['Field']) . '</option>' . "\n";
261 $html .= '</select>'
262 . '<div id="tablefieldinsertbuttoncontainer">';
263 if (PMA_Util::showIcons('ActionLinksMode')) {
264 $html .= '<input type="button" class="button" name="insert"'
265 . ' value="&lt;&lt;" onclick="insertValueQuery()"'
266 . ' title="' . __('Insert') . '" />';
267 } else {
268 $html .= '<input type="button" class="button" name="insert"'
269 . ' value="' . __('Insert') . '"'
270 . ' onclick="insertValueQuery()" />';
272 $html .= '</div>' . "\n"
273 . '</div>' . "\n";
276 $html .= '<div class="clearfloat"></div>' . "\n";
277 $html .= '</div>' . "\n";
279 $cfgBookmark = PMA_Bookmark_getParams();
280 if ($cfgBookmark) {
281 $html .= '<div id="bookmarkoptions">';
282 $html .= '<div class="formelement">';
283 $html .= '<label for="bkm_label">'
284 . __('Bookmark this SQL query:') . '</label>';
285 $html .= '<input type="text" name="bkm_label" id="bkm_label"'
286 . ' tabindex="110" value="" />';
287 $html .= '</div>';
288 $html .= '<div class="formelement">';
289 $html .= '<input type="checkbox" name="bkm_all_users" tabindex="111"'
290 . ' id="id_bkm_all_users" value="true" />';
291 $html .= '<label for="id_bkm_all_users">'
292 . __('Let every user access this bookmark') . '</label>';
293 $html .= '</div>';
294 $html .= '<div class="formelement">';
295 $html .= '<input type="checkbox" name="bkm_replace" tabindex="112"'
296 . ' id="id_bkm_replace" value="true" />';
297 $html .= '<label for="id_bkm_replace">'
298 . __('Replace existing bookmark of same name') . '</label>';
299 $html .= '</div>';
300 $html .= '</div>';
303 $html .= '<div class="clearfloat"></div>' . "\n";
304 $html .= '</fieldset>' . "\n"
305 . '</div>' . "\n";
307 $html .= '<fieldset id="queryboxfooter" class="tblFooters">' . "\n";
308 $html .= '<div class="formelement">' . "\n";
310 $html .= '</div>' . "\n";
311 $html .= '<div class="formelement">' . "\n";
312 $html .= '<label for="id_sql_delimiter">[ ' . __('Delimiter')
313 . '</label>' . "\n";
314 $html .= '<input type="text" name="sql_delimiter" tabindex="131" size="3" '
315 . 'value="' . $delimiter . '" '
316 . 'id="id_sql_delimiter" /> ]';
318 $html .= '<input type="checkbox" name="show_query" value="1" '
319 . 'id="checkbox_show_query" tabindex="132" checked="checked" />'
320 . '<label for="checkbox_show_query">' . __('Show this query here again')
321 . '</label>';
323 $html .= '<input type="checkbox" name="retain_query_box" value="1" '
324 . 'id="retain_query_box" tabindex="133" '
325 . ($GLOBALS['cfg']['RetainQueryBox'] === false
326 ? '' : ' checked="checked"')
327 . ' />'
328 . '<label for="retain_query_box">' . __('Retain query box')
329 . '</label>';
331 $html .= '<input type="checkbox" name="rollback_query" value="1" '
332 . 'id="rollback_query" tabindex="134" />'
333 . '<label for="rollback_query">' . __('Rollback when finished')
334 . '</label>';
336 $html .= '</div>' . "\n";
337 $html .= '<input type="submit" id="button_submit_query" name="SQL"';
339 $html .= ' tabindex="200" value="' . __('Go') . '" />' . "\n";
340 $html .= '<div class="clearfloat"></div>' . "\n";
341 $html .= '</fieldset>' . "\n";
343 return $html;
347 * return HTML for sql Query Form Bookmark
349 * @return string|null
351 * @usedby PMA_getHtmlForSqlQueryForm()
353 function PMA_getHtmlForSqlQueryFormBookmark()
355 $bookmark_list = PMA_Bookmark_getList($GLOBALS['db']);
356 if (! $bookmark_list || count($bookmark_list) < 1) {
357 return null;
360 $html = '<fieldset id="fieldsetBookmarkOptions">';
361 $html .= '<legend>';
362 $html .= __('Bookmarked SQL query') . '</legend>' . "\n";
363 $html .= '<div class="formelement">';
364 $html .= '<select name="id_bookmark" id="id_bookmark">' . "\n";
365 $html .= '<option value="">&nbsp;</option>' . "\n";
366 foreach ($bookmark_list as $key => $value) {
367 $html .= '<option value="' . htmlspecialchars($key) . '">'
368 . htmlspecialchars($value) . '</option>' . "\n";
370 // &nbsp; is required for correct display with styles/line height
371 $html .= '</select>&nbsp;' . "\n";
372 $html .= '</div>' . "\n";
373 $html .= '<div class="formelement">' . "\n";
374 $html .= __('Variable');
375 $html .= PMA_Util::showDocu('faq', 'faqbookmark');
376 $html .= '<input type="text" name="bookmark_variable" class="textfield"'
377 . ' size="10" />' . "\n";
378 $html .= '</div>' . "\n";
379 $html .= '<div class="formelement">' . "\n";
380 $html .= '<input type="radio" name="action_bookmark" value="0"'
381 . ' id="radio_bookmark_exe" checked="checked" />'
382 . '<label for="radio_bookmark_exe">' . __('Submit')
383 . '</label>' . "\n";
384 $html .= '<input type="radio" name="action_bookmark" value="1"'
385 . ' id="radio_bookmark_view" />'
386 . '<label for="radio_bookmark_view">' . __('View only')
387 . '</label>' . "\n";
388 $html .= '<input type="radio" name="action_bookmark" value="2"'
389 . ' id="radio_bookmark_del" />'
390 . '<label for="radio_bookmark_del">' . __('Delete')
391 . '</label>' . "\n";
392 $html .= '</div>' . "\n";
393 $html .= '<div class="clearfloat"></div>' . "\n";
394 $html .= '</fieldset>' . "\n";
396 $html .= '<fieldset id="fieldsetBookmarkOptionsFooter" class="tblFooters">';
397 $html .= '<input type="submit" name="SQL" id="button_submit_bookmark" value="'
398 . __('Go') . '" />';
399 $html .= '<div class="clearfloat"></div>' . "\n";
400 $html .= '</fieldset>' . "\n";
402 return $html;
406 * return HTML for Sql Query Form Upload
408 * @return string
410 * @usedby PMA_getHtmlForSqlQueryForm()
412 function PMA_getHtmlForSqlQueryFormUpload()
414 global $timeout_passed, $local_import_file;
416 $errors = array();
418 // we allow only SQL here
419 $matcher = '@\.sql(\.(' . PMA_supportedDecompressions() . '))?$@';
421 if (!empty($GLOBALS['cfg']['UploadDir'])) {
422 $files = PMA_getFileSelectOptions(
423 PMA_Util::userDir($GLOBALS['cfg']['UploadDir']), $matcher,
424 (isset($timeout_passed) && $timeout_passed && isset($local_import_file))
425 ? $local_import_file
426 : ''
428 } else {
429 $files = '';
432 // start output
433 $html = '<fieldset id="">';
434 $html .= '<legend>';
435 $html .= __('Browse your computer:') . '</legend>';
436 $html .= '<div class="formelement">';
437 $html .= '<input type="file" name="sql_file" class="textfield" /> ';
438 $html .= PMA_Util::getFormattedMaximumUploadSize($GLOBALS['max_upload_size']);
439 // some browsers should respect this :)
440 $html .= PMA_Util::generateHiddenMaxFileSize($GLOBALS['max_upload_size']) . "\n";
441 $html .= '</div>';
443 if ($files === false) {
444 $errors[] = PMA_Message::error(
445 __('The directory you set for upload work cannot be reached.')
447 } elseif (!empty($files)) {
448 $html .= '<div class="formelement">';
449 $html .= '<strong>' . __('web server upload directory:') . '</strong>';
450 $html .= '<select size="1" name="sql_localfile">' . "\n";
451 $html .= '<option value="" selected="selected"></option>' . "\n";
452 $html .= $files;
453 $html .= '</select>' . "\n";
454 $html .= '</div>';
457 $html .= '<div class="clearfloat"></div>' . "\n";
458 $html .= '</fieldset>';
460 $html .= '<fieldset id="" class="tblFooters">';
461 $html .= __('Character set of the file:') . "\n";
462 $html .= PMA_generateCharsetDropdownBox(
463 PMA_CSDROPDOWN_CHARSET,
464 'charset_of_file', null, 'utf8', false
466 $html .= '<input type="submit" name="SQL" value="' . __('Go')
467 . '" />' . "\n";
468 $html .= '<div class="clearfloat"></div>' . "\n";
469 $html .= '</fieldset>';
471 foreach ($errors as $error) {
472 $html .= $error->getDisplay();
475 return $html;