update
[phpmyadmin/crack.git] / tbl_relation.php3
blob1c6f0c80b088f60200ec35b318eb0df888aae378
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 while ($curr_field = PMA_mysql_fetch_array($fi_rs)) {
127 if (isset($curr_field['Key_name']) && $curr_field['Key_name'] == 'PRIMARY') {
128 $field_full = $db . '.' .$curr_field['Table'] . '.' . $curr_field['Column_name'];
129 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
130 break;
131 } else if (isset($curr_field['Non_unique']) && $curr_field['Non_unique'] == 0) {
132 // if we can't find a primary key we take any unique one
133 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
134 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
135 } // end if
136 } // end while over keys
137 if (isset($field_full) && isset($field_v)) {
138 $selectboxall[$field_full] = $field_v;
140 } // end if (mysql_num_rows)
141 // Mike Beck - 24.07.02: i've been asked to add all keys of the
142 // current table (see bug report #574851)
144 else if ($curr_table[0] == $table) {
145 $fi_query = 'SHOW KEYS FROM ' . PMA_backquote($curr_table[0]);
146 $fi_rs = PMA_mysql_query($fi_query) or PMA_mysqlDie('', $fi_query, '', $err_url_0);
147 if ($fi_rs && mysql_num_rows($fi_rs) > 0) {
148 while ($curr_field = PMA_mysql_fetch_array($fi_rs)) {
149 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
150 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
151 if (isset($field_full) && isset($field_v)) {
152 $selectboxall[$field_full] = $field_v;
154 } // end while
155 } // end if (mysql_num_rows)
157 } // end while over tables
159 // Create array of relations (Mike Beck)
160 $rel_dest = PMA_getForeigners($db, $table);
161 } // end if
163 // Now find out the columns of our $table
164 $col_query = 'SHOW COLUMNS FROM ' . PMA_backquote($table);
165 $col_rs = PMA_mysql_query($col_query) or PMA_mysqlDie('', $col_query, '', $err_url_0);
167 if ($col_rs && mysql_num_rows($col_rs) > 0) {
168 while ($row = PMA_mysql_fetch_array($col_rs)) {
169 $save_row[] = $row;
171 $saved_row_cnt = count($save_row);
174 <form method="post" action="tbl_relation.php3">
175 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
176 <input type="hidden" name="submit_rel" value="true" />
178 <table>
179 <tr>
180 <th colspan="2" align="center"><b><?php echo $strLinksTo; ?></b></th>
181 </tr>
182 <?php
183 for ($i = 0; $i < $saved_row_cnt; $i++) {
184 echo "\n";
186 <tr>
187 <th><?php echo $save_row[$i]['Field']; ?></th>
188 <td>
189 <input type="hidden" name="src_field" value="<?php echo $save_row[$i]['Field']; ?>" />
190 <select name="destination[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
191 <?php
192 echo "\n";
193 reset($selectboxall);
194 $myfield = $save_row[$i]['Field'];
195 if (isset($existrel[$myfield])) {
196 $foreign_field = $existrel[$myfield]['foreign_db'] . '.'
197 . $existrel[$myfield]['foreign_table'] . '.'
198 . $existrel[$myfield]['foreign_field'];
199 } else {
200 $foreign_field = FALSE;
202 $seen_key = FALSE;
203 while (list($key, $value) = each($selectboxall)) {
204 echo ' '
205 . '<option value="' . htmlspecialchars($key) . '"';
206 if ($foreign_field && $key == $foreign_field) {
207 echo ' selected="selected"';
208 $seen_key = TRUE;
210 echo '>' . $value . '</option>'. "\n";
211 } // end while
213 // if the link defined in relationtable points to a foreign field
214 // that is not a key in the foreign table, we show the link
215 // (will not be shown with an arrow)
216 if ($foreign_field && !$seen_key) {
217 echo ' '
218 . '<option value="' . htmlspecialchars($foreign_field) . '"';
219 echo ' selected="selected"';
220 echo '>' . $foreign_field . '</option>'. "\n";
223 </select>
224 </td>
225 </tr>
226 <?php
227 } // end for
229 echo "\n";
231 <tr>
232 <td colspan="2" align="center">
233 <input type="submit" value="<?php echo $strGo; ?>" />
234 </td>
235 </tr>
236 </table>
237 </form>
239 <?php
240 if ($cfgRelation['displaywork']) {
241 // Get "display_filed" infos
242 $disp = PMA_getDisplayField($db, $table);
244 echo "\n";
246 <form method="post" action="tbl_relation.php3">
247 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
248 <input type="hidden" name="submit_show" value="true" />
250 <p><?php echo $strChangeDisplay; ?></p>
251 <select name="display_field" onchange="this.form.submit();">
252 <?php
253 echo "\n";
254 mysql_data_seek($col_rs, 0);
255 while ($row = @PMA_mysql_fetch_array($col_rs)) {
256 echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
257 if (isset($disp) && $row['Field'] == $disp) {
258 echo ' selected="selected"';
260 echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
261 } // end while
263 </select>
264 <script type="text/javascript" language="javascript">
265 <!--
266 // Fake js to allow the use of the <noscript> tag
267 //-->
268 </script>
269 <noscript>
270 <input type="submit" value="<?php echo $strGo; ?>" />
271 </noscript>
272 </form>
273 <?php
274 } // end if (displayworks)
276 if ($cfgRelation['commwork']) {
278 echo "\n";
280 <form method="post" action="tbl_relation.php3">
281 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
282 <input type="hidden" name="submit_comm" value="true" />
284 <table>
285 <tr>
286 <th colspan="2" align="center"><b><?php echo $strComments; ?></b></th>
287 </tr>
288 <?php
289 for ($i = 0; $i < $saved_row_cnt; $i++) {
290 $field = $save_row[$i]['Field'];
291 echo "\n";
293 <tr>
294 <th><?php echo $field; ?></th>
295 <td>
296 <input type="text" name="comment[<?php echo $field; ?>]" value="<?php echo (isset($comments[$field]) ? htmlspecialchars($comments[$field]) : ''); ?>" />
297 </td>
298 </tr>
299 <?php
300 } // end for
302 echo "\n";
304 <tr>
305 <td colspan="2" align="center">
306 <input type="submit" value="<?php echo $strGo; ?>" />
307 </td>
308 </tr>
309 </table>
310 </form>
311 <?php
312 } // end if (comments work)
313 } // end if (we have columns in this table)
317 * Displays the footer
319 echo "\n";
320 require('./footer.inc.php3');