Merge branch 'master' of ssh://repo.or.cz/srv/git/phpmyadmin/madhuracj into OpenGIS
[phpmyadmin/madhuracj.git] / libraries / sql_query_form.lib.php
blob4dd2f15c4aa61da7bc70c728153a37585dd8eadf
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 * @param boolean|string $query query to display in the textarea
34 * or true to display last executed
35 * @param boolean|string $display_tab sql|files|history|full|false
36 * what part to display
37 * false if not inside querywindow
38 * @param string $delimiter
40 function PMA_sqlQueryForm($query = true, $display_tab = false, $delimiter = ';')
42 // check tab to display if inside querywindow
43 if (! $display_tab) {
44 $display_tab = 'full';
45 $is_querywindow = false;
46 } else {
47 $is_querywindow = true;
50 // query to show
51 if (true === $query) {
52 $query = $GLOBALS['sql_query'];
55 // set enctype to multipart for file uploads
56 if ($GLOBALS['is_upload']) {
57 $enctype = ' enctype="multipart/form-data"';
58 } else {
59 $enctype = '';
62 $table = '';
63 $db = '';
64 if (! strlen($GLOBALS['db'])) {
65 // prepare for server related
66 $goto = empty($GLOBALS['goto']) ?
67 'server_sql.php' : $GLOBALS['goto'];
68 } elseif (! strlen($GLOBALS['table'])) {
69 // prepare for db related
70 $db = $GLOBALS['db'];
71 $goto = empty($GLOBALS['goto']) ?
72 'db_sql.php' : $GLOBALS['goto'];
73 } else {
74 $table = $GLOBALS['table'];
75 $db = $GLOBALS['db'];
76 $goto = empty($GLOBALS['goto']) ?
77 'tbl_sql.php' : $GLOBALS['goto'];
81 // start output
82 if ($is_querywindow) {
84 <form method="post" id="sqlqueryform" target="frame_content"
85 action="import.php"<?php echo $enctype; ?> name="sqlform"
86 onsubmit="var save_name = window.opener.parent.frame_content.name;
87 window.opener.parent.frame_content.name = save_name + '<?php echo time(); ?>';
88 this.target = window.opener.parent.frame_content.name;
89 return checkSqlQuery(this)">
90 <?php
91 } else {
92 echo '<form method="post" action="import.php" ' . $enctype;
93 if ($GLOBALS['cfg']['AjaxEnable']) {
94 echo ' class="ajax"';
96 echo ' id="sqlqueryform" name="sqlform">' . "\n";
99 if ($is_querywindow) {
100 echo '<input type="hidden" name="focus_querywindow" value="true" />'
101 ."\n";
102 if ($display_tab != 'sql' && $display_tab != 'full') {
103 echo '<input type="hidden" name="sql_query" value="" />' . "\n";
104 echo '<input type="hidden" name="show_query" value="1" />' . "\n";
107 echo '<input type="hidden" name="is_js_confirmed" value="0" />' . "\n"
108 .PMA_generate_common_hidden_inputs($db, $table) . "\n"
109 .'<input type="hidden" name="pos" value="0" />' . "\n"
110 .'<input type="hidden" name="goto" value="'
111 .htmlspecialchars($goto) . '" />' . "\n"
112 .'<input type="hidden" name="message_to_show" value="'
113 . __('Your SQL query has been executed successfully') . '" />' . "\n"
114 .'<input type="hidden" name="prev_sql_query" value="'
115 . htmlspecialchars($query) . '" />' . "\n";
117 // display querybox
118 if ($display_tab === 'full' || $display_tab === 'sql') {
119 PMA_sqlQueryFormInsert($query, $is_querywindow, $delimiter);
122 // display uploads
123 if ($display_tab === 'files' && $GLOBALS['is_upload']) {
124 PMA_sqlQueryFormUpload();
127 // Bookmark Support
128 if ($display_tab === 'full' || $display_tab === 'history') {
129 if (! empty($GLOBALS['cfg']['Bookmark'])) {
130 PMA_sqlQueryFormBookmark();
134 // Encoding setting form appended by Y.Kawada
135 if (function_exists('PMA_set_enc_form')) {
136 echo PMA_set_enc_form(' ');
139 echo '</form>' . "\n";
140 if ($is_querywindow) {
142 <script type="text/javascript">
143 //<![CDATA[
144 if (window.opener) {
145 window.opener.parent.insertQuery();
147 //]]>
148 </script>
149 <?php
152 // print an empty div, which will be later filled with the sql query results by ajax
153 echo '<div id="sqlqueryresults"></div>';
157 * prints querybox fieldset
159 * @usedby PMA_sqlQueryForm()
160 * @param string $query query to display in the textarea
161 * @param boolean $is_querywindow if inside querywindow or not
162 * @param string $delimiter default delimiter to use
164 function PMA_sqlQueryFormInsert($query = '', $is_querywindow = false, $delimiter = ';')
167 // enable auto select text in textarea
168 if ($GLOBALS['cfg']['TextareaAutoSelect']) {
169 $auto_sel = ' onfocus="selectContent(this, sql_box_locked, true)"';
170 } else {
171 $auto_sel = '';
174 // enable locking if inside query window
175 if ($is_querywindow) {
176 $locking = ' onkeypress="document.sqlform.elements[\'LockFromUpdate\'].'
177 .'checked = true;"';
178 $height = $GLOBALS['cfg']['TextareaRows'] * 1.25;
179 } else {
180 $locking = '';
181 $height = $GLOBALS['cfg']['TextareaRows'] * 2;
184 $table = '';
185 $db = '';
186 $fields_list = array();
187 if (! strlen($GLOBALS['db'])) {
188 // prepare for server related
189 $legend = sprintf(__('Run SQL query/queries on server %s'),
190 '&quot;' . htmlspecialchars(
191 ! empty($GLOBALS['cfg']['Servers'][$GLOBALS['server']]['verbose']) ? $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['verbose'] : $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['host']) . '&quot;');
192 } elseif (! strlen($GLOBALS['table'])) {
193 // prepare for db related
194 $db = $GLOBALS['db'];
195 // if you want navigation:
196 $tmp_db_link = '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase']
197 . '?' . PMA_generate_common_url($db) . '"';
198 if ($is_querywindow) {
199 $tmp_db_link .= ' target="_self"'
200 . ' onclick="this.target=window.opener.frame_content.name"';
202 $tmp_db_link .= '>'
203 . htmlspecialchars($db) . '</a>';
204 // else use
205 // $tmp_db_link = htmlspecialchars($db);
206 $legend = sprintf(__('Run SQL query/queries on database %s'), $tmp_db_link);
207 if (empty($query)) {
208 $query = PMA_expandUserString($GLOBALS['cfg']['DefaultQueryDatabase'], 'PMA_backquote');
210 } else {
211 $table = $GLOBALS['table'];
212 $db = $GLOBALS['db'];
213 // Get the list and number of fields
214 // we do a try_query here, because we could be in the query window,
215 // trying to synchonize and the table has not yet been created
216 $fields_list = PMA_DBI_get_columns($db, $GLOBALS['table'], null, true);
218 $tmp_db_link = '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase']
219 . '?' . PMA_generate_common_url($db) . '"';
220 if ($is_querywindow) {
221 $tmp_db_link .= ' target="_self"'
222 . ' onclick="this.target=window.opener.frame_content.name"';
224 $tmp_db_link .= '>'
225 . htmlspecialchars($db) . '</a>';
226 // else use
227 // $tmp_db_link = htmlspecialchars($db);
228 $legend = sprintf(__('Run SQL query/queries on database %s'), $tmp_db_link);
229 if (empty($query)) {
230 $query = PMA_expandUserString($GLOBALS['cfg']['DefaultQueryTable'], 'PMA_backquote');
233 $legend .= ': ' . PMA_showMySQLDocu('SQL-Syntax', 'SELECT');
235 if (count($fields_list)) {
236 $sqlquerycontainer_id = 'sqlquerycontainer';
237 } else {
238 $sqlquerycontainer_id = 'sqlquerycontainerfull';
241 echo '<a name="querybox"></a>' . "\n"
242 .'<div id="queryboxcontainer">' . "\n"
243 .'<fieldset id="querybox">' . "\n";
244 echo '<legend>' . $legend . '</legend>' . "\n";
245 echo '<div id="queryfieldscontainer">' . "\n";
246 echo '<div id="' . $sqlquerycontainer_id . '">' . "\n"
247 .'<textarea tabindex="100" name="sql_query" id="sqlquery"'
248 .' cols="' . $GLOBALS['cfg']['TextareaCols'] . '"'
249 .' rows="' . $height . '"'
250 .' dir="' . $GLOBALS['text_dir'] . '"'
251 .$auto_sel . $locking . '>' . htmlspecialchars($query) . '</textarea>' . "\n";
252 // Add buttons to generate query easily for select all,single select,insert,update and delete
253 if (count($fields_list)) {
254 echo '<input type="button" value="SELECT *" id="selectall" class="sqlbutton" />';
255 echo '<input type="button" value="SELECT" id="select" class="sqlbutton" />';
256 echo '<input type="button" value="INSERT" id="insert" class="sqlbutton" />';
257 echo '<input type="button" value="UPDATE" id="update" class="sqlbutton" />';
258 echo '<input type="button" value="DELETE" id="delete" class="sqlbutton" />';
260 echo '<input type="button" value="' . __('Clear') . '" id="clear" class="sqlbutton" />';
261 echo '</div>' . "\n";
263 if (count($fields_list)) {
264 echo '<div id="tablefieldscontainer">' . "\n"
265 .'<label>' . __('Columns') . '</label>' . "\n"
266 .'<select id="tablefields" name="dummy" '
267 .'size="' . ($GLOBALS['cfg']['TextareaRows'] - 2) . '" '
268 .'multiple="multiple" ondblclick="insertValueQuery()">' . "\n";
269 foreach ($fields_list as $field) {
270 echo '<option value="'
271 .PMA_backquote(htmlspecialchars($field['Field'])) . '"';
272 if (isset($field['Field']) && strlen($field['Field']) && isset($field['Comment'])) {
273 echo ' title="' . htmlspecialchars($field['Comment']) . '"';
275 echo '>' . htmlspecialchars($field['Field']) . '</option>' . "\n";
277 echo '</select>' . "\n"
278 .'<div id="tablefieldinsertbuttoncontainer">' . "\n";
279 if ($GLOBALS['cfg']['PropertiesIconic']) {
280 echo '<input type="button" name="insert" value="&lt;&lt;"'
281 .' onclick="insertValueQuery()"'
282 .' title="' . __('Insert') . '" />' . "\n";
283 } else {
284 echo '<input type="button" name="insert"'
285 .' value="' . __('Insert') . '"'
286 .' onclick="insertValueQuery()" />' . "\n";
288 echo '</div>' . "\n"
289 .'</div>' . "\n";
292 echo '<div class="clearfloat"></div>' . "\n";
293 echo '</div>' . "\n";
295 if (! empty($GLOBALS['cfg']['Bookmark'])) {
297 <div id="bookmarkoptions">
298 <div class="formelement">
299 <label for="bkm_label">
300 <?php echo __('Bookmark this SQL query'); ?>:</label>
301 <input type="text" name="bkm_label" id="bkm_label" tabindex="110" value="" />
302 </div>
303 <div class="formelement">
304 <input type="checkbox" name="bkm_all_users" tabindex="111" id="id_bkm_all_users"
305 value="true" />
306 <label for="id_bkm_all_users">
307 <?php echo __('Let every user access this bookmark'); ?></label>
308 </div>
309 <div class="formelement">
310 <input type="checkbox" name="bkm_replace" tabindex="112" id="id_bkm_replace"
311 value="true" />
312 <label for="id_bkm_replace">
313 <?php echo __('Replace existing bookmark of same name'); ?></label>
314 </div>
315 </div>
316 <?php
319 echo '<div class="clearfloat"></div>' . "\n";
320 echo '</fieldset>' . "\n"
321 .'</div>' . "\n";
323 echo '<fieldset id="queryboxfooter" class="tblFooters">' . "\n";
324 echo '<div class="formelement">' . "\n";
325 if ($is_querywindow) {
327 <script type="text/javascript">
328 //<![CDATA[
329 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> ');
330 //]]>
331 </script>
332 <?php
334 echo '</div>' . "\n";
335 echo '<div class="formelement">' . "\n";
336 echo '<label for="id_sql_delimiter">[ ' . __('Delimiter')
337 .'</label>' . "\n";
338 echo '<input type="text" name="sql_delimiter" tabindex="131" size="3" '
339 .'value="' . $delimiter . '" '
340 .'id="id_sql_delimiter" /> ]' . "\n";
342 echo '<input type="checkbox" name="show_query" value="1" '
343 .'id="checkbox_show_query" tabindex="132" checked="checked" />' . "\n"
344 .'<label for="checkbox_show_query">' . __('Show this query here again')
345 .'</label>' . "\n";
347 echo '</div>' . "\n";
348 echo '<input type="submit" id="button_submit_query" name="SQL" tabindex="200" value="' . __('Go') . '" />'
349 ."\n";
350 echo '<div class="clearfloat"></div>' . "\n";
351 echo '</fieldset>' . "\n";
355 * prints bookmark fieldset
357 * @usedby PMA_sqlQueryForm()
359 function PMA_sqlQueryFormBookmark()
361 $bookmark_list = PMA_Bookmark_getList($GLOBALS['db']);
362 if (! $bookmark_list || count($bookmark_list) < 1) {
363 return;
366 echo '<fieldset id="bookmarkoptions">';
367 echo '<legend>';
368 echo __('Bookmarked SQL query') . '</legend>' . "\n";
369 echo '<div class="formelement">';
370 echo '<select name="id_bookmark" id="id_bookmark">' . "\n";
371 echo '<option value="">&nbsp;</option>' . "\n";
372 foreach ($bookmark_list as $key => $value) {
373 echo '<option value="' . htmlspecialchars($key) . '">'
374 .htmlspecialchars($value) . '</option>' . "\n";
376 // &nbsp; is required for correct display with styles/line height
377 echo '</select>&nbsp;' . "\n";
378 echo '</div>' . "\n";
379 echo '<div class="formelement">' . "\n";
380 echo __('Variable');
381 if ($GLOBALS['cfg']['ReplaceHelpImg']) {
382 echo ' <a href="./Documentation.html#faqbookmark"'
383 .' target="documentation">'
384 . PMA_getImage('b_help.png', __('Documentation')) . '</a>';
385 } else {
386 echo ' (<a href="./Documentation.html#faqbookmark"'
387 .' target="documentation">' . __('Documentation') . '</a>): ';
389 echo '<input type="text" name="bookmark_variable" class="textfield"'
390 .' size="10" />' . "\n";
391 echo '</div>' . "\n";
392 echo '<div class="formelement">' . "\n";
393 echo '<input type="radio" name="action_bookmark" value="0"'
394 .' id="radio_bookmark_exe" checked="checked" />'
395 .'<label for="radio_bookmark_exe">' . __('Submit')
396 .'</label>' . "\n";
397 echo '<input type="radio" name="action_bookmark" value="1"'
398 .' id="radio_bookmark_view" />'
399 .'<label for="radio_bookmark_view">' . __('View only')
400 .'</label>' . "\n";
401 echo '<input type="radio" name="action_bookmark" value="2"'
402 .' id="radio_bookmark_del" />'
403 .'<label for="radio_bookmark_del">' . __('Delete')
404 .'</label>' . "\n";
405 echo '</div>' . "\n";
406 echo '<div class="clearfloat"></div>' . "\n";
407 echo '</fieldset>' . "\n";
409 echo '<fieldset id="bookmarkoptionsfooter" class="tblFooters">' . "\n";
410 echo '<input type="submit" name="SQL" id="button_submit_bookmark" value="' . __('Go') . '" />';
411 echo '<div class="clearfloat"></div>' . "\n";
412 echo '</fieldset>' . "\n";
416 * prints bookmark fieldset
418 * @usedby PMA_sqlQueryForm()
420 function PMA_sqlQueryFormUpload()
422 $errors = array ();
424 $matcher = '@\.sql(\.(' . PMA_supportedDecompressions() . '))?$@'; // we allow only SQL here
426 if (!empty($GLOBALS['cfg']['UploadDir'])) {
427 $files = PMA_getFileSelectOptions(PMA_userDir($GLOBALS['cfg']['UploadDir']), $matcher, (isset($timeout_passed) && $timeout_passed && isset($local_import_file)) ? $local_import_file : '');
428 } else {
429 $files = '';
432 // start output
433 echo '<fieldset id="">';
434 echo '<legend>';
435 echo __('Browse your computer:') . '</legend>';
436 echo '<div class="formelement">';
437 echo '<input type="file" name="sql_file" class="textfield" /> ';
438 echo PMA_displayMaximumUploadSize($GLOBALS['max_upload_size']);
439 // some browsers should respect this :)
440 echo PMA_generateHiddenMaxFileSize($GLOBALS['max_upload_size']) . "\n";
441 echo '</div>';
443 if ($files === false) {
444 $errors[] = PMA_Message::error( __('The directory you set for upload work cannot be reached'));
445 } elseif (!empty($files)) {
446 echo '<div class="formelement">';
447 echo '<strong>' . __('web server upload directory') .':</strong>' . "\n";
448 echo '<select size="1" name="sql_localfile">' . "\n";
449 echo '<option value="" selected="selected"></option>' . "\n";
450 echo $files;
451 echo '</select>' . "\n";
452 echo '</div>';
455 echo '<div class="clearfloat"></div>' . "\n";
456 echo '</fieldset>';
459 echo '<fieldset id="" class="tblFooters">';
460 echo __('Character set of the file:') . "\n";
461 echo PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_CHARSET,
462 'charset_of_file', null, 'utf8', false);
463 echo '<input type="submit" name="SQL" value="' . __('Go')
464 .'" />' . "\n";
465 echo '<div class="clearfloat"></div>' . "\n";
466 echo '</fieldset>';
468 foreach ($errors as $error) {
469 $error->display();