Prepare/refactor ubuntu-debian package for 4.1 (still need to test)
[openemr.git] / phpmyadmin / tbl_select.php
blob183e481d61ef8b578db38c873ba07cb28c814f7d
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @version $Id$
6 */
8 /**
9 * Gets some core libraries
11 require_once './libraries/common.inc.php';
12 require_once './libraries/relation.lib.php'; // foreign keys
13 require_once './libraries/mysql_charsets.lib.php';
15 if ($GLOBALS['cfg']['PropertiesIconic'] == true) {
16 $titles['Browse'] =
17 '<img class="icon" width="16" height="16" src="' . $pmaThemeImage
18 .'b_browse.png" alt="' . $strBrowseForeignValues . '" title="'
19 .$strBrowseForeignValues . '" />';
21 if ($GLOBALS['cfg']['PropertiesIconic'] === 'both') {
22 $titles['Browse'] .= $strBrowseForeignValues;
24 } else {
25 $titles['Browse'] = $strBrowseForeignValues;
28 /**
29 * Not selection yet required -> displays the selection form
31 if (!isset($param) || $param[0] == '') {
32 // Gets some core libraries
33 require_once './libraries/tbl_common.php';
34 //$err_url = 'tbl_select.php' . $err_url;
35 $url_query .= '&amp;goto=tbl_select.php&amp;back=tbl_select.php';
37 /**
38 * Gets tables informations
40 require_once './libraries/tbl_info.inc.php';
42 /**
43 * Displays top menu links
45 require_once './libraries/tbl_links.inc.php';
47 if (!isset($goto)) {
48 $goto = $GLOBALS['cfg']['DefaultTabTable'];
50 // Defines the url to return to in case of error in the next sql statement
51 $err_url = $goto . '?' . PMA_generate_common_url($db, $table);
53 // Gets the list and number of fields
54 $result = PMA_DBI_query('SHOW' . (PMA_MYSQL_INT_VERSION >= 40100 ? ' FULL' : '') . ' FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE);
55 $fields_cnt = PMA_DBI_num_rows($result);
56 // rabue: we'd better ensure, that all arrays are empty.
57 $fields_list = $fields_null = $fields_type = $fields_collation = array();
58 while ($row = PMA_DBI_fetch_assoc($result)) {
59 $fields_list[] = $row['Field'];
60 $type = $row['Type'];
61 // reformat mysql query output - staybyte - 9. June 2001
62 if (strncasecmp($type, 'set', 3) == 0
63 || strncasecmp($type, 'enum', 4) == 0) {
64 $type = str_replace(',', ', ', $type);
65 } else {
67 // strip the "BINARY" attribute, except if we find "BINARY(" because
68 // this would be a BINARY or VARBINARY field type
69 if (!preg_match('@BINARY[\(]@i', $type)) {
70 $type = preg_replace('@BINARY@i', '', $type);
72 $type = preg_replace('@ZEROFILL@i', '', $type);
73 $type = preg_replace('@UNSIGNED@i', '', $type);
75 $type = strtolower($type);
77 if (empty($type)) {
78 $type = '&nbsp;';
80 $fields_null[] = $row['Null'];
81 $fields_type[] = $type;
82 $fields_collation[] = PMA_MYSQL_INT_VERSION >= 40100 && !empty($row['Collation']) && $row['Collation'] != 'NULL'
83 ? $row['Collation']
84 : '';
85 } // end while
86 PMA_DBI_free_result($result);
87 unset($result, $type);
89 // <markus@noga.de>
90 // retrieve keys into foreign fields, if any
91 $cfgRelation = PMA_getRelationsParam();
92 // check also foreigners even if relwork is FALSE (to get
93 // foreign keys from innodb)
94 //$foreigners = ($cfgRelation['relwork'] ? PMA_getForeigners($db, $table) : FALSE);
95 $foreigners = PMA_getForeigners($db, $table);
97 <script type="text/javascript">
98 // <![CDATA[
99 function PMA_tbl_select_operator(f, index, multiple) {
100 switch (f.elements["func[" + index + "]"].options[f.elements["func[" + index + "]"].selectedIndex].value) {
101 <?php
102 reset($GLOBALS['cfg']['UnaryOperators']);
103 while (list($operator) = each($GLOBALS['cfg']['UnaryOperators'])) {
104 echo ' case "' . $operator . "\":\r\n";
107 bDisabled = true;
108 break;
110 default:
111 bDisabled = false;
113 f.elements["fields[" + index + "]" + ((multiple) ? "[]": "")].disabled = bDisabled;
115 // ]]>
116 </script>
117 <form method="post" action="tbl_select.php" name="insertForm">
118 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
119 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
120 <input type="hidden" name="back" value="tbl_select.php" />
122 <fieldset id="fieldset_table_search">
124 <fieldset id="fieldset_select_fields">
125 <legend><?php echo $strSelectFields; ?></legend>
126 <select name="param[]" size="<?php echo min($fields_cnt, 10); ?>"
127 multiple="multiple">
128 <?php
129 // Displays the list of the fields
130 foreach ($fields_list as $each_field) {
131 echo ' '
132 .'<option value="' . htmlspecialchars($each_field) . '"'
133 .' selected="selected">' . htmlspecialchars($each_field)
134 .'</option>' . "\n";
137 </select>
138 <input type="checkbox" name="distinct" value="DISTINCT" id="oDistinct" />
139 <label for="oDistinct">DISTINCT</label>
140 </fieldset>
142 <fieldset id="fieldset_limit_rows">
143 <legend><?php echo $strLimitNumRows; ?></legend>
144 <input type="text" size="4" name="session_max_rows"
145 value="<?php echo $GLOBALS['cfg']['MaxRows']; ?>" class="textfield" />
146 </fieldset>
148 <fieldset id="fieldset_display_order">
149 <legend><?php echo $strDisplayOrder; ?></legend>
150 <select name="orderField" style="vertical-align: middle">
151 <option value="--nil--"></option>
152 <?php
153 foreach ($fields_list as $each_field) {
154 echo ' '
155 .'<option value="' . htmlspecialchars($each_field) . '">'
156 .htmlspecialchars($each_field) . '</option>' . "\n";
157 } // end for
159 </select>
161 <div class="formelement">
162 <input type="radio" name="order" value="ASC" checked="checked" id="sortASC" />
163 <label for="sortASC"><?php echo $strAscending; ?></label>
164 </div>
166 <div class="formelement">
167 <input type="radio" name="order" value="DESC" id="sortDESC" />
168 <label for="sortDESC"><?php echo $strDescending; ?></label>
169 </div>
170 </fieldset>
172 <br class="clearfloat" />
173 <?php echo $strAddSearchConditions; ?>
174 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'Functions'); ?>
176 <input type="text" name="where" class="textfield" size="64" />
178 </fieldset>
179 <fieldset class="tblFooters">
180 <input type="submit" name="submit" value="<?php echo $strGo; ?>" />
181 </fieldset>
183 <fieldset id="fieldset_table_qbe">
184 <legend><?php echo '<em>' . $strOr . '</em> ' . $strDoAQuery; ?></legend>
185 <table class="data">
186 <thead>
187 <tr><th><?php echo $strField; ?></th>
188 <th><?php echo $strType; ?></th>
189 <?php echo PMA_MYSQL_INT_VERSION >= 40100 ? '<th>' . $strCollation . '</th>' . "\n" : ''; ?>
190 <th><?php echo $strOperator; ?></th>
191 <th><?php echo $strValue; ?></th>
192 </tr>
193 </thead>
194 <tbody>
195 <?php
196 $odd_row = true;
198 <script type="text/javascript" src="./js/tbl_change.js"></script>
199 <?php
200 for ($i = 0; $i < $fields_cnt; $i++) {
202 <tr class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
203 <th><?php echo htmlspecialchars($fields_list[$i]); ?></th>
204 <td><?php echo $fields_type[$i]; ?></td>
205 <?php echo PMA_MYSQL_INT_VERSION >= 40100 ? '<td>'
206 . $fields_collation[$i] . '</td>' . "\n" : ''; ?>
207 <td><select name="func[]">
208 <?php
209 if (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
210 foreach ($GLOBALS['cfg']['EnumOperators'] as $fc) {
211 echo "\n" . ' '
212 . '<option value="' . htmlspecialchars($fc) . '">'
213 . htmlspecialchars($fc) . '</option>';
215 } elseif (preg_match('@char|blob|text|set@i', $fields_type[$i])) {
216 foreach ($GLOBALS['cfg']['TextOperators'] as $fc) {
217 echo "\n" . ' '
218 . '<option value="' . htmlspecialchars($fc) . '">'
219 . htmlspecialchars($fc) . '</option>';
221 } else {
222 foreach ($GLOBALS['cfg']['NumOperators'] as $fc) {
223 echo "\n" . ' '
224 . '<option value="' . htmlspecialchars($fc) . '">'
225 . htmlspecialchars($fc) . '</option>';
227 } // end if... else...
228 if ($fields_null[$i]) {
229 foreach ($GLOBALS['cfg']['NullOperators'] as $fc) {
230 echo "\n" . ' '
231 . '<option value="' . htmlspecialchars($fc) . '">'
232 . htmlspecialchars($fc) . '</option>';
237 </select>
238 </td>
239 <td>
240 <?php
241 // <markus@noga.de>
242 $field = $fields_list[$i];
244 // do not use require_once here
245 require './libraries/get_foreign.lib.php';
247 // we got a bug report: in some cases, even if $disp is true,
248 // there are no rows, so we add a fetch_array
250 if ($foreigners && isset($foreigners[$field]) && isset($disp_row) && is_array($disp_row)) {
251 // f o r e i g n k e y s
252 echo ' <select name="fields[' . $i . ']">' . "\n";
253 // go back to first row
255 // here, the 4th parameter is empty because there is no current
256 // value of data for the dropdown (the search page initial values
257 // are displayed empty)
258 echo PMA_foreignDropdown($disp_row, $foreign_field, $foreign_display,
259 '', $GLOBALS['cfg']['ForeignKeyMaxLimit']);
260 echo ' </select>' . "\n";
261 } elseif (isset($foreign_link) && $foreign_link == true) {
263 <input type="text" name="fields[<?php echo $i; ?>]"
264 id="field_<?php echo md5($field); ?>[<?php echo $i; ?>]"
265 class="textfield" />
266 <script type="text/javascript">
267 // <![CDATA[
268 document.writeln('<a target="_blank" onclick="window.open(this.href, \'foreigners\', \'width=640,height=240,scrollbars=yes\'); return false" href="browse_foreigners.php?<?php echo PMA_generate_common_url($db, $table); ?>&amp;field=<?php echo urlencode($field); ?>&amp;fieldkey=<?php echo $i; ?>"><?php echo str_replace("'", "\'", $titles['Browse']); ?></a>');
269 // ]]>
270 </script>
271 <?php
272 } elseif (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
273 // e n u m s
274 $enum_value=explode(', ', str_replace("'", '', substr($fields_type[$i], 5, -1)));
275 $cnt_enum_value = count($enum_value);
276 echo ' <select name="fields[' . $i . '][]"'
277 .' multiple="multiple" size="' . min(3, $cnt_enum_value) . '">' . "\n";
278 for ($j = 0; $j < $cnt_enum_value; $j++) {
279 echo ' <option value="' . $enum_value[$j] . '">'
280 . $enum_value[$j] . '</option>';
281 } // end for
282 echo ' </select>' . "\n";
283 } else {
284 // o t h e r c a s e s
285 echo ' <input type="text" name="fields[' . $i . ']"'
286 .' size="40" class="textfield" id="field_' . $i . '" />' . "\n";
288 $type = $fields_type[$i];
289 if ($type == 'date' || $type == 'datetime' || substr($type, 0, 9) == 'timestamp') {
291 <script type="text/javascript">
292 //<![CDATA[
293 document.write('<a title="<?php echo $strCalendar;?>" href="javascript:openCalendar(\'<?php echo PMA_generate_common_url();?>\', \'insertForm\', \'field_<?php echo ($i); ?>\', \'<?php echo (PMA_MYSQL_INT_VERSION >= 40100 && substr($type, 0, 9) == 'timestamp') ? 'datetime' : substr($type, 0, 9); ?>\')"><img class="calendar" src="<?php echo $pmaThemeImage; ?>b_calendar.png" alt="<?php echo $strCalendar; ?>"/></a>');
294 //]]>
295 </script>
296 <?php
299 <input type="hidden" name="names[<?php echo $i; ?>]"
300 value="<?php echo htmlspecialchars($fields_list[$i]); ?>" />
301 <input type="hidden" name="types[<?php echo $i; ?>]"
302 value="<?php echo $fields_type[$i]; ?>" />
303 <input type="hidden" name="collations[<?php echo $i; ?>]"
304 value="<?php echo $fields_collation[$i]; ?>" />
305 </td>
306 </tr>
307 <?php
308 } // end for
310 </tbody>
311 </table>
312 </fieldset>
313 <fieldset class="tblFooters">
314 <input type="hidden" name="max_number_of_fields"
315 value="<?php echo $fields_cnt; ?>" />
316 <input type="submit" name="submit" value="<?php echo $strGo; ?>" />
317 </fieldset>
318 </form>
319 <?php
320 require_once './libraries/footer.inc.php';
325 * Selection criteria have been submitted -> do the work
327 else {
328 // Builds the query
330 $sql_query = 'SELECT ' . (isset($distinct) ? 'DISTINCT ' : '');
332 // if all fields were selected to display, we do a SELECT *
333 // (more efficient and this helps prevent a problem in IE
334 // if one of the rows is edited and we come back to the Select results)
336 if (count($param) == $max_number_of_fields) {
337 $sql_query .= '* ';
338 } else {
339 $param = PMA_backquote($param);
340 $sql_query .= implode(', ', $param);
341 unset($param);
342 } // end if
344 $sql_query .= ' FROM ' . PMA_backquote($table);
346 // The where clause
347 if (trim($where) != '') {
348 $sql_query .= ' WHERE ' . $where;
349 } else {
350 $w = $charsets = array();
351 $cnt_func = count($func);
352 reset($func);
353 while (list($i, $func_type) = each($func)) {
354 if (PMA_MYSQL_INT_VERSION >= 40100) {
355 list($charsets[$i]) = explode('_', $collations[$i]);
357 if (@$GLOBALS['cfg']['UnaryOperators'][$func_type] == 1) {
358 $fields[$i] = '';
359 $w[] = PMA_backquote(urldecode($names[$i])) . ' ' . $func_type;
361 } elseif (strncasecmp($types[$i], 'enum', 4) == 0) {
362 if (!empty($fields[$i])) {
363 if (!is_array($fields[$i])) {
364 $fields[$i] = explode(',', $fields[$i]);
366 $enum_selected_count = count($fields[$i]);
367 if ($func_type == '=' && $enum_selected_count > 1) {
368 $func_type = $func[$i] = 'IN';
369 $parens_open = '(';
370 $parens_close = ')';
372 } elseif ($func_type == '!=' && $enum_selected_count > 1) {
373 $func_type = $func[$i] = 'NOT IN';
374 $parens_open = '(';
375 $parens_close = ')';
377 } else {
378 $parens_open = '';
379 $parens_close = '';
381 $enum_where = '\'' . PMA_sqlAddslashes($fields[$i][0]) . '\'';
382 if (PMA_MYSQL_INT_VERSION >= 40100 && $charsets[$i] != $charset_connection) {
383 $enum_where = 'CONVERT(_utf8 ' . $enum_where . ' USING ' . $charsets[$i] . ') COLLATE ' . $collations[$i];
385 for ($e = 1; $e < $enum_selected_count; $e++) {
386 $enum_where .= ', ';
387 $tmp_literal = '\'' . PMA_sqlAddslashes($fields[$i][$e]) . '\'';
388 if (PMA_MYSQL_INT_VERSION >= 40100 && $charsets[$i] != $charset_connection) {
389 $tmp_literal = 'CONVERT(_utf8 ' . $tmp_literal . ' USING ' . $charsets[$i] . ') COLLATE ' . $collations[$i];
391 $enum_where .= $tmp_literal;
392 unset($tmp_literal);
395 $w[] = PMA_backquote(urldecode($names[$i])) . ' ' . $func_type . ' ' . $parens_open . $enum_where . $parens_close;
398 } elseif ($fields[$i] != '') {
399 // For these types we quote the value. Even if it's another type (like INT),
400 // for a LIKE we always quote the value. MySQL converts strings to numbers
401 // and numbers to strings as necessary during the comparison
402 if (preg_match('@char|binary|blob|text|set|date|time|year@i', $types[$i]) || strpos(' ' . $func_type, 'LIKE')) {
403 $quot = '\'';
404 } else {
405 $quot = '';
408 // Make query independant from the selected connection charset.
409 // But if the field's type is VARBINARY, it has no charset
410 // and $charsets[$i] is empty, so we cannot generate a CONVERT
412 if (PMA_MYSQL_INT_VERSION >= 40101 && !empty($charsets[$i]) && $charsets[$i] != $charset_connection && preg_match('@char|binary|blob|text|set@i', $types[$i])) {
413 $prefix = 'CONVERT(_utf8 ';
414 $suffix = ' USING ' . $charsets[$i] . ') COLLATE ' . $collations[$i];
415 } else {
416 $prefix = $suffix = '';
419 // LIKE %...%
420 if ($func_type == 'LIKE %...%') {
421 $func_type = 'LIKE';
422 $fields[$i] = '%' . $fields[$i] . '%';
424 $w[] = PMA_backquote(urldecode($names[$i])) . ' ' . $func_type . ' ' . $prefix . $quot . PMA_sqlAddslashes($fields[$i]) . $quot . $suffix;
426 } // end if
427 } // end for
429 if ($w) {
430 $sql_query .= ' WHERE ' . implode(' AND ', $w);
432 } // end if
434 if ($orderField != '--nil--') {
435 $sql_query .= ' ORDER BY ' . PMA_backquote(urldecode($orderField)) . ' ' . $order;
436 } // end if
437 include './sql.php';