RelationStatsDia.class.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Contains Relation_Stats_Dia class
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * Relation preferences/statistics
  13. *
  14. * This class fetches the table master and foreign fields positions
  15. * and helps in generating the Table references and then connects
  16. * master table's master field to foreign table's foreign key
  17. * in dia XML document.
  18. *
  19. * @package PhpMyAdmin
  20. * @name Relation_Stats_Dia
  21. * @see PMA_DIA
  22. */
  23. class Relation_Stats_Dia
  24. {
  25. protected $diagram;
  26. /**
  27. * Defines properties
  28. */
  29. public $srcConnPointsRight;
  30. public $srcConnPointsLeft;
  31. public $destConnPointsRight;
  32. public $destConnPointsLeft;
  33. public $masterTableId;
  34. public $foreignTableId;
  35. public $masterTablePos;
  36. public $foreignTablePos;
  37. public $referenceColor;
  38. /**
  39. * The "Relation_Stats_Dia" constructor
  40. *
  41. * @param object $diagram The DIA diagram
  42. * @param string $master_table The master table name
  43. * @param string $master_field The relation field in the master table
  44. * @param string $foreign_table The foreign table name
  45. * @param string $foreign_field The relation field in the foreign table
  46. *
  47. * @see Relation_Stats_Dia::_getXy
  48. */
  49. public function __construct(
  50. $diagram, $master_table, $master_field, $foreign_table, $foreign_field
  51. ) {
  52. $this->diagram = $diagram;
  53. $src_pos = $this->_getXy($master_table, $master_field);
  54. $dest_pos = $this->_getXy($foreign_table, $foreign_field);
  55. $this->srcConnPointsLeft = $src_pos[0];
  56. $this->srcConnPointsRight = $src_pos[1];
  57. $this->destConnPointsLeft = $dest_pos[0];
  58. $this->destConnPointsRight = $dest_pos[1];
  59. $this->masterTablePos = $src_pos[2];
  60. $this->foreignTablePos = $dest_pos[2];
  61. $this->masterTableId = $master_table->tableId;
  62. $this->foreignTableId = $foreign_table->tableId;
  63. }
  64. /**
  65. * Each Table object have connection points
  66. * which is used to connect to other objects in Dia
  67. * we detect the position of key in fields and
  68. * then determines its left and right connection
  69. * points.
  70. *
  71. * @param string $table The current table name
  72. * @param string $column The relation column name
  73. *
  74. * @return array Table right,left connection points and key position
  75. *
  76. * @access private
  77. */
  78. private function _getXy($table, $column)
  79. {
  80. $pos = array_search($column, $table->fields);
  81. // left, right, position
  82. $value = 12;
  83. if ($pos != 0) {
  84. return array($pos + $value + $pos, $pos + $value + $pos + 1, $pos);
  85. }
  86. return array($pos + $value , $pos + $value + 1, $pos);
  87. }
  88. /**
  89. * Draws relation references
  90. *
  91. * connects master table's master field to foreign table's
  92. * foreign field using Dia object type Database - Reference
  93. * Dia object is used to generate the XML of Dia Document.
  94. * Database reference Object and their attributes are involved
  95. * in the combination of displaying Database - reference on Dia Document.
  96. *
  97. * @param boolean $showColor Whether to use one color per relation or not
  98. * if showColor is true then an array of $listOfColors will be used to choose
  99. * the random colors for references lines. we can change/add more colors to this
  100. *
  101. * @return boolean|void
  102. *
  103. * @access public
  104. * @see PMA_PDF
  105. */
  106. public function relationDraw($showColor)
  107. {
  108. PMA_Dia_Relation_Schema::$objectId += 1;
  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. . PMA_Dia_Relation_Schema::$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. }