RelationStatsPdf.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Contains PhpMyAdmin\Plugins\Schema\Pdf\RelationStatsPdf class
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Schema\Pdf;
  7. use PhpMyAdmin\Plugins\Schema\RelationStats;
  8. use function sqrt;
  9. /**
  10. * Relation 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. * in PDF document.
  16. *
  17. * @see PMA_Schema_PDF::SetDrawColor PMA_Schema_PDF::setLineWidthScale
  18. * Pdf::lineScale
  19. *
  20. * @name Relation_Stats_Pdf
  21. */
  22. class RelationStatsPdf extends RelationStats
  23. {
  24. /**
  25. * @param Pdf $diagram The PDF 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 = 5;
  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 Pdf
  51. *
  52. * @param bool $showColor Whether to use one color per relation or not
  53. * @param int $i The id of the link to draw
  54. *
  55. * @return void
  56. *
  57. * @access public
  58. */
  59. public function relationDraw($showColor, $i)
  60. {
  61. if ($showColor) {
  62. $d = $i % 6;
  63. $j = ($i - $d) / 6;
  64. $j %= 4;
  65. $j++;
  66. $case = [
  67. [
  68. 1,
  69. 0,
  70. 0,
  71. ],
  72. [
  73. 0,
  74. 1,
  75. 0,
  76. ],
  77. [
  78. 0,
  79. 0,
  80. 1,
  81. ],
  82. [
  83. 1,
  84. 1,
  85. 0,
  86. ],
  87. [
  88. 1,
  89. 0,
  90. 1,
  91. ],
  92. [
  93. 0,
  94. 1,
  95. 1,
  96. ],
  97. ];
  98. [$a, $b, $c] = $case[$d];
  99. $e = 1 - ($j - 1) / 6;
  100. $this->diagram->SetDrawColor($a * 255 * $e, $b * 255 * $e, $c * 255 * $e);
  101. } else {
  102. $this->diagram->SetDrawColor(0);
  103. }
  104. $this->diagram->setLineWidthScale(0.2);
  105. $this->diagram->lineScale(
  106. $this->xSrc,
  107. $this->ySrc,
  108. $this->xSrc + $this->srcDir * $this->wTick,
  109. $this->ySrc
  110. );
  111. $this->diagram->lineScale(
  112. $this->xDest + $this->destDir * $this->wTick,
  113. $this->yDest,
  114. $this->xDest,
  115. $this->yDest
  116. );
  117. $this->diagram->setLineWidthScale(0.1);
  118. $this->diagram->lineScale(
  119. $this->xSrc + $this->srcDir * $this->wTick,
  120. $this->ySrc,
  121. $this->xDest + $this->destDir * $this->wTick,
  122. $this->yDest
  123. );
  124. /*
  125. * Draws arrows ->
  126. */
  127. $root2 = 2 * sqrt(2);
  128. $this->diagram->lineScale(
  129. $this->xSrc + $this->srcDir * $this->wTick * 0.75,
  130. $this->ySrc,
  131. $this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick,
  132. $this->ySrc + $this->wTick / $root2
  133. );
  134. $this->diagram->lineScale(
  135. $this->xSrc + $this->srcDir * $this->wTick * 0.75,
  136. $this->ySrc,
  137. $this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick,
  138. $this->ySrc - $this->wTick / $root2
  139. );
  140. $this->diagram->lineScale(
  141. $this->xDest + $this->destDir * $this->wTick / 2,
  142. $this->yDest,
  143. $this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick,
  144. $this->yDest + $this->wTick / $root2
  145. );
  146. $this->diagram->lineScale(
  147. $this->xDest + $this->destDir * $this->wTick / 2,
  148. $this->yDest,
  149. $this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick,
  150. $this->yDest - $this->wTick / $root2
  151. );
  152. $this->diagram->SetDrawColor(0);
  153. }
  154. }