RelationStatsSvg.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Contains PhpMyAdmin\Plugins\Schema\Svg\RelationStatsSvg class
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Schema\Svg;
  7. use PhpMyAdmin\Plugins\Schema\RelationStats;
  8. use function shuffle;
  9. use function sqrt;
  10. /**
  11. * Relation preferences/statistics
  12. *
  13. * This class fetches the table master and foreign fields positions
  14. * and helps in generating the Table references and then connects
  15. * master table's master field to foreign table's foreign key
  16. * in SVG XML document.
  17. *
  18. * @see PMA_SVG::printElementLine
  19. *
  20. * @name Relation_Stats_Svg
  21. */
  22. class RelationStatsSvg extends RelationStats
  23. {
  24. /**
  25. * @param Svg $diagram The SVG diagram
  26. * @param string $master_table The master table name
  27. * @param string $master_field The relation field in the master table
  28. * @param string $foreign_table The foreign table name
  29. * @param string $foreign_field The relation field in the foreign table
  30. */
  31. public function __construct(
  32. $diagram,
  33. $master_table,
  34. $master_field,
  35. $foreign_table,
  36. $foreign_field
  37. ) {
  38. $this->wTick = 10;
  39. parent::__construct(
  40. $diagram,
  41. $master_table,
  42. $master_field,
  43. $foreign_table,
  44. $foreign_field
  45. );
  46. }
  47. /**
  48. * draws relation links and arrows shows foreign key relations
  49. *
  50. * @see PMA_SVG
  51. *
  52. * @param bool $showColor Whether to use one color per relation or not
  53. *
  54. * @return void
  55. *
  56. * @access public
  57. */
  58. public function relationDraw($showColor)
  59. {
  60. if ($showColor) {
  61. $listOfColors = [
  62. '#c00',
  63. '#bbb',
  64. '#333',
  65. '#cb0',
  66. '#0b0',
  67. '#0bf',
  68. '#b0b',
  69. ];
  70. shuffle($listOfColors);
  71. $color = $listOfColors[0];
  72. } else {
  73. $color = '#333';
  74. }
  75. $this->diagram->printElementLine(
  76. 'line',
  77. $this->xSrc,
  78. $this->ySrc,
  79. $this->xSrc + $this->srcDir * $this->wTick,
  80. $this->ySrc,
  81. 'stroke:' . $color . ';stroke-width:1;'
  82. );
  83. $this->diagram->printElementLine(
  84. 'line',
  85. $this->xDest + $this->destDir * $this->wTick,
  86. $this->yDest,
  87. $this->xDest,
  88. $this->yDest,
  89. 'stroke:' . $color . ';stroke-width:1;'
  90. );
  91. $this->diagram->printElementLine(
  92. 'line',
  93. $this->xSrc + $this->srcDir * $this->wTick,
  94. $this->ySrc,
  95. $this->xDest + $this->destDir * $this->wTick,
  96. $this->yDest,
  97. 'stroke:' . $color . ';stroke-width:1;'
  98. );
  99. $root2 = 2 * sqrt(2);
  100. $this->diagram->printElementLine(
  101. 'line',
  102. $this->xSrc + $this->srcDir * $this->wTick * 0.75,
  103. $this->ySrc,
  104. $this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick,
  105. $this->ySrc + $this->wTick / $root2,
  106. 'stroke:' . $color . ';stroke-width:2;'
  107. );
  108. $this->diagram->printElementLine(
  109. 'line',
  110. $this->xSrc + $this->srcDir * $this->wTick * 0.75,
  111. $this->ySrc,
  112. $this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick,
  113. $this->ySrc - $this->wTick / $root2,
  114. 'stroke:' . $color . ';stroke-width:2;'
  115. );
  116. $this->diagram->printElementLine(
  117. 'line',
  118. $this->xDest + $this->destDir * $this->wTick / 2,
  119. $this->yDest,
  120. $this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick,
  121. $this->yDest + $this->wTick / $root2,
  122. 'stroke:' . $color . ';stroke-width:2;'
  123. );
  124. $this->diagram->printElementLine(
  125. 'line',
  126. $this->xDest + $this->destDir * $this->wTick / 2,
  127. $this->yDest,
  128. $this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick,
  129. $this->yDest - $this->wTick / $root2,
  130. 'stroke:' . $color . ';stroke-width:2;'
  131. );
  132. }
  133. }