Eps_Relation_Schema.class.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. *
  5. * @package PhpMyAdmin
  6. */
  7. if (! defined('PHPMYADMIN')) {
  8. exit;
  9. }
  10. require_once 'Export_Relation_Schema.class.php';
  11. /**
  12. * This Class is EPS Library and
  13. * helps in developing structure of EPS Schema Export
  14. *
  15. * @access public
  16. * @see http://php.net/manual/en/book.xmlwriter.php
  17. */
  18. class PMA_EPS
  19. {
  20. public $font;
  21. public $fontSize;
  22. public $stringCommands;
  23. /**
  24. * The "PMA_EPS" constructor
  25. *
  26. * Upon instantiation This starts writing the EPS Document.
  27. * %!PS-Adobe-3.0 EPSF-3.0 This is the MUST first comment to include
  28. * it shows/tells that the Post Script document is purely under
  29. * Document Structuring Convention [DSC] and is Compliant
  30. * Encapsulated Post Script Document
  31. *
  32. * @return void
  33. * @access public
  34. */
  35. function __construct()
  36. {
  37. $this->stringCommands = "";
  38. $this->stringCommands .= "%!PS-Adobe-3.0 EPSF-3.0 \n";
  39. }
  40. /**
  41. * Set document title
  42. *
  43. * @param string $value sets the title text
  44. *
  45. * @return void
  46. *
  47. * @access public
  48. */
  49. 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. * @access public
  61. */
  62. function setAuthor($value)
  63. {
  64. $this->stringCommands .= '%%Creator: ' . $value . "\n";
  65. }
  66. /**
  67. * Set document creation date
  68. *
  69. * @param string $value sets the date
  70. *
  71. * @return void
  72. *
  73. * @access public
  74. */
  75. function setDate($value)
  76. {
  77. $this->stringCommands .= '%%CreationDate: ' . $value . "\n";
  78. }
  79. /**
  80. * Set document orientation
  81. *
  82. * @param string $value sets the author
  83. *
  84. * @return void
  85. *
  86. * @access public
  87. */
  88. function setOrientation($value)
  89. {
  90. $this->stringCommands .= "%%PageOrder: Ascend \n";
  91. if ($value == "L") {
  92. $value = "Landscape";
  93. $this->stringCommands .= '%%Orientation: ' . $value . "\n";
  94. } else {
  95. $value = "Portrait";
  96. $this->stringCommands .= '%%Orientation: ' . $value . "\n";
  97. }
  98. $this->stringCommands .= "%%EndComments \n";
  99. $this->stringCommands .= "%%Pages 1 \n";
  100. $this->stringCommands .= "%%BoundingBox: 72 150 144 170 \n";
  101. }
  102. /**
  103. * Set the font and size
  104. *
  105. * font can be set whenever needed in EPS
  106. *
  107. * @param string $value sets the font name e.g Arial
  108. * @param integer $size sets the size of the font e.g 10
  109. *
  110. * @return void
  111. *
  112. * @access public
  113. */
  114. function setFont($value, $size)
  115. {
  116. $this->font = $value;
  117. $this->fontSize = $size;
  118. $this->stringCommands .= "/" . $value . " findfont % Get the basic font\n";
  119. $this->stringCommands .= "" . $size . " scalefont % Scale the font to $size points\n";
  120. $this->stringCommands .= "setfont % Make it the current font\n";
  121. }
  122. /**
  123. * Get the font
  124. *
  125. * @return string return the font name e.g Arial
  126. * @access public
  127. */
  128. function getFont()
  129. {
  130. return $this->font;
  131. }
  132. /**
  133. * Get the font Size
  134. *
  135. * @return string return the size of the font e.g 10
  136. * @access public
  137. */
  138. function getFontSize()
  139. {
  140. return $this->fontSize;
  141. }
  142. /**
  143. * Draw the line
  144. *
  145. * drawing the lines from x,y source to x,y destination and set the
  146. * width of the line. lines helps in showing relationships of tables
  147. *
  148. * @param integer $x_from The x_from attribute defines the start
  149. * left position of the element
  150. * @param integer $y_from The y_from attribute defines the start
  151. * right position of the element
  152. * @param integer $x_to The x_to attribute defines the end
  153. * left position of the element
  154. * @param integer $y_to The y_to attribute defines the end
  155. * right position of the element
  156. * @param integer $lineWidth Sets the width of the line e.g 2
  157. *
  158. * @return void
  159. *
  160. * @access public
  161. */
  162. function line($x_from = 0, $y_from = 0, $x_to = 0, $y_to = 0, $lineWidth = 0)
  163. {
  164. $this->stringCommands .= $lineWidth . " setlinewidth \n";
  165. $this->stringCommands .= $x_from . ' ' . $y_from . " moveto \n";
  166. $this->stringCommands .= $x_to . ' ' . $y_to . " lineto \n";
  167. $this->stringCommands .= "stroke \n";
  168. }
  169. /**
  170. * Draw the rectangle
  171. *
  172. * drawing the rectangle from x,y source to x,y destination and set the
  173. * width of the line. rectangles drawn around the text shown of fields
  174. *
  175. * @param integer $x_from The x_from attribute defines the start
  176. left position of the element
  177. * @param integer $y_from The y_from attribute defines the start
  178. right position of the element
  179. * @param integer $x_to The x_to attribute defines the end
  180. left position of the element
  181. * @param integer $y_to The y_to attribute defines the end
  182. right position of the element
  183. * @param integer $lineWidth Sets the width of the line e.g 2
  184. *
  185. * @return void
  186. *
  187. * @access public
  188. */
  189. function rect($x_from, $y_from, $x_to, $y_to, $lineWidth)
  190. {
  191. $this->stringCommands .= $lineWidth . " setlinewidth \n";
  192. $this->stringCommands .= "newpath \n";
  193. $this->stringCommands .= $x_from . " " . $y_from . " moveto \n";
  194. $this->stringCommands .= "0 " . $y_to . " rlineto \n";
  195. $this->stringCommands .= $x_to . " 0 rlineto \n";
  196. $this->stringCommands .= "0 -" . $y_to . " rlineto \n";
  197. $this->stringCommands .= "closepath \n";
  198. $this->stringCommands .= "stroke \n";
  199. }
  200. /**
  201. * Set the current point
  202. *
  203. * The moveto operator takes two numbers off the stack and treats
  204. * them as x and y coordinates to which to move. The coordinates
  205. * specified become the current point.
  206. *
  207. * @param integer $x The x attribute defines the left position of the element
  208. * @param integer $y The y attribute defines the right position of the element
  209. *
  210. * @return void
  211. *
  212. * @access public
  213. */
  214. function moveTo($x, $y)
  215. {
  216. $this->stringCommands .= $x . ' ' . $y . " moveto \n";
  217. }
  218. /**
  219. * Output/Display the text
  220. *
  221. * @param string $text The string to be displayed
  222. *
  223. * @return void
  224. *
  225. * @access public
  226. */
  227. function show($text)
  228. {
  229. $this->stringCommands .= '(' . $text . ") show \n";
  230. }
  231. /**
  232. * Output the text at specified co-ordinates
  233. *
  234. * @param string $text String to be displayed
  235. * @param integer $x X attribute defines the left position of the element
  236. * @param integer $y Y attribute defines the right position of the element
  237. *
  238. * @return void
  239. *
  240. * @access public
  241. */
  242. function showXY($text, $x, $y)
  243. {
  244. $this->moveTo($x, $y);
  245. $this->show($text);
  246. }
  247. /**
  248. * get width of string/text
  249. *
  250. * EPS text width is calcualted depending on font name
  251. * and font size. It is very important to know the width of text
  252. * because rectangle is drawn around it.
  253. *
  254. * This is a bit hardcore method. I didn't found any other better than this.
  255. * if someone found better than this. would love to hear that method
  256. *
  257. * @param string $text string that width will be calculated
  258. * @param integer $font name of the font like Arial,sans-serif etc
  259. * @param integer $fontSize size of font
  260. *
  261. * @return integer width of the text
  262. *
  263. * @access public
  264. */
  265. function getStringWidth($text,$font,$fontSize)
  266. {
  267. /*
  268. * Start by counting the width, giving each character a modifying value
  269. */
  270. $count = 0;
  271. $count = $count + ((strlen($text) - strlen(str_replace(array("i", "j", "l"), "", $text))) * 0.23);//ijl
  272. $count = $count + ((strlen($text) - strlen(str_replace(array("f"), "", $text))) * 0.27);//f
  273. $count = $count + ((strlen($text) - strlen(str_replace(array("t", "I"), "", $text))) * 0.28);//tI
  274. $count = $count + ((strlen($text) - strlen(str_replace(array("r"), "", $text))) * 0.34);//r
  275. $count = $count + ((strlen($text) - strlen(str_replace(array("1"), "", $text))) * 0.49);//1
  276. $count = $count + ((strlen($text) - strlen(str_replace(array("c", "k", "s", "v", "x", "y", "z", "J"), "", $text))) * 0.5);//cksvxyzJ
  277. $count = $count + ((strlen($text) - strlen(str_replace(array("a", "b", "d", "e", "g", "h", "n", "o", "p", "q", "u", "L", "0", "2", "3", "4", "5", "6", "7", "8", "9"), "", $text))) * 0.56);//abdeghnopquL023456789
  278. $count = $count + ((strlen($text) - strlen(str_replace(array("F", "T", "Z"), "", $text))) * 0.61);//FTZ
  279. $count = $count + ((strlen($text) - strlen(str_replace(array("A", "B", "E", "K", "P", "S", "V", "X", "Y"), "", $text))) * 0.67);//ABEKPSVXY
  280. $count = $count + ((strlen($text) - strlen(str_replace(array("w", "C", "D", "H", "N", "R", "U"), "", $text))) * 0.73);//wCDHNRU
  281. $count = $count + ((strlen($text) - strlen(str_replace(array("G", "O", "Q"), "", $text))) * 0.78);//GOQ
  282. $count = $count + ((strlen($text) - strlen(str_replace(array("m", "M"), "", $text))) * 0.84);//mM
  283. $count = $count + ((strlen($text) - strlen(str_replace("W", "", $text))) * .95);//W
  284. $count = $count + ((strlen($text) - strlen(str_replace(" ", "", $text))) * .28);//" "
  285. $text = str_replace(" ", "", $text);//remove the " "'s
  286. $count = $count + (strlen(preg_replace("/[a-z0-9]/i", "", $text)) * 0.3); //all other chrs
  287. $modifier = 1;
  288. $font = strtolower($font);
  289. switch ($font) {
  290. /*
  291. * no modifier for arial and sans-serif
  292. */
  293. case 'arial':
  294. case 'sans-serif':
  295. break;
  296. /*
  297. * .92 modifer for time, serif, brushscriptstd, and californian fb
  298. */
  299. case 'times':
  300. case 'serif':
  301. case 'brushscriptstd':
  302. case 'californian fb':
  303. $modifier = .92;
  304. break;
  305. /*
  306. * 1.23 modifier for broadway
  307. */
  308. case 'broadway':
  309. $modifier = 1.23;
  310. break;
  311. }
  312. $textWidth = $count*$fontSize;
  313. return ceil($textWidth*$modifier);
  314. }
  315. /**
  316. * Ends EPS Document
  317. *
  318. * @return void
  319. * @access public
  320. */
  321. function endEpsDoc()
  322. {
  323. $this->stringCommands .= "showpage \n";
  324. }
  325. /**
  326. * Output EPS Document for download
  327. *
  328. * @param string $fileName name of the eps document
  329. *
  330. * @return void
  331. *
  332. * @access public
  333. */
  334. function showOutput($fileName)
  335. {
  336. // if(ob_get_clean()){
  337. //ob_end_clean();
  338. //}
  339. $output = $this->stringCommands;
  340. PMA_Response::getInstance()->disable();
  341. PMA_downloadHeader($fileName . '.eps', 'image/x-eps', strlen($output));
  342. print $output;
  343. }
  344. }
  345. /**
  346. * Table preferences/statistics
  347. *
  348. * This class preserves the table co-ordinates,fields
  349. * and helps in drawing/generating the Tables in EPS.
  350. *
  351. * @name Table_Stats
  352. * @see PMA_EPS
  353. */
  354. class Table_Stats
  355. {
  356. /**
  357. * Defines properties
  358. */
  359. private $_tableName;
  360. private $_showInfo = false;
  361. public $width = 0;
  362. public $height;
  363. public $fields = array();
  364. public $heightCell = 0;
  365. public $currentCell = 0;
  366. public $x, $y;
  367. public $primary = array();
  368. /**
  369. * The "Table_Stats" constructor
  370. *
  371. * @param string $tableName The table name
  372. * @param string $font The font name
  373. * @param integer $fontSize The font size
  374. * @param integer $pageNumber Page number
  375. * @param integer &$same_wide_width The max width among tables
  376. * @param boolean $showKeys Whether to display keys or not
  377. * @param boolean $showInfo Whether to display table position or not
  378. *
  379. * @global object The current eps document
  380. * @global integer The current page number (from the
  381. * $cfg['Servers'][$i]['table_coords'] table)
  382. * @global array The relations settings
  383. * @global string The current db name
  384. *
  385. * @access private
  386. * @see PMA_EPS, Table_Stats::Table_Stats_setWidth,
  387. * Table_Stats::Table_Stats_setHeight
  388. */
  389. function __construct(
  390. $tableName, $font, $fontSize, $pageNumber, &$same_wide_width,
  391. $showKeys = false, $showInfo = false
  392. ) {
  393. global $eps, $cfgRelation, $db;
  394. $this->_tableName = $tableName;
  395. $sql = 'DESCRIBE ' . PMA_Util::backquote($tableName);
  396. $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE);
  397. if (! $result || ! PMA_DBI_num_rows($result)) {
  398. $eps->dieSchema(
  399. $pageNumber, "EPS",
  400. sprintf(__('The %s table doesn\'t exist!'), $tableName)
  401. );
  402. }
  403. /*
  404. * load fields
  405. * check to see if it will load all fields or only the foreign keys
  406. */
  407. if ($showKeys) {
  408. $indexes = PMA_Index::getFromTable($this->_tableName, $db);
  409. $all_columns = array();
  410. foreach ($indexes as $index) {
  411. $all_columns = array_merge(
  412. $all_columns,
  413. array_flip(array_keys($index->getColumns()))
  414. );
  415. }
  416. $this->fields = array_keys($all_columns);
  417. } else {
  418. while ($row = PMA_DBI_fetch_row($result)) {
  419. $this->fields[] = $row[0];
  420. }
  421. }
  422. $this->_showInfo = $showInfo;
  423. // height and width
  424. $this->_setHeightTable($fontSize);
  425. // setWidth must me after setHeight, because title
  426. // can include table height which changes table width
  427. $this->_setWidthTable($font, $fontSize);
  428. if ($same_wide_width < $this->width) {
  429. $same_wide_width = $this->width;
  430. }
  431. // x and y
  432. $sql = 'SELECT x, y FROM '
  433. . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.'
  434. . PMA_Util::backquote($cfgRelation['table_coords'])
  435. . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\''
  436. . ' AND table_name = \'' . PMA_Util::sqlAddSlashes($tableName) . '\''
  437. . ' AND pdf_page_number = ' . $pageNumber;
  438. $result = PMA_queryAsControlUser($sql, false, PMA_DBI_QUERY_STORE);
  439. if (! $result || ! PMA_DBI_num_rows($result)) {
  440. $eps->dieSchema(
  441. $pageNumber, "EPS",
  442. sprintf(
  443. __('Please configure the coordinates for table %s'),
  444. $tableName
  445. )
  446. );
  447. }
  448. list($this->x, $this->y) = PMA_DBI_fetch_row($result);
  449. $this->x = (double) $this->x;
  450. $this->y = (double) $this->y;
  451. // displayfield
  452. $this->displayfield = PMA_getDisplayField($db, $tableName);
  453. // index
  454. $result = PMA_DBI_query(
  455. 'SHOW INDEX FROM ' . PMA_Util::backquote($tableName) . ';',
  456. null, PMA_DBI_QUERY_STORE
  457. );
  458. if (PMA_DBI_num_rows($result) > 0) {
  459. while ($row = PMA_DBI_fetch_assoc($result)) {
  460. if ($row['Key_name'] == 'PRIMARY') {
  461. $this->primary[] = $row['Column_name'];
  462. }
  463. }
  464. }
  465. }
  466. /**
  467. * Returns title of the current table,
  468. * title can have the dimensions/co-ordinates of the table
  469. *
  470. * @return string The relation/table name
  471. * @access private
  472. */
  473. private function _getTitle()
  474. {
  475. return ($this->_showInfo
  476. ? sprintf('%.0f', $this->width) . 'x' . sprintf('%.0f', $this->heightCell)
  477. : '') . ' ' . $this->_tableName;
  478. }
  479. /**
  480. * Sets the width of the table
  481. *
  482. * @param string $font The font name
  483. * @param integer $fontSize The font size
  484. *
  485. * @global object The current eps document
  486. *
  487. * @return void
  488. *
  489. * @access private
  490. * @see PMA_EPS
  491. */
  492. private function _setWidthTable($font,$fontSize)
  493. {
  494. global $eps;
  495. foreach ($this->fields as $field) {
  496. $this->width = max(
  497. $this->width,
  498. $eps->getStringWidth($field, $font, $fontSize)
  499. );
  500. }
  501. $this->width += $eps->getStringWidth(' ', $font, $fontSize);
  502. /*
  503. * it is unknown what value must be added, because
  504. * table title is affected by the tabe width value
  505. */
  506. while ($this->width < $eps->getStringWidth($this->_getTitle(), $font, $fontSize)) {
  507. $this->width += 7;
  508. }
  509. }
  510. /**
  511. * Sets the height of the table
  512. *
  513. * @param integer $fontSize The font size
  514. *
  515. * @return void
  516. * @access private
  517. */
  518. private function _setHeightTable($fontSize)
  519. {
  520. $this->heightCell = $fontSize + 4;
  521. $this->height = (count($this->fields) + 1) * $this->heightCell;
  522. }
  523. /**
  524. * Draw the table
  525. *
  526. * @param boolean $showColor Whether to display color
  527. *
  528. * @global object The current eps document
  529. *
  530. * @return void
  531. *
  532. * @access public
  533. * @see PMA_EPS,PMA_EPS::line,PMA_EPS::rect
  534. */
  535. public function tableDraw($showColor)
  536. {
  537. global $eps;
  538. //echo $this->_tableName.'<br />';
  539. $eps->rect($this->x, $this->y + 12, $this->width, $this->heightCell, 1);
  540. $eps->showXY($this->_getTitle(), $this->x + 5, $this->y + 14);
  541. foreach ($this->fields as $field) {
  542. $this->currentCell += $this->heightCell;
  543. $showColor = 'none';
  544. if ($showColor) {
  545. if (in_array($field, $this->primary)) {
  546. $showColor = '#0c0';
  547. }
  548. if ($field == $this->displayfield) {
  549. $showColor = 'none';
  550. }
  551. }
  552. $eps->rect(
  553. $this->x, $this->y + 12 + $this->currentCell,
  554. $this->width, $this->heightCell, 1
  555. );
  556. $eps->showXY($field, $this->x + 5, $this->y + 14 + $this->currentCell);
  557. }
  558. }
  559. }
  560. /**
  561. * Relation preferences/statistics
  562. *
  563. * This class fetches the table master and foreign fields positions
  564. * and helps in generating the Table references and then connects
  565. * master table's master field to foreign table's foreign key
  566. * in EPS document.
  567. *
  568. * @name Relation_Stats
  569. * @see PMA_EPS
  570. */
  571. class Relation_Stats
  572. {
  573. /**
  574. * Defines properties
  575. */
  576. public $xSrc, $ySrc;
  577. public $srcDir ;
  578. public $destDir;
  579. public $xDest, $yDest;
  580. public $wTick = 10;
  581. /**
  582. * The "Relation_Stats" constructor
  583. *
  584. * @param string $master_table The master table name
  585. * @param string $master_field The relation field in the master table
  586. * @param string $foreign_table The foreign table name
  587. * @param string $foreign_field The relation field in the foreign table
  588. *
  589. * @see Relation_Stats::_getXy
  590. */
  591. function __construct($master_table, $master_field, $foreign_table, $foreign_field)
  592. {
  593. $src_pos = $this->_getXy($master_table, $master_field);
  594. $dest_pos = $this->_getXy($foreign_table, $foreign_field);
  595. /*
  596. * [0] is x-left
  597. * [1] is x-right
  598. * [2] is y
  599. */
  600. $src_left = $src_pos[0] - $this->wTick;
  601. $src_right = $src_pos[1] + $this->wTick;
  602. $dest_left = $dest_pos[0] - $this->wTick;
  603. $dest_right = $dest_pos[1] + $this->wTick;
  604. $d1 = abs($src_left - $dest_left);
  605. $d2 = abs($src_right - $dest_left);
  606. $d3 = abs($src_left - $dest_right);
  607. $d4 = abs($src_right - $dest_right);
  608. $d = min($d1, $d2, $d3, $d4);
  609. if ($d == $d1) {
  610. $this->xSrc = $src_pos[0];
  611. $this->srcDir = -1;
  612. $this->xDest = $dest_pos[0];
  613. $this->destDir = -1;
  614. } elseif ($d == $d2) {
  615. $this->xSrc = $src_pos[1];
  616. $this->srcDir = 1;
  617. $this->xDest = $dest_pos[0];
  618. $this->destDir = -1;
  619. } elseif ($d == $d3) {
  620. $this->xSrc = $src_pos[0];
  621. $this->srcDir = -1;
  622. $this->xDest = $dest_pos[1];
  623. $this->destDir = 1;
  624. } else {
  625. $this->xSrc = $src_pos[1];
  626. $this->srcDir = 1;
  627. $this->xDest = $dest_pos[1];
  628. $this->destDir = 1;
  629. }
  630. $this->ySrc = $src_pos[2] + 10;
  631. $this->yDest = $dest_pos[2] + 10;
  632. }
  633. /**
  634. * Gets arrows coordinates
  635. *
  636. * @param string $table The current table name
  637. * @param string $column The relation column name
  638. *
  639. * @return array Arrows coordinates
  640. *
  641. * @access private
  642. */
  643. private function _getXy($table, $column)
  644. {
  645. $pos = array_search($column, $table->fields);
  646. // x_left, x_right, y
  647. return array(
  648. $table->x,
  649. $table->x + $table->width,
  650. $table->y + ($pos + 1.5) * $table->heightCell
  651. );
  652. }
  653. /**
  654. * draws relation links and arrows
  655. * shows foreign key relations
  656. *
  657. * @param boolean $changeColor Whether to use one color per relation or not
  658. *
  659. * @global object The current EPS document
  660. *
  661. * @access public
  662. * @see PMA_EPS
  663. *
  664. * @return void
  665. */
  666. public function relationDraw($changeColor)
  667. {
  668. global $eps;
  669. if ($changeColor) {
  670. $listOfColors = array(
  671. 'red',
  672. 'grey',
  673. 'black',
  674. 'yellow',
  675. 'green',
  676. 'cyan',
  677. ' orange'
  678. );
  679. shuffle($listOfColors);
  680. $color = $listOfColors[0];
  681. } else {
  682. $color = 'black';
  683. }
  684. // draw a line like -- to foreign field
  685. $eps->line(
  686. $this->xSrc,
  687. $this->ySrc,
  688. $this->xSrc + $this->srcDir * $this->wTick,
  689. $this->ySrc,
  690. 1
  691. );
  692. // draw a line like -- to master field
  693. $eps->line(
  694. $this->xDest + $this->destDir * $this->wTick,
  695. $this->yDest,
  696. $this->xDest,
  697. $this->yDest,
  698. 1
  699. );
  700. // draw a line that connects to master field line and foreign field line
  701. $eps->line(
  702. $this->xSrc + $this->srcDir * $this->wTick,
  703. $this->ySrc,
  704. $this->xDest + $this->destDir * $this->wTick,
  705. $this->yDest,
  706. 1
  707. );
  708. $root2 = 2 * sqrt(2);
  709. $eps->line(
  710. $this->xSrc + $this->srcDir * $this->wTick * 0.75,
  711. $this->ySrc,
  712. $this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick,
  713. $this->ySrc + $this->wTick / $root2,
  714. 1
  715. );
  716. $eps->line(
  717. $this->xSrc + $this->srcDir * $this->wTick * 0.75,
  718. $this->ySrc,
  719. $this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick,
  720. $this->ySrc - $this->wTick / $root2,
  721. 1
  722. );
  723. $eps->line(
  724. $this->xDest + $this->destDir * $this->wTick / 2,
  725. $this->yDest,
  726. $this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick,
  727. $this->yDest + $this->wTick / $root2,
  728. 1
  729. );
  730. $eps->line(
  731. $this->xDest + $this->destDir * $this->wTick / 2,
  732. $this->yDest,
  733. $this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick,
  734. $this->yDest - $this->wTick / $root2,
  735. 1
  736. );
  737. }
  738. }
  739. /*
  740. * end of the "Relation_Stats" class
  741. */
  742. /**
  743. * EPS Relation Schema Class
  744. *
  745. * Purpose of this class is to generate the EPS Document
  746. * which is used for representing the database diagrams.
  747. * This class uses post script commands and with
  748. * the combination of these commands actually helps in preparing EPS Document.
  749. *
  750. * This class inherits Export_Relation_Schema class has common functionality added
  751. * to this class
  752. *
  753. * @name Eps_Relation_Schema
  754. */
  755. class PMA_Eps_Relation_Schema extends PMA_Export_Relation_Schema
  756. {
  757. private $_tables = array();
  758. private $_relations = array();
  759. /**
  760. * The "PMA_EPS_Relation_Schema" constructor
  761. *
  762. * Upon instantiation This starts writing the EPS document
  763. * user will be prompted for download as .eps extension
  764. *
  765. * @return void
  766. * @see PMA_EPS
  767. */
  768. function __construct()
  769. {
  770. global $eps,$db;
  771. $this->setPageNumber($_POST['pdf_page_number']);
  772. $this->setShowColor(isset($_POST['show_color']));
  773. $this->setShowKeys(isset($_POST['show_keys']));
  774. $this->setTableDimension(isset($_POST['show_table_dimension']));
  775. $this->setAllTablesSameWidth(isset($_POST['all_tables_same_width']));
  776. $this->setOrientation($_POST['orientation']);
  777. $this->setExportType($_POST['export_type']);
  778. $eps = new PMA_EPS();
  779. $eps->setTitle(
  780. sprintf(
  781. __('Schema of the %s database - Page %s'),
  782. $db,
  783. $this->pageNumber
  784. )
  785. );
  786. $eps->setAuthor('phpMyAdmin ' . PMA_VERSION);
  787. $eps->setDate(date("j F Y, g:i a"));
  788. $eps->setOrientation($this->orientation);
  789. $eps->setFont('Verdana', '10');
  790. $alltables = $this->getAllTables($db, $this->pageNumber);
  791. foreach ($alltables AS $table) {
  792. if (! isset($this->_tables[$table])) {
  793. $this->_tables[$table] = new Table_Stats(
  794. $table, $eps->getFont(), $eps->getFontSize(), $this->pageNumber,
  795. $this->_tablewidth, $this->showKeys, $this->tableDimension
  796. );
  797. }
  798. if ($this->sameWide) {
  799. $this->_tables[$table]->width = $this->_tablewidth;
  800. }
  801. }
  802. $seen_a_relation = false;
  803. foreach ($alltables as $one_table) {
  804. $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
  805. if ($exist_rel) {
  806. $seen_a_relation = true;
  807. foreach ($exist_rel as $master_field => $rel) {
  808. /* put the foreign table on the schema only if selected
  809. * by the user
  810. * (do not use array_search() because we would have to
  811. * to do a === false and this is not PHP3 compatible)
  812. */
  813. if (in_array($rel['foreign_table'], $alltables)) {
  814. $this->_addRelation(
  815. $one_table, $eps->getFont(), $eps->getFontSize(),
  816. $master_field, $rel['foreign_table'],
  817. $rel['foreign_field'], $this->tableDimension
  818. );
  819. }
  820. }
  821. }
  822. }
  823. if ($seen_a_relation) {
  824. $this->_drawRelations($this->showColor);
  825. }
  826. $this->_drawTables($this->showColor);
  827. $eps->endEpsDoc();
  828. $eps->showOutput($db.'-'.$this->pageNumber);
  829. exit();
  830. }
  831. /**
  832. * Defines relation objects
  833. *
  834. * @param string $masterTable The master table name
  835. * @param string $font The font
  836. * @param int $fontSize The font size
  837. * @param string $masterField The relation field in the master table
  838. * @param string $foreignTable The foreign table name
  839. * @param string $foreignField The relation field in the foreign table
  840. * @param boolean $showInfo Whether to display table position or not
  841. *
  842. * @return void
  843. *
  844. * @access private
  845. * @see _setMinMax,Table_Stats::__construct(),Relation_Stats::__construct()
  846. */
  847. private function _addRelation(
  848. $masterTable, $font, $fontSize, $masterField,
  849. $foreignTable, $foreignField, $showInfo
  850. ) {
  851. if (! isset($this->_tables[$masterTable])) {
  852. $this->_tables[$masterTable] = new Table_Stats(
  853. $masterTable, $font, $fontSize, $this->pageNumber,
  854. $this->_tablewidth, false, $showInfo
  855. );
  856. }
  857. if (! isset($this->_tables[$foreignTable])) {
  858. $this->_tables[$foreignTable] = new Table_Stats(
  859. $foreignTable, $font, $fontSize, $this->pageNumber,
  860. $this->_tablewidth, false, $showInfo
  861. );
  862. }
  863. $this->_relations[] = new Relation_Stats(
  864. $this->_tables[$masterTable], $masterField,
  865. $this->_tables[$foreignTable], $foreignField
  866. );
  867. }
  868. /**
  869. * Draws relation arrows and lines connects master table's master field to
  870. * foreign table's forein field
  871. *
  872. * @param boolean $changeColor Whether to use one color per relation or not
  873. *
  874. * @return void
  875. *
  876. * @access private
  877. * @see Relation_Stats::relationDraw()
  878. */
  879. private function _drawRelations($changeColor)
  880. {
  881. foreach ($this->_relations as $relation) {
  882. $relation->relationDraw($changeColor);
  883. }
  884. }
  885. /**
  886. * Draws tables
  887. *
  888. * @param boolean $changeColor Whether to show color for primary fields or not
  889. *
  890. * @return void
  891. *
  892. * @access private
  893. * @see Table_Stats::Table_Stats_tableDraw()
  894. */
  895. private function _drawTables($changeColor)
  896. {
  897. foreach ($this->_tables as $table) {
  898. $table->tableDraw($changeColor);
  899. }
  900. }
  901. }
  902. ?>