Translation update done using Pootle.
[phpmyadmin-themes.git] / tbl_select.php
blob67bf5d24b24e84a5d968788cba2992647f48833b
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Handles table search tab
6 * display table search form, create SQL query from form data
7 * and include sql.php to execute it
9 * @todo display search form again if no results from previous search
10 * @package phpMyAdmin
13 /**
14 * Gets some core libraries
16 require_once './libraries/common.inc.php';
17 require_once './libraries/mysql_charsets.lib.php';
19 $GLOBALS['js_include'][] = 'sql.js';
20 $GLOBALS['js_include'][] = 'tbl_select.js';
21 $GLOBALS['js_include'][] = 'jquery/jquery-ui-1.8.custom.js';
22 $GLOBALS['js_include'][] = 'jquery/timepicker.js';
23 if ($GLOBALS['cfg']['PropertiesIconic'] == true) {
24 $titles['Browse'] =
25 '<img class="icon" width="16" height="16" src="' . $pmaThemeImage
26 .'b_browse.png" alt="' . __('Browse foreign values') . '" title="'
27 . __('Browse foreign values') . '" />';
29 if ($GLOBALS['cfg']['PropertiesIconic'] === 'both') {
30 $titles['Browse'] .= __('Browse foreign values');
32 } else {
33 $titles['Browse'] = __('Browse foreign values');
36 /**
37 * Not selection yet required -> displays the selection form
39 if (!isset($param) || $param[0] == '') {
40 // Gets some core libraries
41 require_once './libraries/tbl_common.php';
42 //$err_url = 'tbl_select.php' . $err_url;
43 $url_query .= '&amp;goto=tbl_select.php&amp;back=tbl_select.php';
45 /**
46 * Gets tables informations
48 require_once './libraries/tbl_info.inc.php';
50 /**
51 * Displays top menu links
53 require_once './libraries/tbl_links.inc.php';
55 if (!isset($goto)) {
56 $goto = $GLOBALS['cfg']['DefaultTabTable'];
58 // Defines the url to return to in case of error in the next sql statement
59 $err_url = $goto . '?' . PMA_generate_common_url($db, $table);
61 // Gets the list and number of fields
62 $result = PMA_DBI_query('SHOW FULL FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE);
63 $fields_cnt = PMA_DBI_num_rows($result);
64 $fields_list = $fields_null = $fields_type = $fields_collation = array();
65 while ($row = PMA_DBI_fetch_assoc($result)) {
66 $fields_list[] = $row['Field'];
67 $type = $row['Type'];
68 // reformat mysql query output
69 if (strncasecmp($type, 'set', 3) == 0
70 || strncasecmp($type, 'enum', 4) == 0) {
71 $type = str_replace(',', ', ', $type);
72 } else {
74 // strip the "BINARY" attribute, except if we find "BINARY(" because
75 // this would be a BINARY or VARBINARY field type
76 if (!preg_match('@BINARY[\(]@i', $type)) {
77 $type = preg_replace('@BINARY@i', '', $type);
79 $type = preg_replace('@ZEROFILL@i', '', $type);
80 $type = preg_replace('@UNSIGNED@i', '', $type);
82 $type = strtolower($type);
84 if (empty($type)) {
85 $type = '&nbsp;';
87 $fields_null[] = $row['Null'];
88 $fields_type[] = $type;
89 $fields_collation[] = !empty($row['Collation']) && $row['Collation'] != 'NULL'
90 ? $row['Collation']
91 : '';
92 } // end while
93 PMA_DBI_free_result($result);
94 unset($result, $type);
96 // retrieve keys into foreign fields, if any
97 // check also foreigners even if relwork is FALSE (to get
98 // foreign keys from innodb)
99 $foreigners = PMA_getForeigners($db, $table);
101 <form method="post" action="tbl_select.php" name="insertForm" id="tbl_search_form">
102 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
103 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
104 <input type="hidden" name="back" value="tbl_select.php" />
106 <fieldset id="fieldset_table_search">
108 <fieldset id="fieldset_table_qbe">
109 <legend><?php echo __('Do a "query by example" (wildcard: "%")') ?></legend>
110 <table class="data">
111 <thead>
112 <tr><th><?php echo __('Column'); ?></th>
113 <th><?php echo __('Type'); ?></th>
114 <th><?php echo __('Collation'); ?></th>
115 <th><?php echo __('Operator'); ?></th>
116 <th><?php echo __('Value'); ?></th>
117 </tr>
118 </thead>
119 <tbody>
120 <?php
121 $odd_row = true;
123 for ($i = 0; $i < $fields_cnt; $i++) {
125 <tr class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
126 <th><?php echo htmlspecialchars($fields_list[$i]); ?></th>
127 <td><?php echo $fields_type[$i]; ?></td>
128 <td><?php echo $fields_collation[$i]; ?></td>
129 <td><select name="func[]">
130 <?php
131 if (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
132 foreach ($GLOBALS['cfg']['EnumOperators'] as $fc) {
133 echo "\n" . ' '
134 . '<option value="' . htmlspecialchars($fc) . '">'
135 . htmlspecialchars($fc) . '</option>';
137 } elseif (preg_match('@char|blob|text|set@i', $fields_type[$i])) {
138 foreach ($GLOBALS['cfg']['TextOperators'] as $fc) {
139 echo "\n" . ' '
140 . '<option value="' . htmlspecialchars($fc) . '">'
141 . htmlspecialchars($fc) . '</option>';
143 } else {
144 foreach ($GLOBALS['cfg']['NumOperators'] as $fc) {
145 echo "\n" . ' '
146 . '<option value="' . htmlspecialchars($fc) . '">'
147 . htmlspecialchars($fc) . '</option>';
149 } // end if... else...
150 if ($fields_null[$i]) {
151 foreach ($GLOBALS['cfg']['NullOperators'] as $fc) {
152 echo "\n" . ' '
153 . '<option value="' . htmlspecialchars($fc) . '">'
154 . htmlspecialchars($fc) . '</option>';
159 </select>
160 </td>
161 <td>
162 <?php
163 $field = $fields_list[$i];
165 $foreignData = PMA_getForeignData($foreigners, $field, false, '', '');
167 if ($foreigners && isset($foreigners[$field]) && is_array($foreignData['disp_row'])) {
168 // f o r e i g n k e y s
169 echo ' <select name="fields[' . $i . ']">' . "\n";
170 // go back to first row
172 // here, the 4th parameter is empty because there is no current
173 // value of data for the dropdown (the search page initial values
174 // are displayed empty)
175 echo PMA_foreignDropdown($foreignData['disp_row'],
176 $foreignData['foreign_field'],
177 $foreignData['foreign_display'],
178 '', $GLOBALS['cfg']['ForeignKeyMaxLimit']);
179 echo ' </select>' . "\n";
180 } elseif ($foreignData['foreign_link'] == true) {
182 <input type="text" name="fields[<?php echo $i; ?>]"
183 id="field_<?php echo md5($field); ?>[<?php echo $i; ?>]"
184 class="textfield" />
185 <script type="text/javascript">
186 // <![CDATA[
187 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>');
188 // ]]>
189 </script>
190 <?php
191 } elseif (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
192 // e n u m s
193 $enum_value=explode(', ', str_replace("'", '', substr($fields_type[$i], 5, -1)));
194 $cnt_enum_value = count($enum_value);
195 echo ' <select name="fields[' . $i . '][]"'
196 .' multiple="multiple" size="' . min(3, $cnt_enum_value) . '">' . "\n";
197 for ($j = 0; $j < $cnt_enum_value; $j++) {
198 echo ' <option value="' . $enum_value[$j] . '">'
199 . $enum_value[$j] . '</option>';
200 } // end for
201 echo ' </select>' . "\n";
202 } else {
203 // o t h e r c a s e s
204 echo ' <input type="text" name="fields[' . $i . ']"'
205 .' size="40" class="textfield" id="field_' . $i . '" />' . "\n";
207 $type = $fields_type[$i];
208 if ($type == 'date' || $type == 'datetime' || substr($type, 0, 9) == 'timestamp') {
210 <script type="text/javascript">
211 //<![CDATA[
212 $(function() {
213 $('#field_<?php echo $i; ?>').datepicker({
214 duration: '',
215 time24h: true,
216 stepMinutes: 1,
217 stepHours: 1,
218 <?php echo ($type == 'date' ? "showTime: false,":"showTime: true,"); ?>
219 altTimeField: '',
220 constrainInput: false
223 //]]>
224 </script>
225 <?php
228 <input type="hidden" name="names[<?php echo $i; ?>]"
229 value="<?php echo htmlspecialchars($fields_list[$i]); ?>" />
230 <input type="hidden" name="types[<?php echo $i; ?>]"
231 value="<?php echo $fields_type[$i]; ?>" />
232 <input type="hidden" name="collations[<?php echo $i; ?>]"
233 value="<?php echo $fields_collation[$i]; ?>" />
234 </td>
235 </tr>
236 <?php
237 } // end for
239 </tbody>
240 </table>
241 </fieldset>
242 <?php
243 PMA_generate_slider_effect('searchoptions', __('Options'));
245 <fieldset id="fieldset_select_fields">
246 <legend><?php echo __('Select columns (at least one):'); ?></legend>
247 <select name="param[]" size="<?php echo min($fields_cnt, 10); ?>"
248 multiple="multiple">
249 <?php
250 // Displays the list of the fields
251 foreach ($fields_list as $each_field) {
252 echo ' '
253 .'<option value="' . htmlspecialchars($each_field) . '"'
254 .' selected="selected">' . htmlspecialchars($each_field)
255 .'</option>' . "\n";
258 </select>
259 <input type="checkbox" name="distinct" value="DISTINCT" id="oDistinct" />
260 <label for="oDistinct">DISTINCT</label>
261 </fieldset>
263 <fieldset id="fieldset_search_conditions">
264 <legend><?php echo '<em>' . __('Or') . '</em> ' . __('Add search conditions (body of the "where" clause):'); ?></legend>
265 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'Functions'); ?>
267 <input type="text" name="where" class="textfield" size="64" />
268 </fieldset>
270 <fieldset id="fieldset_limit_rows">
271 <legend><?php echo __('Number of rows per page'); ?></legend>
272 <input type="text" size="4" name="session_max_rows"
273 value="<?php echo $GLOBALS['cfg']['MaxRows']; ?>" class="textfield" />
274 </fieldset>
276 <fieldset id="fieldset_display_order">
277 <legend><?php echo __('Display order:'); ?></legend>
278 <select name="orderField">
279 <option value="--nil--"></option>
280 <?php
281 foreach ($fields_list as $each_field) {
282 echo ' '
283 .'<option value="' . htmlspecialchars($each_field) . '">'
284 .htmlspecialchars($each_field) . '</option>' . "\n";
285 } // end for
287 </select>
288 <?php
289 $choices = array(
290 'ASC' => __('Ascending'),
291 'DESC' => __('Descending')
293 PMA_display_html_radio('order', $choices, 'ASC', false, true, "formelement");
294 unset($choices);
296 </fieldset>
297 <br style="clear: both;"/>
298 </div>
299 </fieldset>
300 <fieldset class="tblFooters">
301 <input type="hidden" name="max_number_of_fields"
302 value="<?php echo $fields_cnt; ?>" />
303 <input type="submit" name="submit" value="<?php echo __('Go'); ?>" />
304 </fieldset>
305 </form>
306 <div id="sqlqueryresults"></div>
307 <?php
308 require './libraries/footer.inc.php';
313 * Selection criteria have been submitted -> do the work
315 else {
316 // Builds the query
318 $sql_query = 'SELECT ' . (isset($distinct) ? 'DISTINCT ' : '');
320 // if all fields were selected to display, we do a SELECT *
321 // (more efficient and this helps prevent a problem in IE
322 // if one of the rows is edited and we come back to the Select results)
324 if (count($param) == $max_number_of_fields) {
325 $sql_query .= '* ';
326 } else {
327 $param = PMA_backquote($param);
328 $sql_query .= implode(', ', $param);
329 } // end if
331 // avoid a loop, for example when $cfg['DefaultTabTable'] is set
332 // to 'tbl_select.php'
333 unset($param);
335 $sql_query .= ' FROM ' . PMA_backquote($table);
337 // The where clause
338 if (trim($where) != '') {
339 $sql_query .= ' WHERE ' . $where;
340 } else {
341 $w = $charsets = array();
342 $cnt_func = count($func);
343 reset($func);
344 while (list($i, $func_type) = each($func)) {
345 list($charsets[$i]) = explode('_', $collations[$i]);
346 if (isset($GLOBALS['cfg']['UnaryOperators'][$func_type]) && $GLOBALS['cfg']['UnaryOperators'][$func_type] == 1) {
347 $fields[$i] = '';
348 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type;
350 } elseif (strncasecmp($types[$i], 'enum', 4) == 0) {
351 if (!empty($fields[$i])) {
352 if (!is_array($fields[$i])) {
353 $fields[$i] = explode(',', $fields[$i]);
355 $enum_selected_count = count($fields[$i]);
356 if ($func_type == '=' && $enum_selected_count > 1) {
357 $func_type = $func[$i] = 'IN';
358 $parens_open = '(';
359 $parens_close = ')';
361 } elseif ($func_type == '!=' && $enum_selected_count > 1) {
362 $func_type = $func[$i] = 'NOT IN';
363 $parens_open = '(';
364 $parens_close = ')';
366 } else {
367 $parens_open = '';
368 $parens_close = '';
370 $enum_where = '\'' . PMA_sqlAddslashes($fields[$i][0]) . '\'';
371 for ($e = 1; $e < $enum_selected_count; $e++) {
372 $enum_where .= ', \'' . PMA_sqlAddslashes($fields[$i][$e]) . '\'';
375 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . $parens_open . $enum_where . $parens_close;
378 } elseif ($fields[$i] != '') {
379 // For these types we quote the value. Even if it's another type (like INT),
380 // for a LIKE we always quote the value. MySQL converts strings to numbers
381 // and numbers to strings as necessary during the comparison
382 if (preg_match('@char|binary|blob|text|set|date|time|year@i', $types[$i]) || strpos(' ' . $func_type, 'LIKE')) {
383 $quot = '\'';
384 } else {
385 $quot = '';
388 // LIKE %...%
389 if ($func_type == 'LIKE %...%') {
390 $func_type = 'LIKE';
391 $fields[$i] = '%' . $fields[$i] . '%';
393 if ($func_type == 'REGEXP ^...$') {
394 $func_type = 'REGEXP';
395 $fields[$i] = '^' . $fields[$i] . '$';
398 if ($func_type == 'IN (...)' || $func_type == 'NOT IN (...)' || $func_type == 'BETWEEN' || $func_type == 'NOT BETWEEN') {
399 $func_type = str_replace(' (...)', '', $func_type);
401 // quote values one by one
402 $values = explode(',', $fields[$i]);
403 foreach ($values as &$value)
404 $value = $quot . PMA_sqlAddslashes(trim($value)) . $quot;
406 if ($func_type == 'BETWEEN' || $func_type == 'NOT BETWEEN')
407 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . (isset($values[0]) ? $values[0] : '') . ' AND ' . (isset($values[1]) ? $values[1] : '');
408 else
409 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' (' . implode(',', $values) . ')';
411 else {
412 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . $quot . PMA_sqlAddslashes($fields[$i]) . $quot;;
415 } // end if
416 } // end for
418 if ($w) {
419 $sql_query .= ' WHERE ' . implode(' AND ', $w);
421 } // end if
423 if ($orderField != '--nil--') {
424 $sql_query .= ' ORDER BY ' . PMA_backquote($orderField) . ' ' . $order;
425 } // end if
427 require './sql.php';