TableStatsSvg.php 5.5 KB

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