Svg_Relation_Schema.class.php 30 KB

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