TableStatsEps.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Contains PhpMyAdmin\Plugins\Schema\Eps\TableStatsEps class
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Schema\Eps;
  7. use PhpMyAdmin\Plugins\Schema\ExportRelationSchema;
  8. use PhpMyAdmin\Plugins\Schema\TableStats;
  9. use function count;
  10. use function max;
  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 EPS.
  17. *
  18. * @see Eps
  19. *
  20. * @name TableStatsEps
  21. */
  22. class TableStatsEps extends TableStats
  23. {
  24. /** @var int */
  25. public $height;
  26. /** @var int */
  27. public $currentCell = 0;
  28. /**
  29. * @see Eps
  30. * @see TableStatsEps::setWidthTable
  31. * @see TableStatsEps::setHeightTable
  32. *
  33. * @param object $diagram The EPS diagram
  34. * @param string $db The database name
  35. * @param string $tableName The table name
  36. * @param string $font The font name
  37. * @param int $fontSize The font size
  38. * @param int $pageNumber Page number
  39. * @param int $same_wide_width The max width among tables
  40. * @param bool $showKeys Whether to display keys or not
  41. * @param bool $tableDimension Whether to display table position or not
  42. * @param bool $offline Whether the coordinates are sent
  43. * from the browser
  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. * @return void
  80. */
  81. protected function showMissingTableError()
  82. {
  83. ExportRelationSchema::dieSchema(
  84. $this->pageNumber,
  85. 'EPS',
  86. sprintf(__('The %s table doesn\'t exist!'), $this->tableName)
  87. );
  88. }
  89. /**
  90. * Sets the width of the table
  91. *
  92. * @see Eps
  93. *
  94. * @param string $font The font name
  95. * @param int $fontSize The font size
  96. *
  97. * @return void
  98. */
  99. private function setWidthTable($font, $fontSize)
  100. {
  101. foreach ($this->fields as $field) {
  102. $this->width = max(
  103. $this->width,
  104. $this->font->getStringWidth($field, $font, (int) $fontSize)
  105. );
  106. }
  107. $this->width += $this->font->getStringWidth(
  108. ' ',
  109. $font,
  110. (int) $fontSize
  111. );
  112. /*
  113. * it is unknown what value must be added, because
  114. * table title is affected by the table width value
  115. */
  116. while ($this->width
  117. < $this->font->getStringWidth(
  118. $this->getTitle(),
  119. $font,
  120. (int) $fontSize
  121. )
  122. ) {
  123. $this->width += 7;
  124. }
  125. }
  126. /**
  127. * Sets the height of the table
  128. *
  129. * @param int $fontSize The font size
  130. *
  131. * @return void
  132. */
  133. private function setHeightTable($fontSize)
  134. {
  135. $this->heightCell = $fontSize + 4;
  136. $this->height = (count($this->fields) + 1) * $this->heightCell;
  137. }
  138. /**
  139. * Draw the table
  140. *
  141. * @see Eps
  142. * @see Eps::line
  143. * @see Eps::rect
  144. *
  145. * @param bool $showColor Whether to display color
  146. *
  147. * @return void
  148. */
  149. public function tableDraw($showColor)
  150. {
  151. $this->diagram->rect(
  152. $this->x,
  153. $this->y + 12,
  154. $this->width,
  155. $this->heightCell,
  156. 1
  157. );
  158. $this->diagram->showXY($this->getTitle(), $this->x + 5, $this->y + 14);
  159. foreach ($this->fields as $field) {
  160. $this->currentCell += $this->heightCell;
  161. $this->diagram->rect(
  162. $this->x,
  163. $this->y + 12 + $this->currentCell,
  164. $this->width,
  165. $this->heightCell,
  166. 1
  167. );
  168. $this->diagram->showXY(
  169. $field,
  170. $this->x + 5,
  171. $this->y + 14 + $this->currentCell
  172. );
  173. }
  174. }
  175. }