Fixed: Not selecting a datalabel used to issue a notice(undefined offset)
[phpmyadmin/ammaryasirr.git] / libraries / schema / Dia_Relation_Schema.class.php
blobfa2884d977523f6c3d6c62a0be0fd88ff5f20881
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @package phpMyAdmin
6 */
8 include_once("Export_Relation_Schema.class.php");
10 /**
11 * This Class inherits the XMLwriter class and
12 * helps in developing structure of DIA Schema Export
14 * @access public
15 * @see http://php.net/manual/en/book.xmlwriter.php
18 class PMA_DIA extends XMLWriter
20 public $title;
21 public $author;
22 public $font;
23 public $fontSize;
25 /**
26 * The "PMA_DIA" constructor
28 * Upon instantiation This starts writing the Dia XML document
30 * @return void
31 * @see XMLWriter::openMemory(),XMLWriter::setIndent(),XMLWriter::startDocument()
33 function __construct()
35 $this->openMemory();
37 * Set indenting using three spaces,
38 * so output is formatted
41 $this->setIndent(true);
42 $this->setIndentString(' ');
44 * Create the XML document
47 $this->startDocument('1.0','UTF-8');
50 /**
51 * Starts Dia Document
53 * dia document starts by first initializing dia:diagram tag
54 * then dia:diagramdata contains all the attributes that needed
55 * to define the document, then finally a Layer starts which
56 * holds all the objects.
58 * @param string paper The size of the paper/document
59 * @param float topMargin top margin of the paper/document in cm
60 * @param float bottomMargin bottom margin of the paper/document in cm
61 * @param float leftMargin left margin of the paper/document in cm
62 * @param float rightMargin right margin of the paper/document in cm
63 * @param string portrait document will be portrait or landscape
64 * @return void
65 * @access public
66 * @see XMLWriter::startElement(),XMLWriter::writeAttribute(),XMLWriter::writeRaw()
68 function startDiaDoc($paper,$topMargin,$bottomMargin,$leftMargin,$rightMargin,$portrait)
70 if($portrait == 'P'){
71 $isPortrait='true';
72 }else{
73 $isPortrait='false';
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 $this->endElement();
140 $this->startElement('dia:layer');
141 $this->writeAttribute('name', 'Background');
142 $this->writeAttribute('visible', 'true');
143 $this->writeAttribute('active', 'true');
148 * Ends Dia Document
150 * @return void
151 * @access public
152 * @see XMLWriter::endElement(),XMLWriter::endDocument()
154 function endDiaDoc()
156 $this->endElement();
157 $this->endDocument();
161 * Output Dia Document for download
163 * @param string fileName name of the dia document
164 * @return void
165 * @access public
166 * @see XMLWriter::flush()
168 function showOutput($fileName)
170 if(ob_get_clean()){
171 ob_end_clean();
173 $output = $this->flush();
174 PMA_download_header($fileName . '.dia', 'application/x-dia-diagram', strlen($output));
175 print $output;
180 * Table preferences/statistics
182 * This class preserves the table co-ordinates,fields
183 * and helps in drawing/generating the Tables in dia XML document.
185 * @name Table_Stats
186 * @see PMA_DIA
188 class Table_Stats
191 * Defines properties
193 public $tableName;
194 public $fields = array();
195 public $x, $y;
196 public $primary = array();
197 public $tableId;
198 public $tableColor;
201 * The "Table_Stats" constructor
203 * @param string table_name 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 * @return void
208 * @global object The current dia document
209 * @global array The relations settings
210 * @global string The current db name
211 * @see PMA_DIA
213 function __construct($tableName, $pageNumber, $showKeys = false)
215 global $dia, $cfgRelation, $db;
217 $this->tableName = $tableName;
218 $sql = 'DESCRIBE ' . PMA_backquote($tableName);
219 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE);
220 if (!$result || !PMA_DBI_num_rows($result)) {
221 $dia->dieSchema($pageNumber,"DIA",sprintf(__('The %s table doesn\'t exist!'), $tableName));
224 * load fields
225 * check to see if it will load all fields or only the foreign keys
227 if ($showKeys) {
228 $indexes = PMA_Index::getFromTable($this->tableName, $db);
229 $all_columns = array();
230 foreach ($indexes as $index) {
231 $all_columns = array_merge($all_columns, array_flip(array_keys($index->getColumns())));
233 $this->fields = array_keys($all_columns);
234 } else {
235 while ($row = PMA_DBI_fetch_row($result)) {
236 $this->fields[] = $row[0];
240 $sql = 'SELECT x, y FROM '
241 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
242 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
243 . ' AND table_name = \'' . PMA_sqlAddSlashes($tableName) . '\''
244 . ' AND pdf_page_number = ' . $pageNumber;
245 $result = PMA_query_as_controluser($sql, false, PMA_DBI_QUERY_STORE);
246 if (!$result || !PMA_DBI_num_rows($result)) {
247 $dia->dieSchema($pageNumber,"DIA",sprintf(__('Please configure the coordinates for table %s'), $tableName));
249 list($this->x, $this->y) = PMA_DBI_fetch_row($result);
250 $this->x = (double) $this->x;
251 $this->y = (double) $this->y;
253 * displayfield
255 $this->displayfield = PMA_getDisplayField($db, $tableName);
257 * index
259 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tableName) . ';', null, PMA_DBI_QUERY_STORE);
260 if (PMA_DBI_num_rows($result) > 0) {
261 while ($row = PMA_DBI_fetch_assoc($result)) {
262 if ($row['Key_name'] == 'PRIMARY') {
263 $this->primary[] = $row['Column_name'];
268 * Every object in Dia document needs an ID to identify
269 * so, we used a static variable to keep the things unique
271 PMA_Dia_Relation_Schema::$objectId += 1;
272 $this->tableId = PMA_Dia_Relation_Schema::$objectId;
276 * Do draw the table
278 * Tables are generated using object type Database - Table
279 * primary fields are underlined in tables. Dia object
280 * is used to generate the XML of Dia Document. Database Table
281 * Object and their attributes are involved in the combination
282 * of displaing Database - Table on Dia Document.
284 * @param boolean changeColor Whether to show color for tables text or not
285 if changeColor is true then an array of $listOfColors
286 will be used to choose the random colors for tables text
287 we can change/add more colors to this array
288 @return void
289 * @global object The current Dia document
290 * @access public
291 * @see PMA_DIA
293 public function tableDraw($changeColor)
295 global $dia;
297 if ($changeColor) {
298 $listOfColors = array(
299 'FF0000',
300 '000099',
301 '00FF00'
303 shuffle($listOfColors);
304 $this->tableColor = '#'.$listOfColors[0].'';
305 } else {
306 $this->tableColor = '#000000';
309 $factor = 0.1;
311 $dia->startElement('dia:object');
312 $dia->writeAttribute('type', 'Database - Table');
313 $dia->writeAttribute('version', '0');
314 $dia->writeAttribute('id', ''.$this->tableId.'');
315 $dia->writeRaw(
316 '<dia:attribute name="obj_pos">
317 <dia:point val="'.($this->x * $factor).','.($this->y * $factor).'"/>
318 </dia:attribute>
319 <dia:attribute name="obj_bb">
320 <dia:rectangle val="'.($this->x * $factor).','.($this->y * $factor).';9.97,9.2"/>
321 </dia:attribute>
322 <dia:attribute name="meta">
323 <dia:composite type="dict"/>
324 </dia:attribute>
325 <dia:attribute name="elem_corner">
326 <dia:point val="'.($this->x * $factor).','.($this->y * $factor).'"/>
327 </dia:attribute>
328 <dia:attribute name="elem_width">
329 <dia:real val="5.9199999999999999"/>
330 </dia:attribute>
331 <dia:attribute name="elem_height">
332 <dia:real val="3.5"/>
333 </dia:attribute>
334 <dia:attribute name="text_colour">
335 <dia:color val="'.$this->tableColor.'"/>
336 </dia:attribute>
337 <dia:attribute name="line_colour">
338 <dia:color val="#000000"/>
339 </dia:attribute>
340 <dia:attribute name="fill_colour">
341 <dia:color val="#ffffff"/>
342 </dia:attribute>
343 <dia:attribute name="line_width">
344 <dia:real val="0.10000000000000001"/>
345 </dia:attribute>
346 <dia:attribute name="name">
347 <dia:string>#'.$this->tableName.'#</dia:string>
348 </dia:attribute>
349 <dia:attribute name="comment">
350 <dia:string>##</dia:string>
351 </dia:attribute>
352 <dia:attribute name="visible_comment">
353 <dia:boolean val="false"/>
354 </dia:attribute>
355 <dia:attribute name="tagging_comment">
356 <dia:boolean val="false"/>
357 </dia:attribute>
358 <dia:attribute name="underline_primary_key">
359 <dia:boolean val="true"/>
360 </dia:attribute>
361 <dia:attribute name="bold_primary_keys">
362 <dia:boolean val="true"/>
363 </dia:attribute>
364 <dia:attribute name="normal_font">
365 <dia:font family="monospace" style="0" name="Courier"/>
366 </dia:attribute>
367 <dia:attribute name="name_font">
368 <dia:font family="sans" style="80" name="Helvetica-Bold"/>
369 </dia:attribute>
370 <dia:attribute name="comment_font">
371 <dia:font family="sans" style="0" name="Helvetica"/>
372 </dia:attribute>
373 <dia:attribute name="normal_font_height">
374 <dia:real val="0.80000000000000004"/>
375 </dia:attribute>
376 <dia:attribute name="name_font_height">
377 <dia:real val="0.69999999999999996"/>
378 </dia:attribute>
379 <dia:attribute name="comment_font_height">
380 <dia:real val="0.69999999999999996"/>
381 </dia:attribute>'
384 $dia->startElement('dia:attribute');
385 $dia->writeAttribute('name', 'attributes');
387 foreach ($this->fields as $field) {
388 $dia->writeRaw(
389 '<dia:composite type="table_attribute">
390 <dia:attribute name="name">
391 <dia:string>#'.$field.'#</dia:string>
392 </dia:attribute>
393 <dia:attribute name="type">
394 <dia:string>##</dia:string>
395 </dia:attribute>
396 <dia:attribute name="comment">
397 <dia:string>##</dia:string>
398 </dia:attribute>'
400 unset($pm);
401 $pm = 'false';
402 if (in_array($field, $this->primary)) {
403 $pm = 'true';
405 if ($field == $this->displayfield) {
406 $pm = 'false';
408 $dia->writeRaw(
409 '<dia:attribute name="primary_key">
410 <dia:boolean val="'.$pm.'"/>
411 </dia:attribute>
412 <dia:attribute name="nullable">
413 <dia:boolean val="false"/>
414 </dia:attribute>
415 <dia:attribute name="unique">
416 <dia:boolean val="'.$pm.'"/>
417 </dia:attribute>
418 </dia:composite>'
421 $dia->endElement();
422 $dia->endElement();
427 * Relation preferences/statistics
429 * This class fetches the table master and foreign fields positions
430 * and helps in generating the Table references and then connects
431 * master table's master field to foreign table's foreign key
432 * in dia XML document.
434 * @name Relation_Stats
435 * @see PMA_DIA
437 class Relation_Stats
440 * Defines properties
442 public $srcConnPointsRight;
443 public $srcConnPointsLeft;
444 public $destConnPointsRight;
445 public $destConnPointsLeft;
446 public $masterTableId;
447 public $foreignTableId;
448 public $masterTablePos;
449 public $foreignTablePos;
450 public $referenceColor;
453 * The "Relation_Stats" constructor
455 * @param string master_table The master table name
456 * @param string master_field The relation field in the master table
457 * @param string foreign_table The foreign table name
458 * @param string foreigh_field The relation field in the foreign table
459 * @return void
460 * @see Relation_Stats::_getXy
462 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
464 $src_pos = $this->_getXy($master_table, $master_field);
465 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
466 $this->srcConnPointsLeft = $src_pos[0];
467 $this->srcConnPointsRight = $src_pos[1];
468 $this->destConnPointsLeft = $dest_pos[0];
469 $this->destConnPointsRight = $dest_pos[1];
470 $this->masterTablePos = $src_pos[2];
471 $this->foreignTablePos = $dest_pos[2];
472 $this->masterTableId = $master_table->tableId;
473 $this->foreignTableId = $foreign_table->tableId;
477 * Each Table object have connection points
478 * which is used to connect to other objects in Dia
479 * we detect the position of key in fields and
480 * then determines its left and right connection
481 * points.
483 * @param string table The current table name
484 * @param string column The relation column name
485 * @return array Table right,left connection points and key position
486 * @access private
488 private function _getXy($table, $column)
490 $pos = array_search($column, $table->fields);
491 // left, right, position
492 $value = 12;
493 if($pos != 0)
495 return array($pos + $value + $pos, $pos + $value + $pos + 1, $pos);
497 return array($pos + $value , $pos + $value + 1, $pos);
501 * Draws relation references
503 * connects master table's master field to foreign table's
504 * forein field using Dia object type Database - Reference
505 * Dia object is used to generate the XML of Dia Document.
506 * Database reference Object and their attributes are involved
507 * in the combination of displaing Database - reference on Dia Document.
509 * @param boolean changeColor Whether to use one color per relation or not
510 if changeColor is true then an array of $listOfColors
511 will be used to choose the random colors for references
512 lines. we can change/add more colors to this array
513 * @return void
514 * @global object The current Dia document
515 * @access public
516 * @see PMA_PDF
518 public function relationDraw($changeColor)
520 global $dia;
522 PMA_Dia_Relation_Schema::$objectId += 1;
524 * if source connection points and destination connection
525 * points are same then return it false and don't draw that
526 * relation
528 if ( $this->srcConnPointsRight == $this->destConnPointsRight ){
529 if ( $this->srcConnPointsLeft == $this->destConnPointsLeft ){
530 return false;
534 if ($changeColor) {
535 $listOfColors = array(
536 'FF0000',
537 '000099',
538 '00FF00'
540 shuffle($listOfColors);
541 $this->referenceColor = '#'.$listOfColors[0].'';
542 } else {
543 $this->referenceColor = '#000000';
546 $dia->writeRaw(
547 '<dia:object type="Database - Reference" version="0" id="'.PMA_Dia_Relation_Schema::$objectId.'">
548 <dia:attribute name="obj_pos">
549 <dia:point val="3.27,18.9198"/>
550 </dia:attribute>
551 <dia:attribute name="obj_bb">
552 <dia:rectangle val="2.27,8.7175;17.7679,18.9198"/>
553 </dia:attribute>
554 <dia:attribute name="meta">
555 <dia:composite type="dict"/>
556 </dia:attribute>
557 <dia:attribute name="orth_points">
558 <dia:point val="3.27,18.9198"/>
559 <dia:point val="2.27,18.9198"/>
560 <dia:point val="2.27,14.1286"/>
561 <dia:point val="17.7679,14.1286"/>
562 <dia:point val="17.7679,9.3375"/>
563 <dia:point val="16.7679,9.3375"/>
564 </dia:attribute>
565 <dia:attribute name="orth_orient">
566 <dia:enum val="0"/>
567 <dia:enum val="1"/>
568 <dia:enum val="0"/>
569 <dia:enum val="1"/>
570 <dia:enum val="0"/>
571 </dia:attribute>
572 <dia:attribute name="orth_autoroute">
573 <dia:boolean val="true"/>
574 </dia:attribute>
575 <dia:attribute name="text_colour">
576 <dia:color val="#000000"/>
577 </dia:attribute>
578 <dia:attribute name="line_colour">
579 <dia:color val="'.$this->referenceColor.'"/>
580 </dia:attribute>
581 <dia:attribute name="line_width">
582 <dia:real val="0.10000000000000001"/>
583 </dia:attribute>
584 <dia:attribute name="line_style">
585 <dia:enum val="0"/>
586 <dia:real val="1"/>
587 </dia:attribute>
588 <dia:attribute name="corner_radius">
589 <dia:real val="0"/>
590 </dia:attribute>
591 <dia:attribute name="end_arrow">
592 <dia:enum val="22"/>
593 </dia:attribute>
594 <dia:attribute name="end_arrow_length">
595 <dia:real val="0.5"/>
596 </dia:attribute>
597 <dia:attribute name="end_arrow_width">
598 <dia:real val="0.5"/>
599 </dia:attribute>
600 <dia:attribute name="start_point_desc">
601 <dia:string>#1#</dia:string>
602 </dia:attribute>
603 <dia:attribute name="end_point_desc">
604 <dia:string>#n#</dia:string>
605 </dia:attribute>
606 <dia:attribute name="normal_font">
607 <dia:font family="monospace" style="0" name="Courier"/>
608 </dia:attribute>
609 <dia:attribute name="normal_font_height">
610 <dia:real val="0.59999999999999998"/>
611 </dia:attribute>
612 <dia:connections>
613 <dia:connection handle="0" to="'.$this->masterTableId.'" connection="'.$this->srcConnPointsRight.'"/>
614 <dia:connection handle="1" to="'.$this->foreignTableId.'" connection="'.$this->destConnPointsRight.'"/>
615 </dia:connections>
616 </dia:object>'
622 * Dia Relation Schema Class
624 * Purpose of this class is to generate the Dia XML Document
625 * which is used for representing the database diagrams in Dia IDE
626 * This class uses Database Table and Reference Objects of Dia and with
627 * the combination of these objects actually helps in preparing Dia XML.
629 * Dia XML is generated by using XMLWriter php extension and this class
630 * inherits Export_Relation_Schema class has common functionality added
631 * to this class
633 * @name Dia_Relation_Schema
635 class PMA_Dia_Relation_Schema extends PMA_Export_Relation_Schema
638 * Defines properties
640 private $_tables = array();
641 private $_relations = array();
642 private $_topMargin = 2.8222000598907471;
643 private $_bottomMargin = 2.8222000598907471;
644 private $_leftMargin = 2.8222000598907471;
645 private $_rightMargin = 2.8222000598907471;
646 public static $objectId = 0;
649 * The "PMA_Dia_Relation_Schema" constructor
651 * Upon instantiation This outputs the Dia XML document
652 * that user can download
654 * @return void
655 * @see PMA_DIA,Table_Stats,Relation_Stats
657 function __construct()
659 global $dia,$db;
661 $this->setPageNumber($_POST['pdf_page_number']);
662 $this->setShowGrid(isset($_POST['show_grid']));
663 $this->setShowColor($_POST['show_color']);
664 $this->setShowKeys(isset($_POST['show_keys']));
665 $this->setOrientation(isset($_POST['orientation']));
666 $this->setPaper($_POST['paper']);
667 $this->setExportType($_POST['export_type']);
669 $dia = new PMA_DIA();
670 $dia->startDiaDoc($this->paper,$this->_topMargin,$this->_bottomMargin,$this->_leftMargin,$this->_rightMargin,$this->orientation);
671 $alltables = $this->getAllTables($db,$this->pageNumber);
672 foreach ($alltables as $table) {
673 if (! isset($this->tables[$table])) {
674 $this->tables[$table] = new Table_Stats($table, $this->pageNumber, $this->showKeys);
678 $seen_a_relation = false;
679 foreach ($alltables as $one_table) {
680 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
681 if ($exist_rel) {
682 $seen_a_relation = true;
683 foreach ($exist_rel as $master_field => $rel) {
684 /* put the foreign table on the schema only if selected
685 * by the user
686 * (do not use array_search() because we would have to
687 * to do a === false and this is not PHP3 compatible)
689 if (in_array($rel['foreign_table'], $alltables)) {
690 $this->_addRelation($one_table, $master_field, $rel['foreign_table'], $rel['foreign_field'],$this->showKeys);
695 $this->_drawTables($this->showColor);
697 if ($seen_a_relation) {
698 $this->_drawRelations($this->showColor);
700 $dia->endDiaDoc();
701 $dia->showOutput($db.'-'.$this->pageNumber);
702 exit();
706 * Defines relation objects
708 * @param string masterTable The master table name
709 * @param string masterField The relation field in the master table
710 * @param string foreignTable The foreign table name
711 * @param string foreignField The relation field in the foreign table
712 * @return void
713 * @access private
714 * @see Table_Stats::__construct(),Relation_Stats::__construct()
716 private function _addRelation($masterTable, $masterField, $foreignTable, $foreignField, $showKeys)
718 if (! isset($this->tables[$masterTable])) {
719 $this->tables[$masterTable] = new Table_Stats($masterTable, $this->pageNumber, $showKeys);
721 if (! isset($this->tables[$foreignTable])) {
722 $this->tables[$foreignTable] = new Table_Stats($foreignTable, $this->pageNumber, $showKeys);
724 $this->_relations[] = new Relation_Stats($this->tables[$masterTable], $masterField, $this->tables[$foreignTable], $foreignField);
728 * Draws relation references
730 * connects master table's master field to
731 * foreign table's forein field using Dia object
732 * type Database - Reference
734 * @param boolean changeColor Whether to use one color per relation or not
735 * @return void
736 * @access private
737 * @see Relation_Stats::relationDraw()
739 private function _drawRelations($changeColor)
741 foreach ($this->_relations as $relation) {
742 $relation->relationDraw($changeColor);
747 * Draws tables
749 * Tables are generated using Dia object type Database - Table
750 * primary fields are underlined and bold in tables
752 * @param boolean changeColor Whether to show color for tables text or not
753 * @return void
754 * @access private
755 * @see Table_Stats::tableDraw()
757 private function _drawTables($changeColor)
759 foreach ($this->tables as $table) {
760 $table->tableDraw($changeColor);