Dia_Relation_Schema.class.php 28 KB

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