tbl_relation.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Display table relations for viewing and editing
  5. *
  6. * includes phpMyAdmin relations and InnoDB relations
  7. *
  8. * @todo fix name handling: currently names with dots (.) are not properly handled
  9. * for internal relations (but foreign keys relations are correct)
  10. * @todo foreign key constraints require both fields being of equal type and size
  11. * @todo check foreign fields to be from same type and size, all other makes no sense
  12. * @todo if above todos are fullfilled we can add all fields meet requirements
  13. * in the select dropdown
  14. * @package PhpMyAdmin
  15. */
  16. /**
  17. * Gets some core libraries
  18. */
  19. require_once 'libraries/common.inc.php';
  20. require_once 'libraries/index.lib.php';
  21. require_once 'libraries/tbl_relation.lib.php';
  22. $response = PMA_Response::getInstance();
  23. // Send table of column names to populate corresponding dropdowns depending
  24. // on the current selection
  25. if (isset($_REQUEST['getDropdownValues'])
  26. && $_REQUEST['getDropdownValues'] === 'true'
  27. ) {
  28. PMA_sendHtmlForTableOrColumnDropdownList();
  29. }
  30. $header = $response->getHeader();
  31. $scripts = $header->getScripts();
  32. $scripts->addFile('tbl_relation.js');
  33. $scripts->addFile('indexes.js');
  34. /**
  35. * Sets globals from $_POST
  36. */
  37. $post_params = array(
  38. 'destination_foreign_db',
  39. 'destination_foreign_table',
  40. 'destination_foreign_column',
  41. 'display_field',
  42. 'fields_name',
  43. 'on_delete',
  44. 'on_update'
  45. );
  46. foreach ($post_params as $one_post_param) {
  47. if (isset($_POST[$one_post_param])) {
  48. $GLOBALS[$one_post_param] = $_POST[$one_post_param];
  49. }
  50. }
  51. /**
  52. * Gets tables informations
  53. */
  54. require_once 'libraries/tbl_info.inc.php';
  55. $options_array = array(
  56. 'CASCADE' => 'CASCADE',
  57. 'SET_NULL' => 'SET NULL',
  58. 'NO_ACTION' => 'NO ACTION',
  59. 'RESTRICT' => 'RESTRICT',
  60. );
  61. /**
  62. * Gets the relation settings
  63. */
  64. $cfgRelation = PMA_getRelationsParam();
  65. /**
  66. * Updates
  67. */
  68. if ($cfgRelation['relwork']) {
  69. $existrel = PMA_getForeigners($db, $table, '', 'internal');
  70. }
  71. if (PMA_Util::isForeignKeySupported($tbl_storage_engine)) {
  72. $existrel_foreign = PMA_getForeigners($db, $table, '', 'foreign');
  73. }
  74. if ($cfgRelation['displaywork']) {
  75. $disp = PMA_getDisplayField($db, $table);
  76. }
  77. // will be used in the logic for internal relations and foreign keys:
  78. $multi_edit_columns_name = isset($_REQUEST['fields_name'])
  79. ? $_REQUEST['fields_name']
  80. : null;
  81. // 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
  82. if (isset($_POST['destination_db']) && $cfgRelation['relwork']) {
  83. PMA_handleUpdatesForInternalRelations(
  84. $_POST['destination_db'], $multi_edit_columns_name,
  85. $_POST['destination_table'],
  86. $_POST['destination_column'], $cfgRelation, $db, $table,
  87. isset($existrel) ? $existrel : null
  88. );
  89. } // end if (updates for internal relations)
  90. $html_output = '';
  91. // u p d a t e s f o r f o r e i g n k e y s
  92. // (for now, one index name only; we keep the definitions if the
  93. // foreign db is not the same)
  94. if (isset($destination_foreign_db)) {
  95. $html_output .= PMA_handleUpdatesForForeignKeys(
  96. $destination_foreign_db,
  97. $multi_edit_columns_name, $destination_foreign_table,
  98. $destination_foreign_column, $options_array, $table,
  99. isset($existrel_foreign) ? $existrel_foreign : null
  100. );
  101. } // end if isset($destination_foreign)
  102. // U p d a t e s f o r d i s p l a y f i e l d
  103. if ($cfgRelation['displaywork'] && isset($display_field)) {
  104. PMA_handleUpdateForDisplayField(
  105. $disp, $display_field, $db, $table, $cfgRelation
  106. );
  107. } // end if
  108. // If we did an update, refresh our data
  109. if (isset($_POST['destination_db']) && $cfgRelation['relwork']) {
  110. $existrel = PMA_getForeigners($db, $table, '', 'internal');
  111. }
  112. if (isset($destination_foreign_db)
  113. && PMA_Util::isForeignKeySupported($tbl_storage_engine)
  114. ) {
  115. $existrel_foreign = PMA_getForeigners($db, $table, '', 'foreign');
  116. }
  117. if ($cfgRelation['displaywork']) {
  118. $disp = PMA_getDisplayField($db, $table);
  119. }
  120. /**
  121. * Dialog
  122. */
  123. // Now find out the columns of our $table
  124. // need to use PMA_DatabaseInterface::QUERY_STORE with $GLOBALS['dbi']->numRows()
  125. // in mysqli
  126. $columns = $GLOBALS['dbi']->getColumns($db, $table);
  127. // common form
  128. $html_output .= PMA_getHtmlForCommonForm(
  129. $db, $table, $columns, $cfgRelation, $tbl_storage_engine, $existrel,
  130. isset($existrel_foreign) ? $existrel_foreign : null, $options_array
  131. );
  132. if (PMA_Util::isForeignKeySupported($tbl_storage_engine)) {
  133. $html_output .= PMA_getHtmlForDisplayIndexes();
  134. }
  135. // Render HTML output
  136. PMA_Response::getInstance()->addHTML($html_output);
  137. ?>