2 /* vim: set expandtab sw=4 ts=4 sts=4: */
9 include_once("Export_Relation_Schema.class.php");
12 * This Class inherits the XMLwriter class and
13 * helps in developing structure of DIA Schema Export
19 * @see http://php.net/manual/en/book.xmlwriter.php
22 class PMA_DIA
extends XMLWriter
30 * The "PMA_DIA" constructor
32 * Upon instantiation This starts writing the Dia XML document
35 * @see XMLWriter::openMemory(),XMLWriter::setIndent(),XMLWriter::startDocument()
37 function __construct()
41 * Set indenting using three spaces,
42 * so output is formatted
45 $this->setIndent(TRUE);
46 $this->setIndentString(' ');
48 * Create the XML document
51 $this->startDocument('1.0','UTF-8');
57 * dia document starts by first initializing dia:diagram tag
58 * then dia:diagramdata contains all the attributes that needed
59 * to define the document, then finally a Layer starts which
60 * holds all the objects.
62 * @param string paper The size of the paper/document
63 * @param float topMargin top margin of the paper/document in cm
64 * @param float bottomMargin bottom margin of the paper/document in cm
65 * @param float leftMargin left margin of the paper/document in cm
66 * @param float rightMargin right margin of the paper/document in cm
67 * @param string portrait document will be portrait or landscape
70 * @see XMLWriter::startElement(),XMLWriter::writeAttribute(),XMLWriter::writeRaw()
72 function startDiaDoc($paper,$topMargin,$bottomMargin,$leftMargin,$rightMargin,$portrait)
79 $this->startElement('dia:diagram');
80 $this->writeAttribute('xmlns:dia', 'http://www.lysator.liu.se/~alla/dia/');
81 $this->startElement('dia:diagramdata');
83 '<dia:attribute name="background">
84 <dia:color val="#ffffff"/>
86 <dia:attribute name="pagebreak">
87 <dia:color val="#000099"/>
89 <dia:attribute name="paper">
90 <dia:composite type="paper">
91 <dia:attribute name="name">
92 <dia:string>#'.$paper.'#</dia:string>
94 <dia:attribute name="tmargin">
95 <dia:real val="'.$topMargin.'"/>
97 <dia:attribute name="bmargin">
98 <dia:real val="'.$bottomMargin.'"/>
100 <dia:attribute name="lmargin">
101 <dia:real val="'.$leftMargin.'"/>
103 <dia:attribute name="rmargin">
104 <dia:real val="'.$rightMargin.'"/>
106 <dia:attribute name="is_portrait">
107 <dia:boolean val="'.$isPortrait.'"/>
109 <dia:attribute name="scaling">
112 <dia:attribute name="fitto">
113 <dia:boolean val="false"/>
117 <dia:attribute name="grid">
118 <dia:composite type="grid">
119 <dia:attribute name="width_x">
122 <dia:attribute name="width_y">
125 <dia:attribute name="visible_x">
128 <dia:attribute name="visible_y">
131 <dia:composite type="color"/>
134 <dia:attribute name="color">
135 <dia:color val="#d8e5e5"/>
137 <dia:attribute name="guides">
138 <dia:composite type="guides">
139 <dia:attribute name="hguides"/>
140 <dia:attribute name="vguides"/>
144 $this->startElement('dia:layer');
145 $this->writeAttribute('name', 'Background');
146 $this->writeAttribute('visible', 'true');
147 $this->writeAttribute('active', 'true');
156 * @see XMLWriter::endElement(),XMLWriter::endDocument()
161 $this->endDocument();
165 * Output Dia Document for download
167 * @param string fileName name of the dia document
170 * @see XMLWriter::flush()
172 function showOutput($fileName)
177 header('Content-type: application/x-dia-diagram');
178 header('Content-Disposition: attachment; filename="'.$fileName.'.dia"');
179 $output = $this->flush();
185 * Table preferences/statistics
187 * This class preserves the table co-ordinates,fields
188 * and helps in drawing/generating the Tables in dia XML document.
201 public $fields = array();
203 public $primary = array();
208 * The "Table_Stats" constructor
210 * @param string table_name The table name
211 * @param integer pageNumber The current page number (from the
212 * $cfg['Servers'][$i]['table_coords'] table)
213 * @param boolean showKeys Whether to display ONLY keys or not
215 * @global object The current dia document
216 * @global array The relations settings
217 * @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)) {
228 $dia->dieSchema($pageNumber,"DIA",sprintf(__('The %s table doesn\'t exist!'), $tableName));
232 * check to see if it will load all fields or only the foreign keys
235 $indexes = PMA_Index
::getFromTable($this->tableName
, $db);
236 $all_columns = array();
237 foreach ($indexes as $index) {
238 $all_columns = array_merge($all_columns, array_flip(array_keys($index->getColumns())));
240 $this->fields
= array_keys($all_columns);
242 while ($row = PMA_DBI_fetch_row($result)) {
243 $this->fields
[] = $row[0];
247 $sql = 'SELECT x, y FROM '
248 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
249 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
250 . ' AND table_name = \'' . PMA_sqlAddslashes($tableName) . '\''
251 . ' AND pdf_page_number = ' . $pageNumber;
252 $result = PMA_query_as_controluser($sql, false, PMA_DBI_QUERY_STORE
);
253 if (!$result ||
!PMA_DBI_num_rows($result)) {
254 $dia->dieSchema($pageNumber,"DIA",sprintf(__('Please configure the coordinates for table %s'), $tableName));
256 list($this->x
, $this->y
) = PMA_DBI_fetch_row($result);
257 $this->x
= (double) $this->x
;
258 $this->y
= (double) $this->y
;
262 $this->displayfield
= PMA_getDisplayField($db, $tableName);
266 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tableName) . ';', null, PMA_DBI_QUERY_STORE
);
267 if (PMA_DBI_num_rows($result) > 0) {
268 while ($row = PMA_DBI_fetch_assoc($result)) {
269 if ($row['Key_name'] == 'PRIMARY') {
270 $this->primary
[] = $row['Column_name'];
275 * Every object in Dia document needs an ID to identify
276 * so, we used a static variable to keep the things unique
278 PMA_Dia_Relation_Schema
::$objectId +
= 1;
279 $this->tableId
= PMA_Dia_Relation_Schema
::$objectId;
285 * Tables are generated using object type Database - Table
286 * primary fields are underlined in tables. Dia object
287 * is used to generate the XML of Dia Document. Database Table
288 * Object and their attributes are involved in the combination
289 * of displaing Database - Table on Dia Document.
291 * @param boolean changeColor Whether to show color for tables text or not
292 if changeColor is true then an array of $listOfColors
293 will be used to choose the random colors for tables text
294 we can change/add more colors to this array
296 * @global object The current Dia document
300 public function tableDraw($changeColor)
305 $listOfColors = array(
310 shuffle($listOfColors);
311 $this->tableColor
= '#'.$listOfColors[0].'';
313 $this->tableColor
= '#000000';
318 $dia->startElement('dia:object');
319 $dia->writeAttribute('type', 'Database - Table');
320 $dia->writeAttribute('version', '0');
321 $dia->writeAttribute('id', ''.$this->tableId
.'');
323 '<dia:attribute name="obj_pos">
324 <dia:point val="'.($this->x
* $factor).','.($this->y
* $factor).'"/>
326 <dia:attribute name="obj_bb">
327 <dia:rectangle val="'.($this->x
* $factor).','.($this->y
* $factor).';9.97,9.2"/>
329 <dia:attribute name="meta">
330 <dia:composite type="dict"/>
332 <dia:attribute name="elem_corner">
333 <dia:point val="'.($this->x
* $factor).','.($this->y
* $factor).'"/>
335 <dia:attribute name="elem_width">
336 <dia:real val="5.9199999999999999"/>
338 <dia:attribute name="elem_height">
339 <dia:real val="3.5"/>
341 <dia:attribute name="text_colour">
342 <dia:color val="'.$this->tableColor
.'"/>
344 <dia:attribute name="line_colour">
345 <dia:color val="#000000"/>
347 <dia:attribute name="fill_colour">
348 <dia:color val="#ffffff"/>
350 <dia:attribute name="line_width">
351 <dia:real val="0.10000000000000001"/>
353 <dia:attribute name="name">
354 <dia:string>#'.$this->tableName
.'#</dia:string>
356 <dia:attribute name="comment">
357 <dia:string>##</dia:string>
359 <dia:attribute name="visible_comment">
360 <dia:boolean val="false"/>
362 <dia:attribute name="tagging_comment">
363 <dia:boolean val="false"/>
365 <dia:attribute name="underline_primary_key">
366 <dia:boolean val="true"/>
368 <dia:attribute name="bold_primary_keys">
369 <dia:boolean val="true"/>
371 <dia:attribute name="normal_font">
372 <dia:font family="monospace" style="0" name="Courier"/>
374 <dia:attribute name="name_font">
375 <dia:font family="sans" style="80" name="Helvetica-Bold"/>
377 <dia:attribute name="comment_font">
378 <dia:font family="sans" style="0" name="Helvetica"/>
380 <dia:attribute name="normal_font_height">
381 <dia:real val="0.80000000000000004"/>
383 <dia:attribute name="name_font_height">
384 <dia:real val="0.69999999999999996"/>
386 <dia:attribute name="comment_font_height">
387 <dia:real val="0.69999999999999996"/>
391 $dia->startElement('dia:attribute');
392 $dia->writeAttribute('name', 'attributes');
394 foreach ($this->fields
as $field) {
396 '<dia:composite type="table_attribute">
397 <dia:attribute name="name">
398 <dia:string>#'.$field.'#</dia:string>
400 <dia:attribute name="type">
401 <dia:string>##</dia:string>
403 <dia:attribute name="comment">
404 <dia:string>##</dia:string>
409 if (in_array($field, $this->primary
)) {
412 if ($field == $this->displayfield
) {
416 '<dia:attribute name="primary_key">
417 <dia:boolean val="'.$pm.'"/>
419 <dia:attribute name="nullable">
420 <dia:boolean val="false"/>
422 <dia:attribute name="unique">
423 <dia:boolean val="'.$pm.'"/>
434 * Relation preferences/statistics
436 * This class fetches the table master and foreign fields positions
437 * and helps in generating the Table references and then connects
438 * master table's master field to foreign table's foreign key
439 * in dia XML document.
441 * @name Relation_Stats
451 public $srcConnPointsRight;
452 public $srcConnPointsLeft;
453 public $destConnPointsRight;
454 public $destConnPointsLeft;
455 public $masterTableId;
456 public $foreignTableId;
457 public $masterTablePos;
458 public $foreignTablePos;
459 public $referenceColor;
462 * The "Relation_Stats" constructor
464 * @param string master_table The master table name
465 * @param string master_field The relation field in the master table
466 * @param string foreign_table The foreign table name
467 * @param string foreigh_field The relation field in the foreign table
469 * @see Relation_Stats::_getXy
471 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
473 $src_pos = $this->_getXy($master_table, $master_field);
474 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
475 $this->srcConnPointsLeft
= $src_pos[0];
476 $this->srcConnPointsRight
= $src_pos[1];
477 $this->destConnPointsLeft
= $dest_pos[0];
478 $this->destConnPointsRight
= $dest_pos[1];
479 $this->masterTablePos
= $src_pos[2];
480 $this->foreignTablePos
= $dest_pos[2];
481 $this->masterTableId
= $master_table->tableId
;
482 $this->foreignTableId
= $foreign_table->tableId
;
486 * Each Table object have connection points
487 * which is used to connect to other objects in Dia
488 * we detect the position of key in fields and
489 * then determines its left and right connection
492 * @param string table The current table name
493 * @param string column The relation column name
494 * @return array Table right,left connection points and key position
497 private function _getXy($table, $column)
499 $pos = array_search($column, $table->fields
);
500 // left, right, position
504 return array($pos +
$value +
$pos, $pos +
$value +
$pos +
1, $pos);
506 return array($pos +
$value , $pos +
$value +
1, $pos);
510 * Draws relation references
512 * connects master table's master field to foreign table's
513 * forein field using Dia object type Database - Reference
514 * Dia object is used to generate the XML of Dia Document.
515 * Database reference Object and their attributes are involved
516 * in the combination of displaing Database - reference on Dia Document.
518 * @param boolean changeColor Whether to use one color per relation or not
519 if changeColor is true then an array of $listOfColors
520 will be used to choose the random colors for references
521 lines. we can change/add more colors to this array
523 * @global object The current Dia document
527 public function relationDraw($changeColor)
531 PMA_Dia_Relation_Schema
::$objectId +
= 1;
533 * if source connection points and destination connection
534 * points are same then return it false and don't draw that
537 if ( $this->srcConnPointsRight
== $this->destConnPointsRight
){
538 if ( $this->srcConnPointsLeft
== $this->destConnPointsLeft
){
544 $listOfColors = array(
549 shuffle($listOfColors);
550 $this->referenceColor
= '#'.$listOfColors[0].'';
552 $this->referenceColor
= '#000000';
556 '<dia:object type="Database - Reference" version="0" id="'.PMA_Dia_Relation_Schema
::$objectId.'">
557 <dia:attribute name="obj_pos">
558 <dia:point val="3.27,18.9198"/>
560 <dia:attribute name="obj_bb">
561 <dia:rectangle val="2.27,8.7175;17.7679,18.9198"/>
563 <dia:attribute name="meta">
564 <dia:composite type="dict"/>
566 <dia:attribute name="orth_points">
567 <dia:point val="3.27,18.9198"/>
568 <dia:point val="2.27,18.9198"/>
569 <dia:point val="2.27,14.1286"/>
570 <dia:point val="17.7679,14.1286"/>
571 <dia:point val="17.7679,9.3375"/>
572 <dia:point val="16.7679,9.3375"/>
574 <dia:attribute name="orth_orient">
581 <dia:attribute name="orth_autoroute">
582 <dia:boolean val="true"/>
584 <dia:attribute name="text_colour">
585 <dia:color val="#000000"/>
587 <dia:attribute name="line_colour">
588 <dia:color val="'.$this->referenceColor
.'"/>
590 <dia:attribute name="line_width">
591 <dia:real val="0.10000000000000001"/>
593 <dia:attribute name="line_style">
597 <dia:attribute name="corner_radius">
600 <dia:attribute name="end_arrow">
603 <dia:attribute name="end_arrow_length">
604 <dia:real val="0.5"/>
606 <dia:attribute name="end_arrow_width">
607 <dia:real val="0.5"/>
609 <dia:attribute name="start_point_desc">
610 <dia:string>#1#</dia:string>
612 <dia:attribute name="end_point_desc">
613 <dia:string>#n#</dia:string>
615 <dia:attribute name="normal_font">
616 <dia:font family="monospace" style="0" name="Courier"/>
618 <dia:attribute name="normal_font_height">
619 <dia:real val="0.59999999999999998"/>
622 <dia:connection handle="0" to="'.$this->masterTableId
.'" connection="'.$this->srcConnPointsRight
.'"/>
623 <dia:connection handle="1" to="'.$this->foreignTableId
.'" connection="'.$this->destConnPointsRight
.'"/>
631 * Dia Relation Schema Class
633 * Purpose of this class is to generate the Dia XML Document
634 * which is used for representing the database diagrams in Dia IDE
635 * This class uses Database Table and Reference Objects of Dia and with
636 * the combination of these objects actually helps in preparing Dia XML.
638 * Dia XML is generated by using XMLWriter php extension and this class
639 * inherits Export_Relation_Schema class has common functionality added
642 * @name Dia_Relation_Schema
646 class PMA_Dia_Relation_Schema
extends PMA_Export_Relation_Schema
651 private $_tables = array();
652 private $_relations = array();
653 private $_topMargin = 2.8222000598907471;
654 private $_bottomMargin = 2.8222000598907471;
655 private $_leftMargin = 2.8222000598907471;
656 private $_rightMargin = 2.8222000598907471;
657 public static $objectId = 0;
660 * The "PMA_Dia_Relation_Schema" constructor
662 * Upon instantiation This outputs the Dia XML document
663 * that user can download
666 * @see PMA_DIA,Table_Stats,Relation_Stats
668 function __construct()
672 $this->setPageNumber($_POST['pdf_page_number']);
673 $this->setShowGrid(isset($_POST['show_grid']));
674 $this->setShowColor($_POST['show_color']);
675 $this->setShowKeys(isset($_POST['show_keys']));
676 $this->setOrientation(isset($_POST['orientation']));
677 $this->setPaper($_POST['paper']);
678 $this->setExportType($_POST['export_type']);
680 $dia = new PMA_DIA();
681 $dia->startDiaDoc($this->paper
,$this->_topMargin
,$this->_bottomMargin
,$this->_leftMargin
,$this->_rightMargin
,$this->orientation
);
682 $alltables = $this->getAllTables($db,$this->pageNumber
);
683 foreach ($alltables as $table) {
684 if (!isset($this->tables
[$table])) {
685 $this->tables
[$table] = new Table_Stats($table, $this->pageNumber
, $this->showKeys
);
689 $seen_a_relation = false;
690 foreach ($alltables as $one_table) {
691 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
693 $seen_a_relation = true;
694 foreach ($exist_rel as $master_field => $rel) {
695 /* put the foreign table on the schema only if selected
697 * (do not use array_search() because we would have to
698 * to do a === FALSE and this is not PHP3 compatible)
700 if (in_array($rel['foreign_table'], $alltables)) {
701 $this->_addRelation($one_table, $master_field, $rel['foreign_table'], $rel['foreign_field'],$this->showKeys
);
706 $this->_drawTables($this->showColor
);
708 if ($seen_a_relation) {
709 $this->_drawRelations($this->showColor
);
712 $dia->showOutput($db.'-'.$this->pageNumber
);
717 * Defines relation objects
719 * @param string masterTable The master table name
720 * @param string masterField The relation field in the master table
721 * @param string foreignTable The foreign table name
722 * @param string foreignField The relation field in the foreign table
725 * @see Table_Stats::__construct(),Relation_Stats::__construct()
727 private function _addRelation($masterTable, $masterField, $foreignTable, $foreignField, $showKeys)
729 if (!isset($this->tables
[$masterTable])) {
730 $this->tables
[$masterTable] = new Table_Stats($masterTable, $this->pageNumber
, $showKeys);
732 if (!isset($this->tables
[$foreignTable])) {
733 $this->tables
[$foreignTable] = new Table_Stats($foreignTable, $this->pageNumber
, $showKeys);
735 $this->_relations
[] = new Relation_Stats($this->tables
[$masterTable], $masterField, $this->tables
[$foreignTable], $foreignField);
739 * Draws relation references
741 * connects master table's master field to
742 * foreign table's forein field using Dia object
743 * type Database - Reference
745 * @param boolean changeColor Whether to use one color per relation or not
748 * @see Relation_Stats::relationDraw()
750 private function _drawRelations($changeColor)
752 foreach ($this->_relations
as $relation) {
753 $relation->relationDraw($changeColor);
760 * Tables are generated using Dia object type Database - Table
761 * primary fields are underlined and bold in tables
763 * @param boolean changeColor Whether to show color for tables text or not
766 * @see Table_Stats::tableDraw()
768 private function _drawTables($changeColor)
770 foreach ($this->tables
as $table) {
771 $table->tableDraw($changeColor);