PHP3 compatibility
[phpmyadmin/crack.git] / tbl_relation.php3
blob9c46717c0a5cbea6db1ca20efb9d134f6ade00ba
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
6 /**
7 * Gets some core libraries
8 */
9 require('./libraries/grab_globals.lib.php3');
10 require('./libraries/common.lib.php3');
11 require('./tbl_properties_common.php3');
12 $url_query .= '&amp;goto=tbl_properties.php3';
13 require('./tbl_properties_table_info.php3');
14 require('./libraries/relation.lib.php3');
16 /**
17 * Gets the relation settings
19 $cfgRelation = PMA_getRelationsParam();
22 /**
23 * Updates
26 if ($cfgRelation['relwork']) {
27 $existrel = PMA_getForeigners($db, $table);
29 if ($cfgRelation['displaywork']) {
30 $disp = PMA_getDisplayField($db, $table);
32 if ($cfgRelation['relwork']
33 && isset($submit_rel) && $submit_rel == 'true') {
35 while (list($key, $value) = each($destination)) {
36 if ($value != 'nix') {
37 $for = explode('.', $value);
38 if (!isset($existrel[$key])) {
39 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['relation'])
40 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
41 . ' values('
42 . '\'' . PMA_sqlAddslashes($db) . '\', '
43 . '\'' . PMA_sqlAddslashes($table) . '\', '
44 . '\'' . PMA_sqlAddslashes($key) . '\', '
45 . '\'' . PMA_sqlAddslashes($for[0]) . '\', '
46 . '\'' . PMA_sqlAddslashes($for[1]) . '\','
47 . '\'' . PMA_sqlAddslashes($for[2]) . '\')';
48 } else if ($existrel[$key] != $value) {
49 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['relation']) . ' SET'
50 . ' foreign_db = \'' . PMA_sqlAddslashes($for[0]) . '\', '
51 . ' foreign_table = \'' . PMA_sqlAddslashes($for[1]) . '\', '
52 . ' foreign_field = \'' . PMA_sqlAddslashes($for[2]) . '\' '
53 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
54 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
55 . ' AND master_field = \'' . PMA_sqlAddslashes($key) . '\'';
56 } // end if... else....
57 } else if (isset($existrel[$key])) {
58 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['relation'])
59 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
60 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
61 . ' AND master_field = \'' . PMA_sqlAddslashes($key) . '\'';
62 } // end if... else....
63 if (isset($upd_query)) {
64 $upd_rs = PMA_query_as_cu($upd_query);
65 unset($upd_query);
67 } // end while
68 } // end if
70 if ($cfgRelation['displaywork']
71 && isset($submit_show) && $submit_show == 'true') {
73 if ($disp) {
74 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['table_info'])
75 . ' SET display_field = \'' . PMA_sqlAddslashes($display_field) . '\''
76 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
77 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
78 } else {
79 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['table_info'])
80 . '(db_name, table_name, display_field) '
81 . ' VALUES('
82 . '\'' . PMA_sqlAddslashes($db) . '\','
83 . '\'' . PMA_sqlAddslashes($table) . '\','
84 . '\'' . PMA_sqlAddslashes($display_field) . '\')';
87 if (isset($upd_query)) {
88 $upd_rs = PMA_query_as_cu($upd_query);
90 } // end if
92 if ($cfgRelation['commwork']
93 && isset($submit_comm) && $submit_comm == 'true') {
94 while (list($key, $value) = each($comment)) {
95 // garvin: I exported the snippet here to a function (relation.lib.php3) , so it can be used multiple times throughout other pages where you can set comments.
96 PMA_setComment($db, $table, $key, $value);
97 } // end while (transferred data)
98 } // end if (commwork)
100 // Now that we might have changed we have to see again
101 if ($cfgRelation['relwork']) {
102 $existrel = PMA_getForeigners($db, $table);
104 if ($cfgRelation['displaywork']) {
105 $disp = PMA_getDisplayField($db, $table);
107 if ($cfgRelation['commwork']) {
108 $comments = PMA_getComments($db, $table);
113 * Dialog
115 if ($cfgRelation['relwork']) {
117 // To choose relations we first need all tables names in current db
118 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($db);
119 $tab_rs = PMA_mysql_query($tab_query) or PMA_mysqlDie('', $tab_query, '', $err_url_0);
120 $selectboxall['nix'] = '--';
121 while ($curr_table = @PMA_mysql_fetch_array($tab_rs)) {
122 if (($curr_table[0] != $table) && ($curr_table[0] != $cfg['Server']['relation'])) {
123 $fi_query = 'SHOW KEYS FROM ' . PMA_backquote($curr_table[0]);
124 $fi_rs = PMA_mysql_query($fi_query) or PMA_mysqlDie('', $fi_query, '', $err_url_0);
125 if ($fi_rs && mysql_num_rows($fi_rs) > 0) {
126 $seen_a_primary=FALSE;
127 while ($curr_field = PMA_mysql_fetch_array($fi_rs)) {
128 if (isset($curr_field['Key_name']) && $curr_field['Key_name'] == 'PRIMARY') {
129 $seen_a_primary=TRUE;
130 $field_full = $db . '.' .$curr_field['Table'] . '.' . $curr_field['Column_name'];
131 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
132 $selectboxall[$field_full] = $field_v;
133 // there could be more than one segment of the primary
134 // so do not break
136 } else if (isset($curr_field['Non_unique']) && $curr_field['Non_unique'] == 0 && $seen_a_primary==FALSE) {
137 // if we can't find a primary key we take any unique one
138 // (in fact, we show all segments of unique keys
139 // and all unique keys)
140 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
141 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
142 $selectboxall[$field_full] = $field_v;
143 } // end if
144 } // end while over keys
145 } // end if (mysql_num_rows)
147 // Mike Beck - 24.07.02: i've been asked to add all keys of the
148 // current table (see bug report #574851)
150 else if ($curr_table[0] == $table) {
151 $fi_query = 'SHOW KEYS FROM ' . PMA_backquote($curr_table[0]);
152 $fi_rs = PMA_mysql_query($fi_query) or PMA_mysqlDie('', $fi_query, '', $err_url_0);
153 if ($fi_rs && mysql_num_rows($fi_rs) > 0) {
154 while ($curr_field = PMA_mysql_fetch_array($fi_rs)) {
155 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
156 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
157 if (isset($field_full) && isset($field_v)) {
158 $selectboxall[$field_full] = $field_v;
160 } // end while
161 } // end if (mysql_num_rows)
163 } // end while over tables
165 // Create array of relations (Mike Beck)
166 $rel_dest = PMA_getForeigners($db, $table);
167 } // end if
169 // Now find out the columns of our $table
170 $col_query = 'SHOW COLUMNS FROM ' . PMA_backquote($table);
171 $col_rs = PMA_mysql_query($col_query) or PMA_mysqlDie('', $col_query, '', $err_url_0);
173 if ($col_rs && mysql_num_rows($col_rs) > 0) {
174 while ($row = PMA_mysql_fetch_array($col_rs)) {
175 $save_row[] = $row;
177 $saved_row_cnt = count($save_row);
180 <form method="post" action="tbl_relation.php3">
181 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
182 <input type="hidden" name="submit_rel" value="true" />
184 <table>
185 <tr>
186 <th colspan="2" align="center"><b><?php echo $strLinksTo; ?></b></th>
187 </tr>
188 <?php
189 for ($i = 0; $i < $saved_row_cnt; $i++) {
190 echo "\n";
192 <tr>
193 <th><?php echo $save_row[$i]['Field']; ?></th>
194 <td>
195 <input type="hidden" name="src_field" value="<?php echo $save_row[$i]['Field']; ?>" />
196 <select name="destination[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
197 <?php
198 echo "\n";
199 reset($selectboxall);
200 $myfield = $save_row[$i]['Field'];
201 if (isset($existrel[$myfield])) {
202 $foreign_field = $existrel[$myfield]['foreign_db'] . '.'
203 . $existrel[$myfield]['foreign_table'] . '.'
204 . $existrel[$myfield]['foreign_field'];
205 } else {
206 $foreign_field = FALSE;
208 $seen_key = FALSE;
209 while (list($key, $value) = each($selectboxall)) {
210 echo ' '
211 . '<option value="' . htmlspecialchars($key) . '"';
212 if ($foreign_field && $key == $foreign_field) {
213 echo ' selected="selected"';
214 $seen_key = TRUE;
216 echo '>' . $value . '</option>'. "\n";
217 } // end while
219 // if the link defined in relationtable points to a foreign field
220 // that is not a key in the foreign table, we show the link
221 // (will not be shown with an arrow)
222 if ($foreign_field && !$seen_key) {
223 echo ' '
224 . '<option value="' . htmlspecialchars($foreign_field) . '"';
225 echo ' selected="selected"';
226 echo '>' . $foreign_field . '</option>'. "\n";
229 </select>
230 </td>
231 </tr>
232 <?php
233 } // end for
235 echo "\n";
237 <tr>
238 <td colspan="2" align="center">
239 <input type="submit" value="<?php echo $strGo; ?>" />
240 </td>
241 </tr>
242 </table>
243 </form>
245 <?php
246 if ($cfgRelation['displaywork']) {
247 // Get "display_filed" infos
248 $disp = PMA_getDisplayField($db, $table);
250 echo "\n";
252 <form method="post" action="tbl_relation.php3">
253 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
254 <input type="hidden" name="submit_show" value="true" />
256 <p><?php echo $strChangeDisplay; ?></p>
257 <select name="display_field" onchange="this.form.submit();">
258 <?php
259 echo "\n";
260 mysql_data_seek($col_rs, 0);
261 while ($row = @PMA_mysql_fetch_array($col_rs)) {
262 echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
263 if (isset($disp) && $row['Field'] == $disp) {
264 echo ' selected="selected"';
266 echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
267 } // end while
269 </select>
270 <script type="text/javascript" language="javascript">
271 <!--
272 // Fake js to allow the use of the <noscript> tag
273 //-->
274 </script>
275 <noscript>
276 <input type="submit" value="<?php echo $strGo; ?>" />
277 </noscript>
278 </form>
279 <?php
280 } // end if (displayworks)
282 if ($cfgRelation['commwork']) {
284 echo "\n";
286 <form method="post" action="tbl_relation.php3">
287 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
288 <input type="hidden" name="submit_comm" value="true" />
290 <table>
291 <tr>
292 <th colspan="2" align="center"><b><?php echo $strComments; ?></b></th>
293 </tr>
294 <?php
295 for ($i = 0; $i < $saved_row_cnt; $i++) {
296 $field = $save_row[$i]['Field'];
297 echo "\n";
299 <tr>
300 <th><?php echo $field; ?></th>
301 <td>
302 <input type="text" name="comment[<?php echo $field; ?>]" value="<?php echo (isset($comments[$field]) ? htmlspecialchars($comments[$field]) : ''); ?>" />
303 </td>
304 </tr>
305 <?php
306 } // end for
308 echo "\n";
310 <tr>
311 <td colspan="2" align="center">
312 <input type="submit" value="<?php echo $strGo; ?>" />
313 </td>
314 </tr>
315 </table>
316 </form>
317 <?php
318 } // end if (comments work)
319 } // end if (we have columns in this table)
323 * Displays the footer
325 echo "\n";
326 require('./footer.inc.php3');