display message when search returns zero rows
[phpmyadmin/dkf.git] / libraries / schema / Dia_Relation_Schema.class.php
blobe58381e91f04ad82d1b43bdf0a05cfbe3a664a5a
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 * @name PMA_DIA
15 * @copyright
16 * @license
17 * @access public
18 * @see http://php.net/manual/en/book.xmlwriter.php
21 class PMA_DIA extends XMLWriter
23 public $title;
24 public $author;
25 public $font;
26 public $fontSize;
28 /**
29 * The "PMA_DIA" constructor
31 * Upon instantiation This starts writing the Dia XML document
33 * @return void
34 * @see XMLWriter::openMemory(),XMLWriter::setIndent(),XMLWriter::startDocument()
36 function __construct()
38 $this->openMemory();
40 * Set indenting using three spaces,
41 * so output is formatted
44 $this->setIndent(TRUE);
45 $this->setIndentString(' ');
47 * Create the XML document
50 $this->startDocument('1.0','UTF-8');
53 /**
54 * Starts Dia Document
56 * dia document starts by first initializing dia:diagram tag
57 * then dia:diagramdata contains all the attributes that needed
58 * to define the document, then finally a Layer starts which
59 * holds all the objects.
61 * @param string paper The size of the paper/document
62 * @param float topMargin top margin of the paper/document in cm
63 * @param float bottomMargin bottom margin of the paper/document in cm
64 * @param float leftMargin left margin of the paper/document in cm
65 * @param float rightMargin right margin of the paper/document in cm
66 * @param string portrait document will be portrait or landscape
67 * @return void
68 * @access public
69 * @see XMLWriter::startElement(),XMLWriter::writeAttribute(),XMLWriter::writeRaw()
71 function startDiaDoc($paper,$topMargin,$bottomMargin,$leftMargin,$rightMargin,$portrait)
73 if($portrait == 'P'){
74 $isPortrait='true';
75 }else{
76 $isPortrait='false';
78 $this->startElement('dia:diagram');
79 $this->writeAttribute('xmlns:dia', 'http://www.lysator.liu.se/~alla/dia/');
80 $this->startElement('dia:diagramdata');
81 $this->writeRaw (
82 '<dia:attribute name="background">
83 <dia:color val="#ffffff"/>
84 </dia:attribute>
85 <dia:attribute name="pagebreak">
86 <dia:color val="#000099"/>
87 </dia:attribute>
88 <dia:attribute name="paper">
89 <dia:composite type="paper">
90 <dia:attribute name="name">
91 <dia:string>#'.$paper.'#</dia:string>
92 </dia:attribute>
93 <dia:attribute name="tmargin">
94 <dia:real val="'.$topMargin.'"/>
95 </dia:attribute>
96 <dia:attribute name="bmargin">
97 <dia:real val="'.$bottomMargin.'"/>
98 </dia:attribute>
99 <dia:attribute name="lmargin">
100 <dia:real val="'.$leftMargin.'"/>
101 </dia:attribute>
102 <dia:attribute name="rmargin">
103 <dia:real val="'.$rightMargin.'"/>
104 </dia:attribute>
105 <dia:attribute name="is_portrait">
106 <dia:boolean val="'.$isPortrait.'"/>
107 </dia:attribute>
108 <dia:attribute name="scaling">
109 <dia:real val="1"/>
110 </dia:attribute>
111 <dia:attribute name="fitto">
112 <dia:boolean val="false"/>
113 </dia:attribute>
114 </dia:composite>
115 </dia:attribute>
116 <dia:attribute name="grid">
117 <dia:composite type="grid">
118 <dia:attribute name="width_x">
119 <dia:real val="1"/>
120 </dia:attribute>
121 <dia:attribute name="width_y">
122 <dia:real val="1"/>
123 </dia:attribute>
124 <dia:attribute name="visible_x">
125 <dia:int val="1"/>
126 </dia:attribute>
127 <dia:attribute name="visible_y">
128 <dia:int val="1"/>
129 </dia:attribute>
130 <dia:composite type="color"/>
131 </dia:composite>
132 </dia:attribute>
133 <dia:attribute name="color">
134 <dia:color val="#d8e5e5"/>
135 </dia:attribute>
136 <dia:attribute name="guides">
137 <dia:composite type="guides">
138 <dia:attribute name="hguides"/>
139 <dia:attribute name="vguides"/>
140 </dia:composite>
141 </dia:attribute>');
142 $this->endElement();
143 $this->startElement('dia:layer');
144 $this->writeAttribute('name', 'Background');
145 $this->writeAttribute('visible', 'true');
146 $this->writeAttribute('active', 'true');
151 * Ends Dia Document
153 * @return void
154 * @access public
155 * @see XMLWriter::endElement(),XMLWriter::endDocument()
157 function endDiaDoc()
159 $this->endElement();
160 $this->endDocument();
164 * Output Dia Document for download
166 * @param string fileName name of the dia document
167 * @return void
168 * @access public
169 * @see XMLWriter::flush()
171 function showOutput($fileName)
173 if(ob_get_clean()){
174 ob_end_clean();
176 header('Content-type: application/x-dia-diagram');
177 header('Content-Disposition: attachment; filename="'.$fileName.'.dia"');
178 $output = $this->flush();
179 print $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.
189 * @name Table_Stats
190 * @copyright
191 * @license
192 * @see PMA_DIA
194 class Table_Stats
197 * Defines properties
199 public $tableName;
200 public $fields = array();
201 public $x, $y;
202 public $primary = array();
203 public $tableId;
204 public $tableColor;
207 * The "Table_Stats" constructor
209 * @param string table_name The table name
210 * @param integer pageNumber The current page number (from the
211 * $cfg['Servers'][$i]['table_coords'] table)
212 * @param boolean showKeys Whether to display ONLY keys or not
213 * @return void
214 * @global object The current dia document
215 * @global array The relations settings
216 * @global string The current db name
217 * @see PMA_DIA
219 function __construct($tableName, $pageNumber, $showKeys = false)
221 global $dia, $cfgRelation, $db;
223 $this->tableName = $tableName;
224 $sql = 'DESCRIBE ' . PMA_backquote($tableName);
225 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE);
226 if (!$result || !PMA_DBI_num_rows($result)) {
227 $dia->dieSchema($pageNumber,"DIA",sprintf(__('The %s table doesn\'t exist!'), $tableName));
230 * load fields
231 * check to see if it will load all fields or only the foreign keys
233 if ($showKeys) {
234 $indexes = PMA_Index::getFromTable($this->tableName, $db);
235 $all_columns = array();
236 foreach ($indexes as $index) {
237 $all_columns = array_merge($all_columns, array_flip(array_keys($index->getColumns())));
239 $this->fields = array_keys($all_columns);
240 } else {
241 while ($row = PMA_DBI_fetch_row($result)) {
242 $this->fields[] = $row[0];
246 $sql = 'SELECT x, y FROM '
247 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
248 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
249 . ' AND table_name = \'' . PMA_sqlAddslashes($tableName) . '\''
250 . ' AND pdf_page_number = ' . $pageNumber;
251 $result = PMA_query_as_controluser($sql, false, PMA_DBI_QUERY_STORE);
252 if (!$result || !PMA_DBI_num_rows($result)) {
253 $dia->dieSchema($pageNumber,"DIA",sprintf(__('Please configure the coordinates for table %s'), $tableName));
255 list($this->x, $this->y) = PMA_DBI_fetch_row($result);
256 $this->x = (double) $this->x;
257 $this->y = (double) $this->y;
259 * displayfield
261 $this->displayfield = PMA_getDisplayField($db, $tableName);
263 * index
265 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tableName) . ';', null, PMA_DBI_QUERY_STORE);
266 if (PMA_DBI_num_rows($result) > 0) {
267 while ($row = PMA_DBI_fetch_assoc($result)) {
268 if ($row['Key_name'] == 'PRIMARY') {
269 $this->primary[] = $row['Column_name'];
274 * Every object in Dia document needs an ID to identify
275 * so, we used a static variable to keep the things unique
277 PMA_Dia_Relation_Schema::$objectId += 1;
278 $this->tableId = PMA_Dia_Relation_Schema::$objectId;
282 * Do draw the table
284 * Tables are generated using object type Database - Table
285 * primary fields are underlined in tables. Dia object
286 * is used to generate the XML of Dia Document. Database Table
287 * Object and their attributes are involved in the combination
288 * of displaing Database - Table on Dia Document.
290 * @param boolean changeColor Whether to show color for tables text or not
291 if changeColor is true then an array of $listOfColors
292 will be used to choose the random colors for tables text
293 we can change/add more colors to this array
294 @return void
295 * @global object The current Dia document
296 * @access public
297 * @see PMA_DIA
299 public function tableDraw($changeColor)
301 global $dia;
303 if ($changeColor) {
304 $listOfColors = array(
305 'FF0000',
306 '000099',
307 '00FF00'
309 shuffle($listOfColors);
310 $this->tableColor = '#'.$listOfColors[0].'';
311 } else {
312 $this->tableColor = '#000000';
315 $factor = 0.1;
317 $dia->startElement('dia:object');
318 $dia->writeAttribute('type', 'Database - Table');
319 $dia->writeAttribute('version', '0');
320 $dia->writeAttribute('id', ''.$this->tableId.'');
321 $dia->writeRaw(
322 '<dia:attribute name="obj_pos">
323 <dia:point val="'.($this->x * $factor).','.($this->y * $factor).'"/>
324 </dia:attribute>
325 <dia:attribute name="obj_bb">
326 <dia:rectangle val="'.($this->x * $factor).','.($this->y * $factor).';9.97,9.2"/>
327 </dia:attribute>
328 <dia:attribute name="meta">
329 <dia:composite type="dict"/>
330 </dia:attribute>
331 <dia:attribute name="elem_corner">
332 <dia:point val="'.($this->x * $factor).','.($this->y * $factor).'"/>
333 </dia:attribute>
334 <dia:attribute name="elem_width">
335 <dia:real val="5.9199999999999999"/>
336 </dia:attribute>
337 <dia:attribute name="elem_height">
338 <dia:real val="3.5"/>
339 </dia:attribute>
340 <dia:attribute name="text_colour">
341 <dia:color val="'.$this->tableColor.'"/>
342 </dia:attribute>
343 <dia:attribute name="line_colour">
344 <dia:color val="#000000"/>
345 </dia:attribute>
346 <dia:attribute name="fill_colour">
347 <dia:color val="#ffffff"/>
348 </dia:attribute>
349 <dia:attribute name="line_width">
350 <dia:real val="0.10000000000000001"/>
351 </dia:attribute>
352 <dia:attribute name="name">
353 <dia:string>#'.$this->tableName.'#</dia:string>
354 </dia:attribute>
355 <dia:attribute name="comment">
356 <dia:string>##</dia:string>
357 </dia:attribute>
358 <dia:attribute name="visible_comment">
359 <dia:boolean val="false"/>
360 </dia:attribute>
361 <dia:attribute name="tagging_comment">
362 <dia:boolean val="false"/>
363 </dia:attribute>
364 <dia:attribute name="underline_primary_key">
365 <dia:boolean val="true"/>
366 </dia:attribute>
367 <dia:attribute name="bold_primary_keys">
368 <dia:boolean val="true"/>
369 </dia:attribute>
370 <dia:attribute name="normal_font">
371 <dia:font family="monospace" style="0" name="Courier"/>
372 </dia:attribute>
373 <dia:attribute name="name_font">
374 <dia:font family="sans" style="80" name="Helvetica-Bold"/>
375 </dia:attribute>
376 <dia:attribute name="comment_font">
377 <dia:font family="sans" style="0" name="Helvetica"/>
378 </dia:attribute>
379 <dia:attribute name="normal_font_height">
380 <dia:real val="0.80000000000000004"/>
381 </dia:attribute>
382 <dia:attribute name="name_font_height">
383 <dia:real val="0.69999999999999996"/>
384 </dia:attribute>
385 <dia:attribute name="comment_font_height">
386 <dia:real val="0.69999999999999996"/>
387 </dia:attribute>'
390 $dia->startElement('dia:attribute');
391 $dia->writeAttribute('name', 'attributes');
393 foreach ($this->fields as $field) {
394 $dia->writeRaw(
395 '<dia:composite type="table_attribute">
396 <dia:attribute name="name">
397 <dia:string>#'.$field.'#</dia:string>
398 </dia:attribute>
399 <dia:attribute name="type">
400 <dia:string>##</dia:string>
401 </dia:attribute>
402 <dia:attribute name="comment">
403 <dia:string>##</dia:string>
404 </dia:attribute>'
406 unset($pm);
407 $pm = 'false';
408 if (in_array($field, $this->primary)) {
409 $pm = 'true';
411 if ($field == $this->displayfield) {
412 $pm = 'false';
414 $dia->writeRaw(
415 '<dia:attribute name="primary_key">
416 <dia:boolean val="'.$pm.'"/>
417 </dia:attribute>
418 <dia:attribute name="nullable">
419 <dia:boolean val="false"/>
420 </dia:attribute>
421 <dia:attribute name="unique">
422 <dia:boolean val="'.$pm.'"/>
423 </dia:attribute>
424 </dia:composite>'
427 $dia->endElement();
428 $dia->endElement();
433 * Relation preferences/statistics
435 * This class fetches the table master and foreign fields positions
436 * and helps in generating the Table references and then connects
437 * master table's master field to foreign table's foreign key
438 * in dia XML document.
440 * @name Relation_Stats
441 * @copyright
442 * @license
443 * @see PMA_DIA
445 class Relation_Stats
448 * Defines properties
450 public $srcConnPointsRight;
451 public $srcConnPointsLeft;
452 public $destConnPointsRight;
453 public $destConnPointsLeft;
454 public $masterTableId;
455 public $foreignTableId;
456 public $masterTablePos;
457 public $foreignTablePos;
458 public $referenceColor;
461 * The "Relation_Stats" constructor
463 * @param string master_table The master table name
464 * @param string master_field The relation field in the master table
465 * @param string foreign_table The foreign table name
466 * @param string foreigh_field The relation field in the foreign table
467 * @return void
468 * @see Relation_Stats::_getXy
470 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
472 $src_pos = $this->_getXy($master_table, $master_field);
473 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
474 $this->srcConnPointsLeft = $src_pos[0];
475 $this->srcConnPointsRight = $src_pos[1];
476 $this->destConnPointsLeft = $dest_pos[0];
477 $this->destConnPointsRight = $dest_pos[1];
478 $this->masterTablePos = $src_pos[2];
479 $this->foreignTablePos = $dest_pos[2];
480 $this->masterTableId = $master_table->tableId;
481 $this->foreignTableId = $foreign_table->tableId;
485 * Each Table object have connection points
486 * which is used to connect to other objects in Dia
487 * we detect the position of key in fields and
488 * then determines its left and right connection
489 * points.
491 * @param string table The current table name
492 * @param string column The relation column name
493 * @return array Table right,left connection points and key position
494 * @access private
496 private function _getXy($table, $column)
498 $pos = array_search($column, $table->fields);
499 // left, right, position
500 $value = 12;
501 if($pos != 0)
503 return array($pos + $value + $pos, $pos + $value + $pos + 1, $pos);
505 return array($pos + $value , $pos + $value + 1, $pos);
509 * Draws relation references
511 * connects master table's master field to foreign table's
512 * forein field using Dia object type Database - Reference
513 * Dia object is used to generate the XML of Dia Document.
514 * Database reference Object and their attributes are involved
515 * in the combination of displaing Database - reference on Dia Document.
517 * @param boolean changeColor Whether to use one color per relation or not
518 if changeColor is true then an array of $listOfColors
519 will be used to choose the random colors for references
520 lines. we can change/add more colors to this array
521 * @return void
522 * @global object The current Dia document
523 * @access public
524 * @see PMA_PDF
526 public function relationDraw($changeColor)
528 global $dia;
530 PMA_Dia_Relation_Schema::$objectId += 1;
532 * if source connection points and destination connection
533 * points are same then return it false and don't draw that
534 * relation
536 if ( $this->srcConnPointsRight == $this->destConnPointsRight ){
537 if ( $this->srcConnPointsLeft == $this->destConnPointsLeft ){
538 return false;
542 if ($changeColor) {
543 $listOfColors = array(
544 'FF0000',
545 '000099',
546 '00FF00'
548 shuffle($listOfColors);
549 $this->referenceColor = '#'.$listOfColors[0].'';
550 } else {
551 $this->referenceColor = '#000000';
554 $dia->writeRaw(
555 '<dia:object type="Database - Reference" version="0" id="'.PMA_Dia_Relation_Schema::$objectId.'">
556 <dia:attribute name="obj_pos">
557 <dia:point val="3.27,18.9198"/>
558 </dia:attribute>
559 <dia:attribute name="obj_bb">
560 <dia:rectangle val="2.27,8.7175;17.7679,18.9198"/>
561 </dia:attribute>
562 <dia:attribute name="meta">
563 <dia:composite type="dict"/>
564 </dia:attribute>
565 <dia:attribute name="orth_points">
566 <dia:point val="3.27,18.9198"/>
567 <dia:point val="2.27,18.9198"/>
568 <dia:point val="2.27,14.1286"/>
569 <dia:point val="17.7679,14.1286"/>
570 <dia:point val="17.7679,9.3375"/>
571 <dia:point val="16.7679,9.3375"/>
572 </dia:attribute>
573 <dia:attribute name="orth_orient">
574 <dia:enum val="0"/>
575 <dia:enum val="1"/>
576 <dia:enum val="0"/>
577 <dia:enum val="1"/>
578 <dia:enum val="0"/>
579 </dia:attribute>
580 <dia:attribute name="orth_autoroute">
581 <dia:boolean val="true"/>
582 </dia:attribute>
583 <dia:attribute name="text_colour">
584 <dia:color val="#000000"/>
585 </dia:attribute>
586 <dia:attribute name="line_colour">
587 <dia:color val="'.$this->referenceColor.'"/>
588 </dia:attribute>
589 <dia:attribute name="line_width">
590 <dia:real val="0.10000000000000001"/>
591 </dia:attribute>
592 <dia:attribute name="line_style">
593 <dia:enum val="0"/>
594 <dia:real val="1"/>
595 </dia:attribute>
596 <dia:attribute name="corner_radius">
597 <dia:real val="0"/>
598 </dia:attribute>
599 <dia:attribute name="end_arrow">
600 <dia:enum val="22"/>
601 </dia:attribute>
602 <dia:attribute name="end_arrow_length">
603 <dia:real val="0.5"/>
604 </dia:attribute>
605 <dia:attribute name="end_arrow_width">
606 <dia:real val="0.5"/>
607 </dia:attribute>
608 <dia:attribute name="start_point_desc">
609 <dia:string>#1#</dia:string>
610 </dia:attribute>
611 <dia:attribute name="end_point_desc">
612 <dia:string>#n#</dia:string>
613 </dia:attribute>
614 <dia:attribute name="normal_font">
615 <dia:font family="monospace" style="0" name="Courier"/>
616 </dia:attribute>
617 <dia:attribute name="normal_font_height">
618 <dia:real val="0.59999999999999998"/>
619 </dia:attribute>
620 <dia:connections>
621 <dia:connection handle="0" to="'.$this->masterTableId.'" connection="'.$this->srcConnPointsRight.'"/>
622 <dia:connection handle="1" to="'.$this->foreignTableId.'" connection="'.$this->destConnPointsRight.'"/>
623 </dia:connections>
624 </dia:object>'
630 * Dia Relation Schema Class
632 * Purpose of this class is to generate the Dia XML Document
633 * which is used for representing the database diagrams in Dia IDE
634 * This class uses Database Table and Reference Objects of Dia and with
635 * the combination of these objects actually helps in preparing Dia XML.
637 * Dia XML is generated by using XMLWriter php extension and this class
638 * inherits Export_Relation_Schema class has common functionality added
639 * to this class
641 * @name Dia_Relation_Schema
642 * @copyright
643 * @license
645 class PMA_Dia_Relation_Schema extends PMA_Export_Relation_Schema
648 * Defines properties
650 private $_tables = array();
651 private $_relations = array();
652 private $_topMargin = 2.8222000598907471;
653 private $_bottomMargin = 2.8222000598907471;
654 private $_leftMargin = 2.8222000598907471;
655 private $_rightMargin = 2.8222000598907471;
656 public static $objectId = 0;
659 * The "PMA_Dia_Relation_Schema" constructor
661 * Upon instantiation This outputs the Dia XML document
662 * that user can download
664 * @return void
665 * @see PMA_DIA,Table_Stats,Relation_Stats
667 function __construct()
669 global $dia,$db;
671 $this->setPageNumber($_POST['pdf_page_number']);
672 $this->setShowGrid(isset($_POST['show_grid']));
673 $this->setShowColor($_POST['show_color']);
674 $this->setShowKeys(isset($_POST['show_keys']));
675 $this->setOrientation(isset($_POST['orientation']));
676 $this->setPaper($_POST['paper']);
677 $this->setExportType($_POST['export_type']);
679 $dia = new PMA_DIA();
680 $dia->startDiaDoc($this->paper,$this->_topMargin,$this->_bottomMargin,$this->_leftMargin,$this->_rightMargin,$this->orientation);
681 $alltables = $this->getAllTables($db,$this->pageNumber);
682 foreach ($alltables as $table) {
683 if (!isset($this->tables[$table])) {
684 $this->tables[$table] = new Table_Stats($table, $this->pageNumber, $this->showKeys);
688 $seen_a_relation = false;
689 foreach ($alltables as $one_table) {
690 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
691 if ($exist_rel) {
692 $seen_a_relation = true;
693 foreach ($exist_rel as $master_field => $rel) {
694 /* put the foreign table on the schema only if selected
695 * by the user
696 * (do not use array_search() because we would have to
697 * to do a === FALSE and this is not PHP3 compatible)
699 if (in_array($rel['foreign_table'], $alltables)) {
700 $this->_addRelation($one_table, $master_field, $rel['foreign_table'], $rel['foreign_field'],$this->showKeys);
705 $this->_drawTables($this->showColor);
707 if ($seen_a_relation) {
708 $this->_drawRelations($this->showColor);
710 $dia->endDiaDoc();
711 $dia->showOutput($db.'-'.$this->pageNumber);
712 exit();
716 * Defines relation objects
718 * @param string masterTable The master table name
719 * @param string masterField The relation field in the master table
720 * @param string foreignTable The foreign table name
721 * @param string foreignField The relation field in the foreign table
722 * @return void
723 * @access private
724 * @see Table_Stats::__construct(),Relation_Stats::__construct()
726 private function _addRelation($masterTable, $masterField, $foreignTable, $foreignField, $showKeys)
728 if (!isset($this->tables[$masterTable])) {
729 $this->tables[$masterTable] = new Table_Stats($masterTable, $this->pageNumber, $showKeys);
731 if (!isset($this->tables[$foreignTable])) {
732 $this->tables[$foreignTable] = new Table_Stats($foreignTable, $this->pageNumber, $showKeys);
734 $this->_relations[] = new Relation_Stats($this->tables[$masterTable], $masterField, $this->tables[$foreignTable], $foreignField);
738 * Draws relation references
740 * connects master table's master field to
741 * foreign table's forein field using Dia object
742 * type Database - Reference
744 * @param boolean changeColor Whether to use one color per relation or not
745 * @return void
746 * @access private
747 * @see Relation_Stats::relationDraw()
749 private function _drawRelations($changeColor)
751 foreach ($this->_relations as $relation) {
752 $relation->relationDraw($changeColor);
757 * Draws tables
759 * Tables are generated using Dia object type Database - Table
760 * primary fields are underlined and bold in tables
762 * @param boolean changeColor Whether to show color for tables text or not
763 * @return void
764 * @access private
765 * @see Table_Stats::tableDraw()
767 private function _drawTables($changeColor)
769 foreach ($this->tables as $table) {
770 $table->tableDraw($changeColor);