RelationStatsDia.php 7.7 KB

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