Eps_Relation_Schema.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. require_once 'libraries/plugins/schema/Export_Relation_Schema.class.php';
  12. require_once 'libraries/plugins/schema/eps/RelationStatsEps.class.php';
  13. require_once 'libraries/plugins/schema/eps/TableStatsEps.class.php';
  14. require_once 'libraries/Font.class.php';
  15. /**
  16. * This Class is EPS Library and
  17. * helps in developing structure of EPS Schema Export
  18. *
  19. * @package PhpMyAdmin
  20. * @access public
  21. * @see http://php.net/manual/en/book.xmlwriter.php
  22. */
  23. class PMA_EPS
  24. {
  25. public $font;
  26. public $fontSize;
  27. public $stringCommands;
  28. /**
  29. * The "PMA_EPS" constructor
  30. *
  31. * Upon instantiation This starts writing the EPS Document.
  32. * %!PS-Adobe-3.0 EPSF-3.0 This is the MUST first comment to include
  33. * it shows/tells that the Post Script document is purely under
  34. * Document Structuring Convention [DSC] and is Compliant
  35. * Encapsulated Post Script Document
  36. */
  37. public function __construct()
  38. {
  39. $this->stringCommands = "";
  40. $this->stringCommands .= "%!PS-Adobe-3.0 EPSF-3.0 \n";
  41. }
  42. /**
  43. * Set document title
  44. *
  45. * @param string $value sets the title text
  46. *
  47. * @return void
  48. */
  49. public function setTitle($value)
  50. {
  51. $this->stringCommands .= '%%Title: ' . $value . "\n";
  52. }
  53. /**
  54. * Set document author
  55. *
  56. * @param string $value sets the author
  57. *
  58. * @return void
  59. */
  60. public function setAuthor($value)
  61. {
  62. $this->stringCommands .= '%%Creator: ' . $value . "\n";
  63. }
  64. /**
  65. * Set document creation date
  66. *
  67. * @param string $value sets the date
  68. *
  69. * @return void
  70. */
  71. public function setDate($value)
  72. {
  73. $this->stringCommands .= '%%CreationDate: ' . $value . "\n";
  74. }
  75. /**
  76. * Set document orientation
  77. *
  78. * @param string $orientation sets the orientation
  79. *
  80. * @return void
  81. */
  82. public function setOrientation($orientation)
  83. {
  84. $this->stringCommands .= "%%PageOrder: Ascend \n";
  85. if ($orientation == "L") {
  86. $orientation = "Landscape";
  87. $this->stringCommands .= '%%Orientation: ' . $orientation . "\n";
  88. } else {
  89. $orientation = "Portrait";
  90. $this->stringCommands .= '%%Orientation: ' . $orientation . "\n";
  91. }
  92. $this->stringCommands .= "%%EndComments \n";
  93. $this->stringCommands .= "%%Pages 1 \n";
  94. $this->stringCommands .= "%%BoundingBox: 72 150 144 170 \n";
  95. }
  96. /**
  97. * Set the font and size
  98. *
  99. * font can be set whenever needed in EPS
  100. *
  101. * @param string $value sets the font name e.g Arial
  102. * @param integer $size sets the size of the font e.g 10
  103. *
  104. * @return void
  105. */
  106. public function setFont($value, $size)
  107. {
  108. $this->font = $value;
  109. $this->fontSize = $size;
  110. $this->stringCommands .= "/" . $value . " findfont % Get the basic font\n";
  111. $this->stringCommands .= ""
  112. . $size . " scalefont % Scale the font to $size points\n";
  113. $this->stringCommands
  114. .= "setfont % Make it the current font\n";
  115. }
  116. /**
  117. * Get the font
  118. *
  119. * @return string return the font name e.g Arial
  120. */
  121. public function getFont()
  122. {
  123. return $this->font;
  124. }
  125. /**
  126. * Get the font Size
  127. *
  128. * @return string return the size of the font e.g 10
  129. */
  130. public function getFontSize()
  131. {
  132. return $this->fontSize;
  133. }
  134. /**
  135. * Draw the line
  136. *
  137. * drawing the lines from x,y source to x,y destination and set the
  138. * width of the line. lines helps in showing relationships of tables
  139. *
  140. * @param integer $x_from The x_from attribute defines the start
  141. * left position of the element
  142. * @param integer $y_from The y_from attribute defines the start
  143. * right position of the element
  144. * @param integer $x_to The x_to attribute defines the end
  145. * left position of the element
  146. * @param integer $y_to The y_to attribute defines the end
  147. * right position of the element
  148. * @param integer $lineWidth Sets the width of the line e.g 2
  149. *
  150. * @return void
  151. */
  152. public function line($x_from = 0, $y_from = 0, $x_to = 0, $y_to = 0,
  153. $lineWidth = 0
  154. ) {
  155. $this->stringCommands .= $lineWidth . " setlinewidth \n";
  156. $this->stringCommands .= $x_from . ' ' . $y_from . " moveto \n";
  157. $this->stringCommands .= $x_to . ' ' . $y_to . " lineto \n";
  158. $this->stringCommands .= "stroke \n";
  159. }
  160. /**
  161. * Draw the rectangle
  162. *
  163. * drawing the rectangle from x,y source to x,y destination and set the
  164. * width of the line. rectangles drawn around the text shown of fields
  165. *
  166. * @param integer $x_from The x_from attribute defines the start
  167. * left position of the element
  168. * @param integer $y_from The y_from attribute defines the start
  169. * right position of the element
  170. * @param integer $x_to The x_to attribute defines the end
  171. * left position of the element
  172. * @param integer $y_to The y_to attribute defines the end
  173. * right position of the element
  174. * @param integer $lineWidth Sets the width of the line e.g 2
  175. *
  176. * @return void
  177. */
  178. public function rect($x_from, $y_from, $x_to, $y_to, $lineWidth)
  179. {
  180. $this->stringCommands .= $lineWidth . " setlinewidth \n";
  181. $this->stringCommands .= "newpath \n";
  182. $this->stringCommands .= $x_from . " " . $y_from . " moveto \n";
  183. $this->stringCommands .= "0 " . $y_to . " rlineto \n";
  184. $this->stringCommands .= $x_to . " 0 rlineto \n";
  185. $this->stringCommands .= "0 -" . $y_to . " rlineto \n";
  186. $this->stringCommands .= "closepath \n";
  187. $this->stringCommands .= "stroke \n";
  188. }
  189. /**
  190. * Set the current point
  191. *
  192. * The moveto operator takes two numbers off the stack and treats
  193. * them as x and y coordinates to which to move. The coordinates
  194. * specified become the current point.
  195. *
  196. * @param integer $x The x attribute defines the left position of the element
  197. * @param integer $y The y attribute defines the right position of the element
  198. *
  199. * @return void
  200. */
  201. public function moveTo($x, $y)
  202. {
  203. $this->stringCommands .= $x . ' ' . $y . " moveto \n";
  204. }
  205. /**
  206. * Output/Display the text
  207. *
  208. * @param string $text The string to be displayed
  209. *
  210. * @return void
  211. */
  212. public function show($text)
  213. {
  214. $this->stringCommands .= '(' . $text . ") show \n";
  215. }
  216. /**
  217. * Output the text at specified co-ordinates
  218. *
  219. * @param string $text String to be displayed
  220. * @param integer $x X attribute defines the left position of the element
  221. * @param integer $y Y attribute defines the right position of the element
  222. *
  223. * @return void
  224. */
  225. public function showXY($text, $x, $y)
  226. {
  227. $this->moveTo($x, $y);
  228. $this->show($text);
  229. }
  230. /**
  231. * Ends EPS Document
  232. *
  233. * @return void
  234. */
  235. public function endEpsDoc()
  236. {
  237. $this->stringCommands .= "showpage \n";
  238. }
  239. /**
  240. * Output EPS Document for download
  241. *
  242. * @param string $fileName name of the eps document
  243. *
  244. * @return void
  245. */
  246. public function showOutput($fileName)
  247. {
  248. // if(ob_get_clean()){
  249. //ob_end_clean();
  250. //}
  251. $output = $this->stringCommands;
  252. PMA_Response::getInstance()->disable();
  253. PMA_downloadHeader(
  254. $fileName,
  255. 'image/x-eps',
  256. /*overload*/mb_strlen($output)
  257. );
  258. print $output;
  259. }
  260. }
  261. /**
  262. * EPS Relation Schema Class
  263. *
  264. * Purpose of this class is to generate the EPS Document
  265. * which is used for representing the database diagrams.
  266. * This class uses post script commands and with
  267. * the combination of these commands actually helps in preparing EPS Document.
  268. *
  269. * This class inherits Export_Relation_Schema class has common functionality added
  270. * to this class
  271. *
  272. * @package PhpMyAdmin
  273. * @name Eps_Relation_Schema
  274. */
  275. class PMA_Eps_Relation_Schema extends PMA_Export_Relation_Schema
  276. {
  277. /**
  278. * @var Table_Stats_Dia[]|Table_Stats_Eps[]|Table_Stats_Pdf[]|Table_Stats_Svg[]
  279. */
  280. private $_tables = array();
  281. /** @var Relation_Stats_Dia[] Relations */
  282. private $_relations = array();
  283. /**
  284. * The "PMA_EPS_Relation_Schema" constructor
  285. *
  286. * Upon instantiation This starts writing the EPS document
  287. * user will be prompted for download as .eps extension
  288. *
  289. * @param string $db database name
  290. *
  291. * @see PMA_EPS
  292. */
  293. public function __construct($db)
  294. {
  295. parent::__construct($db, new PMA_EPS());
  296. $this->setShowColor(isset($_REQUEST['eps_show_color']));
  297. $this->setShowKeys(isset($_REQUEST['eps_show_keys']));
  298. $this->setTableDimension(isset($_REQUEST['eps_show_table_dimension']));
  299. $this->setAllTablesSameWidth(isset($_REQUEST['eps_all_tables_same_width']));
  300. $this->setOrientation($_REQUEST['eps_orientation']);
  301. $this->diagram->setTitle(
  302. sprintf(
  303. __('Schema of the %s database - Page %s'),
  304. $this->db,
  305. $this->pageNumber
  306. )
  307. );
  308. $this->diagram->setAuthor('phpMyAdmin ' . PMA_VERSION);
  309. $this->diagram->setDate(date("j F Y, g:i a"));
  310. $this->diagram->setOrientation($this->orientation);
  311. $this->diagram->setFont('Verdana', '10');
  312. $alltables = $this->getTablesFromRequest();
  313. foreach ($alltables as $table) {
  314. if (! isset($this->_tables[$table])) {
  315. $this->_tables[$table] = new Table_Stats_Eps(
  316. $this->diagram, $this->db,
  317. $table, $this->diagram->getFont(),
  318. $this->diagram->getFontSize(), $this->pageNumber,
  319. $this->_tablewidth, $this->showKeys,
  320. $this->tableDimension, $this->offline
  321. );
  322. }
  323. if ($this->sameWide) {
  324. $this->_tables[$table]->width = $this->_tablewidth;
  325. }
  326. }
  327. $seen_a_relation = false;
  328. foreach ($alltables as $one_table) {
  329. $exist_rel = PMA_getForeigners($this->db, $one_table, '', 'both');
  330. if (!$exist_rel) {
  331. continue;
  332. }
  333. $seen_a_relation = true;
  334. foreach ($exist_rel as $master_field => $rel) {
  335. /* put the foreign table on the schema only if selected
  336. * by the user
  337. * (do not use array_search() because we would have to
  338. * to do a === false and this is not PHP3 compatible)
  339. */
  340. if ($master_field != 'foreign_keys_data') {
  341. if (in_array($rel['foreign_table'], $alltables)) {
  342. $this->_addRelation(
  343. $one_table, $this->diagram->getFont(), $this->diagram->getFontSize(),
  344. $master_field, $rel['foreign_table'],
  345. $rel['foreign_field'], $this->tableDimension
  346. );
  347. }
  348. continue;
  349. }
  350. foreach ($rel as $one_key) {
  351. if (!in_array($one_key['ref_table_name'], $alltables)) {
  352. continue;
  353. }
  354. foreach ($one_key['index_list']
  355. as $index => $one_field
  356. ) {
  357. $this->_addRelation(
  358. $one_table, $this->diagram->getFont(),
  359. $this->diagram->getFontSize(),
  360. $one_field, $one_key['ref_table_name'],
  361. $one_key['ref_index_list'][$index],
  362. $this->tableDimension
  363. );
  364. }
  365. }
  366. }
  367. }
  368. if ($seen_a_relation) {
  369. $this->_drawRelations();
  370. }
  371. $this->_drawTables();
  372. $this->diagram->endEpsDoc();
  373. }
  374. /**
  375. * Output Eps Document for download
  376. *
  377. * @return void
  378. */
  379. public function showOutput()
  380. {
  381. $this->diagram->showOutput($this->getFileName('.eps'));
  382. }
  383. /**
  384. * Defines relation objects
  385. *
  386. * @param string $masterTable The master table name
  387. * @param string $font The font
  388. * @param int $fontSize The font size
  389. * @param string $masterField The relation field in the master table
  390. * @param string $foreignTable The foreign table name
  391. * @param string $foreignField The relation field in the foreign table
  392. * @param boolean $tableDimension Whether to display table position or not
  393. *
  394. * @return void
  395. *
  396. * @see _setMinMax,Table_Stats_Eps::__construct(),
  397. * Relation_Stats_Eps::__construct()
  398. */
  399. private function _addRelation(
  400. $masterTable, $font, $fontSize, $masterField,
  401. $foreignTable, $foreignField, $tableDimension
  402. ) {
  403. if (! isset($this->_tables[$masterTable])) {
  404. $this->_tables[$masterTable] = new Table_Stats_Eps(
  405. $this->diagram, $this->db, $masterTable, $font, $fontSize,
  406. $this->pageNumber, $this->_tablewidth, false, $tableDimension
  407. );
  408. }
  409. if (! isset($this->_tables[$foreignTable])) {
  410. $this->_tables[$foreignTable] = new Table_Stats_Eps(
  411. $this->diagram, $this->db, $foreignTable, $font, $fontSize,
  412. $this->pageNumber, $this->_tablewidth, false, $tableDimension
  413. );
  414. }
  415. $this->_relations[] = new Relation_Stats_Eps(
  416. $this->diagram,
  417. $this->_tables[$masterTable],
  418. $masterField,
  419. $this->_tables[$foreignTable],
  420. $foreignField
  421. );
  422. }
  423. /**
  424. * Draws relation arrows and lines connects master table's master field to
  425. * foreign table's foreign field
  426. *
  427. * @return void
  428. *
  429. * @see Relation_Stats_Eps::relationDraw()
  430. */
  431. private function _drawRelations()
  432. {
  433. foreach ($this->_relations as $relation) {
  434. $relation->relationDraw($this->showColor);
  435. }
  436. }
  437. /**
  438. * Draws tables
  439. *
  440. * @return void
  441. *
  442. * @see Table_Stats_Eps::Table_Stats_tableDraw()
  443. */
  444. private function _drawTables()
  445. {
  446. foreach ($this->_tables as $table) {
  447. $table->tableDraw($this->showColor);
  448. }
  449. }
  450. }