RelationStats.php 3.2 KB

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