Dia_Relation_Schema.class.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Classes to create relation schema in Dia format.
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. require_once 'Export_Relation_Schema.class.php';
  12. /**
  13. * This Class inherits the XMLwriter class and
  14. * helps in developing structure of DIA Schema Export
  15. *
  16. * @package PhpMyAdmin
  17. * @access public
  18. * @see http://php.net/manual/en/book.xmlwriter.php
  19. */
  20. class PMA_DIA extends XMLWriter
  21. {
  22. public $title;
  23. public $author;
  24. public $font;
  25. public $fontSize;
  26. /**
  27. * The "PMA_DIA" constructor
  28. *
  29. * Upon instantiation This starts writing the Dia XML document
  30. *
  31. * @see XMLWriter::openMemory(),XMLWriter::setIndent(),XMLWriter::startDocument()
  32. */
  33. function __construct()
  34. {
  35. $this->openMemory();
  36. /*
  37. * Set indenting using three spaces,
  38. * so output is formatted
  39. */
  40. $this->setIndent(true);
  41. $this->setIndentString(' ');
  42. /*
  43. * Create the XML document
  44. */
  45. $this->startDocument('1.0', 'UTF-8');
  46. }
  47. /**
  48. * Starts Dia Document
  49. *
  50. * dia document starts by first initializing dia:diagram tag
  51. * then dia:diagramdata contains all the attributes that needed
  52. * to define the document, then finally a Layer starts which
  53. * holds all the objects.
  54. *
  55. * @param string $paper the size of the paper/document
  56. * @param float $topMargin top margin of the paper/document in cm
  57. * @param float $bottomMargin bottom margin of the paper/document in cm
  58. * @param float $leftMargin left margin of the paper/document in cm
  59. * @param float $rightMargin right margin of the paper/document in cm
  60. * @param string $portrait document will be portrait or landscape
  61. *
  62. * @return void
  63. *
  64. * @access public
  65. * @see XMLWriter::startElement(),XMLWriter::writeAttribute(),
  66. * XMLWriter::writeRaw()
  67. */
  68. function startDiaDoc($paper, $topMargin, $bottomMargin, $leftMargin,
  69. $rightMargin, $portrait
  70. ) {
  71. if ($portrait == 'P') {
  72. $isPortrait='true';
  73. } else {
  74. $isPortrait='false';
  75. }
  76. $this->startElement('dia:diagram');
  77. $this->writeAttribute('xmlns:dia', 'http://www.lysator.liu.se/~alla/dia/');
  78. $this->startElement('dia:diagramdata');
  79. $this->writeRaw(
  80. '<dia:attribute name="background">
  81. <dia:color val="#ffffff"/>
  82. </dia:attribute>
  83. <dia:attribute name="pagebreak">
  84. <dia:color val="#000099"/>
  85. </dia:attribute>
  86. <dia:attribute name="paper">
  87. <dia:composite type="paper">
  88. <dia:attribute name="name">
  89. <dia:string>#' . $paper . '#</dia:string>
  90. </dia:attribute>
  91. <dia:attribute name="tmargin">
  92. <dia:real val="' . $topMargin . '"/>
  93. </dia:attribute>
  94. <dia:attribute name="bmargin">
  95. <dia:real val="' . $bottomMargin . '"/>
  96. </dia:attribute>
  97. <dia:attribute name="lmargin">
  98. <dia:real val="' . $leftMargin . '"/>
  99. </dia:attribute>
  100. <dia:attribute name="rmargin">
  101. <dia:real val="' . $rightMargin . '"/>
  102. </dia:attribute>
  103. <dia:attribute name="is_portrait">
  104. <dia:boolean val="' . $isPortrait . '"/>
  105. </dia:attribute>
  106. <dia:attribute name="scaling">
  107. <dia:real val="1"/>
  108. </dia:attribute>
  109. <dia:attribute name="fitto">
  110. <dia:boolean val="false"/>
  111. </dia:attribute>
  112. </dia:composite>
  113. </dia:attribute>
  114. <dia:attribute name="grid">
  115. <dia:composite type="grid">
  116. <dia:attribute name="width_x">
  117. <dia:real val="1"/>
  118. </dia:attribute>
  119. <dia:attribute name="width_y">
  120. <dia:real val="1"/>
  121. </dia:attribute>
  122. <dia:attribute name="visible_x">
  123. <dia:int val="1"/>
  124. </dia:attribute>
  125. <dia:attribute name="visible_y">
  126. <dia:int val="1"/>
  127. </dia:attribute>
  128. <dia:composite type="color"/>
  129. </dia:composite>
  130. </dia:attribute>
  131. <dia:attribute name="color">
  132. <dia:color val="#d8e5e5"/>
  133. </dia:attribute>
  134. <dia:attribute name="guides">
  135. <dia:composite type="guides">
  136. <dia:attribute name="hguides"/>
  137. <dia:attribute name="vguides"/>
  138. </dia:composite>
  139. </dia:attribute>'
  140. );
  141. $this->endElement();
  142. $this->startElement('dia:layer');
  143. $this->writeAttribute('name', 'Background');
  144. $this->writeAttribute('visible', 'true');
  145. $this->writeAttribute('active', 'true');
  146. }
  147. /**
  148. * Ends Dia Document
  149. *
  150. * @return void
  151. * @access public
  152. * @see XMLWriter::endElement(),XMLWriter::endDocument()
  153. */
  154. function endDiaDoc()
  155. {
  156. $this->endElement();
  157. $this->endDocument();
  158. }
  159. /**
  160. * Output Dia Document for download
  161. *
  162. * @param string $fileName name of the dia document
  163. *
  164. * @return void
  165. * @access public
  166. * @see XMLWriter::flush()
  167. */
  168. function showOutput($fileName)
  169. {
  170. if (ob_get_clean()) {
  171. ob_end_clean();
  172. }
  173. $output = $this->flush();
  174. PMA_Response::getInstance()->disable();
  175. PMA_downloadHeader(
  176. $fileName . '.dia', 'application/x-dia-diagram', strlen($output)
  177. );
  178. print $output;
  179. }
  180. }
  181. require_once './libraries/schema/TableStats.class.php';
  182. /**
  183. * Table preferences/statistics
  184. *
  185. * This class preserves the table co-ordinates,fields
  186. * and helps in drawing/generating the Tables in dia XML document.
  187. *
  188. * @package PhpMyAdmin
  189. * @name Table_Stats_Dia
  190. * @see PMA_DIA
  191. */
  192. class Table_Stats_Dia extends TableStats
  193. {
  194. /**
  195. * Defines properties
  196. */
  197. public $tableId;
  198. public $tableColor;
  199. /**
  200. * The "Table_Stats_Dia" constructor
  201. *
  202. * @param string $tableName The table name
  203. * @param integer $pageNumber The current page number (from the
  204. * $cfg['Servers'][$i]['table_coords'] table)
  205. * @param boolean $showKeys Whether to display ONLY keys or not
  206. *
  207. * @global object $dia The current dia document
  208. * @global array $cfgRelation The relations settings
  209. * @global string $db The current db name
  210. *
  211. * @see PMA_DIA
  212. */
  213. function __construct($tableName, $pageNumber, $showKeys = false)
  214. {
  215. global $dia, $cfgRelation, $db;
  216. parent::__construct($dia, $db, $pageNumber, $tableName, $showKeys, false);
  217. /**
  218. * Every object in Dia document needs an ID to identify
  219. * so, we used a static variable to keep the things unique
  220. */
  221. PMA_Dia_Relation_Schema::$objectId += 1;
  222. $this->tableId = PMA_Dia_Relation_Schema::$objectId;
  223. }
  224. /**
  225. * Displays an error when the table cannot be found.
  226. *
  227. * @return void
  228. */
  229. protected function showMissingTableError()
  230. {
  231. $this->diagram->dieSchema(
  232. $this->pageNumber,
  233. "DIA",
  234. sprintf(__('The %s table doesn\'t exist!'), $this->tableName)
  235. );
  236. }
  237. /**
  238. * Displays an error on missing coordinates
  239. *
  240. * @return void
  241. */
  242. protected function showMissingCoordinatesError()
  243. {
  244. $this->diagram->dieSchema(
  245. $this->pageNumber,
  246. "DIA",
  247. sprintf(
  248. __('Please configure the coordinates for table %s'),
  249. $this->tableName
  250. )
  251. );
  252. }
  253. /**
  254. * Do draw the table
  255. *
  256. * Tables are generated using object type Database - Table
  257. * primary fields are underlined in tables. Dia object
  258. * is used to generate the XML of Dia Document. Database Table
  259. * Object and their attributes are involved in the combination
  260. * of displaing Database - Table on Dia Document.
  261. *
  262. * @param boolean $changeColor Whether to show color for tables text or not
  263. * if changeColor is true then an array of $listOfColors will be used to choose
  264. * the random colors for tables text we can change/add more colors to this array
  265. *
  266. * @return void
  267. *
  268. * @global object $dia The current Dia document
  269. *
  270. * @access public
  271. * @see PMA_DIA
  272. */
  273. public function tableDraw($changeColor)
  274. {
  275. global $dia;
  276. if ($changeColor) {
  277. $listOfColors = array(
  278. 'FF0000',
  279. '000099',
  280. '00FF00'
  281. );
  282. shuffle($listOfColors);
  283. $this->tableColor = '#' . $listOfColors[0] . '';
  284. } else {
  285. $this->tableColor = '#000000';
  286. }
  287. $factor = 0.1;
  288. $dia->startElement('dia:object');
  289. $dia->writeAttribute('type', 'Database - Table');
  290. $dia->writeAttribute('version', '0');
  291. $dia->writeAttribute('id', '' . $this->tableId . '');
  292. $dia->writeRaw(
  293. '<dia:attribute name="obj_pos">
  294. <dia:point val="'
  295. . ($this->x * $factor) . ',' . ($this->y * $factor) . '"/>
  296. </dia:attribute>
  297. <dia:attribute name="obj_bb">
  298. <dia:rectangle val="'
  299. . ($this->x * $factor) . ',' . ($this->y * $factor) . ';9.97,9.2"/>
  300. </dia:attribute>
  301. <dia:attribute name="meta">
  302. <dia:composite type="dict"/>
  303. </dia:attribute>
  304. <dia:attribute name="elem_corner">
  305. <dia:point val="'
  306. . ($this->x * $factor) . ',' . ($this->y * $factor) . '"/>
  307. </dia:attribute>
  308. <dia:attribute name="elem_width">
  309. <dia:real val="5.9199999999999999"/>
  310. </dia:attribute>
  311. <dia:attribute name="elem_height">
  312. <dia:real val="3.5"/>
  313. </dia:attribute>
  314. <dia:attribute name="text_colour">
  315. <dia:color val="' . $this->tableColor . '"/>
  316. </dia:attribute>
  317. <dia:attribute name="line_colour">
  318. <dia:color val="#000000"/>
  319. </dia:attribute>
  320. <dia:attribute name="fill_colour">
  321. <dia:color val="#ffffff"/>
  322. </dia:attribute>
  323. <dia:attribute name="line_width">
  324. <dia:real val="0.10000000000000001"/>
  325. </dia:attribute>
  326. <dia:attribute name="name">
  327. <dia:string>#' . $this->tableName . '#</dia:string>
  328. </dia:attribute>
  329. <dia:attribute name="comment">
  330. <dia:string>##</dia:string>
  331. </dia:attribute>
  332. <dia:attribute name="visible_comment">
  333. <dia:boolean val="false"/>
  334. </dia:attribute>
  335. <dia:attribute name="tagging_comment">
  336. <dia:boolean val="false"/>
  337. </dia:attribute>
  338. <dia:attribute name="underline_primary_key">
  339. <dia:boolean val="true"/>
  340. </dia:attribute>
  341. <dia:attribute name="bold_primary_keys">
  342. <dia:boolean val="true"/>
  343. </dia:attribute>
  344. <dia:attribute name="normal_font">
  345. <dia:font family="monospace" style="0" name="Courier"/>
  346. </dia:attribute>
  347. <dia:attribute name="name_font">
  348. <dia:font family="sans" style="80" name="Helvetica-Bold"/>
  349. </dia:attribute>
  350. <dia:attribute name="comment_font">
  351. <dia:font family="sans" style="0" name="Helvetica"/>
  352. </dia:attribute>
  353. <dia:attribute name="normal_font_height">
  354. <dia:real val="0.80000000000000004"/>
  355. </dia:attribute>
  356. <dia:attribute name="name_font_height">
  357. <dia:real val="0.69999999999999996"/>
  358. </dia:attribute>
  359. <dia:attribute name="comment_font_height">
  360. <dia:real val="0.69999999999999996"/>
  361. </dia:attribute>'
  362. );
  363. $dia->startElement('dia:attribute');
  364. $dia->writeAttribute('name', 'attributes');
  365. foreach ($this->fields as $field) {
  366. $dia->writeRaw(
  367. '<dia:composite type="table_attribute">
  368. <dia:attribute name="name">
  369. <dia:string>#' . $field . '#</dia:string>
  370. </dia:attribute>
  371. <dia:attribute name="type">
  372. <dia:string>##</dia:string>
  373. </dia:attribute>
  374. <dia:attribute name="comment">
  375. <dia:string>##</dia:string>
  376. </dia:attribute>'
  377. );
  378. unset($pm);
  379. $pm = 'false';
  380. if (in_array($field, $this->primary)) {
  381. $pm = 'true';
  382. }
  383. if ($field == $this->displayfield) {
  384. $pm = 'false';
  385. }
  386. $dia->writeRaw(
  387. '<dia:attribute name="primary_key">
  388. <dia:boolean val="' . $pm . '"/>
  389. </dia:attribute>
  390. <dia:attribute name="nullable">
  391. <dia:boolean val="false"/>
  392. </dia:attribute>
  393. <dia:attribute name="unique">
  394. <dia:boolean val="' . $pm . '"/>
  395. </dia:attribute>
  396. </dia:composite>'
  397. );
  398. }
  399. $dia->endElement();
  400. $dia->endElement();
  401. }
  402. }
  403. /**
  404. * Relation preferences/statistics
  405. *
  406. * This class fetches the table master and foreign fields positions
  407. * and helps in generating the Table references and then connects
  408. * master table's master field to foreign table's foreign key
  409. * in dia XML document.
  410. *
  411. * @package PhpMyAdmin
  412. * @name Relation_Stats_Dia
  413. * @see PMA_DIA
  414. */
  415. class Relation_Stats_Dia
  416. {
  417. /**
  418. * Defines properties
  419. */
  420. public $srcConnPointsRight;
  421. public $srcConnPointsLeft;
  422. public $destConnPointsRight;
  423. public $destConnPointsLeft;
  424. public $masterTableId;
  425. public $foreignTableId;
  426. public $masterTablePos;
  427. public $foreignTablePos;
  428. public $referenceColor;
  429. /**
  430. * The "Relation_Stats_Dia" constructor
  431. *
  432. * @param string $master_table The master table name
  433. * @param string $master_field The relation field in the master table
  434. * @param string $foreign_table The foreign table name
  435. * @param string $foreign_field The relation field in the foreign table
  436. *
  437. * @see Relation_Stats_Dia::_getXy
  438. */
  439. function __construct($master_table, $master_field, $foreign_table,
  440. $foreign_field
  441. ) {
  442. $src_pos = $this->_getXy($master_table, $master_field);
  443. $dest_pos = $this->_getXy($foreign_table, $foreign_field);
  444. $this->srcConnPointsLeft = $src_pos[0];
  445. $this->srcConnPointsRight = $src_pos[1];
  446. $this->destConnPointsLeft = $dest_pos[0];
  447. $this->destConnPointsRight = $dest_pos[1];
  448. $this->masterTablePos = $src_pos[2];
  449. $this->foreignTablePos = $dest_pos[2];
  450. $this->masterTableId = $master_table->tableId;
  451. $this->foreignTableId = $foreign_table->tableId;
  452. }
  453. /**
  454. * Each Table object have connection points
  455. * which is used to connect to other objects in Dia
  456. * we detect the position of key in fields and
  457. * then determines its left and right connection
  458. * points.
  459. *
  460. * @param string $table The current table name
  461. * @param string $column The relation column name
  462. *
  463. * @return array Table right,left connection points and key position
  464. *
  465. * @access private
  466. */
  467. private function _getXy($table, $column)
  468. {
  469. $pos = array_search($column, $table->fields);
  470. // left, right, position
  471. $value = 12;
  472. if ($pos != 0) {
  473. return array($pos + $value + $pos, $pos + $value + $pos + 1, $pos);
  474. }
  475. return array($pos + $value , $pos + $value + 1, $pos);
  476. }
  477. /**
  478. * Draws relation references
  479. *
  480. * connects master table's master field to foreign table's
  481. * forein field using Dia object type Database - Reference
  482. * Dia object is used to generate the XML of Dia Document.
  483. * Database reference Object and their attributes are involved
  484. * in the combination of displaing Database - reference on Dia Document.
  485. *
  486. * @param boolean $changeColor Whether to use one color per relation or not
  487. * if changeColor is true then an array of $listOfColors will be used to choose
  488. * the random colors for references lines. we can change/add more colors to this
  489. *
  490. * @return boolean|void
  491. *
  492. * @global object $dia The current Dia document
  493. *
  494. * @access public
  495. * @see PMA_PDF
  496. */
  497. public function relationDraw($changeColor)
  498. {
  499. global $dia;
  500. PMA_Dia_Relation_Schema::$objectId += 1;
  501. /*
  502. * if source connection points and destination connection
  503. * points are same then return it false and don't draw that
  504. * relation
  505. */
  506. if ( $this->srcConnPointsRight == $this->destConnPointsRight) {
  507. if ( $this->srcConnPointsLeft == $this->destConnPointsLeft) {
  508. return false;
  509. }
  510. }
  511. if ($changeColor) {
  512. $listOfColors = array(
  513. 'FF0000',
  514. '000099',
  515. '00FF00'
  516. );
  517. shuffle($listOfColors);
  518. $this->referenceColor = '#' . $listOfColors[0] . '';
  519. } else {
  520. $this->referenceColor = '#000000';
  521. }
  522. $dia->writeRaw(
  523. '<dia:object type="Database - Reference" version="0" id="'
  524. . PMA_Dia_Relation_Schema::$objectId . '">
  525. <dia:attribute name="obj_pos">
  526. <dia:point val="3.27,18.9198"/>
  527. </dia:attribute>
  528. <dia:attribute name="obj_bb">
  529. <dia:rectangle val="2.27,8.7175;17.7679,18.9198"/>
  530. </dia:attribute>
  531. <dia:attribute name="meta">
  532. <dia:composite type="dict"/>
  533. </dia:attribute>
  534. <dia:attribute name="orth_points">
  535. <dia:point val="3.27,18.9198"/>
  536. <dia:point val="2.27,18.9198"/>
  537. <dia:point val="2.27,14.1286"/>
  538. <dia:point val="17.7679,14.1286"/>
  539. <dia:point val="17.7679,9.3375"/>
  540. <dia:point val="16.7679,9.3375"/>
  541. </dia:attribute>
  542. <dia:attribute name="orth_orient">
  543. <dia:enum val="0"/>
  544. <dia:enum val="1"/>
  545. <dia:enum val="0"/>
  546. <dia:enum val="1"/>
  547. <dia:enum val="0"/>
  548. </dia:attribute>
  549. <dia:attribute name="orth_autoroute">
  550. <dia:boolean val="true"/>
  551. </dia:attribute>
  552. <dia:attribute name="text_colour">
  553. <dia:color val="#000000"/>
  554. </dia:attribute>
  555. <dia:attribute name="line_colour">
  556. <dia:color val="' . $this->referenceColor . '"/>
  557. </dia:attribute>
  558. <dia:attribute name="line_width">
  559. <dia:real val="0.10000000000000001"/>
  560. </dia:attribute>
  561. <dia:attribute name="line_style">
  562. <dia:enum val="0"/>
  563. <dia:real val="1"/>
  564. </dia:attribute>
  565. <dia:attribute name="corner_radius">
  566. <dia:real val="0"/>
  567. </dia:attribute>
  568. <dia:attribute name="end_arrow">
  569. <dia:enum val="22"/>
  570. </dia:attribute>
  571. <dia:attribute name="end_arrow_length">
  572. <dia:real val="0.5"/>
  573. </dia:attribute>
  574. <dia:attribute name="end_arrow_width">
  575. <dia:real val="0.5"/>
  576. </dia:attribute>
  577. <dia:attribute name="start_point_desc">
  578. <dia:string>#1#</dia:string>
  579. </dia:attribute>
  580. <dia:attribute name="end_point_desc">
  581. <dia:string>#n#</dia:string>
  582. </dia:attribute>
  583. <dia:attribute name="normal_font">
  584. <dia:font family="monospace" style="0" name="Courier"/>
  585. </dia:attribute>
  586. <dia:attribute name="normal_font_height">
  587. <dia:real val="0.59999999999999998"/>
  588. </dia:attribute>
  589. <dia:connections>
  590. <dia:connection handle="0" to="'
  591. . $this->masterTableId . '" connection="'
  592. . $this->srcConnPointsRight . '"/>
  593. <dia:connection handle="1" to="'
  594. . $this->foreignTableId . '" connection="'
  595. . $this->destConnPointsRight . '"/>
  596. </dia:connections>
  597. </dia:object>'
  598. );
  599. }
  600. }
  601. /**
  602. * Dia Relation Schema Class
  603. *
  604. * Purpose of this class is to generate the Dia XML Document
  605. * which is used for representing the database diagrams in Dia IDE
  606. * This class uses Database Table and Reference Objects of Dia and with
  607. * the combination of these objects actually helps in preparing Dia XML.
  608. *
  609. * Dia XML is generated by using XMLWriter php extension and this class
  610. * inherits Export_Relation_Schema class has common functionality added
  611. * to this class
  612. *
  613. * @package PhpMyAdmin
  614. * @name Dia_Relation_Schema
  615. */
  616. class PMA_Dia_Relation_Schema extends PMA_Export_Relation_Schema
  617. {
  618. /**
  619. * Defines properties
  620. */
  621. private $_tables = array();
  622. private $_relations = array();
  623. private $_topMargin = 2.8222000598907471;
  624. private $_bottomMargin = 2.8222000598907471;
  625. private $_leftMargin = 2.8222000598907471;
  626. private $_rightMargin = 2.8222000598907471;
  627. public static $objectId = 0;
  628. /**
  629. * The "PMA_Dia_Relation_Schema" constructor
  630. *
  631. * Upon instantiation This outputs the Dia XML document
  632. * that user can download
  633. *
  634. * @see PMA_DIA,Table_Stats_Dia,Relation_Stats_Dia
  635. */
  636. function __construct()
  637. {
  638. global $dia,$db;
  639. $this->setPageNumber($_POST['pdf_page_number']);
  640. $this->setShowGrid(isset($_POST['show_grid']));
  641. $this->setShowColor($_POST['show_color']);
  642. $this->setShowKeys(isset($_POST['show_keys']));
  643. $this->setOrientation(isset($_POST['orientation']));
  644. $this->setPaper($_POST['paper']);
  645. $this->setExportType($_POST['export_type']);
  646. $dia = new PMA_DIA();
  647. $dia->startDiaDoc(
  648. $this->paper, $this->_topMargin, $this->_bottomMargin,
  649. $this->_leftMargin, $this->_rightMargin, $this->orientation
  650. );
  651. $alltables = $this->getAllTables($db, $this->pageNumber);
  652. foreach ($alltables as $table) {
  653. if (! isset($this->tables[$table])) {
  654. $this->_tables[$table] = new Table_Stats_Dia(
  655. $table, $this->pageNumber, $this->showKeys
  656. );
  657. }
  658. }
  659. $seen_a_relation = false;
  660. foreach ($alltables as $one_table) {
  661. $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
  662. if ($exist_rel) {
  663. $seen_a_relation = true;
  664. foreach ($exist_rel as $master_field => $rel) {
  665. /* put the foreign table on the schema only if selected
  666. * by the user
  667. * (do not use array_search() because we would have to
  668. * to do a === false and this is not PHP3 compatible)
  669. */
  670. if (in_array($rel['foreign_table'], $alltables)) {
  671. $this->_addRelation(
  672. $one_table, $master_field, $rel['foreign_table'],
  673. $rel['foreign_field'], $this->showKeys
  674. );
  675. }
  676. }
  677. }
  678. }
  679. $this->_drawTables($this->showColor);
  680. if ($seen_a_relation) {
  681. $this->_drawRelations($this->showColor);
  682. }
  683. $dia->endDiaDoc();
  684. }
  685. /**
  686. * Output Dia Document for download
  687. *
  688. * @return void
  689. * @access public
  690. */
  691. function showOutput()
  692. {
  693. global $dia, $db;
  694. $dia->showOutput($db . '-' . $this->pageNumber);
  695. }
  696. /**
  697. * Defines relation objects
  698. *
  699. * @param string $masterTable The master table name
  700. * @param string $masterField The relation field in the master table
  701. * @param string $foreignTable The foreign table name
  702. * @param string $foreignField The relation field in the foreign table
  703. * @param bool $showKeys Whether to display ONLY keys or not
  704. *
  705. * @return void
  706. *
  707. * @access private
  708. * @see Table_Stats_Dia::__construct(),Relation_Stats_Dia::__construct()
  709. */
  710. private function _addRelation($masterTable, $masterField, $foreignTable,
  711. $foreignField, $showKeys
  712. ) {
  713. if (! isset($this->_tables[$masterTable])) {
  714. $this->_tables[$masterTable] = new Table_Stats_Dia(
  715. $masterTable, $this->pageNumber, $showKeys
  716. );
  717. }
  718. if (! isset($this->_tables[$foreignTable])) {
  719. $this->_tables[$foreignTable] = new Table_Stats_Dia(
  720. $foreignTable, $this->pageNumber, $showKeys
  721. );
  722. }
  723. $this->_relations[] = new Relation_Stats_Dia(
  724. $this->_tables[$masterTable], $masterField,
  725. $this->_tables[$foreignTable], $foreignField
  726. );
  727. }
  728. /**
  729. * Draws relation references
  730. *
  731. * connects master table's master field to
  732. * foreign table's forein field using Dia object
  733. * type Database - Reference
  734. *
  735. * @param boolean $changeColor Whether to use one color per relation or not
  736. *
  737. * @return void
  738. *
  739. * @access private
  740. * @see Relation_Stats_Dia::relationDraw()
  741. */
  742. private function _drawRelations($changeColor)
  743. {
  744. foreach ($this->_relations as $relation) {
  745. $relation->relationDraw($changeColor);
  746. }
  747. }
  748. /**
  749. * Draws tables
  750. *
  751. * Tables are generated using Dia object type Database - Table
  752. * primary fields are underlined and bold in tables
  753. *
  754. * @param boolean $changeColor Whether to show color for tables text or not
  755. *
  756. * @return void
  757. *
  758. * @access private
  759. * @see Table_Stats_Dia::tableDraw()
  760. */
  761. private function _drawTables($changeColor)
  762. {
  763. foreach ($this->_tables as $table) {
  764. $table->tableDraw($changeColor);
  765. }
  766. }
  767. }
  768. ?>