Initial import.
[openemr.git] / interface / main / myadmin / tbl_select.php
blobd8936e6139b4df590a95eb877308785f3c7d800d
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
6 /**
7 * Gets some core libraries
8 */
9 require_once('./libraries/grab_globals.lib.php');
10 require_once('./libraries/common.lib.php');
11 require_once('./libraries/relation.lib.php'); // foreign keys
13 if ($cfg['PropertiesIconic'] == true) {
14 // We need to copy the value or else the == 'both' check will always return true
15 $propicon = (string)$cfg['PropertiesIconic'];
17 if ($propicon == 'both') {
18 $iconic_spacer = '<nobr>';
19 } else {
20 $iconic_spacer = '';
23 $titles['Browse'] = $iconic_spacer . '<img width="12" height="13" src="images/button_browse.png" alt="' . $strBrowseForeignValues . '" title="' . $strBrowseForeignValues . '" border="0" />';
25 if ($propicon == 'both') {
26 $titles['Browse'] .= '&nbsp;' . $strBrowseForeignValues . '</nobr>';
28 } else {
29 $titles['Browse'] = $strBrowseForeignValues;
32 /**
33 * Defines arrays of functions (should possibly be in config.inc.php
34 * so it can also be used in tbl_qbe.php)
36 * LIKE works also on integers and dates so I added it in numfunctions
38 $numfunctions = array('=', '>', '>=', '<', '<=', '!=', 'LIKE');
39 $textfunctions = array('LIKE', '=', '!=');
41 /**
42 * Not selection yet required -> displays the selection form
44 if (!isset($param) || $param[0] == '') {
45 // Gets some core libraries
46 require('./tbl_properties_common.php');
47 //$err_url = 'tbl_select.php' . $err_url;
48 $url_query .= '&amp;goto=tbl_select.php&amp;back=tbl_select.php';
49 require('./tbl_properties_table_info.php');
51 if (!isset($goto)) {
52 $goto = $cfg['DefaultTabTable'];
54 // Defines the url to return to in case of error in the next sql statement
55 $err_url = $goto . '?' . PMA_generate_common_url($db, $table);
57 // Gets the list and number of fields
58 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db);
59 $result = @PMA_mysql_query($local_query);
60 if (!$result) {
61 PMA_mysqlDie('', $local_query, '', $err_url);
63 else {
64 $fields_cnt = mysql_num_rows($result);
65 while ($row = PMA_mysql_fetch_array($result)) {
66 $fields_list[] = $row['Field'];
67 $type = $row['Type'];
68 // reformat mysql query output - staybyte - 9. June 2001
69 $shorttype = substr($type, 0, 3);
70 if ($shorttype == 'set' || $shorttype == 'enu') {
71 $type = str_replace(',', ', ', $type);
72 } else {
73 $type = preg_replace('@BINARY@i', '', $type);
74 $type = preg_replace('@ZEROFILL@i', '', $type);
75 $type = preg_replace('@UNSIGNED@i', '', $type);
77 if (empty($type)) {
78 $type = '&nbsp;';
80 $fields_type[] = $type;
81 } // end while
82 mysql_free_result($result);
84 // <markus@noga.de>
85 // retrieve keys into foreign fields, if any
86 $cfgRelation = PMA_getRelationsParam();
87 // check also foreigners even if relwork is FALSE (to get
88 // foreign keys from innodb)
89 //$foreigners = ($cfgRelation['relwork'] ? PMA_getForeigners($db, $table) : FALSE);
90 $foreigners = PMA_getForeigners($db, $table);
92 <form method="post" action="tbl_select.php" name="insertForm">
93 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
94 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
95 <input type="hidden" name="back" value="tbl_select.php" />
96 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
97 <?php echo $strSelectFields; ?>&nbsp;:<br />
98 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
99 <select name="param[]" size="<?php echo ($fields_cnt < 10) ? $fields_cnt : 10; ?>" multiple="multiple">
100 <?php
101 echo "\n";
102 // Displays the list of the fields
103 for ($i = 0 ; $i < $fields_cnt; $i++) {
104 echo ' <option value="' . htmlspecialchars($fields_list[$i]) . '" selected="selected">' . htmlspecialchars($fields_list[$i]) . '</option>' . "\n";
107 </select><br />
108 <ul>
109 <li>
110 <div style="margin-bottom: 10px">
111 <?php echo $strLimitNumRows . "\n"; ?>
112 <input type="text" size="4" name="session_max_rows" value="<?php echo $cfg['MaxRows']; ?>" class="textfield" />
113 </div>
114 </li>
115 <li>
116 <?php echo $strAddSearchConditions; ?><br />
117 <input type="text" name="where" class="textfield" />&nbsp;
118 <?php echo PMA_showMySQLDocu('Reference', 'Functions') . "\n"; ?>
119 <br /><br />
120 <?php echo '<i>' . $strOr . '</i> ' . $strDoAQuery; ?><br />
121 <table border="<?php echo $cfg['Border']; ?>">
122 <tr>
123 <th><?php echo $strField; ?></th>
124 <th><?php echo $strType; ?></th>
125 <th><?php echo $strFunction; ?></th>
126 <th><?php echo $strValue; ?></th>
127 </tr>
128 <?php
129 for ($i = 0; $i < $fields_cnt; $i++) {
130 echo "\n";
131 $bgcolor = ($i % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo'];
133 <tr>
134 <td bgcolor="<?php echo $bgcolor; ?>"><?php echo htmlspecialchars($fields_list[$i]); ?></td>
135 <td bgcolor="<?php echo $bgcolor; ?>"><?php echo $fields_type[$i]; ?></td>
136 <td bgcolor="<?php echo $bgcolor; ?>">
137 <select name="func[]">
138 <?php
139 if (preg_match('@char|blob|text|set|enum@i', $fields_type[$i])) {
140 foreach($textfunctions AS $k => $fc) {
141 echo "\n" . ' '
142 . '<option value="' . htmlspecialchars($fc) . '">' . htmlspecialchars($fc) . '</option>';
143 } // end while
144 } else {
145 foreach($numfunctions AS $k => $fc) {
146 echo "\n" . ' '
147 . '<option value="' . htmlspecialchars($fc) . '">' . htmlspecialchars($fc) . '</option>';
148 } // end while
149 } // end if... else...
150 echo "\n";
152 </select>
153 </td>
154 <td bgcolor="<?php echo $bgcolor; ?>">
155 <?php
156 // <markus@noga.de>
157 $field = $fields_list[$i];
159 require('./libraries/get_foreign.lib.php');
161 echo "\n";
162 // we got a bug report: in some cases, even if $disp is true,
163 // there are no rows, so we add a fetch_array
164 if ($foreigners && isset($foreigners[$field]) && isset($disp) && $disp && @PMA_mysql_fetch_array($disp)) {
165 // f o r e i g n k e y s
166 echo ' <select name="fields[]">' . "\n";
167 // go back to first row
168 mysql_data_seek($disp,0);
169 echo PMA_foreignDropdown($disp, $foreign_field, $foreign_display, $data, 100);
170 echo ' </select>' . "\n";
171 } else if (isset($foreign_link) && $foreign_link == true) {
173 <input type="text" name="fields[]" id="field_<?php echo md5($field); ?>[]" class="textfield" />
174 <script type="text/javascript" language="javascript">
175 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); ?>"><?php echo str_replace("'", "\'", $titles['Browse']); ?></a>');
176 </script>
177 <?php
178 } else if (substr($fields_type[$i], 0, 3)=='enu'){
179 // e n u m s
180 $enum_value=explode(', ', str_replace("'", '', substr($fields_type[$i], 5, -1)));
181 echo ' <select name="fields[]">' . "\n";
182 echo ' <option value=""></option>' . "\n";
183 $cnt_enum_value = count($enum_value);
184 for ($j=0; $j<$cnt_enum_value;$j++){
185 echo ' <option value="' . $enum_value[$j] . '">' . $enum_value[$j] . '</option>';
186 } // end for
187 echo ' </select>' . "\n";
188 } else {
189 // o t h e r c a s e s
190 echo ' <input type="text" name="fields[]" size="40" class="textfield" />' . "\n";
194 <input type="hidden" name="names[]" value="<?php echo htmlspecialchars($fields_list[$i]); ?>" />
195 <input type="hidden" name="types[]" value="<?php echo $fields_type[$i]; ?>" />
196 </td>
197 </tr>
198 <?php
199 } // end for
200 echo "\n";
202 </table><br />
203 </li>
204 <li>
205 <?php echo $strDisplayOrder; ?><br />
206 <select name="orderField" style="vertical-align: middle">
207 <option value="--nil--"></option>
208 <?php
209 echo "\n";
210 for ($i = 0; $i < $fields_cnt; $i++) {
211 echo ' ';
212 echo '<option value="' . htmlspecialchars($fields_list[$i]) . '">' . htmlspecialchars($fields_list[$i]) . '</option>' . "\n";
213 } // end for
215 </select>
216 <input type="radio" name="order" value="ASC" checked="checked" />
217 <?php echo $strAscending; ?>&nbsp;
218 <input type="radio" name="order" value="DESC" />
219 <?php echo $strDescending; ?><br /><br />
220 </li>
221 </ul>
223 &nbsp;&nbsp;&nbsp;&nbsp;
224 <input type="hidden" name="max_number_of_fields" value="<?php echo $fields_cnt; ?>" />
225 <input type="submit" name="submit" value="<?php echo $strGo; ?>" />
226 </form>
227 <?php
228 } // end if
229 require_once('./footer.inc.php');
234 * Selection criteria have been submitted -> do the work
236 else {
237 // Builds the query
239 $sql_query = 'SELECT ';
241 // if all fields were selected to display, we do a SELECT *
242 // (more efficient and this helps prevent a problem in IE
243 // if one of the rows is edited and we come back to the Select results)
245 if (count($param) == $max_number_of_fields) {
246 $sql_query .= '* ';
247 } else {
249 $sql_query .= PMA_backquote(urldecode($param[0]));
250 $i = 0;
251 $c = count($param);
252 while ($i < $c) {
253 if ($i > 0) {
254 $sql_query .= ',' . PMA_backquote(urldecode($param[$i]));
256 $i++;
258 } // end if
260 $sql_query .= ' FROM ' . PMA_backquote($table);
261 // The where clause
262 if ($where != '') {
263 $sql_query .= ' WHERE ' . $where;
265 else {
266 $sql_query .= ' WHERE 1';
267 $cnt_fields = count($fields);
268 for ($i = 0; $i < $cnt_fields; $i++) {
269 if (!empty($fields) && $fields[$i] != '') {
270 if (preg_match('@char|blob|text|set|enum|date|time|year@i', $types[$i])) {
271 $quot = '\'';
272 } else {
273 $quot = '';
275 if (strtoupper($fields[$i]) == 'NULL' || strtoupper($fields[$i]) == 'NOT NULL') {
276 $quot = '';
277 $func[$i] = 'IS';
279 //$sql_query .= ' AND ' . PMA_backquote(urldecode($names[$i])) . " $func[$i] $quot$fields[$i]$quot";
281 $sql_query .= ' AND ' . PMA_backquote(urldecode($names[$i])) . ' ' . $func[$i] . ' ' . $quot . PMA_sqlAddslashes($fields[$i]) . $quot;
283 } // end if
284 } // end for
285 } // end if
287 if ($orderField != '--nil--') {
288 $sql_query .= ' ORDER BY ' . PMA_backquote(urldecode($orderField)) . ' ' . $order;
289 } // end if
291 include('./sql.php');