EpsRelationSchema.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * Classes to create relation schema in EPS format.
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Schema\Eps;
  7. use PhpMyAdmin\Plugins\Schema\Dia\TableStatsDia;
  8. use PhpMyAdmin\Plugins\Schema\ExportRelationSchema;
  9. use PhpMyAdmin\Plugins\Schema\Pdf\TableStatsPdf;
  10. use PhpMyAdmin\Plugins\Schema\Svg\TableStatsSvg;
  11. use function date;
  12. use function in_array;
  13. use function sprintf;
  14. /**
  15. * EPS Relation Schema Class
  16. *
  17. * Purpose of this class is to generate the EPS Document
  18. * which is used for representing the database diagrams.
  19. * This class uses post script commands and with
  20. * the combination of these commands actually helps in preparing EPS Document.
  21. *
  22. * This class inherits ExportRelationSchema class has common functionality added
  23. * to this class
  24. *
  25. * @name EpsRelationSchema
  26. */
  27. class EpsRelationSchema extends ExportRelationSchema
  28. {
  29. /** @var TableStatsDia[]|TableStatsEps[]|TableStatsPdf[]|TableStatsSvg[] */
  30. private $tables = [];
  31. /** @var RelationStatsEps[] Relations */
  32. private $relations = [];
  33. /** @var int */
  34. private $tablewidth;
  35. /**
  36. * Upon instantiation This starts writing the EPS document
  37. * user will be prompted for download as .eps extension
  38. *
  39. * @see PMA_EPS
  40. *
  41. * @param string $db database name
  42. */
  43. public function __construct($db)
  44. {
  45. parent::__construct($db, new Eps());
  46. $this->setShowColor(isset($_REQUEST['eps_show_color']));
  47. $this->setShowKeys(isset($_REQUEST['eps_show_keys']));
  48. $this->setTableDimension(isset($_REQUEST['eps_show_table_dimension']));
  49. $this->setAllTablesSameWidth(isset($_REQUEST['eps_all_tables_same_width']));
  50. $this->setOrientation((string) $_REQUEST['eps_orientation']);
  51. $this->diagram->setTitle(
  52. sprintf(
  53. __('Schema of the %s database - Page %s'),
  54. $this->db,
  55. $this->pageNumber
  56. )
  57. );
  58. $this->diagram->setAuthor('phpMyAdmin ' . PMA_VERSION);
  59. $this->diagram->setDate(date('j F Y, g:i a'));
  60. $this->diagram->setOrientation($this->orientation);
  61. $this->diagram->setFont('Verdana', '10');
  62. $alltables = $this->getTablesFromRequest();
  63. foreach ($alltables as $table) {
  64. if (! isset($this->tables[$table])) {
  65. $this->tables[$table] = new TableStatsEps(
  66. $this->diagram,
  67. $this->db,
  68. $table,
  69. $this->diagram->getFont(),
  70. $this->diagram->getFontSize(),
  71. $this->pageNumber,
  72. $this->tablewidth,
  73. $this->showKeys,
  74. $this->tableDimension,
  75. $this->offline
  76. );
  77. }
  78. if (! $this->sameWide) {
  79. continue;
  80. }
  81. $this->tables[$table]->width = $this->tablewidth;
  82. }
  83. $seen_a_relation = false;
  84. foreach ($alltables as $one_table) {
  85. $exist_rel = $this->relation->getForeigners($this->db, $one_table, '', 'both');
  86. if (! $exist_rel) {
  87. continue;
  88. }
  89. $seen_a_relation = true;
  90. foreach ($exist_rel as $master_field => $rel) {
  91. /* put the foreign table on the schema only if selected
  92. * by the user
  93. * (do not use array_search() because we would have to
  94. * to do a === false and this is not PHP3 compatible)
  95. */
  96. if ($master_field !== 'foreign_keys_data') {
  97. if (in_array($rel['foreign_table'], $alltables)) {
  98. $this->addRelation(
  99. $one_table,
  100. $this->diagram->getFont(),
  101. $this->diagram->getFontSize(),
  102. $master_field,
  103. $rel['foreign_table'],
  104. $rel['foreign_field'],
  105. $this->tableDimension
  106. );
  107. }
  108. continue;
  109. }
  110. foreach ($rel as $one_key) {
  111. if (! in_array($one_key['ref_table_name'], $alltables)) {
  112. continue;
  113. }
  114. foreach ($one_key['index_list'] as $index => $one_field) {
  115. $this->addRelation(
  116. $one_table,
  117. $this->diagram->getFont(),
  118. $this->diagram->getFontSize(),
  119. $one_field,
  120. $one_key['ref_table_name'],
  121. $one_key['ref_index_list'][$index],
  122. $this->tableDimension
  123. );
  124. }
  125. }
  126. }
  127. }
  128. if ($seen_a_relation) {
  129. $this->drawRelations();
  130. }
  131. $this->drawTables();
  132. $this->diagram->endEpsDoc();
  133. }
  134. /**
  135. * Output Eps Document for download
  136. *
  137. * @return void
  138. */
  139. public function showOutput()
  140. {
  141. $this->diagram->showOutput($this->getFileName('.eps'));
  142. }
  143. /**
  144. * Defines relation objects
  145. *
  146. * @see _setMinMax
  147. * @see TableStatsEps::__construct()
  148. * @see PhpMyAdmin\Plugins\Schema\Eps\RelationStatsEps::__construct()
  149. *
  150. * @param string $masterTable The master table name
  151. * @param string $font The font
  152. * @param int $fontSize The font size
  153. * @param string $masterField The relation field in the master table
  154. * @param string $foreignTable The foreign table name
  155. * @param string $foreignField The relation field in the foreign table
  156. * @param bool $tableDimension Whether to display table position or not
  157. *
  158. * @return void
  159. */
  160. private function addRelation(
  161. $masterTable,
  162. $font,
  163. $fontSize,
  164. $masterField,
  165. $foreignTable,
  166. $foreignField,
  167. $tableDimension
  168. ) {
  169. if (! isset($this->tables[$masterTable])) {
  170. $this->tables[$masterTable] = new TableStatsEps(
  171. $this->diagram,
  172. $this->db,
  173. $masterTable,
  174. $font,
  175. $fontSize,
  176. $this->pageNumber,
  177. $this->tablewidth,
  178. false,
  179. $tableDimension
  180. );
  181. }
  182. if (! isset($this->tables[$foreignTable])) {
  183. $this->tables[$foreignTable] = new TableStatsEps(
  184. $this->diagram,
  185. $this->db,
  186. $foreignTable,
  187. $font,
  188. $fontSize,
  189. $this->pageNumber,
  190. $this->tablewidth,
  191. false,
  192. $tableDimension
  193. );
  194. }
  195. $this->relations[] = new RelationStatsEps(
  196. $this->diagram,
  197. $this->tables[$masterTable],
  198. $masterField,
  199. $this->tables[$foreignTable],
  200. $foreignField
  201. );
  202. }
  203. /**
  204. * Draws relation arrows and lines connects master table's master field to
  205. * foreign table's foreign field
  206. *
  207. * @see RelationStatsEps::relationDraw()
  208. *
  209. * @return void
  210. */
  211. private function drawRelations()
  212. {
  213. foreach ($this->relations as $relation) {
  214. $relation->relationDraw();
  215. }
  216. }
  217. /**
  218. * Draws tables
  219. *
  220. * @see TableStatsEps::Table_Stats_tableDraw()
  221. *
  222. * @return void
  223. */
  224. private function drawTables()
  225. {
  226. foreach ($this->tables as $table) {
  227. $table->tableDraw($this->showColor);
  228. }
  229. }
  230. }