RelationStatsDia.php 7.5 KB

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