RelationStatsDia.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * Contains PhpMyAdmin\Plugins\Schema\Dia\RelationStatsDia class
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Schema\Dia;
  7. use function array_search;
  8. use function shuffle;
  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 dia XML document.
  16. *
  17. * @see PMA_DIA
  18. *
  19. * @name Relation_Stats_Dia
  20. */
  21. class RelationStatsDia
  22. {
  23. /** @var Dia */
  24. protected $diagram;
  25. /** @var mixed */
  26. public $srcConnPointsRight;
  27. /** @var mixed */
  28. public $srcConnPointsLeft;
  29. /** @var mixed */
  30. public $destConnPointsRight;
  31. /** @var mixed */
  32. public $destConnPointsLeft;
  33. /** @var int */
  34. public $masterTableId;
  35. /** @var int */
  36. public $foreignTableId;
  37. /** @var mixed */
  38. public $masterTablePos;
  39. /** @var mixed */
  40. public $foreignTablePos;
  41. /** @var string */
  42. public $referenceColor;
  43. /**
  44. * @see Relation_Stats_Dia::getXy
  45. *
  46. * @param Dia $diagram The DIA diagram
  47. * @param TableStatsDia $master_table The master table name
  48. * @param string $master_field The relation field in the master table
  49. * @param TableStatsDia $foreign_table The foreign table name
  50. * @param string $foreign_field The relation field in the foreign table
  51. */
  52. public function __construct(
  53. $diagram,
  54. $master_table,
  55. $master_field,
  56. $foreign_table,
  57. $foreign_field
  58. ) {
  59. $this->diagram = $diagram;
  60. $src_pos = $this->getXy($master_table, $master_field);
  61. $dest_pos = $this->getXy($foreign_table, $foreign_field);
  62. $this->srcConnPointsLeft = $src_pos[0];
  63. $this->srcConnPointsRight = $src_pos[1];
  64. $this->destConnPointsLeft = $dest_pos[0];
  65. $this->destConnPointsRight = $dest_pos[1];
  66. $this->masterTablePos = $src_pos[2];
  67. $this->foreignTablePos = $dest_pos[2];
  68. $this->masterTableId = $master_table->tableId;
  69. $this->foreignTableId = $foreign_table->tableId;
  70. }
  71. /**
  72. * Each Table object have connection points
  73. * which is used to connect to other objects in Dia
  74. * we detect the position of key in fields and
  75. * then determines its left and right connection
  76. * points.
  77. *
  78. * @param TableStatsDia $table The current table name
  79. * @param string $column The relation column name
  80. *
  81. * @return array Table right,left connection points and key position
  82. *
  83. * @access private
  84. */
  85. private function getXy($table, $column)
  86. {
  87. $pos = array_search($column, $table->fields);
  88. // left, right, position
  89. $value = 12;
  90. if ($pos != 0) {
  91. return [
  92. $pos + $value + $pos,
  93. $pos + $value + $pos + 1,
  94. $pos,
  95. ];
  96. }
  97. return [
  98. $pos + $value,
  99. $pos + $value + 1,
  100. $pos,
  101. ];
  102. }
  103. /**
  104. * Draws relation references
  105. *
  106. * connects master table's master field to foreign table's
  107. * foreign field using Dia object type Database - Reference
  108. * Dia object is used to generate the XML of Dia Document.
  109. * Database reference Object and their attributes are involved
  110. * in the combination of displaying Database - reference on Dia Document.
  111. *
  112. * @see PDF
  113. *
  114. * @param bool $showColor Whether to use one color per relation or not
  115. * if showColor is true then an array of $listOfColors
  116. * will be used to choose the random colors for
  117. * references lines. we can change/add more colors to
  118. * this
  119. *
  120. * @return bool|void
  121. *
  122. * @access public
  123. */
  124. public function relationDraw($showColor)
  125. {
  126. ++DiaRelationSchema::$objectId;
  127. /*
  128. * if source connection points and destination connection
  129. * points are same then return it false and don't draw that
  130. * relation
  131. */
  132. if ($this->srcConnPointsRight == $this->destConnPointsRight) {
  133. if ($this->srcConnPointsLeft == $this->destConnPointsLeft) {
  134. return false;
  135. }
  136. }
  137. if ($showColor) {
  138. $listOfColors = [
  139. 'FF0000',
  140. '000099',
  141. '00FF00',
  142. ];
  143. shuffle($listOfColors);
  144. $this->referenceColor = '#' . $listOfColors[0] . '';
  145. } else {
  146. $this->referenceColor = '#000000';
  147. }
  148. $this->diagram->writeRaw(
  149. '<dia:object type="Database - Reference" version="0" id="'
  150. . DiaRelationSchema::$objectId . '">
  151. <dia:attribute name="obj_pos">
  152. <dia:point val="3.27,18.9198"/>
  153. </dia:attribute>
  154. <dia:attribute name="obj_bb">
  155. <dia:rectangle val="2.27,8.7175;17.7679,18.9198"/>
  156. </dia:attribute>
  157. <dia:attribute name="meta">
  158. <dia:composite type="dict"/>
  159. </dia:attribute>
  160. <dia:attribute name="orth_points">
  161. <dia:point val="3.27,18.9198"/>
  162. <dia:point val="2.27,18.9198"/>
  163. <dia:point val="2.27,14.1286"/>
  164. <dia:point val="17.7679,14.1286"/>
  165. <dia:point val="17.7679,9.3375"/>
  166. <dia:point val="16.7679,9.3375"/>
  167. </dia:attribute>
  168. <dia:attribute name="orth_orient">
  169. <dia:enum val="0"/>
  170. <dia:enum val="1"/>
  171. <dia:enum val="0"/>
  172. <dia:enum val="1"/>
  173. <dia:enum val="0"/>
  174. </dia:attribute>
  175. <dia:attribute name="orth_autoroute">
  176. <dia:boolean val="true"/>
  177. </dia:attribute>
  178. <dia:attribute name="text_colour">
  179. <dia:color val="#000000"/>
  180. </dia:attribute>
  181. <dia:attribute name="line_colour">
  182. <dia:color val="' . $this->referenceColor . '"/>
  183. </dia:attribute>
  184. <dia:attribute name="line_width">
  185. <dia:real val="0.10000000000000001"/>
  186. </dia:attribute>
  187. <dia:attribute name="line_style">
  188. <dia:enum val="0"/>
  189. <dia:real val="1"/>
  190. </dia:attribute>
  191. <dia:attribute name="corner_radius">
  192. <dia:real val="0"/>
  193. </dia:attribute>
  194. <dia:attribute name="end_arrow">
  195. <dia:enum val="22"/>
  196. </dia:attribute>
  197. <dia:attribute name="end_arrow_length">
  198. <dia:real val="0.5"/>
  199. </dia:attribute>
  200. <dia:attribute name="end_arrow_width">
  201. <dia:real val="0.5"/>
  202. </dia:attribute>
  203. <dia:attribute name="start_point_desc">
  204. <dia:string>#1#</dia:string>
  205. </dia:attribute>
  206. <dia:attribute name="end_point_desc">
  207. <dia:string>#n#</dia:string>
  208. </dia:attribute>
  209. <dia:attribute name="normal_font">
  210. <dia:font family="monospace" style="0" name="Courier"/>
  211. </dia:attribute>
  212. <dia:attribute name="normal_font_height">
  213. <dia:real val="0.59999999999999998"/>
  214. </dia:attribute>
  215. <dia:connections>
  216. <dia:connection handle="0" to="'
  217. . $this->masterTableId . '" connection="'
  218. . $this->srcConnPointsRight . '"/>
  219. <dia:connection handle="1" to="'
  220. . $this->foreignTableId . '" connection="'
  221. . $this->destConnPointsRight . '"/>
  222. </dia:connections>
  223. </dia:object>'
  224. );
  225. }
  226. }