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');
17 * Gets the relation settings
19 $cfgRelation = PMA_getRelationsParam();
23 * Adds/removes slashes if required
25 * @param string the string to slash
27 * @return string the slashed string
31 function PMA_handleSlashes($val) {
32 return (get_magic_quotes_gpc() ?
str_replace('\\"', '"', $val) : PMA_sqlAddslashes($val));
33 } // end of the "PMA_handleSlashes()" function
40 if ($cfgRelation['relwork']) {
41 $existrel = PMA_getForeigners($db, $table);
43 if ($cfgRelation['displaywork']) {
44 $disp = PMA_getDisplayField($db, $table);
46 if ($cfgRelation['relwork']
47 && isset($submit_rel) && $submit_rel == 'true') {
49 while (list($key, $value) = each($destination)) {
50 if ($value != 'nix') {
51 $for = explode('.', $value);
52 if (!isset($existrel[$key])) {
53 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['relation'])
54 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
56 . '\'' . PMA_sqlAddslashes($db) . '\', '
57 . '\'' . PMA_sqlAddslashes($table) . '\', '
58 . '\'' . PMA_handleSlashes($key) . '\', '
59 . '\'' . PMA_handleSlashes($for[0]) . '\', '
60 . '\'' . PMA_handleSlashes($for[1]) . '\','
61 . '\'' . PMA_handleSlashes($for[2]) . '\')';
62 } else if ($existrel[$key] != $value) {
63 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['relation']) . ' SET'
64 . ' foreign_db = \'' . PMA_handleSlashes($for[0]) . '\', '
65 . ' foreign_table = \'' . PMA_handleSlashes($for[1]) . '\', '
66 . ' foreign_field = \'' . PMA_handleSlashes($for[2]) . '\' '
67 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
68 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
69 . ' AND master_field = \'' . PMA_handleSlashes($key) . '\'';
70 } // end if... else....
71 } else if (isset($existrel[$key])) {
72 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['relation'])
73 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
74 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
75 . ' AND master_field = \'' . PMA_handleSlashes($key) . '\'';
76 } // end if... else....
77 if (isset($upd_query)) {
78 $upd_rs = PMA_query_as_cu($upd_query);
84 if ($cfgRelation['displaywork']
85 && isset($submit_show) && $submit_show == 'true') {
88 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['table_info'])
89 . ' SET display_field = \'' . PMA_handleSlashes($display_field) . '\''
90 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
91 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
93 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['table_info'])
94 . '(db_name, table_name, display_field) '
96 . '\'' . PMA_sqlAddslashes($db) . '\','
97 . '\'' . PMA_sqlAddslashes($table) . '\','
98 . '\'' . PMA_handleSlashes($display_field) . '\')';
100 if (isset($upd_query)) {
101 $upd_rs = PMA_query_as_cu($upd_query);
105 if ($cfgRelation['commwork']
106 && isset($submit_comm) && $submit_comm == 'true') {
107 while (list($key, $value) = each($comment)) {
108 $test_qry = 'SELECT ' . PMA_backquote('comment') . ' FROM ' . PMA_backquote($cfgRelation['column_comments'])
109 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
110 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
111 . ' AND column_name = \'' . PMA_handleSlashes($key) . '\'';
112 $test_rs = PMA_query_as_cu($test_qry);
113 if ($test_rs && mysql_num_rows($test_rs) > 0) {
114 if (strlen($value) > 0) {
115 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['column_comments'])
116 . ' SET ' . PMA_backquote('comment') . ' = \'' . PMA_handleSlashes($value) . '\''
117 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
118 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
119 . ' AND column_name = \'' . PMA_handleSlashes($key) . '\'';
121 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['column_comments'])
122 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
123 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
124 . ' AND column_name = \'' . PMA_handleSlashes($key) . '\'';
126 } else if (strlen($value) > 0) {
127 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['column_comments'])
128 . ' (db_name, table_name, column_name, ' . PMA_backquote('comment') . ') '
130 . '\'' . PMA_sqlAddslashes($db) . '\','
131 . '\'' . PMA_sqlAddslashes($table) . '\','
132 . '\'' . PMA_handleSlashes($key) . '\','
133 . '\'' . PMA_handleSlashes($value) . '\')';
135 if (isset($upd_query)){
136 $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 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
218 <input type
="hidden" name
="submit_rel" value
="true" />
222 <th colspan
="2" align
="center"><b
><?php
echo $strLinksTo; ?
></b
></th
>
225 for ($i = 0; $i < $saved_row_cnt; $i++
) {
229 <th
><?php
echo $save_row[$i]['Field']; ?
></th
>
231 <input type
="hidden" name
="src_field" value
="<?php echo $save_row[$i]['Field']; ?>" />
232 <select name
="destination[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
235 reset($selectboxall);
236 $myfield = $save_row[$i]['Field'];
237 if (isset($existrel[$myfield])) {
238 $foreign_field = $existrel[$myfield]['foreign_db'] . '.'
239 . $existrel[$myfield]['foreign_table'] . '.'
240 . $existrel[$myfield]['foreign_field'];
242 $foreign_field = FALSE;
245 while (list($key, $value) = each($selectboxall)) {
247 . '<option value="' . htmlspecialchars($key) . '"';
248 if ($foreign_field && $key == $foreign_field) {
249 echo ' selected="selected"';
252 echo '>' . $value . '</option>'. "\n";
255 // if the link defined in relationtable points to a foreign field
256 // that is not a key in the foreign table, we show the link
257 // (will not be shown with an arrow)
258 if ($foreign_field && !$seen_key) {
260 . '<option value="' . htmlspecialchars($foreign_field) . '"';
261 echo ' selected="selected"';
262 echo '>' . $foreign_field . '</option>'. "\n";
274 <td colspan
="2" align
="center">
275 <input type
="submit" value
="<?php echo $strGo; ?>" />
282 if ($cfgRelation['displaywork']) {
283 // Get "display_filed" infos
284 $disp = PMA_getDisplayField($db, $table);
288 <form method
="post" action
="tbl_relation.php3">
289 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
290 <input type
="hidden" name
="submit_show" value
="true" />
292 <p
><?php
echo $strChangeDisplay; ?
></p
>
293 <select name
="display_field" onchange
="this.form.submit();">
296 mysql_data_seek($col_rs, 0);
297 while ($row = @PMA_mysql_fetch_array
($col_rs)) {
298 echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
299 if (isset($disp) && $row['Field'] == $disp) {
300 echo ' selected="selected"';
302 echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
306 <script type
="text/javascript" language
="javascript">
308 // Fake js to allow the use of the <noscript> tag
312 <input type
="submit" value
="<?php echo $strGo; ?>" />
316 } // end if (displayworks)
318 if ($cfgRelation['commwork']) {
322 <form method
="post" action
="tbl_relation.php3">
323 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
324 <input type
="hidden" name
="submit_comm" value
="true" />
328 <th colspan
="2" align
="center"><b
><?php
echo $strComments; ?
></b
></th
>
331 for ($i = 0; $i < $saved_row_cnt; $i++
) {
332 $field = $save_row[$i]['Field'];
336 <th
><?php
echo $field; ?
></th
>
338 <input type
="text" name
="comment[<?php echo $field; ?>]" value
="<?php echo (isset($comments[$field]) ? htmlspecialchars($comments[$field]) : ''); ?>" />
347 <td colspan
="2" align
="center">
348 <input type
="submit" value
="<?php echo $strGo; ?>" />
354 } // end if (comments work)
355 } // end if (we have columns in this table)
359 * Displays the footer
362 require('./footer.inc.php3');