TableStatsSvg.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * Contains PhpMyAdmin\Plugins\Schema\Svg\TableStatsSvg class
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Schema\Svg;
  7. use PhpMyAdmin\Plugins\Schema\ExportRelationSchema;
  8. use PhpMyAdmin\Plugins\Schema\TableStats;
  9. use function count;
  10. use function in_array;
  11. use function max;
  12. use function sprintf;
  13. /**
  14. * Table preferences/statistics
  15. *
  16. * This class preserves the table co-ordinates,fields
  17. * and helps in drawing/generating the Tables in SVG XML document.
  18. *
  19. * @see PMA_SVG
  20. *
  21. * @name TableStatsSvg
  22. */
  23. class TableStatsSvg extends TableStats
  24. {
  25. /** @var int */
  26. public $height;
  27. /** @var int */
  28. public $currentCell = 0;
  29. /**
  30. * @see Svg
  31. * @see TableStatsSvg::setWidthTable
  32. * @see TableStatsSvg::setHeightTable
  33. *
  34. * @param object $diagram The current SVG image document
  35. * @param string $db The database name
  36. * @param string $tableName The table name
  37. * @param string $font Font face
  38. * @param int $fontSize The font size
  39. * @param int $pageNumber Page number
  40. * @param int $same_wide_width The max. width among tables
  41. * @param bool $showKeys Whether to display keys or not
  42. * @param bool $tableDimension Whether to display table position or not
  43. * @param bool $offline Whether the coordinates are sent
  44. */
  45. public function __construct(
  46. $diagram,
  47. $db,
  48. $tableName,
  49. $font,
  50. $fontSize,
  51. $pageNumber,
  52. &$same_wide_width,
  53. $showKeys = false,
  54. $tableDimension = false,
  55. $offline = false
  56. ) {
  57. parent::__construct(
  58. $diagram,
  59. $db,
  60. $pageNumber,
  61. $tableName,
  62. $showKeys,
  63. $tableDimension,
  64. $offline
  65. );
  66. // height and width
  67. $this->setHeightTable($fontSize);
  68. // setWidth must me after setHeight, because title
  69. // can include table height which changes table width
  70. $this->setWidthTable($font, $fontSize);
  71. if ($same_wide_width >= $this->width) {
  72. return;
  73. }
  74. $same_wide_width = $this->width;
  75. }
  76. /**
  77. * Displays an error when the table cannot be found.
  78. */
  79. protected function showMissingTableError(): void
  80. {
  81. ExportRelationSchema::dieSchema(
  82. $this->pageNumber,
  83. 'SVG',
  84. sprintf(__('The %s table doesn\'t exist!'), $this->tableName)
  85. );
  86. }
  87. /**
  88. * Sets the width of the table
  89. *
  90. * @see PMA_SVG
  91. *
  92. * @param string $font The font size
  93. * @param int $fontSize The font size
  94. *
  95. * @access private
  96. */
  97. private function setWidthTable($font, $fontSize): void
  98. {
  99. foreach ($this->fields as $field) {
  100. $this->width = max(
  101. $this->width,
  102. $this->font->getStringWidth($field, $font, $fontSize)
  103. );
  104. }
  105. $this->width += $this->font->getStringWidth(' ', $font, $fontSize);
  106. /*
  107. * it is unknown what value must be added, because
  108. * table title is affected by the table width value
  109. */
  110. while ($this->width
  111. < $this->font->getStringWidth($this->getTitle(), $font, $fontSize)
  112. ) {
  113. $this->width += 7;
  114. }
  115. }
  116. /**
  117. * Sets the height of the table
  118. *
  119. * @param int $fontSize font size
  120. */
  121. private function setHeightTable($fontSize): void
  122. {
  123. $this->heightCell = $fontSize + 4;
  124. $this->height = (count($this->fields) + 1) * $this->heightCell;
  125. }
  126. /**
  127. * draw the table
  128. *
  129. * @see Svg::printElement
  130. *
  131. * @param bool $showColor Whether to display color
  132. *
  133. * @access public
  134. */
  135. public function tableDraw($showColor): void
  136. {
  137. $this->diagram->printElement(
  138. 'rect',
  139. $this->x,
  140. $this->y,
  141. $this->width,
  142. $this->heightCell,
  143. null,
  144. 'fill:#007;stroke:black;'
  145. );
  146. $this->diagram->printElement(
  147. 'text',
  148. $this->x + 5,
  149. $this->y + 14,
  150. $this->width,
  151. $this->heightCell,
  152. $this->getTitle(),
  153. 'fill:#fff;'
  154. );
  155. foreach ($this->fields as $field) {
  156. $this->currentCell += $this->heightCell;
  157. $fillColor = 'none';
  158. if ($showColor) {
  159. if (in_array($field, $this->primary)) {
  160. $fillColor = '#aea';
  161. }
  162. if ($field == $this->displayfield) {
  163. $fillColor = 'none';
  164. }
  165. }
  166. $this->diagram->printElement(
  167. 'rect',
  168. $this->x,
  169. $this->y + $this->currentCell,
  170. $this->width,
  171. $this->heightCell,
  172. null,
  173. 'fill:' . $fillColor . ';stroke:black;'
  174. );
  175. $this->diagram->printElement(
  176. 'text',
  177. $this->x + 5,
  178. $this->y + 14 + $this->currentCell,
  179. $this->width,
  180. $this->heightCell,
  181. $field,
  182. 'fill:black;'
  183. );
  184. }
  185. }
  186. }