TableStatsDia.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * Contains PhpMyAdmin\Plugins\Schema\Dia\TableStatsDia class
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Schema\Dia;
  7. use PhpMyAdmin\Plugins\Schema\ExportRelationSchema;
  8. use PhpMyAdmin\Plugins\Schema\TableStats;
  9. use function in_array;
  10. use function shuffle;
  11. use function sprintf;
  12. /**
  13. * Table preferences/statistics
  14. *
  15. * This class preserves the table co-ordinates,fields
  16. * and helps in drawing/generating the Tables in dia XML document.
  17. *
  18. * @see PMA_DIA
  19. *
  20. * @name Table_Stats_Dia
  21. */
  22. class TableStatsDia extends TableStats
  23. {
  24. /** @var int */
  25. public $tableId;
  26. /** @var string */
  27. public $tableColor;
  28. /**
  29. * @param Dia $diagram The current dia document
  30. * @param string $db The database name
  31. * @param string $tableName The table name
  32. * @param int $pageNumber The current page number (from the
  33. * $cfg['Servers'][$i]['table_coords'] table)
  34. * @param bool $showKeys Whether to display ONLY keys or not
  35. * @param bool $offline Whether the coordinates are sent from the browser
  36. */
  37. public function __construct(
  38. $diagram,
  39. $db,
  40. $tableName,
  41. $pageNumber,
  42. $showKeys = false,
  43. $offline = false
  44. ) {
  45. parent::__construct(
  46. $diagram,
  47. $db,
  48. $pageNumber,
  49. $tableName,
  50. $showKeys,
  51. false,
  52. $offline
  53. );
  54. /**
  55. * Every object in Dia document needs an ID to identify
  56. * so, we used a static variable to keep the things unique
  57. */
  58. $this->tableId = ++DiaRelationSchema::$objectId;
  59. }
  60. /**
  61. * Displays an error when the table cannot be found.
  62. *
  63. * @return void
  64. */
  65. protected function showMissingTableError()
  66. {
  67. ExportRelationSchema::dieSchema(
  68. $this->pageNumber,
  69. 'DIA',
  70. sprintf(__('The %s table doesn\'t exist!'), $this->tableName)
  71. );
  72. }
  73. /**
  74. * Do draw the table
  75. *
  76. * Tables are generated using object type Database - Table
  77. * primary fields are underlined in tables. Dia object
  78. * is used to generate the XML of Dia Document. Database Table
  79. * Object and their attributes are involved in the combination
  80. * of displaying Database - Table on Dia Document.
  81. *
  82. * @see Dia
  83. *
  84. * @param bool $showColor Whether to show color for tables text or not
  85. * if showColor is true then an array of $listOfColors
  86. * will be used to choose the random colors for tables
  87. * text we can change/add more colors to this array
  88. *
  89. * @return void
  90. *
  91. * @access public
  92. */
  93. public function tableDraw($showColor)
  94. {
  95. if ($showColor) {
  96. $listOfColors = [
  97. 'FF0000',
  98. '000099',
  99. '00FF00',
  100. ];
  101. shuffle($listOfColors);
  102. $this->tableColor = '#' . $listOfColors[0] . '';
  103. } else {
  104. $this->tableColor = '#000000';
  105. }
  106. $factor = 0.1;
  107. $this->diagram->startElement('dia:object');
  108. $this->diagram->writeAttribute('type', 'Database - Table');
  109. $this->diagram->writeAttribute('version', '0');
  110. $this->diagram->writeAttribute('id', '' . $this->tableId . '');
  111. $this->diagram->writeRaw(
  112. '<dia:attribute name="obj_pos">
  113. <dia:point val="'
  114. . ($this->x * $factor) . ',' . ($this->y * $factor) . '"/>
  115. </dia:attribute>
  116. <dia:attribute name="obj_bb">
  117. <dia:rectangle val="'
  118. . ($this->x * $factor) . ',' . ($this->y * $factor) . ';9.97,9.2"/>
  119. </dia:attribute>
  120. <dia:attribute name="meta">
  121. <dia:composite type="dict"/>
  122. </dia:attribute>
  123. <dia:attribute name="elem_corner">
  124. <dia:point val="'
  125. . ($this->x * $factor) . ',' . ($this->y * $factor) . '"/>
  126. </dia:attribute>
  127. <dia:attribute name="elem_width">
  128. <dia:real val="5.9199999999999999"/>
  129. </dia:attribute>
  130. <dia:attribute name="elem_height">
  131. <dia:real val="3.5"/>
  132. </dia:attribute>
  133. <dia:attribute name="text_colour">
  134. <dia:color val="' . $this->tableColor . '"/>
  135. </dia:attribute>
  136. <dia:attribute name="line_colour">
  137. <dia:color val="#000000"/>
  138. </dia:attribute>
  139. <dia:attribute name="fill_colour">
  140. <dia:color val="#ffffff"/>
  141. </dia:attribute>
  142. <dia:attribute name="line_width">
  143. <dia:real val="0.10000000000000001"/>
  144. </dia:attribute>
  145. <dia:attribute name="name">
  146. <dia:string>#' . $this->tableName . '#</dia:string>
  147. </dia:attribute>
  148. <dia:attribute name="comment">
  149. <dia:string>##</dia:string>
  150. </dia:attribute>
  151. <dia:attribute name="visible_comment">
  152. <dia:boolean val="false"/>
  153. </dia:attribute>
  154. <dia:attribute name="tagging_comment">
  155. <dia:boolean val="false"/>
  156. </dia:attribute>
  157. <dia:attribute name="underline_primary_key">
  158. <dia:boolean val="true"/>
  159. </dia:attribute>
  160. <dia:attribute name="bold_primary_keys">
  161. <dia:boolean val="true"/>
  162. </dia:attribute>
  163. <dia:attribute name="normal_font">
  164. <dia:font family="monospace" style="0" name="Courier"/>
  165. </dia:attribute>
  166. <dia:attribute name="name_font">
  167. <dia:font family="sans" style="80" name="Helvetica-Bold"/>
  168. </dia:attribute>
  169. <dia:attribute name="comment_font">
  170. <dia:font family="sans" style="0" name="Helvetica"/>
  171. </dia:attribute>
  172. <dia:attribute name="normal_font_height">
  173. <dia:real val="0.80000000000000004"/>
  174. </dia:attribute>
  175. <dia:attribute name="name_font_height">
  176. <dia:real val="0.69999999999999996"/>
  177. </dia:attribute>
  178. <dia:attribute name="comment_font_height">
  179. <dia:real val="0.69999999999999996"/>
  180. </dia:attribute>'
  181. );
  182. $this->diagram->startElement('dia:attribute');
  183. $this->diagram->writeAttribute('name', 'attributes');
  184. foreach ($this->fields as $field) {
  185. $this->diagram->writeRaw(
  186. '<dia:composite type="table_attribute">
  187. <dia:attribute name="name">
  188. <dia:string>#' . $field . '#</dia:string>
  189. </dia:attribute>
  190. <dia:attribute name="type">
  191. <dia:string>##</dia:string>
  192. </dia:attribute>
  193. <dia:attribute name="comment">
  194. <dia:string>##</dia:string>
  195. </dia:attribute>'
  196. );
  197. unset($pm);
  198. $pm = 'false';
  199. if (in_array($field, $this->primary)) {
  200. $pm = 'true';
  201. }
  202. if ($field == $this->displayfield) {
  203. $pm = 'false';
  204. }
  205. $this->diagram->writeRaw(
  206. '<dia:attribute name="primary_key">
  207. <dia:boolean val="' . $pm . '"/>
  208. </dia:attribute>
  209. <dia:attribute name="nullable">
  210. <dia:boolean val="false"/>
  211. </dia:attribute>
  212. <dia:attribute name="unique">
  213. <dia:boolean val="' . $pm . '"/>
  214. </dia:attribute>
  215. </dia:composite>'
  216. );
  217. }
  218. $this->diagram->endElement();
  219. $this->diagram->endElement();
  220. }
  221. }