Translation update done using Pootle.
[phpmyadmin/madhuracj.git] / libraries / tbl_select.lib.php
blob86601d12df6c57d502daddd8cdd38f3e71747c90
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Functions for the table-search page and zoom-search page
6 * @package PhpMyAdmin
7 */
9 require_once 'url_generating.lib.php';
11 /**
12 * Gets all the fields of a table along with their types, collations
13 * and whether null or not.
15 * @param string $db Selected database
16 * @param string $table Selected table
18 * @return array Array containing the field list, field types, collations
19 * and null constraint
21 function PMA_tbl_getFields($db, $table)
23 // Gets the list and number of fields
24 $fields = PMA_DBI_get_columns($db, $table, null, true);
25 $fields_list = $fields_null = $fields_type = $fields_collation = array();
26 $geom_column_present = false;
27 $geom_types = PMA_getGISDatatypes();
29 foreach ($fields as $key => $row) {
30 $fields_list[] = $row['Field'];
31 $type = $row['Type'];
33 // check whether table contains geometric columns
34 if (in_array($type, $geom_types)) {
35 $geom_column_present = true;
38 // reformat mysql query output
39 if (strncasecmp($type, 'set', 3) == 0
40 || strncasecmp($type, 'enum', 4) == 0
41 ) {
42 $type = str_replace(',', ', ', $type);
43 } else {
44 // strip the "BINARY" attribute, except if we find "BINARY(" because
45 // this would be a BINARY or VARBINARY field type
46 if (!preg_match('@BINARY[\(]@i', $type)) {
47 $type = preg_replace('@BINARY@i', '', $type);
49 $type = preg_replace('@ZEROFILL@i', '', $type);
50 $type = preg_replace('@UNSIGNED@i', '', $type);
52 $type = strtolower($type);
54 if (empty($type)) {
55 $type = '&nbsp;';
57 $fields_null[] = $row['Null'];
58 $fields_type[] = $type;
59 $fields_collation[] = ! empty($row['Collation']) && $row['Collation'] != 'NULL'
60 ? $row['Collation']
61 : '';
62 } // end while
64 return array($fields_list, $fields_type, $fields_collation, $fields_null, $geom_column_present);
67 /**
68 * Sets the table header for displaying a table in query-by-example format.
70 * @param bool $geom_column_present whether a geometry column is present
72 * @return HTML content, the tags and content for table header
74 function PMA_tbl_setTableHeader($geom_column_present = false)
76 // Display the Function column only if there is alteast one geomety colum
77 $func = '';
78 if ($geom_column_present) {
79 $func = '<th>' . __('Function') . '</th>';
82 return '<thead>
83 <tr>' . $func . '<th>' . __('Column') . '</th>
84 <th>' . __('Type') . '</th>
85 <th>' . __('Collation') . '</th>
86 <th>' . __('Operator') . '</th>
87 <th>' . __('Value') . '</th>
88 </tr>
89 </thead>';
92 /**
93 * Returns an array with necessary configrations to create
94 * sub-tabs(Table Search and Zoom Search) in the table_select page.
96 * @return array Array containing configuration (icon, text, link, id, args)
97 * of sub-tabs for Table Search and Zoom search
99 function PMA_tbl_getSubTabs()
101 $subtabs = array();
102 $subtabs['search']['icon'] = 'b_search.png';
103 $subtabs['search']['text'] = __('Table Search');
104 $subtabs['search']['link'] = 'tbl_select.php';
105 $subtabs['search']['id'] = 'tbl_search_id';
106 $subtabs['search']['args']['pos'] = 0;
108 $subtabs['zoom']['icon'] = 'b_props.png';
109 $subtabs['zoom']['link'] = 'tbl_zoom_select.php';
110 $subtabs['zoom']['text'] = __('Zoom Search');
111 $subtabs['zoom']['id'] = 'zoom_search_id';
113 return $subtabs;
117 * Creates the HTML content for:
118 * 1) Browsing foreign data for a field.
119 * 2) Creating elements for search criteria input on fields.
121 * @param array $foreigners Array of foreign keys
122 * @param array $foreignData Foreign keys data
123 * @param string $field Column name
124 * @param string $tbl_fields_type Column type
125 * @param int $i Column index
126 * @param string $db Selected database
127 * @param string $table Selected table
128 * @param array $titles Selected title
129 * @param int $foreignMaxLimit Max limit of displaying foreign elements
130 * @param array $fields Array of search criteria inputs
131 * @param bool $in_fbs Whether we are in 'function based search'
132 * @param bool $in_edit Whether in search mode
133 * or edit mode (used for zoom search)
135 * @return string HTML content for viewing foreing data and elements
136 * for search criteria input.
138 function PMA_getForeignFields_Values($foreigners, $foreignData, $field, $tbl_fields_type, $i, $db, $table, $titles, $foreignMaxLimit, $fields, $in_fbs = false, $in_edit = false)
140 $str = '';
141 if ($foreigners && isset($foreigners[$field]) && is_array($foreignData['disp_row'])) {
142 // f o r e i g n k e y s
143 $str .= '<select name="fields[' . $i . ']" id="fieldID_' . $i .'">' . "\n";
144 // go back to first row
145 // here, the 4th parameter is empty because there is no current
146 // value of data for the dropdown (the search page initial values
147 // are displayed empty)
148 $str .= PMA_foreignDropdown(
149 $foreignData['disp_row'], $foreignData['foreign_field'],
150 $foreignData['foreign_display'], '', $foreignMaxLimit
152 $str .= '</select>' . "\n";
154 } elseif ($foreignData['foreign_link'] == true) {
155 if (isset($fields[$i]) && is_string($fields[$i])) {
156 $str .= '<input type="text" id="fieldID_' . $i .'"name="fields[' . $i . ']" value="' . $fields[$i] . '"';
157 'id="field_' . md5($field) . '[' . $i .']"
158 class="textfield"/>' ;
159 } else {
160 $str .= '<input type="text" id="fieldID_' . $i .'"name="fields[' . $i . ']"';
161 'id="field_' . md5($field) . '[' . $i .']"
162 class="textfield" />' ;
165 <?php $str .= '<script type="text/javascript">';
166 // <![CDATA[
167 $str .= <<<EOT
168 <a target="_blank" onclick="window.open(this.href, 'foreigners', 'width=640,height=240,scrollbars=yes'); return false" href="browse_foreigners.php?
169 EOT;
170 $str .= '' . PMA_generate_common_url($db, $table) . '&amp;field=' . urlencode($field) . '&amp;fieldkey=' . $i . '">' . str_replace("'", "\'", $titles['Browse']) . '</a>';
171 // ]]
172 $str .= '</script>';
174 } elseif (in_array($tbl_fields_type[$i], PMA_getGISDatatypes())) {
175 // g e o m e t r y
176 $str .= '<input type="text" name="fields[' . $i . ']"'
177 .' size="40" class="textfield" id="field_' . $i . '" />' . "\n";
179 if ($in_fbs) {
180 $edit_url = 'gis_data_editor.php?' . PMA_generate_common_url();
181 $edit_str = PMA_getIcon('b_edit.png', __('Edit/Insert'));
182 $str .= '<span class="open_search_gis_editor">';
183 $str .= PMA_linkOrButton($edit_url, $edit_str, array(), false, false, '_blank');
184 $str .= '</span>';
187 } elseif (strncasecmp($tbl_fields_type[$i], 'enum', 4) == 0
188 || (strncasecmp($tbl_fields_type[$i], 'set', 3) == 0 && $in_edit)
190 // e n u m s a n d s e t s
192 // Enum in edit mode --> dropdown
193 // Enum in search mode --> multiselect
194 // Set in edit mode --> multiselect
195 // Set in search mode --> input (skipped here, so the 'else'
196 // section would handle it)
198 $value = explode(', ', str_replace("'", '', substr($tbl_fields_type[$i], 5, -1)));
199 $cnt_value = count($value);
201 if ((strncasecmp($tbl_fields_type[$i], 'enum', 4) && ! $in_edit)
202 || (strncasecmp($tbl_fields_type[$i], 'set', 3) && $in_edit)
204 $str .= '<select name="fields[' . ($i) . '][]" id="fieldID_' . $i .'">' . "\n";
205 } else {
206 $str .= '<select name="fields[' . ($i) . '][]" id="fieldID_' . $i .'"'
207 . ' multiple="multiple" size="' . min(3, $cnt_value) . '">' . "\n";
210 for ($j = 0; $j < $cnt_value; $j++) {
211 if (isset($fields[$i])
212 && is_array($fields[$i])
213 && in_array($value[$j], $fields[$i])
215 $str .= '<option value="' . $value[$j] . '" Selected>'
216 . $value[$j] . '</option>';
217 } else {
218 $str .= '<option value="' . $value[$j] . '">'
219 . $value[$j] . '</option>';
221 } // end for
222 $str .= '</select>' . "\n";
224 } else {
225 // o t h e r c a s e s
226 $the_class = 'textfield';
227 $type = $tbl_fields_type[$i];
229 if ($type == 'date') {
230 $the_class .= ' datefield';
231 } elseif ($type == 'datetime' || substr($type, 0, 9) == 'timestamp') {
232 $the_class .= ' datetimefield';
233 } elseif (substr($type, 0, 3) == 'bit') {
234 $the_class .= ' bit';
237 if (isset($fields[$i]) && is_string($fields[$i])) {
238 $str .= '<input type="text" name="fields[' . $i . ']"'
239 .' size="40" class="' . $the_class . '" id="fieldID_'
240 . $i .'" value = "' . $fields[$i] . '"/>' . "\n";
241 } else {
242 $str .= '<input type="text" name="fields[' . $i . ']"'
243 .' size="40" class="' . $the_class . '" id="fieldID_'
244 . $i .'" />' . "\n";
247 return $str;
251 * Return the where clause for query generation based on the inputs provided.
253 * @param mixed $fields Search criteria input
254 * @param string $names Name of the column on which search is submitted
255 * @param string $types Type of the field
256 * @param string $collations Field collation
257 * @param string $func_type Search fucntion/operator
258 * @param bool $unaryFlag Whether operator unary or not
259 * @param bool $geom_func Whether geometry functions should be applied
261 * @return string HTML content for viewing foreing data and elements
262 * for search criteria input.
264 function PMA_tbl_search_getWhereClause($fields, $names, $types, $collations, $func_type, $unaryFlag, $geom_func = null)
267 * @todo move this to a more apropriate place
269 $geom_unary_functions = array(
270 'IsEmpty' => 1,
271 'IsSimple' => 1,
272 'IsRing' => 1,
273 'IsClosed' => 1,
276 $w = '';
277 // If geometry function is set apply it to the field name
278 if ($geom_func != null && trim($geom_func) != '') {
279 // Get details about the geometry fucntions
280 $geom_funcs = PMA_getGISFunctions($types, true, false);
282 // If the function takes a single parameter
283 if ($geom_funcs[$geom_func]['params'] == 1) {
284 $backquoted_name = $geom_func . '(' . PMA_backquote($names) . ')';
285 } else {
286 // If the function takes two parameters
287 // create gis data from the string
288 $gis_data = PMA_createGISData($fields);
290 $w = $geom_func . '(' . PMA_backquote($names) . ',' . $gis_data . ')';
291 return $w;
294 // New output type is the output type of the function being applied
295 $types = $geom_funcs[$geom_func]['type'];
297 // If the where clause is something like 'IsEmpty(`spatial_col_name`)'
298 if (isset($geom_unary_functions[$geom_func]) && trim($fields) == '') {
299 $w = $backquoted_name;
300 return $w;
302 } else {
303 $backquoted_name = PMA_backquote($names);
306 if ($unaryFlag) {
307 $fields = '';
308 $w = $backquoted_name . ' ' . $func_type;
310 } elseif (in_array($types, PMA_getGISDatatypes()) && ! empty($fields)) {
311 // create gis data from the string
312 $gis_data = PMA_createGISData($fields);
313 $w = $backquoted_name . ' ' . $func_type . ' ' . $gis_data;
315 } elseif (strncasecmp($types, 'enum', 4) == 0) {
316 if (!empty($fields)) {
317 if (! is_array($fields)) {
318 $fields = explode(',', $fields);
320 $enum_selected_count = count($fields);
321 if ($func_type == '=' && $enum_selected_count > 1) {
322 $func_type = 'IN';
323 $parens_open = '(';
324 $parens_close = ')';
326 } elseif ($func_type == '!=' && $enum_selected_count > 1) {
327 $func_type = 'NOT IN';
328 $parens_open = '(';
329 $parens_close = ')';
331 } else {
332 $parens_open = '';
333 $parens_close = '';
335 $enum_where = '\'' . PMA_sqlAddslashes($fields[0]) . '\'';
336 for ($e = 1; $e < $enum_selected_count; $e++) {
337 $enum_where .= ', \'' . PMA_sqlAddslashes($fields[$e]) . '\'';
340 $w = $backquoted_name . ' ' . $func_type . ' ' . $parens_open . $enum_where . $parens_close;
343 } elseif ($fields != '') {
344 // For these types we quote the value. Even if it's another type (like INT),
345 // for a LIKE we always quote the value. MySQL converts strings to numbers
346 // and numbers to strings as necessary during the comparison
347 if (preg_match('@char|binary|blob|text|set|date|time|year@i', $types)
348 || strpos(' ' . $func_type, 'LIKE')
350 $quot = '\'';
351 } else {
352 $quot = '';
355 // LIKE %...%
356 if ($func_type == 'LIKE %...%') {
357 $func_type = 'LIKE';
358 $fields = '%' . $fields . '%';
360 if ($func_type == 'REGEXP ^...$') {
361 $func_type = 'REGEXP';
362 $fields = '^' . $fields . '$';
365 if ($func_type == 'IN (...)'
366 || $func_type == 'NOT IN (...)'
367 || $func_type == 'BETWEEN'
368 || $func_type == 'NOT BETWEEN'
370 $func_type = str_replace(' (...)', '', $func_type);
372 // quote values one by one
373 $values = explode(',', $fields);
374 foreach ($values as &$value) {
375 $value = $quot . PMA_sqlAddslashes(trim($value)) . $quot;
378 if ($func_type == 'BETWEEN' || $func_type == 'NOT BETWEEN') {
379 $w = $backquoted_name . ' ' . $func_type . ' ' . (isset($values[0]) ? $values[0] : '')
380 . ' AND ' . (isset($values[1]) ? $values[1] : '');
381 } else {
382 $w = $backquoted_name . ' ' . $func_type . ' (' . implode(',', $values) . ')';
384 } else {
385 $w = $backquoted_name . ' ' . $func_type . ' ' . $quot . PMA_sqlAddslashes($fields) . $quot;;
387 } // end if
389 return $w;
393 * Formats a SVG plot for the query results.
395 * @param array $data Data for the status chart
396 * @param array &$settings Settings used to generate the chart
398 * @return string HTML and JS code for the SVG plot
400 function PMA_SVG_scatter_plot($data, &$settings)
402 include_once './libraries/svg_plot/pma_scatter_plot.php';
404 if (empty($data)) {
405 // empty data
406 return '';
407 } else {
408 $scatter_plot = new PMA_Scatter_Plot($data, $settings);
410 if ($settings != null) {
411 foreach ($scatter_plot->getSettings() as $setting => $val) {
412 if (! isset($settings[$setting])) {
413 $settings[$setting] = $val;
417 return $scatter_plot->asSVG();