2 /* vim: set expandtab sw=4 ts=4 sts=4: */
8 require_once "Export_Relation_Schema.class.php";
11 * This Class inherits the XMLwriter class and
12 * helps in developing structure of DIA Schema Export
15 * @see http://php.net/manual/en/book.xmlwriter.php
17 class PMA_DIA
extends XMLWriter
25 * The "PMA_DIA" constructor
27 * Upon instantiation This starts writing the Dia XML document
30 * @see XMLWriter::openMemory(),XMLWriter::setIndent(),XMLWriter::startDocument()
32 function __construct()
36 * Set indenting using three spaces,
37 * so output is formatted
40 $this->setIndent(true);
41 $this->setIndentString(' ');
43 * Create the XML document
46 $this->startDocument('1.0', 'UTF-8');
52 * dia document starts by first initializing dia:diagram tag
53 * then dia:diagramdata contains all the attributes that needed
54 * to define the document, then finally a Layer starts which
55 * holds all the objects.
57 * @param string $paper the size of the paper/document
58 * @param float $topMargin top margin of the paper/document in cm
59 * @param float $bottomMargin bottom margin of the paper/document in cm
60 * @param float $leftMargin left margin of the paper/document in cm
61 * @param float $rightMargin right margin of the paper/document in cm
62 * @param string $portrait document will be portrait or landscape
67 * @see XMLWriter::startElement(),XMLWriter::writeAttribute(),XMLWriter::writeRaw()
69 function startDiaDoc($paper,$topMargin,$bottomMargin,$leftMargin,$rightMargin,$portrait)
71 if ($portrait == 'P') {
76 $this->startElement('dia:diagram');
77 $this->writeAttribute('xmlns:dia', 'http://www.lysator.liu.se/~alla/dia/');
78 $this->startElement('dia:diagramdata');
80 '<dia:attribute name="background">
81 <dia:color val="#ffffff"/>
83 <dia:attribute name="pagebreak">
84 <dia:color val="#000099"/>
86 <dia:attribute name="paper">
87 <dia:composite type="paper">
88 <dia:attribute name="name">
89 <dia:string>#' . $paper . '#</dia:string>
91 <dia:attribute name="tmargin">
92 <dia:real val="' . $topMargin . '"/>
94 <dia:attribute name="bmargin">
95 <dia:real val="' . $bottomMargin . '"/>
97 <dia:attribute name="lmargin">
98 <dia:real val="' . $leftMargin . '"/>
100 <dia:attribute name="rmargin">
101 <dia:real val="' . $rightMargin . '"/>
103 <dia:attribute name="is_portrait">
104 <dia:boolean val="' . $isPortrait . '"/>
106 <dia:attribute name="scaling">
109 <dia:attribute name="fitto">
110 <dia:boolean val="false"/>
114 <dia:attribute name="grid">
115 <dia:composite type="grid">
116 <dia:attribute name="width_x">
119 <dia:attribute name="width_y">
122 <dia:attribute name="visible_x">
125 <dia:attribute name="visible_y">
128 <dia:composite type="color"/>
131 <dia:attribute name="color">
132 <dia:color val="#d8e5e5"/>
134 <dia:attribute name="guides">
135 <dia:composite type="guides">
136 <dia:attribute name="hguides"/>
137 <dia:attribute name="vguides"/>
141 $this->startElement('dia:layer');
142 $this->writeAttribute('name', 'Background');
143 $this->writeAttribute('visible', 'true');
144 $this->writeAttribute('active', 'true');
153 * @see XMLWriter::endElement(),XMLWriter::endDocument()
158 $this->endDocument();
162 * Output Dia Document for download
164 * @param string $fileName name of the dia document
168 * @see XMLWriter::flush()
170 function showOutput($fileName)
172 if (ob_get_clean()) {
175 $output = $this->flush();
177 $fileName . '.dia', 'application/x-dia-diagram', strlen($output)
184 * Table preferences/statistics
186 * This class preserves the table co-ordinates,fields
187 * and helps in drawing/generating the Tables in dia XML document.
198 public $fields = array();
200 public $primary = array();
205 * The "Table_Stats" constructor
207 * @param string $tableName The table name
208 * @param integer $pageNumber The current page number (from the
209 * $cfg['Servers'][$i]['table_coords'] table)
210 * @param boolean $showKeys Whether to display ONLY keys or not
214 * @global object The current dia document
215 * @global array The relations settings
216 * @global string The current db name
220 function __construct($tableName, $pageNumber, $showKeys = false)
222 global $dia, $cfgRelation, $db;
224 $this->tableName
= $tableName;
225 $sql = 'DESCRIBE ' . PMA_backquote($tableName);
226 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE
);
227 if (!$result ||
!PMA_DBI_num_rows($result)) {
230 sprintf(__('The %s table doesn\'t exist!'), $tableName)
235 * check to see if it will load all fields or only the foreign keys
238 $indexes = PMA_Index
::getFromTable($this->tableName
, $db);
239 $all_columns = array();
240 foreach ($indexes as $index) {
241 $all_columns = array_merge(
243 array_flip(array_keys($index->getColumns()))
246 $this->fields
= array_keys($all_columns);
248 while ($row = PMA_DBI_fetch_row($result)) {
249 $this->fields
[] = $row[0];
253 $sql = 'SELECT x, y FROM '
254 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.'
255 . PMA_backquote($cfgRelation['table_coords'])
256 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
257 . ' AND table_name = \'' . PMA_sqlAddSlashes($tableName) . '\''
258 . ' AND pdf_page_number = ' . $pageNumber;
259 $result = PMA_query_as_controluser($sql, false, PMA_DBI_QUERY_STORE
);
260 if (! $result ||
! PMA_DBI_num_rows($result)) {
265 __('Please configure the coordinates for table %s'),
270 list($this->x
, $this->y
) = PMA_DBI_fetch_row($result);
271 $this->x
= (double) $this->x
;
272 $this->y
= (double) $this->y
;
276 $this->displayfield
= PMA_getDisplayField($db, $tableName);
280 $result = PMA_DBI_query(
281 'SHOW INDEX FROM ' . PMA_backquote($tableName) . ';',
285 if (PMA_DBI_num_rows($result) > 0) {
286 while ($row = PMA_DBI_fetch_assoc($result)) {
287 if ($row['Key_name'] == 'PRIMARY') {
288 $this->primary
[] = $row['Column_name'];
293 * Every object in Dia document needs an ID to identify
294 * so, we used a static variable to keep the things unique
296 PMA_Dia_Relation_Schema
::$objectId +
= 1;
297 $this->tableId
= PMA_Dia_Relation_Schema
::$objectId;
303 * Tables are generated using object type Database - Table
304 * primary fields are underlined in tables. Dia object
305 * is used to generate the XML of Dia Document. Database Table
306 * Object and their attributes are involved in the combination
307 * of displaing Database - Table on Dia Document.
309 * @param boolean $changeColor Whether to show color for tables text or not
310 * if changeColor is true then an array of $listOfColors will be used to choose
311 * the random colors for tables text we can change/add more colors to this array
315 * @global object The current Dia document
320 public function tableDraw($changeColor)
325 $listOfColors = array(
330 shuffle($listOfColors);
331 $this->tableColor
= '#' . $listOfColors[0] . '';
333 $this->tableColor
= '#000000';
338 $dia->startElement('dia:object');
339 $dia->writeAttribute('type', 'Database - Table');
340 $dia->writeAttribute('version', '0');
341 $dia->writeAttribute('id', '' . $this->tableId
. '');
343 '<dia:attribute name="obj_pos">
345 . ($this->x
* $factor) . ',' . ($this->y
* $factor) . '"/>
347 <dia:attribute name="obj_bb">
348 <dia:rectangle val="'
349 .($this->x
* $factor) . ',' . ($this->y
* $factor) . ';9.97,9.2"/>
351 <dia:attribute name="meta">
352 <dia:composite type="dict"/>
354 <dia:attribute name="elem_corner">
356 . ($this->x
* $factor) . ',' . ($this->y
* $factor) . '"/>
358 <dia:attribute name="elem_width">
359 <dia:real val="5.9199999999999999"/>
361 <dia:attribute name="elem_height">
362 <dia:real val="3.5"/>
364 <dia:attribute name="text_colour">
365 <dia:color val="' . $this->tableColor
. '"/>
367 <dia:attribute name="line_colour">
368 <dia:color val="#000000"/>
370 <dia:attribute name="fill_colour">
371 <dia:color val="#ffffff"/>
373 <dia:attribute name="line_width">
374 <dia:real val="0.10000000000000001"/>
376 <dia:attribute name="name">
377 <dia:string>#' . $this->tableName
. '#</dia:string>
379 <dia:attribute name="comment">
380 <dia:string>##</dia:string>
382 <dia:attribute name="visible_comment">
383 <dia:boolean val="false"/>
385 <dia:attribute name="tagging_comment">
386 <dia:boolean val="false"/>
388 <dia:attribute name="underline_primary_key">
389 <dia:boolean val="true"/>
391 <dia:attribute name="bold_primary_keys">
392 <dia:boolean val="true"/>
394 <dia:attribute name="normal_font">
395 <dia:font family="monospace" style="0" name="Courier"/>
397 <dia:attribute name="name_font">
398 <dia:font family="sans" style="80" name="Helvetica-Bold"/>
400 <dia:attribute name="comment_font">
401 <dia:font family="sans" style="0" name="Helvetica"/>
403 <dia:attribute name="normal_font_height">
404 <dia:real val="0.80000000000000004"/>
406 <dia:attribute name="name_font_height">
407 <dia:real val="0.69999999999999996"/>
409 <dia:attribute name="comment_font_height">
410 <dia:real val="0.69999999999999996"/>
414 $dia->startElement('dia:attribute');
415 $dia->writeAttribute('name', 'attributes');
417 foreach ($this->fields
as $field) {
419 '<dia:composite type="table_attribute">
420 <dia:attribute name="name">
421 <dia:string>#' . $field . '#</dia:string>
423 <dia:attribute name="type">
424 <dia:string>##</dia:string>
426 <dia:attribute name="comment">
427 <dia:string>##</dia:string>
432 if (in_array($field, $this->primary
)) {
435 if ($field == $this->displayfield
) {
439 '<dia:attribute name="primary_key">
440 <dia:boolean val="' . $pm . '"/>
442 <dia:attribute name="nullable">
443 <dia:boolean val="false"/>
445 <dia:attribute name="unique">
446 <dia:boolean val="' . $pm . '"/>
457 * Relation preferences/statistics
459 * This class fetches the table master and foreign fields positions
460 * and helps in generating the Table references and then connects
461 * master table's master field to foreign table's foreign key
462 * in dia XML document.
464 * @name Relation_Stats
472 public $srcConnPointsRight;
473 public $srcConnPointsLeft;
474 public $destConnPointsRight;
475 public $destConnPointsLeft;
476 public $masterTableId;
477 public $foreignTableId;
478 public $masterTablePos;
479 public $foreignTablePos;
480 public $referenceColor;
483 * The "Relation_Stats" constructor
485 * @param string $master_table The master table name
486 * @param string $master_field The relation field in the master table
487 * @param string $foreign_table The foreign table name
488 * @param string $foreign_field The relation field in the foreign table
492 * @see Relation_Stats::_getXy
494 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
496 $src_pos = $this->_getXy($master_table, $master_field);
497 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
498 $this->srcConnPointsLeft
= $src_pos[0];
499 $this->srcConnPointsRight
= $src_pos[1];
500 $this->destConnPointsLeft
= $dest_pos[0];
501 $this->destConnPointsRight
= $dest_pos[1];
502 $this->masterTablePos
= $src_pos[2];
503 $this->foreignTablePos
= $dest_pos[2];
504 $this->masterTableId
= $master_table->tableId
;
505 $this->foreignTableId
= $foreign_table->tableId
;
509 * Each Table object have connection points
510 * which is used to connect to other objects in Dia
511 * we detect the position of key in fields and
512 * then determines its left and right connection
515 * @param string $table The current table name
516 * @param string $column The relation column name
518 * @return array Table right,left connection points and key position
522 private function _getXy($table, $column)
524 $pos = array_search($column, $table->fields
);
525 // left, right, position
528 return array($pos +
$value +
$pos, $pos +
$value +
$pos +
1, $pos);
530 return array($pos +
$value , $pos +
$value +
1, $pos);
534 * Draws relation references
536 * connects master table's master field to foreign table's
537 * forein field using Dia object type Database - Reference
538 * Dia object is used to generate the XML of Dia Document.
539 * Database reference Object and their attributes are involved
540 * in the combination of displaing Database - reference on Dia Document.
542 * @param boolean $changeColor Whether to use one color per relation or not
543 * if changeColor is true then an array of $listOfColors will be used to choose
544 * the random colors for references lines. we can change/add more colors to this
548 * @global object The current Dia document
553 public function relationDraw($changeColor)
557 PMA_Dia_Relation_Schema
::$objectId +
= 1;
559 * if source connection points and destination connection
560 * points are same then return it false and don't draw that
563 if ( $this->srcConnPointsRight
== $this->destConnPointsRight
) {
564 if ( $this->srcConnPointsLeft
== $this->destConnPointsLeft
) {
570 $listOfColors = array(
575 shuffle($listOfColors);
576 $this->referenceColor
= '#' . $listOfColors[0] . '';
578 $this->referenceColor
= '#000000';
582 '<dia:object type="Database - Reference" version="0" id="'
583 . PMA_Dia_Relation_Schema
::$objectId . '">
584 <dia:attribute name="obj_pos">
585 <dia:point val="3.27,18.9198"/>
587 <dia:attribute name="obj_bb">
588 <dia:rectangle val="2.27,8.7175;17.7679,18.9198"/>
590 <dia:attribute name="meta">
591 <dia:composite type="dict"/>
593 <dia:attribute name="orth_points">
594 <dia:point val="3.27,18.9198"/>
595 <dia:point val="2.27,18.9198"/>
596 <dia:point val="2.27,14.1286"/>
597 <dia:point val="17.7679,14.1286"/>
598 <dia:point val="17.7679,9.3375"/>
599 <dia:point val="16.7679,9.3375"/>
601 <dia:attribute name="orth_orient">
608 <dia:attribute name="orth_autoroute">
609 <dia:boolean val="true"/>
611 <dia:attribute name="text_colour">
612 <dia:color val="#000000"/>
614 <dia:attribute name="line_colour">
615 <dia:color val="' . $this->referenceColor
. '"/>
617 <dia:attribute name="line_width">
618 <dia:real val="0.10000000000000001"/>
620 <dia:attribute name="line_style">
624 <dia:attribute name="corner_radius">
627 <dia:attribute name="end_arrow">
630 <dia:attribute name="end_arrow_length">
631 <dia:real val="0.5"/>
633 <dia:attribute name="end_arrow_width">
634 <dia:real val="0.5"/>
636 <dia:attribute name="start_point_desc">
637 <dia:string>#1#</dia:string>
639 <dia:attribute name="end_point_desc">
640 <dia:string>#n#</dia:string>
642 <dia:attribute name="normal_font">
643 <dia:font family="monospace" style="0" name="Courier"/>
645 <dia:attribute name="normal_font_height">
646 <dia:real val="0.59999999999999998"/>
649 <dia:connection handle="0" to="'
650 . $this->masterTableId
. '" connection="'
651 . $this->srcConnPointsRight
. '"/>
652 <dia:connection handle="1" to="'
653 . $this->foreignTableId
. '" connection="'
654 . $this->destConnPointsRight
. '"/>
662 * Dia Relation Schema Class
664 * Purpose of this class is to generate the Dia XML Document
665 * which is used for representing the database diagrams in Dia IDE
666 * This class uses Database Table and Reference Objects of Dia and with
667 * the combination of these objects actually helps in preparing Dia XML.
669 * Dia XML is generated by using XMLWriter php extension and this class
670 * inherits Export_Relation_Schema class has common functionality added
673 * @name Dia_Relation_Schema
675 class PMA_Dia_Relation_Schema
extends PMA_Export_Relation_Schema
680 private $_tables = array();
681 private $_relations = array();
682 private $_topMargin = 2.8222000598907471;
683 private $_bottomMargin = 2.8222000598907471;
684 private $_leftMargin = 2.8222000598907471;
685 private $_rightMargin = 2.8222000598907471;
686 public static $objectId = 0;
689 * The "PMA_Dia_Relation_Schema" constructor
691 * Upon instantiation This outputs the Dia XML document
692 * that user can download
695 * @see PMA_DIA,Table_Stats,Relation_Stats
697 function __construct()
701 $this->setPageNumber($_POST['pdf_page_number']);
702 $this->setShowGrid(isset($_POST['show_grid']));
703 $this->setShowColor($_POST['show_color']);
704 $this->setShowKeys(isset($_POST['show_keys']));
705 $this->setOrientation(isset($_POST['orientation']));
706 $this->setPaper($_POST['paper']);
707 $this->setExportType($_POST['export_type']);
709 $dia = new PMA_DIA();
711 $this->paper
, $this->_topMargin
, $this->_bottomMargin
,
712 $this->_leftMargin
, $this->_rightMargin
, $this->orientation
714 $alltables = $this->getAllTables($db, $this->pageNumber
);
715 foreach ($alltables as $table) {
716 if (! isset($this->tables
[$table])) {
717 $this->tables
[$table] = new Table_Stats(
718 $table, $this->pageNumber
, $this->showKeys
723 $seen_a_relation = false;
724 foreach ($alltables as $one_table) {
725 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
727 $seen_a_relation = true;
728 foreach ($exist_rel as $master_field => $rel) {
729 /* put the foreign table on the schema only if selected
731 * (do not use array_search() because we would have to
732 * to do a === false and this is not PHP3 compatible)
734 if (in_array($rel['foreign_table'], $alltables)) {
736 $one_table, $master_field, $rel['foreign_table'],
737 $rel['foreign_field'], $this->showKeys
743 $this->_drawTables($this->showColor
);
745 if ($seen_a_relation) {
746 $this->_drawRelations($this->showColor
);
749 $dia->showOutput($db . '-' . $this->pageNumber
);
754 * Defines relation objects
756 * @param string $masterTable The master table name
757 * @param string $masterField The relation field in the master table
758 * @param string $foreignTable The foreign table name
759 * @param string $foreignField The relation field in the foreign table
760 * @param bool $showKeys Whether to display ONLY keys or not
765 * @see Table_Stats::__construct(),Relation_Stats::__construct()
767 private function _addRelation($masterTable, $masterField, $foreignTable, $foreignField, $showKeys)
769 if (! isset($this->tables
[$masterTable])) {
770 $this->tables
[$masterTable] = new Table_Stats(
771 $masterTable, $this->pageNumber
, $showKeys
774 if (! isset($this->tables
[$foreignTable])) {
775 $this->tables
[$foreignTable] = new Table_Stats(
776 $foreignTable, $this->pageNumber
, $showKeys
779 $this->_relations
[] = new Relation_Stats(
780 $this->tables
[$masterTable], $masterField,
781 $this->tables
[$foreignTable], $foreignField
786 * Draws relation references
788 * connects master table's master field to
789 * foreign table's forein field using Dia object
790 * type Database - Reference
792 * @param boolean $changeColor Whether to use one color per relation or not
797 * @see Relation_Stats::relationDraw()
799 private function _drawRelations($changeColor)
801 foreach ($this->_relations
as $relation) {
802 $relation->relationDraw($changeColor);
809 * Tables are generated using Dia object type Database - Table
810 * primary fields are underlined and bold in tables
812 * @param boolean $changeColor Whether to show color for tables text or not
817 * @see Table_Stats::tableDraw()
819 private function _drawTables($changeColor)
821 foreach ($this->tables
as $table) {
822 $table->tableDraw($changeColor);