TableStatsPdf.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Contains PhpMyAdmin\Plugins\Schema\Pdf\TableStatsPdf class
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. declare(strict_types=1);
  9. namespace PhpMyAdmin\Plugins\Schema\Pdf;
  10. use PhpMyAdmin\Pdf as PdfLib;
  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 PDF document.
  18. *
  19. * @name Table_Stats_Pdf
  20. * @package PhpMyAdmin
  21. * @see PMA_Schema_PDF
  22. */
  23. class TableStatsPdf extends TableStats
  24. {
  25. /**
  26. * Defines properties
  27. */
  28. public $nb_fiels;
  29. public $height;
  30. private $_ff = PdfLib::PMA_PDF_FONT;
  31. /**
  32. * The "PhpMyAdmin\Plugins\Schema\Pdf\TableStatsPdf" constructor
  33. *
  34. * @param object $diagram The PDF diagram
  35. * @param string $db The database name
  36. * @param string $tableName The table name
  37. * @param integer $fontSize The font size
  38. * @param integer $pageNumber The current page number (from the
  39. * $cfg['Servers'][$i]['table_coords'] table)
  40. * @param integer $sameWideWidth The max. width among tables
  41. * @param boolean $showKeys Whether to display keys or not
  42. * @param boolean $tableDimension Whether to display table position or not
  43. * @param boolean $offline Whether the coordinates are sent
  44. * from the browser
  45. *
  46. * @see PMA_Schema_PDF, Table_Stats_Pdf::Table_Stats_setWidth,
  47. * PhpMyAdmin\Plugins\Schema\Pdf\TableStatsPdf::Table_Stats_setHeight
  48. */
  49. public function __construct(
  50. $diagram,
  51. $db,
  52. $tableName,
  53. $fontSize,
  54. $pageNumber,
  55. &$sameWideWidth,
  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. $this->heightCell = 6;
  70. $this->_setHeight();
  71. /*
  72. * setWidth must me after setHeight, because title
  73. * can include table height which changes table width
  74. */
  75. $this->_setWidth($fontSize);
  76. if ($sameWideWidth < $this->width) {
  77. $sameWideWidth = $this->width;
  78. }
  79. }
  80. /**
  81. * Displays an error when the table cannot be found.
  82. *
  83. * @return void
  84. */
  85. protected function showMissingTableError()
  86. {
  87. ExportRelationSchema::dieSchema(
  88. $this->pageNumber,
  89. "PDF",
  90. sprintf(__('The %s table doesn\'t exist!'), $this->tableName)
  91. );
  92. }
  93. /**
  94. * Returns title of the current table,
  95. * title can have the dimensions of the table
  96. *
  97. * @return string
  98. */
  99. protected function getTitle()
  100. {
  101. $ret = '';
  102. if ($this->tableDimension) {
  103. $ret = sprintf('%.0fx%0.f', $this->width, $this->height);
  104. }
  105. return $ret . ' ' . $this->tableName;
  106. }
  107. /**
  108. * Sets the width of the table
  109. *
  110. * @param integer $fontSize The font size
  111. *
  112. * @access private
  113. *
  114. * @return void
  115. *
  116. * @see PMA_Schema_PDF
  117. */
  118. private function _setWidth($fontSize)
  119. {
  120. foreach ($this->fields as $field) {
  121. $this->width = max($this->width, $this->diagram->GetStringWidth($field));
  122. }
  123. $this->width += $this->diagram->GetStringWidth(' ');
  124. $this->diagram->SetFont($this->_ff, 'B', $fontSize);
  125. /*
  126. * it is unknown what value must be added, because
  127. * table title is affected by the table width value
  128. */
  129. while ($this->width < $this->diagram->GetStringWidth($this->getTitle())) {
  130. $this->width += 5;
  131. }
  132. $this->diagram->SetFont($this->_ff, '', $fontSize);
  133. }
  134. /**
  135. * Sets the height of the table
  136. *
  137. * @return void
  138. *
  139. * @access private
  140. */
  141. private function _setHeight()
  142. {
  143. $this->height = (count($this->fields) + 1) * $this->heightCell;
  144. }
  145. /**
  146. * Do draw the table
  147. *
  148. * @param integer $fontSize The font size
  149. * @param boolean $withDoc Whether to include links to documentation
  150. * @param boolean|integer $setColor Whether to display color
  151. *
  152. * @access public
  153. *
  154. * @return void
  155. *
  156. * @see PMA_Schema_PDF
  157. */
  158. public function tableDraw($fontSize, $withDoc, $setColor = 0)
  159. {
  160. $this->diagram->setXyScale($this->x, $this->y);
  161. $this->diagram->SetFont($this->_ff, 'B', $fontSize);
  162. if ($setColor) {
  163. $this->diagram->SetTextColor(200);
  164. $this->diagram->SetFillColor(0, 0, 128);
  165. }
  166. if ($withDoc) {
  167. $this->diagram->SetLink(
  168. $this->diagram->PMA_links['RT'][$this->tableName]['-'],
  169. -1
  170. );
  171. } else {
  172. $this->diagram->PMA_links['doc'][$this->tableName]['-'] = '';
  173. }
  174. $this->diagram->cellScale(
  175. $this->width,
  176. $this->heightCell,
  177. $this->getTitle(),
  178. 1,
  179. 1,
  180. 'C',
  181. $setColor,
  182. $this->diagram->PMA_links['doc'][$this->tableName]['-']
  183. );
  184. $this->diagram->setXScale($this->x);
  185. $this->diagram->SetFont($this->_ff, '', $fontSize);
  186. $this->diagram->SetTextColor(0);
  187. $this->diagram->SetFillColor(255);
  188. foreach ($this->fields as $field) {
  189. if ($setColor) {
  190. if (in_array($field, $this->primary)) {
  191. $this->diagram->SetFillColor(215, 121, 123);
  192. }
  193. if ($field == $this->displayfield) {
  194. $this->diagram->SetFillColor(142, 159, 224);
  195. }
  196. }
  197. if ($withDoc) {
  198. $this->diagram->SetLink(
  199. $this->diagram->PMA_links['RT'][$this->tableName][$field],
  200. -1
  201. );
  202. } else {
  203. $this->diagram->PMA_links['doc'][$this->tableName][$field] = '';
  204. }
  205. $this->diagram->cellScale(
  206. $this->width,
  207. $this->heightCell,
  208. ' ' . $field,
  209. 1,
  210. 1,
  211. 'L',
  212. $setColor,
  213. $this->diagram->PMA_links['doc'][$this->tableName][$field]
  214. );
  215. $this->diagram->setXScale($this->x);
  216. $this->diagram->SetFillColor(255);
  217. }
  218. }
  219. }