Translation update done using Pootle.
[phpmyadmin/dkf.git] / libraries / schema / Dia_Relation_Schema.class.php
blobaa5f43c7f58cc9e4505c5e504aea963e5e00a7da
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @version $Id$
6 * @package phpMyAdmin
7 */
9 include_once("Export_Relation_Schema.class.php");
11 /**
12 * This Class inherits the XMLwriter class and
13 * helps in developing structure of DIA Schema Export
15 * @name PMA_DIA
16 * @copyright
17 * @license
18 * @access public
19 * @see http://php.net/manual/en/book.xmlwriter.php
22 class PMA_DIA extends XMLWriter
24 public $title;
25 public $author;
26 public $font;
27 public $fontSize;
29 /**
30 * The "PMA_DIA" constructor
32 * Upon instantiation This starts writing the Dia XML document
34 * @return void
35 * @see XMLWriter::openMemory(),XMLWriter::setIndent(),XMLWriter::startDocument()
37 function __construct()
39 $this->openMemory();
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');
54 /**
55 * Starts Dia Document
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
68 * @return void
69 * @access public
70 * @see XMLWriter::startElement(),XMLWriter::writeAttribute(),XMLWriter::writeRaw()
72 function startDiaDoc($paper,$topMargin,$bottomMargin,$leftMargin,$rightMargin,$portrait)
74 if($portrait == 'P'){
75 $isPortrait='true';
76 }else{
77 $isPortrait='false';
79 $this->startElement('dia:diagram');
80 $this->writeAttribute('xmlns:dia', 'http://www.lysator.liu.se/~alla/dia/');
81 $this->startElement('dia:diagramdata');
82 $this->writeRaw (
83 '<dia:attribute name="background">
84 <dia:color val="#ffffff"/>
85 </dia:attribute>
86 <dia:attribute name="pagebreak">
87 <dia:color val="#000099"/>
88 </dia:attribute>
89 <dia:attribute name="paper">
90 <dia:composite type="paper">
91 <dia:attribute name="name">
92 <dia:string>#'.$paper.'#</dia:string>
93 </dia:attribute>
94 <dia:attribute name="tmargin">
95 <dia:real val="'.$topMargin.'"/>
96 </dia:attribute>
97 <dia:attribute name="bmargin">
98 <dia:real val="'.$bottomMargin.'"/>
99 </dia:attribute>
100 <dia:attribute name="lmargin">
101 <dia:real val="'.$leftMargin.'"/>
102 </dia:attribute>
103 <dia:attribute name="rmargin">
104 <dia:real val="'.$rightMargin.'"/>
105 </dia:attribute>
106 <dia:attribute name="is_portrait">
107 <dia:boolean val="'.$isPortrait.'"/>
108 </dia:attribute>
109 <dia:attribute name="scaling">
110 <dia:real val="1"/>
111 </dia:attribute>
112 <dia:attribute name="fitto">
113 <dia:boolean val="false"/>
114 </dia:attribute>
115 </dia:composite>
116 </dia:attribute>
117 <dia:attribute name="grid">
118 <dia:composite type="grid">
119 <dia:attribute name="width_x">
120 <dia:real val="1"/>
121 </dia:attribute>
122 <dia:attribute name="width_y">
123 <dia:real val="1"/>
124 </dia:attribute>
125 <dia:attribute name="visible_x">
126 <dia:int val="1"/>
127 </dia:attribute>
128 <dia:attribute name="visible_y">
129 <dia:int val="1"/>
130 </dia:attribute>
131 <dia:composite type="color"/>
132 </dia:composite>
133 </dia:attribute>
134 <dia:attribute name="color">
135 <dia:color val="#d8e5e5"/>
136 </dia:attribute>
137 <dia:attribute name="guides">
138 <dia:composite type="guides">
139 <dia:attribute name="hguides"/>
140 <dia:attribute name="vguides"/>
141 </dia:composite>
142 </dia:attribute>');
143 $this->endElement();
144 $this->startElement('dia:layer');
145 $this->writeAttribute('name', 'Background');
146 $this->writeAttribute('visible', 'true');
147 $this->writeAttribute('active', 'true');
152 * Ends Dia Document
154 * @return void
155 * @access public
156 * @see XMLWriter::endElement(),XMLWriter::endDocument()
158 function endDiaDoc()
160 $this->endElement();
161 $this->endDocument();
165 * Output Dia Document for download
167 * @param string fileName name of the dia document
168 * @return void
169 * @access public
170 * @see XMLWriter::flush()
172 function showOutput($fileName)
174 if(ob_get_clean()){
175 ob_end_clean();
177 header('Content-type: application/x-dia-diagram');
178 header('Content-Disposition: attachment; filename="'.$fileName.'.dia"');
179 $output = $this->flush();
180 print $output;
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.
190 * @name Table_Stats
191 * @copyright
192 * @license
193 * @see PMA_DIA
195 class Table_Stats
198 * Defines properties
200 public $tableName;
201 public $fields = array();
202 public $x, $y;
203 public $primary = array();
204 public $tableId;
205 public $tableColor;
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
214 * @return void
215 * @global object The current dia document
216 * @global array The relations settings
217 * @global string The current db name
218 * @see PMA_DIA
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));
231 * load fields
232 * check to see if it will load all fields or only the foreign keys
234 if ($showKeys) {
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);
241 } else {
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;
260 * displayfield
262 $this->displayfield = PMA_getDisplayField($db, $tableName);
264 * index
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;
283 * Do draw the table
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
295 @return void
296 * @global object The current Dia document
297 * @access public
298 * @see PMA_DIA
300 public function tableDraw($changeColor)
302 global $dia;
304 if ($changeColor) {
305 $listOfColors = array(
306 'FF0000',
307 '000099',
308 '00FF00'
310 shuffle($listOfColors);
311 $this->tableColor = '#'.$listOfColors[0].'';
312 } else {
313 $this->tableColor = '#000000';
316 $factor = 0.1;
318 $dia->startElement('dia:object');
319 $dia->writeAttribute('type', 'Database - Table');
320 $dia->writeAttribute('version', '0');
321 $dia->writeAttribute('id', ''.$this->tableId.'');
322 $dia->writeRaw(
323 '<dia:attribute name="obj_pos">
324 <dia:point val="'.($this->x * $factor).','.($this->y * $factor).'"/>
325 </dia:attribute>
326 <dia:attribute name="obj_bb">
327 <dia:rectangle val="'.($this->x * $factor).','.($this->y * $factor).';9.97,9.2"/>
328 </dia:attribute>
329 <dia:attribute name="meta">
330 <dia:composite type="dict"/>
331 </dia:attribute>
332 <dia:attribute name="elem_corner">
333 <dia:point val="'.($this->x * $factor).','.($this->y * $factor).'"/>
334 </dia:attribute>
335 <dia:attribute name="elem_width">
336 <dia:real val="5.9199999999999999"/>
337 </dia:attribute>
338 <dia:attribute name="elem_height">
339 <dia:real val="3.5"/>
340 </dia:attribute>
341 <dia:attribute name="text_colour">
342 <dia:color val="'.$this->tableColor.'"/>
343 </dia:attribute>
344 <dia:attribute name="line_colour">
345 <dia:color val="#000000"/>
346 </dia:attribute>
347 <dia:attribute name="fill_colour">
348 <dia:color val="#ffffff"/>
349 </dia:attribute>
350 <dia:attribute name="line_width">
351 <dia:real val="0.10000000000000001"/>
352 </dia:attribute>
353 <dia:attribute name="name">
354 <dia:string>#'.$this->tableName.'#</dia:string>
355 </dia:attribute>
356 <dia:attribute name="comment">
357 <dia:string>##</dia:string>
358 </dia:attribute>
359 <dia:attribute name="visible_comment">
360 <dia:boolean val="false"/>
361 </dia:attribute>
362 <dia:attribute name="tagging_comment">
363 <dia:boolean val="false"/>
364 </dia:attribute>
365 <dia:attribute name="underline_primary_key">
366 <dia:boolean val="true"/>
367 </dia:attribute>
368 <dia:attribute name="bold_primary_keys">
369 <dia:boolean val="true"/>
370 </dia:attribute>
371 <dia:attribute name="normal_font">
372 <dia:font family="monospace" style="0" name="Courier"/>
373 </dia:attribute>
374 <dia:attribute name="name_font">
375 <dia:font family="sans" style="80" name="Helvetica-Bold"/>
376 </dia:attribute>
377 <dia:attribute name="comment_font">
378 <dia:font family="sans" style="0" name="Helvetica"/>
379 </dia:attribute>
380 <dia:attribute name="normal_font_height">
381 <dia:real val="0.80000000000000004"/>
382 </dia:attribute>
383 <dia:attribute name="name_font_height">
384 <dia:real val="0.69999999999999996"/>
385 </dia:attribute>
386 <dia:attribute name="comment_font_height">
387 <dia:real val="0.69999999999999996"/>
388 </dia:attribute>'
391 $dia->startElement('dia:attribute');
392 $dia->writeAttribute('name', 'attributes');
394 foreach ($this->fields as $field) {
395 $dia->writeRaw(
396 '<dia:composite type="table_attribute">
397 <dia:attribute name="name">
398 <dia:string>#'.$field.'#</dia:string>
399 </dia:attribute>
400 <dia:attribute name="type">
401 <dia:string>##</dia:string>
402 </dia:attribute>
403 <dia:attribute name="comment">
404 <dia:string>##</dia:string>
405 </dia:attribute>'
407 unset($pm);
408 $pm = 'false';
409 if (in_array($field, $this->primary)) {
410 $pm = 'true';
412 if ($field == $this->displayfield) {
413 $pm = 'false';
415 $dia->writeRaw(
416 '<dia:attribute name="primary_key">
417 <dia:boolean val="'.$pm.'"/>
418 </dia:attribute>
419 <dia:attribute name="nullable">
420 <dia:boolean val="false"/>
421 </dia:attribute>
422 <dia:attribute name="unique">
423 <dia:boolean val="'.$pm.'"/>
424 </dia:attribute>
425 </dia:composite>'
428 $dia->endElement();
429 $dia->endElement();
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
442 * @copyright
443 * @license
444 * @see PMA_DIA
446 class Relation_Stats
449 * Defines properties
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
468 * @return void
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
490 * points.
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
495 * @access private
497 private function _getXy($table, $column)
499 $pos = array_search($column, $table->fields);
500 // left, right, position
501 $value = 12;
502 if($pos != 0)
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
522 * @return void
523 * @global object The current Dia document
524 * @access public
525 * @see PMA_PDF
527 public function relationDraw($changeColor)
529 global $dia;
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
535 * relation
537 if ( $this->srcConnPointsRight == $this->destConnPointsRight ){
538 if ( $this->srcConnPointsLeft == $this->destConnPointsLeft ){
539 return false;
543 if ($changeColor) {
544 $listOfColors = array(
545 'FF0000',
546 '000099',
547 '00FF00'
549 shuffle($listOfColors);
550 $this->referenceColor = '#'.$listOfColors[0].'';
551 } else {
552 $this->referenceColor = '#000000';
555 $dia->writeRaw(
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"/>
559 </dia:attribute>
560 <dia:attribute name="obj_bb">
561 <dia:rectangle val="2.27,8.7175;17.7679,18.9198"/>
562 </dia:attribute>
563 <dia:attribute name="meta">
564 <dia:composite type="dict"/>
565 </dia:attribute>
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"/>
573 </dia:attribute>
574 <dia:attribute name="orth_orient">
575 <dia:enum val="0"/>
576 <dia:enum val="1"/>
577 <dia:enum val="0"/>
578 <dia:enum val="1"/>
579 <dia:enum val="0"/>
580 </dia:attribute>
581 <dia:attribute name="orth_autoroute">
582 <dia:boolean val="true"/>
583 </dia:attribute>
584 <dia:attribute name="text_colour">
585 <dia:color val="#000000"/>
586 </dia:attribute>
587 <dia:attribute name="line_colour">
588 <dia:color val="'.$this->referenceColor.'"/>
589 </dia:attribute>
590 <dia:attribute name="line_width">
591 <dia:real val="0.10000000000000001"/>
592 </dia:attribute>
593 <dia:attribute name="line_style">
594 <dia:enum val="0"/>
595 <dia:real val="1"/>
596 </dia:attribute>
597 <dia:attribute name="corner_radius">
598 <dia:real val="0"/>
599 </dia:attribute>
600 <dia:attribute name="end_arrow">
601 <dia:enum val="22"/>
602 </dia:attribute>
603 <dia:attribute name="end_arrow_length">
604 <dia:real val="0.5"/>
605 </dia:attribute>
606 <dia:attribute name="end_arrow_width">
607 <dia:real val="0.5"/>
608 </dia:attribute>
609 <dia:attribute name="start_point_desc">
610 <dia:string>#1#</dia:string>
611 </dia:attribute>
612 <dia:attribute name="end_point_desc">
613 <dia:string>#n#</dia:string>
614 </dia:attribute>
615 <dia:attribute name="normal_font">
616 <dia:font family="monospace" style="0" name="Courier"/>
617 </dia:attribute>
618 <dia:attribute name="normal_font_height">
619 <dia:real val="0.59999999999999998"/>
620 </dia:attribute>
621 <dia:connections>
622 <dia:connection handle="0" to="'.$this->masterTableId.'" connection="'.$this->srcConnPointsRight.'"/>
623 <dia:connection handle="1" to="'.$this->foreignTableId.'" connection="'.$this->destConnPointsRight.'"/>
624 </dia:connections>
625 </dia:object>'
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
640 * to this class
642 * @name Dia_Relation_Schema
643 * @copyright
644 * @license
646 class PMA_Dia_Relation_Schema extends PMA_Export_Relation_Schema
649 * Defines properties
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
665 * @return void
666 * @see PMA_DIA,Table_Stats,Relation_Stats
668 function __construct()
670 global $dia,$db;
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');
692 if ($exist_rel) {
693 $seen_a_relation = true;
694 foreach ($exist_rel as $master_field => $rel) {
695 /* put the foreign table on the schema only if selected
696 * by the user
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);
711 $dia->endDiaDoc();
712 $dia->showOutput($db.'-'.$this->pageNumber);
713 exit();
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
723 * @return void
724 * @access private
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
746 * @return void
747 * @access private
748 * @see Relation_Stats::relationDraw()
750 private function _drawRelations($changeColor)
752 foreach ($this->_relations as $relation) {
753 $relation->relationDraw($changeColor);
758 * Draws tables
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
764 * @return void
765 * @access private
766 * @see Table_Stats::tableDraw()
768 private function _drawTables($changeColor)
770 foreach ($this->tables as $table) {
771 $table->tableDraw($changeColor);