3 // vim: expandtab sw=4 ts=4 sts=4:
7 * Gets some core libraries
9 require('./libraries/grab_globals.lib.php3');
10 require('./libraries/common.lib.php3');
11 require('./tbl_properties_common.php3');
12 $url_query .= '&goto=tbl_properties.php3';
13 require('./tbl_properties_table_info.php3');
14 require('./libraries/relation.lib.php3');
18 * Gets the relation settings
20 $cfgRelation = PMA_getRelationsParam();
24 * Adds/removes slashes if required
26 * @param string the string to slash
28 * @return string the slashed string
32 function PMA_handleSlashes($val) {
33 return (get_magic_quotes_gpc() ?
str_replace('\\"', '"', $val) : PMA_sqlAddslashes($val));
34 } // end of the "PMA_handleSlashes()" function
41 if ($cfgRelation['relwork']) {
42 $existrel = PMA_getForeigners($db, $table);
44 if ($cfgRelation['displaywork']) {
45 $disp = PMA_getDisplayField($db, $table);
47 if ($cfgRelation['relwork']
48 && isset($submit_rel) && $submit_rel == 'true') {
50 while (list($key, $value) = each($destination)) {
51 if ($value != 'nix') {
52 $for = explode('.', $value);
53 if (!isset($existrel[$key])) {
54 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['relation'])
55 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
57 . '\'' . PMA_sqlAddslashes($db) . '\', '
58 . '\'' . PMA_sqlAddslashes($table) . '\', '
59 . '\'' . PMA_handleSlashes($key) . '\', '
60 . '\'' . PMA_handleSlashes($for[0]) . '\', '
61 . '\'' . PMA_handleSlashes($for[1]) . '\','
62 . '\'' . PMA_handleSlashes($for[2]) . '\')';
63 } else if ($existrel[$key] != $value) {
64 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['relation']) . ' SET'
65 . ' foreign_db = \'' . PMA_handleSlashes($for[0]) . '\', '
66 . ' foreign_table = \'' . PMA_handleSlashes($for[1]) . '\', '
67 . ' foreign_field = \'' . PMA_handleSlashes($for[2]) . '\' '
68 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
69 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
70 . ' AND master_field = \'' . PMA_handleSlashes($key) . '\'';
71 } // end if... else....
72 } else if (isset($existrel[$key])) {
73 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['relation'])
74 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
75 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
76 . ' AND master_field = \'' . PMA_handleSlashes($key) . '\'';
77 } // end if... else....
78 if (isset($upd_query)) {
79 $upd_rs = PMA_query_as_cu($upd_query);
85 if ($cfgRelation['displaywork']
86 && isset($submit_show) && $submit_show == 'true') {
89 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['table_info'])
90 . ' SET display_field = \'' . PMA_handleSlashes($display_field) . '\''
91 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
92 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
94 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['table_info'])
95 . '(db_name, table_name, display_field) '
97 . '\'' . PMA_sqlAddslashes($db) . '\','
98 . '\'' . PMA_sqlAddslashes($table) . '\','
99 . '\'' . PMA_handleSlashes($display_field) . '\')';
101 if (isset($upd_query)) {
102 $upd_rs = PMA_query_as_cu($upd_query);
106 if ($cfgRelation['commwork']
107 && isset($submit_comm) && $submit_comm == 'true') {
108 while (list($key, $value) = each($comment)) {
109 $test_qry = 'SELECT comment FROM ' . PMA_backquote($cfgRelation['column_comments'])
110 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
111 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
112 . ' AND column_name = \'' . PMA_handleSlashes($key) . '\'';
113 $test_rs = PMA_query_as_cu($test_qry);
114 if ($test_rs && mysql_num_rows($test_rs) > 0) {
115 if (strlen($value) > 0) {
116 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['column_comments'])
117 . ' SET comment = \'' . PMA_handleSlashes($value) . '\''
118 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
119 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
120 . ' AND column_name = \'' . PMA_handleSlashes($key) . '\'';
122 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['column_comments'])
123 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
124 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
125 . ' AND column_name = \'' . PMA_handleSlashes($key) . '\'';
127 } else if (strlen($value) > 0) {
128 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['column_comments'])
129 . ' (db_name, table_name, column_name, comment) '
131 . '\'' . PMA_sqlAddslashes($db) . '\','
132 . '\'' . PMA_sqlAddslashes($table) . '\','
133 . '\'' . PMA_handleSlashes($key) . '\','
134 . '\'' . PMA_handleSlashes($value) . '\')';
136 if (isset($upd_query)){
137 $upd_rs = PMA_query_as_cu($upd_query);
139 } // end while (transferred data)
140 } // end if (commwork)
142 // Now that we might have changed we have to see again
143 if ($cfgRelation['relwork']) {
144 $existrel = PMA_getForeigners($db, $table);
146 if ($cfgRelation['displaywork']) {
147 $disp = PMA_getDisplayField($db, $table);
149 if ($cfgRelation['commwork']) {
150 $comments = PMA_getComments($db, $table);
157 if ($cfgRelation['relwork']) {
159 // To choose relations we first need all tables names in current db
160 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($db);
161 $tab_rs = PMA_mysql_query($tab_query) or PMA_mysqlDie('', $tab_query, '', $err_url_0);
162 $selectboxall['nix'] = '--';
163 while ($curr_table = @PMA_mysql_fetch_array
($tab_rs)) {
164 if (($curr_table[0] != $table) && ($curr_table[0] != $cfg['Server']['relation'])) {
165 $fi_query = 'SHOW KEYS FROM ' . PMA_backquote($curr_table[0]);
166 $fi_rs = PMA_mysql_query($fi_query) or PMA_mysqlDie('', $fi_query, '', $err_url_0);
167 if ($fi_rs && mysql_num_rows($fi_rs) > 0) {
168 while ($curr_field = PMA_mysql_fetch_array($fi_rs)) {
169 if (isset($curr_field['Key_name']) && $curr_field['Key_name'] == 'PRIMARY') {
170 $field_full = $db . '.' .$curr_field['Table'] . '.' . $curr_field['Column_name'];
171 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
173 } else if (isset($curr_field['Non_unique']) && $curr_field['Non_unique'] == 0) {
174 // if we can't find a primary key we take any unique one
175 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
176 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
178 } // end while over keys
179 if (isset($field_full) && isset($field_v)) {
180 $selectboxall[$field_full] = $field_v;
182 } // end if (mysql_num_rows)
183 // Mike Beck - 24.07.02: i've been asked to add all keys of the
184 // current table (see bug report #574851)
186 else if ($curr_table[0] == $table) {
187 $fi_query = 'SHOW KEYS FROM ' . PMA_backquote($curr_table[0]);
188 $fi_rs = PMA_mysql_query($fi_query) or PMA_mysqlDie('', $fi_query, '', $err_url_0);
189 if ($fi_rs && mysql_num_rows($fi_rs) > 0) {
190 while ($curr_field = PMA_mysql_fetch_array($fi_rs)) {
191 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
192 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
193 if (isset($field_full) && isset($field_v)) {
194 $selectboxall[$field_full] = $field_v;
197 } // end if (mysql_num_rows)
199 } // end while over tables
201 // Create array of relations (Mike Beck)
202 $rel_dest = PMA_getForeigners($db, $table);
205 // Now find out the columns of our $table
206 $col_query = 'SHOW COLUMNS FROM ' . PMA_backquote($table);
207 $col_rs = PMA_mysql_query($col_query) or PMA_mysqlDie('', $col_query, '', $err_url_0);
209 if ($col_rs && mysql_num_rows($col_rs) > 0) {
210 while ($row = PMA_mysql_fetch_array($col_rs)) {
213 $saved_row_cnt = count($save_row);
216 <form method
="post" action
="tbl_relation.php3">
217 <input type
="hidden" name
="lang" value
="<?php echo $lang; ?>" />
218 <input type
="hidden" name
="server" value
="<?php echo $server; ?>" />
219 <input type
="hidden" name
="db" value
="<?php echo htmlspecialchars($db); ?>" />
220 <input type
="hidden" name
="table" value
="<?php echo htmlspecialchars($table); ?>" />
221 <input type
="hidden" name
="submit_rel" value
="true" />
225 <th colspan
="2" align
="center"><b
><?php
echo $strLinksTo; ?
></b
></th
>
228 for ($i = 0; $i < $saved_row_cnt; $i++
) {
232 <th
><?php
echo $save_row[$i]['Field']; ?
></th
>
234 <input type
="hidden" name
="src_field" value
="<?php echo $save_row[$i]['Field']; ?>" />
235 <select name
="destination[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
238 reset($selectboxall);
239 $myfield = $save_row[$i]['Field'];
240 if (isset($existrel[$myfield])) {
241 $foreign_field = $existrel[$myfield]['foreign_db'] . '.'
242 . $existrel[$myfield]['foreign_table'] . '.'
243 . $existrel[$myfield]['foreign_field'];
245 $foreign_field = FALSE;
248 while (list($key, $value) = each($selectboxall)) {
250 . '<option value="' . htmlspecialchars($key) . '"';
251 if ($foreign_field && $key == $foreign_field) {
252 echo ' selected="selected"';
255 echo '>' . $value . '</option>'. "\n";
258 // if the link defined in relationtable points to a foreign field
259 // that is not a key in the foreign table, we show the link
260 // (will not be shown with an arrow)
261 if ($foreign_field && !$seen_key) {
263 . '<option value="' . htmlspecialchars($foreign_field) . '"';
264 echo ' selected="selected"';
265 echo '>' . $foreign_field . '</option>'. "\n";
277 <td colspan
="2" align
="center">
278 <input type
="submit" value
="<?php echo $strGo; ?>" />
285 if ($cfgRelation['displaywork']) {
286 // Get "display_filed" infos
287 $disp = PMA_getDisplayField($db, $table);
291 <form method
="post" action
="tbl_relation.php3">
292 <input type
="hidden" name
="lang" value
="<?php echo $lang; ?>" />
293 <input type
="hidden" name
="server" value
="<?php echo $server; ?>" />
294 <input type
="hidden" name
="db" value
="<?php echo htmlspecialchars($db); ?>" />
295 <input type
="hidden" name
="table" value
="<?php echo htmlspecialchars($table); ?>" />
296 <input type
="hidden" name
="submit_show" value
="true" />
298 <p
><?php
echo $strChangeDisplay; ?
></p
>
299 <select name
="display_field" onchange
="this.form.submit();">
302 mysql_data_seek($col_rs, 0);
303 while ($row = @PMA_mysql_fetch_array
($col_rs)) {
304 echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
305 if (isset($disp) && $row['Field'] == $disp) {
306 echo ' selected="selected"';
308 echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
312 <script type
="text/javascript" language
="javascript">
314 // Fake js to allow the use of the <noscript> tag
318 <input type
="submit" value
="<?php echo $strGo; ?>" />
322 } // end if (displayworks)
324 if ($cfgRelation['commwork']) {
328 <form method
="post" action
="tbl_relation.php3">
329 <input type
="hidden" name
="lang" value
="<?php echo $lang; ?>" />
330 <input type
="hidden" name
="server" value
="<?php echo $server; ?>" />
331 <input type
="hidden" name
="db" value
="<?php echo htmlspecialchars($db); ?>" />
332 <input type
="hidden" name
="table" value
="<?php echo htmlspecialchars($table); ?>" />
333 <input type
="hidden" name
="submit_comm" value
="true" />
337 <th colspan
="2" align
="center"><b
><?php
echo $strComments; ?
></b
></th
>
340 for ($i = 0; $i < $saved_row_cnt; $i++
) {
341 $field = $save_row[$i]['Field'];
345 <th
><?php
echo $field; ?
></th
>
347 <input type
="text" name
="comment[<?php echo $field; ?>]" value
="<?php echo (isset($comments[$field]) ? htmlspecialchars($comments[$field]) : ''); ?>" />
356 <td colspan
="2" align
="center">
357 <input type
="submit" value
="<?php echo $strGo; ?>" />
363 } // end if (comments work)
364 } // end if (we have columns in this table)
368 * Displays the footer
371 require('./footer.inc.php3');