EpsRelationSchema.php 7.8 KB

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