EpsRelationSchema.php 7.3 KB

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