RelationStats.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Contains abstract class to hold relation preferences/statistics
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. namespace PhpMyAdmin\Plugins\Schema;
  9. /**
  10. * Relations preferences/statistics
  11. *
  12. * This class fetches the table master and foreign fields positions
  13. * and helps in generating the Table references and then connects
  14. * master table's master field to foreign table's foreign key.
  15. *
  16. * @package PhpMyAdmin
  17. * @abstract
  18. */
  19. abstract class RelationStats
  20. {
  21. protected $diagram;
  22. /**
  23. * Defines properties
  24. */
  25. public $xSrc, $ySrc;
  26. public $srcDir;
  27. public $destDir;
  28. public $xDest, $yDest;
  29. public $wTick;
  30. /**
  31. * The constructor
  32. *
  33. * @param object $diagram The diagram
  34. * @param string $master_table The master table name
  35. * @param string $master_field The relation field in the master table
  36. * @param string $foreign_table The foreign table name
  37. * @param string $foreign_field The relation field in the foreign table
  38. */
  39. public function __construct(
  40. $diagram,
  41. $master_table,
  42. $master_field,
  43. $foreign_table,
  44. $foreign_field
  45. ) {
  46. $this->diagram = $diagram;
  47. $src_pos = $this->_getXy($master_table, $master_field);
  48. $dest_pos = $this->_getXy($foreign_table, $foreign_field);
  49. /*
  50. * [0] is x-left
  51. * [1] is x-right
  52. * [2] is y
  53. */
  54. $src_left = $src_pos[0] - $this->wTick;
  55. $src_right = $src_pos[1] + $this->wTick;
  56. $dest_left = $dest_pos[0] - $this->wTick;
  57. $dest_right = $dest_pos[1] + $this->wTick;
  58. $d1 = abs($src_left - $dest_left);
  59. $d2 = abs($src_right - $dest_left);
  60. $d3 = abs($src_left - $dest_right);
  61. $d4 = abs($src_right - $dest_right);
  62. $d = min($d1, $d2, $d3, $d4);
  63. if ($d == $d1) {
  64. $this->xSrc = $src_pos[0];
  65. $this->srcDir = -1;
  66. $this->xDest = $dest_pos[0];
  67. $this->destDir = -1;
  68. } elseif ($d == $d2) {
  69. $this->xSrc = $src_pos[1];
  70. $this->srcDir = 1;
  71. $this->xDest = $dest_pos[0];
  72. $this->destDir = -1;
  73. } elseif ($d == $d3) {
  74. $this->xSrc = $src_pos[0];
  75. $this->srcDir = -1;
  76. $this->xDest = $dest_pos[1];
  77. $this->destDir = 1;
  78. } else {
  79. $this->xSrc = $src_pos[1];
  80. $this->srcDir = 1;
  81. $this->xDest = $dest_pos[1];
  82. $this->destDir = 1;
  83. }
  84. $this->ySrc = $src_pos[2];
  85. $this->yDest = $dest_pos[2];
  86. }
  87. /**
  88. * Gets arrows coordinates
  89. *
  90. * @param string $table The current table name
  91. * @param string $column The relation column name
  92. *
  93. * @return array Arrows coordinates
  94. *
  95. * @access private
  96. */
  97. private function _getXy($table, $column)
  98. {
  99. $pos = array_search($column, $table->fields);
  100. // x_left, x_right, y
  101. return array(
  102. $table->x,
  103. $table->x + $table->width,
  104. $table->y + ($pos + 1.5) * $table->heightCell,
  105. );
  106. }
  107. }