RelationStats.class.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  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, $ySrc;
  28. public $srcDir ;
  29. public $destDir;
  30. public $xDest, $yDest;
  31. public $wTick;
  32. /**
  33. * The constructor
  34. *
  35. * @param object $diagram The diagram
  36. * @param string $master_table The master table name
  37. * @param string $master_field The relation field in the master table
  38. * @param string $foreign_table The foreign table name
  39. * @param string $foreign_field The relation field in the foreign table
  40. */
  41. public function __construct(
  42. $diagram, $master_table, $master_field, $foreign_table, $foreign_field
  43. ) {
  44. $this->diagram = $diagram;
  45. $src_pos = $this->_getXy($master_table, $master_field);
  46. $dest_pos = $this->_getXy($foreign_table, $foreign_field);
  47. /*
  48. * [0] is x-left
  49. * [1] is x-right
  50. * [2] is y
  51. */
  52. $src_left = $src_pos[0] - $this->wTick;
  53. $src_right = $src_pos[1] + $this->wTick;
  54. $dest_left = $dest_pos[0] - $this->wTick;
  55. $dest_right = $dest_pos[1] + $this->wTick;
  56. $d1 = abs($src_left - $dest_left);
  57. $d2 = abs($src_right - $dest_left);
  58. $d3 = abs($src_left - $dest_right);
  59. $d4 = abs($src_right - $dest_right);
  60. $d = min($d1, $d2, $d3, $d4);
  61. if ($d == $d1) {
  62. $this->xSrc = $src_pos[0];
  63. $this->srcDir = -1;
  64. $this->xDest = $dest_pos[0];
  65. $this->destDir = -1;
  66. } elseif ($d == $d2) {
  67. $this->xSrc = $src_pos[1];
  68. $this->srcDir = 1;
  69. $this->xDest = $dest_pos[0];
  70. $this->destDir = -1;
  71. } elseif ($d == $d3) {
  72. $this->xSrc = $src_pos[0];
  73. $this->srcDir = -1;
  74. $this->xDest = $dest_pos[1];
  75. $this->destDir = 1;
  76. } else {
  77. $this->xSrc = $src_pos[1];
  78. $this->srcDir = 1;
  79. $this->xDest = $dest_pos[1];
  80. $this->destDir = 1;
  81. }
  82. $this->ySrc = $src_pos[2];
  83. $this->yDest = $dest_pos[2];
  84. }
  85. /**
  86. * Gets arrows coordinates
  87. *
  88. * @param string $table The current table name
  89. * @param string $column The relation column name
  90. *
  91. * @return array Arrows coordinates
  92. *
  93. * @access private
  94. */
  95. private function _getXy($table, $column)
  96. {
  97. $pos = array_search($column, $table->fields);
  98. // x_left, x_right, y
  99. return array(
  100. $table->x,
  101. $table->x + $table->width,
  102. $table->y + ($pos + 1.5) * $table->heightCell
  103. );
  104. }
  105. }