Initial import.
[openemr.git] / interface / main / myadmin / tbl_relation.php
blob87ab383e1762bd0c7a2c9e15d2eaec4a4a019803
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Gets some core libraries
7 */
8 require_once('./libraries/grab_globals.lib.php');
9 require_once('./libraries/common.lib.php');
10 require('./tbl_properties_common.php');
11 $url_query .= '&amp;goto=tbl_properties.php';
13 // Note: in tbl_properties_table_info we get and display the table comment.
14 // For InnoDB, this comment contains the REFER information but any update
15 // has not been done yet (will be done in tbl_relation later).
16 $avoid_show_comment = TRUE;
17 require('./tbl_properties_table_info.php');
18 require_once('./libraries/relation.lib.php');
20 $options_array = array('CASCADE' => 'CASCADE', 'SET_NULL' => 'SET NULL', 'NO_ACTION' => 'NO ACTION', 'RESTRICT' => 'RESTRICT');
22 /**
23 * Generate dropdown choices
25 * @param string Message to display
26 * @param string Name of the <select> field
27 * @param array Choices for dropdown
28 * @return string The existing value (for selected)
30 * @access public
32 function PMA_generate_dropdown($dropdown_question,$radio_name,$choices,$selected_value) {
33 global $font_smallest;
35 echo $dropdown_question . '&nbsp;&nbsp;';
37 echo '<select name="' . $radio_name . '" style="font-size: ' . $font_smallest . '">' . "\n";
38 echo '<option value="nix" style="font-size: ' . $font_smallest . '" >--</option>' . "\n";
40 foreach($choices AS $one_value => $one_label) {
41 echo '<option value="' . $one_value . '"';
42 if ($selected_value == $one_value) {
43 echo ' selected="selected" ';
45 echo ' style="font-size: ' . $font_smallest . '">' . $one_label . '</option>' . "\n";
47 echo '</select>' . "\n";
48 echo "\n";
52 /**
53 * Gets the relation settings
55 $cfgRelation = PMA_getRelationsParam();
58 /**
59 * Updates
62 if ($cfgRelation['relwork']) {
63 $existrel = PMA_getForeigners($db, $table, '', 'internal');
64 if ($tbl_type=='INNODB') {
65 $existrel_innodb = PMA_getForeigners($db, $table, '', 'innodb');
68 if ($cfgRelation['displaywork']) {
69 $disp = PMA_getDisplayField($db, $table);
71 if ($cfgRelation['relwork']
72 && isset($submit_rel) && $submit_rel == 'true') {
74 // u p d a t e s f o r I n t e r n a l r e l a t i o n s
76 foreach($destination AS $master_field => $foreign_string) {
77 if ($foreign_string != 'nix') {
78 list($foreign_db, $foreign_table, $foreign_field) = explode('.', $foreign_string);
79 if (!isset($existrel[$master_field])) {
80 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['relation'])
81 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
82 . ' values('
83 . '\'' . PMA_sqlAddslashes($db) . '\', '
84 . '\'' . PMA_sqlAddslashes($table) . '\', '
85 . '\'' . PMA_sqlAddslashes($master_field) . '\', '
86 . '\'' . PMA_sqlAddslashes($foreign_db) . '\', '
87 . '\'' . PMA_sqlAddslashes($foreign_table) . '\','
88 . '\'' . PMA_sqlAddslashes($foreign_field) . '\')';
89 } else if ($existrel[$master_field]['foreign_db'] . '.' .$existrel[$master_field]['foreign_table'] . '.' . $existrel[$master_field]['foreign_field'] != $foreign_string) {
90 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['relation']) . ' SET'
91 . ' foreign_db = \'' . PMA_sqlAddslashes($foreign_db) . '\', '
92 . ' foreign_table = \'' . PMA_sqlAddslashes($foreign_table) . '\', '
93 . ' foreign_field = \'' . PMA_sqlAddslashes($foreign_field) . '\' '
94 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
95 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
96 . ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
97 } // end if... else....
98 } else if (isset($existrel[$master_field])) {
99 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['relation'])
100 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
101 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
102 . ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
103 } // end if... else....
104 if (isset($upd_query)) {
105 $upd_rs = PMA_query_as_cu($upd_query);
106 unset($upd_query);
108 } // end while
111 // u p d a t e s f o r I n n o D B
112 // ( for now, same db only, and one index name)
113 if (isset($destination_innodb)) {
114 foreach($destination_innodb AS $master_field => $foreign_string) {
115 if ($foreign_string != 'nix') {
116 list($foreign_db, $foreign_table, $foreign_field) = explode('.', $foreign_string);
117 if (!isset($existrel_innodb[$master_field])) {
118 // no key defined for this field
120 // The next few lines are repeated below, so they
121 // could be put in an include file
122 $upd_query = 'ALTER TABLE ' . $table
123 . ' ADD FOREIGN KEY ('
124 . PMA_backquote(PMA_sqlAddslashes($master_field)) . ')'
125 . ' REFERENCES '
126 . PMA_backquote(PMA_sqlAddslashes($foreign_table)) . '('
127 . PMA_backquote(PMA_sqlAddslashes($foreign_field)) . ')';
129 if (${$master_field . '_on_delete'} != 'nix') {
130 $upd_query .= ' ON DELETE ' . $options_array[${$master_field . '_on_delete'}];
132 if (${$master_field . '_on_update'} != 'nix') {
133 $upd_query .= ' ON UPDATE ' . $options_array[${$master_field . '_on_update'}];
136 // end repeated code
138 } else if (($existrel_innodb[$master_field]['foreign_db'] . '.' .$existrel_innodb[$master_field]['foreign_table'] . '.' . $existrel_innodb[$master_field]['foreign_field'] != $foreign_string)
139 || ( ${$master_field . '_on_delete'} != (!empty($existrel_innodb[$master_field]['on_delete']) ? $existrel_innodb[$master_field]['on_delete'] : ''))
140 || ( ${$master_field . '_on_update'} != (!empty($existrel_innodb[$master_field]['on_update']) ? $existrel_innodb[$master_field]['on_update'] : ''))
142 // another foreign key is already defined for this field
143 // or
144 // an option has been changed for ON DELETE or ON UPDATE
146 // remove existing key
147 if (PMA_MYSQL_INT_VERSION >= 40013) {
148 $upd_query = 'ALTER TABLE ' . $table
149 . ' DROP FOREIGN KEY '
150 . PMA_backquote($existrel_innodb[$master_field]['constraint']);
152 // I tried to send both in one query but it failed
153 $upd_rs = PMA_mysql_query($upd_query);
156 // add another
157 $upd_query = 'ALTER TABLE ' . $table
158 . ' ADD FOREIGN KEY ('
159 . PMA_backquote(PMA_sqlAddslashes($master_field)) . ')'
160 . ' REFERENCES '
161 . PMA_backquote(PMA_sqlAddslashes($foreign_table)) . '('
162 . PMA_backquote(PMA_sqlAddslashes($foreign_field)) . ')';
164 if (${$master_field . '_on_delete'} != 'nix') {
165 $upd_query .= ' ON DELETE ' . $options_array[${$master_field . '_on_delete'}];
167 if (${$master_field . '_on_update'} != 'nix') {
168 $upd_query .= ' ON UPDATE ' . $options_array[${$master_field . '_on_update'}];
171 } // end if... else....
172 } else if (isset($existrel_innodb[$master_field])) {
173 if (PMA_MYSQL_INT_VERSION >= 40013) {
174 $upd_query = 'ALTER TABLE ' . $table
175 . ' DROP FOREIGN KEY '
176 . PMA_backquote($existrel_innodb[$master_field]['constraint']);
178 } // end if... else....
180 if (isset($upd_query)) {
181 $upd_rs = PMA_mysql_query($upd_query);
182 if (PMA_mysql_error() && mysql_errno() == 1005) {
183 echo '<p class="warning">' . $strNoIndex . ' (' . $master_field .')</p>' . PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
185 unset($upd_query);
187 } // end while
188 } // end if isset($destination_innodb)
190 } // end if
193 // U p d a t e s f o r d i s p l a y f i e l d
195 if ($cfgRelation['displaywork']
196 && isset($submit_show) && $submit_show == 'true') {
198 if ($disp) {
199 if ($display_field != '') {
200 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['table_info'])
201 . ' SET display_field = \'' . PMA_sqlAddslashes($display_field) . '\''
202 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
203 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
204 } else {
205 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['table_info'])
206 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
207 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
209 } elseif ($display_field != '') {
210 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['table_info'])
211 . '(db_name, table_name, display_field) '
212 . ' VALUES('
213 . '\'' . PMA_sqlAddslashes($db) . '\','
214 . '\'' . PMA_sqlAddslashes($table) . '\','
215 . '\'' . PMA_sqlAddslashes($display_field) . '\')';
218 if (isset($upd_query)) {
219 $upd_rs = PMA_query_as_cu($upd_query);
221 } // end if
223 if ($cfgRelation['commwork']
224 && isset($submit_comm) && $submit_comm == 'true') {
225 foreach($comment AS $key => $value) {
226 // garvin: I exported the snippet here to a function (relation.lib.php) , so it can be used multiple times throughout other pages where you can set comments.
227 PMA_setComment($db, $table, $key, $value);
228 } // end while (transferred data)
229 } // end if (commwork)
231 // If we did an update, refresh our data
232 if ($cfgRelation['relwork']
233 && isset($submit_rel) && $submit_rel == 'true') {
234 $existrel = PMA_getForeigners($db, $table, '', 'internal');
235 if ($tbl_type=='INNODB') {
236 $existrel_innodb = PMA_getForeigners($db, $table, '', 'innodb');
239 if ($cfgRelation['displaywork']) {
240 $disp = PMA_getDisplayField($db, $table);
242 if ($cfgRelation['commwork']) {
243 $comments = PMA_getComments($db, $table);
248 * Dialog
250 if ($cfgRelation['relwork']) {
252 // To choose relations we first need all tables names in current db
253 // and if PMA version permits and the main table is innodb,
254 // we use SHOW TABLE STATUS because we need to find other InnoDB tables
256 if ($tbl_type=='INNODB') {
257 $tab_query = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db);
258 // [0] of the row is the name
259 // [1] is the type
260 } else {
261 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($db);
263 // [0] of the row is the name
265 $tab_rs = PMA_mysql_query($tab_query) or PMA_mysqlDie('', $tab_query, '', $err_url_0);
266 $selectboxall['nix'] = '--';
267 $selectboxall_innodb['nix'] = '--';
269 while ($curr_table = @PMA_mysql_fetch_array($tab_rs)) {
270 if (($curr_table[0] != $table) && ($curr_table[0] != $cfg['Server']['relation'])) {
271 $fi_query = 'SHOW KEYS FROM ' . PMA_backquote($curr_table[0]);
272 $fi_rs = PMA_mysql_query($fi_query) or PMA_mysqlDie('', $fi_query, '', $err_url_0);
273 if ($fi_rs && mysql_num_rows($fi_rs) > 0) {
274 $seen_a_primary=FALSE;
275 while ($curr_field = PMA_mysql_fetch_array($fi_rs)) {
276 if (isset($curr_field['Key_name']) && $curr_field['Key_name'] == 'PRIMARY') {
277 $seen_a_primary=TRUE;
278 $field_full = $db . '.' .$curr_field['Table'] . '.' . $curr_field['Column_name'];
279 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
280 $selectboxall[$field_full] = $field_v;
281 // there could be more than one segment of the primary
282 // so do not break
284 // Please watch here, tbl_type is INNODB but the
285 // resulting value of SHOW KEYS is InnoDB
287 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
288 $selectboxall_innodb[$field_full] = $field_v;
291 } else if (isset($curr_field['Non_unique']) && $curr_field['Non_unique'] == 0 && $seen_a_primary==FALSE) {
292 // if we can't find a primary key we take any unique one
293 // (in fact, we show all segments of unique keys
294 // and all unique keys)
295 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
296 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
297 $selectboxall[$field_full] = $field_v;
298 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
299 $selectboxall_innodb[$field_full] = $field_v;
302 // for InnoDB, any index is allowed
303 } else if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
304 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
305 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
306 $selectboxall_innodb[$field_full] = $field_v;
308 } // end if
309 } // end while over keys
310 } // end if (mysql_num_rows)
312 // Mike Beck - 24.07.02: i've been asked to add all keys of the
313 // current table (see bug report #574851)
315 else if ($curr_table[0] == $table) {
316 $fi_query = 'SHOW KEYS FROM ' . PMA_backquote($curr_table[0]);
317 $fi_rs = PMA_mysql_query($fi_query) or PMA_mysqlDie('', $fi_query, '', $err_url_0);
318 if ($fi_rs && mysql_num_rows($fi_rs) > 0) {
319 while ($curr_field = PMA_mysql_fetch_array($fi_rs)) {
320 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
321 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
322 $selectboxall[$field_full] = $field_v;
323 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
324 $selectboxall_innodb[$field_full] = $field_v;
326 } // end while
327 } // end if (mysql_num_rows)
329 } // end while over tables
331 } // end if
334 // Now find out the columns of our $table
335 $col_query = 'SHOW COLUMNS FROM ' . PMA_backquote($table);
336 $col_rs = PMA_mysql_query($col_query) or PMA_mysqlDie('', $col_query, '', $err_url_0);
338 if ($col_rs && mysql_num_rows($col_rs) > 0) {
339 while ($row = PMA_mysql_fetch_array($col_rs)) {
340 $save_row[] = $row;
342 $saved_row_cnt = count($save_row);
345 <form method="post" action="tbl_relation.php">
346 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
347 <input type="hidden" name="submit_rel" value="true" />
349 <table>
350 <tr>
351 <th colspan="4" align="center"><b><?php echo $strLinksTo; ?></b></th>
352 </tr>
353 <tr>
354 <th></th><th><b><?php echo $strInternalRelations;
355 if ($tbl_type=='INNODB') {
356 echo '&nbsp;(*)';
358 ?></b></th>
359 <?php
360 if ($tbl_type=='INNODB') {
361 echo '<th colspan="2">InnoDB';
362 if (PMA_MYSQL_INT_VERSION < 40013) {
363 echo '&nbsp;(**)';
365 echo '</th>';
368 </tr>
369 <?php
370 for ($i = 0; $i < $saved_row_cnt; $i++) {
371 $myfield = $save_row[$i]['Field'];
372 echo "\n";
374 <tr>
375 <th><?php echo $save_row[$i]['Field']; ?></th>
376 <td>
377 <select name="destination[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
378 <?php
379 echo "\n";
381 // PMA internal relations
382 if (isset($existrel[$myfield])) {
383 $foreign_field = $existrel[$myfield]['foreign_db'] . '.'
384 . $existrel[$myfield]['foreign_table'] . '.'
385 . $existrel[$myfield]['foreign_field'];
386 } else {
387 $foreign_field = FALSE;
389 $seen_key = FALSE;
390 foreach($selectboxall AS $key => $value) {
391 echo ' '
392 . '<option value="' . htmlspecialchars($key) . '"';
393 if ($foreign_field && $key == $foreign_field) {
394 echo ' selected="selected"';
395 $seen_key = TRUE;
397 echo '>' . $value . '</option>'. "\n";
398 } // end while
400 // if the link defined in relationtable points to a foreign field
401 // that is not a key in the foreign table, we show the link
402 // (will not be shown with an arrow)
403 if ($foreign_field && !$seen_key) {
404 echo ' '
405 . '<option value="' . htmlspecialchars($foreign_field) . '"';
406 echo ' selected="selected"';
407 echo '>' . $foreign_field . '</option>'. "\n";
410 </select>
411 </td>
412 <?php
413 if ($tbl_type=='INNODB') {
415 <td>
416 <select name="destination_innodb[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
417 <?php
418 if (isset($existrel_innodb[$myfield])) {
419 $foreign_field = $existrel_innodb[$myfield]['foreign_db'] . '.'
420 . $existrel_innodb[$myfield]['foreign_table'] . '.'
421 . $existrel_innodb[$myfield]['foreign_field'];
422 } else {
423 $foreign_field = FALSE;
425 foreach($selectboxall_innodb AS $key => $value) {
426 echo ' '
427 . '<option value="' . htmlspecialchars($key) . '"';
428 if ($foreign_field && $key == $foreign_field) {
429 echo ' selected="selected"';
431 echo '>' . $value . '</option>'. "\n";
432 } // end while
435 </select>
436 </td>
437 <td>
438 <?php
439 PMA_generate_dropdown('ON DELETE',
440 htmlspecialchars($save_row[$i]['Field']) . '_on_delete',
441 $options_array,
442 (isset($existrel_innodb[$myfield]['on_delete']) ? $existrel_innodb[$myfield]['on_delete']: '') );
444 echo '&nbsp;&nbsp;&nbsp;';
446 PMA_generate_dropdown('ON UPDATE',
447 htmlspecialchars($save_row[$i]['Field']) . '_on_update',
448 $options_array,
449 (isset($existrel_innodb[$myfield]['on_update']) ? $existrel_innodb[$myfield]['on_update']: '') );
453 </td>
454 </tr>
455 <?php
456 } // end for
458 echo "\n";
460 <tr>
461 <td colspan="2" align="center">
462 <input type="submit" value="<?php echo ' ' . $strGo . ' '; ?>" />
463 </td>
464 </tr>
465 </table>
466 <?php
467 if ($tbl_type=='INNODB') {
468 echo $strInternalNotNecessary . '<br />';
469 if (PMA_MYSQL_INT_VERSION < 40013) {
470 echo '** ' . sprintf($strUpgrade, 'MySQL', '4.0.13') . '<br />';
474 </form>
476 <?php
477 if ($cfgRelation['displaywork']) {
478 // Get "display_field" infos
479 $disp = PMA_getDisplayField($db, $table);
481 echo "\n";
483 <form method="post" action="tbl_relation.php">
484 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
485 <input type="hidden" name="submit_show" value="true" />
487 <p><?php echo $strChangeDisplay . ': '; ?>
488 <select name="display_field" onchange="this.form.submit();">
489 <option value="">---</option>
490 <?php
491 echo "\n";
492 mysql_data_seek($col_rs, 0);
493 while ($row = @PMA_mysql_fetch_array($col_rs)) {
494 echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
495 if (isset($disp) && $row['Field'] == $disp) {
496 echo ' selected="selected"';
498 echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
499 } // end while
501 </select>
502 <script type="text/javascript" language="javascript">
503 <!--
504 // Fake js to allow the use of the <noscript> tag
505 //-->
506 </script>
507 <noscript>
508 <input type="submit" value="<?php echo $strGo; ?>" />
509 </noscript>
510 </form>
511 </p>
512 <?php
513 } // end if (displayworks)
515 if ($cfgRelation['commwork']) {
517 echo "\n";
519 <form method="post" action="tbl_relation.php">
520 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
521 <input type="hidden" name="submit_comm" value="true" />
523 <table>
524 <tr>
525 <th colspan="2" align="center"><b><?php echo $strComments; ?></b></th>
526 </tr>
527 <?php
528 for ($i = 0; $i < $saved_row_cnt; $i++) {
529 $field = $save_row[$i]['Field'];
530 echo "\n";
532 <tr>
533 <th><?php echo $field; ?></th>
534 <td>
535 <input type="text" name="comment[<?php echo $field; ?>]" value="<?php echo (isset($comments[$field]) ? htmlspecialchars($comments[$field]) : ''); ?>" />
536 </td>
537 </tr>
538 <?php
539 } // end for
541 echo "\n";
543 <tr>
544 <td colspan="2" align="center">
545 <input type="submit" value="<?php echo $strGo; ?>" />
546 </td>
547 </tr>
548 </table>
549 </form>
550 <?php
551 } // end if (comments work)
552 } // end if (we have columns in this table)
556 * Displays the footer
558 require_once('./footer.inc.php');