display message when search returns zero rows
[phpmyadmin/dkf.git] / libraries / schema / Visio_Relation_Schema.class.php
blobab45b13b9c637a55f990e33c84930ffe6710c25e
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 MS Visio Schema Export
14 * @name PMA_VISIO
15 * @copyright
16 * @license
17 * @access public
18 * @see http://php.net/manual/en/book.xmlwriter.php
21 class PMA_VISIO extends XMLWriter
23 public $title;
24 public $author;
25 public $font;
26 public $fontSize;
28 /**
29 * The "PMA_VISIO" constructor
31 * Upon instantiation This starts writing the Visio XML .VDX 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 Visio XML .VDX Document
56 * Visio XML document starts by first initializing VisioDocument tag
57 * then DocumentProperties & DocumentSettings contains all the
58 * attributes that needed to define the document. Order of elements
59 * should be maintained while generating XML of Visio.
61 * @return void
62 * @access public
63 * @see XMLWriter::startElement(),XMLWriter::writeAttribute(),_documentProperties,_documentSettings
65 function startVisioDoc()
67 $this->startElement('VisioDocument');
68 $this->writeAttribute('xmlns', 'http://schemas.microsoft.com/visio/2003/core');
69 $this->writeAttribute('xmlns:vx', 'http://schemas.microsoft.com/visio/2006/extension');
70 $this->writeAttribute('xml:space', 'preserve');
71 $this->_documentProperties();
72 $this->_documentSettings();
75 /**
76 * Set document title
78 * @param string value sets the title text
79 * @return void
80 * @access public
82 function setTitle($value)
84 $this->title = $value;
87 /**
88 * Set document author
90 * @param string value sets the author
91 * @return void
92 * @access public
94 function setAuthor($value)
96 $this->author = $value;
99 /**
100 * Sets Visio XML .VDX Document Properties
102 * DocumentProperties tag contains document property elements such as
103 the document's Title,Subject,Creator and templates tags
105 * @return void
106 * @access private
107 * @see XMLWriter::startElement(),XMLWriter::endElement(),XMLWriter::writeRaw()
109 private function _documentProperties()
111 $this->startElement('DocumentProperties');
112 $this->writeRaw('<Title>'.$this->title.'</Title>');
113 $this->writeRaw('<Subject>'.$this->title.'</Subject>');
114 $this->writeRaw('<Creator>'.$this->author.'</Creator>');
115 $this->writeRaw('<Company>phpMyAdmin</Company>');
116 $this->writeRaw('<Template>c:\program files\microsoft office\office12\1033\DBMODL_U.VST</Template>');
117 $this->endElement();
121 * Sets Visio XML .VDX Document Settings
123 * DocumentSettings tag contains elements that specify document settings.
125 * @return void
126 * @access private
127 * @see XMLWriter::startElement(),XMLWriter::endElement()
129 private function _documentSettings()
131 $this->startElement('DocumentSettings');
132 $this->endElement();
136 * Ends Visio XML Document
138 * @return void
139 * @access public
140 * @see XMLWriter::endElement(),XMLWriter::endDocument()
142 function endVisioDoc()
144 $this->endElement();
145 $this->endDocument();
149 * Output Visio XML .VDX Document for download
151 * @param string fileName name of the Visio XML document
152 * @return void
153 * @access public
154 * @see XMLWriter::flush()
156 function showOutput($fileName)
158 //if(ob_get_clean()){
159 //ob_end_clean();
161 header('Content-type: application/visio');
162 header('Content-Disposition: attachment; filename="'.$fileName.'.vdx"');
163 $output = $this->flush();
164 print $output;
170 * Draws tables schema
172 class Table_Stats
175 * Defines properties
178 private $_tableName;
179 private $_showInfo = false;
181 public $width = 0;
182 public $height;
183 public $fields = array();
184 public $heightCell = 0;
185 public $currentCell = 0;
186 public $x, $y;
187 public $primary = array();
190 * The "Table_Stats" constructor
192 * @param string tableName The table name
193 * @param integer same_wide_width The max. with among tables
194 * @param boolean showKeys Whether to display keys or not
195 * @param boolean showInfo Whether to display table position or not
196 * @global object The current Visio XML document
197 * @global integer The current page number (from the
198 * $cfg['Servers'][$i]['table_coords'] table)
199 * @global array The relations settings
200 * @global string The current db name
201 * @access private
202 * @see PMA_VISIO, Table_Stats::Table_Stats_setWidth,
203 Table_Stats::Table_Stats_setHeight
205 function __construct($tableName, $pageNumber, &$same_wide_width, $showKeys = false, $showInfo = false)
207 global $visio, $cfgRelation, $db;
209 $this->_tableName = $tableName;
210 $sql = 'DESCRIBE ' . PMA_backquote($tableName);
211 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE);
212 if (!$result || !PMA_DBI_num_rows($result)) {
213 $visio->dieSchema($pageNumber,"VISIO",sprintf(__('The %s table doesn\'t exist!'), $tableName));
217 * load fields
218 * check to see if it will load all fields or only the foreign keys
221 if ($showKeys) {
222 $indexes = PMA_Index::getFromTable($this->_tableName, $db);
223 $all_columns = array();
224 foreach ($indexes as $index) {
225 $all_columns = array_merge($all_columns, array_flip(array_keys($index->getColumns())));
227 $this->fields = array_keys($all_columns);
228 } else {
229 while ($row = PMA_DBI_fetch_row($result)) {
230 $this->fields[] = $row[0];
234 $this->_showInfo = $showInfo;
236 // height and width
237 $this->_setHeightTable($fontSize);
239 // setWidth must me after setHeight, because title
240 // can include table height which changes table width
241 $this->_setWidthTable($font,$fontSize);
242 if ($same_wide_width < $this->width) {
243 $same_wide_width = $this->width;
246 // x and y
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);
254 if (!$result || !PMA_DBI_num_rows($result)) {
255 $visio->dieSchema($pageNumber,"VISIO",sprintf(__('Please configure the coordinates for table %s'), $tableName));
257 list($this->x, $this->y) = PMA_DBI_fetch_row($result);
258 $this->x = (double) $this->x;
259 $this->y = (double) $this->y;
260 // displayfield
261 $this->displayfield = PMA_getDisplayField($db, $tableName);
262 // index
263 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tableName) . ';', null, PMA_DBI_QUERY_STORE);
264 if (PMA_DBI_num_rows($result) > 0) {
265 while ($row = PMA_DBI_fetch_assoc($result)) {
266 if ($row['Key_name'] == 'PRIMARY') {
267 $this->primary[] = $row['Column_name'];
274 * Returns title of the current table,
275 * title can have the dimensions/co-ordinates of the table
277 * @access private
279 private function _getTitle()
281 return ($this->_showInfo ? sprintf('%.0f', $this->width) . 'x' . sprintf('%.0f', $this->heightCell) : '') . ' ' . $this->_tableName;
285 * Sets the width of the table
287 * @param string font The font name
288 * @param integer fontSize The font size
289 * @global object The current Visio XML document
290 * @access private
291 * @see PMA_VISIO
293 function _setWidthTable($font,$fontSize)
295 global $visio;
300 * Sets the height of the table
302 * @access private
304 function _setHeightTable($fontSize)
306 $this->heightCell = $fontSize + 4;
307 $this->height = (count($this->fields) + 1) * $this->heightCell;
311 * draw the table
313 * @param boolean showColor Whether to display color
314 * @global object The current Visio XML document
315 * @access public
316 * @see PMA_VISIO
318 public function tableDraw($showColor)
320 global $visio;
321 //echo $this->_tableName.'<br />';
323 foreach ($this->fields as $field) {
324 $this->currentCell += $this->heightCell;
325 $showColor = 'none';
326 if ($showColor) {
327 if (in_array($field, $this->primary)) {
328 $showColor = '#0c0';
330 if ($field == $this->displayfield) {
331 $showColor = 'none';
334 // code here for drawing table diagrams
340 * Draws relation links
342 * @access public
343 * @see PMA_VISIO
345 class Relation_Stats
348 * Defines properties
350 public $xSrc, $ySrc;
351 public $srcDir ;
352 public $destDir;
353 public $xDest, $yDest;
354 public $wTick = 10;
357 * The "Relation_Stats" constructor
359 * @param string master_table The master table name
360 * @param string master_field The relation field in the master table
361 * @param string foreign_table The foreign table name
362 * @param string foreigh_field The relation field in the foreign table
363 * @see Relation_Stats::_getXy
365 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
367 $src_pos = $this->_getXy($master_table, $master_field);
368 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
370 * [0] is x-left
371 * [1] is x-right
372 * [2] is y
374 $src_left = $src_pos[0] - $this->wTick;
375 $src_right = $src_pos[1] + $this->wTick;
376 $dest_left = $dest_pos[0] - $this->wTick;
377 $dest_right = $dest_pos[1] + $this->wTick;
379 $d1 = abs($src_left - $dest_left);
380 $d2 = abs($src_right - $dest_left);
381 $d3 = abs($src_left - $dest_right);
382 $d4 = abs($src_right - $dest_right);
383 $d = min($d1, $d2, $d3, $d4);
385 if ($d == $d1) {
386 $this->xSrc = $src_pos[0];
387 $this->srcDir = -1;
388 $this->xDest = $dest_pos[0];
389 $this->destDir = -1;
390 } elseif ($d == $d2) {
391 $this->xSrc = $src_pos[1];
392 $this->srcDir = 1;
393 $this->xDest = $dest_pos[0];
394 $this->destDir = -1;
395 } elseif ($d == $d3) {
396 $this->xSrc = $src_pos[0];
397 $this->srcDir = -1;
398 $this->xDest = $dest_pos[1];
399 $this->destDir = 1;
400 } else {
401 $this->xSrc = $src_pos[1];
402 $this->srcDir = 1;
403 $this->xDest = $dest_pos[1];
404 $this->destDir = 1;
406 $this->ySrc = $src_pos[2];
407 $this->yDest = $dest_pos[2];
411 * Gets arrows coordinates
413 * @param string table The current table name
414 * @param string column The relation column name
415 * @return array Arrows coordinates
416 * @access private
418 function _getXy($table, $column)
420 $pos = array_search($column, $table->fields);
421 // x_left, x_right, y
422 return array($table->x, $table->x + $table->width, $table->y + ($pos + 1.5) * $table->heightCell);
426 * draws relation links and arrows
427 * shows foreign key relations
429 * @param boolean changeColor Whether to use one color per relation or not
430 * @global object The current Visio XML document
431 * @access public
432 * @see PMA_VISIO
434 public function relationDraw($changeColor)
436 global $visio;
438 if ($changeColor) {
439 $listOfColors = array(
440 'red',
441 'grey',
442 'black',
443 'yellow',
444 'green',
445 'cyan',
446 ' orange'
448 shuffle($listOfColors);
449 $color = $listOfColors[0];
450 } else {
451 $color = 'black';
454 // code here for making connections b/w relation objects
459 * end of the "Relation_Stats" class
463 * Visio Relation Schema Class
465 * Purpose of this class is to generate the Visio XML .VDX Document
466 * which is used for representing the database diagrams in any version of MS Visio IDE.
467 * This class uses Software and Database Template and Database model diagram of Visio
468 * and with the combination of these objects actually helps in preparing Visio XML .VDX document.
470 * Visio XML is generated by using XMLWriter php extension and this class
471 * inherits Export_Relation_Schema class has common functionality added
472 * to this class
474 * @name Visio_Relation_Schema
475 * @copyright
476 * @license
478 class PMA_Visio_Relation_Schema extends PMA_Export_Relation_Schema
481 * The "PMA_Visio_Relation_Schema" constructor
483 * Upon instantiation This outputs the Visio XML document
484 * that user can download
486 * @return void
487 * @see PMA_VISIO,Table_Stats,Relation_Stats
489 function __construct()
491 global $visio,$db;
493 $this->setPageNumber($_POST['pdf_page_number']);
494 $this->setShowGrid(isset($_POST['show_grid']));
495 $this->setShowColor($_POST['show_color']);
496 $this->setShowKeys(isset($_POST['show_keys']));
497 $this->setOrientation(isset($_POST['orientation']));
498 $this->setPaper($_POST['paper']);
499 $this->setExportType($_POST['export_type']);
501 $visio = new PMA_VISIO();
502 $visio->setTitle(sprintf(__('Schema of the %s database - Page %s'), $db, $this->pageNumber));
503 $visio->SetAuthor('phpMyAdmin ' . PMA_VERSION);
504 $visio->startVisioDoc();
505 $alltables = $this->getAllTables($db,$this->pageNumber);
507 foreach ($alltables as $table) {
508 if (!isset($this->tables[$table])) {
509 $this->tables[$table] = new Table_Stats($table, $this->pageNumber, $this->showKeys);
513 $seen_a_relation = false;
514 foreach ($alltables as $one_table) {
515 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
516 if ($exist_rel) {
517 $seen_a_relation = true;
518 foreach ($exist_rel as $master_field => $rel) {
519 /* put the foreign table on the schema only if selected
520 * by the user
521 * (do not use array_search() because we would have to
522 * to do a === FALSE and this is not PHP3 compatible)
524 if (in_array($rel['foreign_table'], $alltables)) {
525 $this->_addRelation($one_table, $master_field, $rel['foreign_table'], $rel['foreign_field'],$this->showKeys);
530 $this->_drawTables($this->showColor);
532 if ($seen_a_relation) {
533 $this->_drawRelations($this->showColor);
535 $visio->endVisioDoc();
536 $visio->showOutput($db.'-'.$this->pageNumber);
537 exit();
541 * Defines relation objects
543 * @param string masterTable The master table name
544 * @param string masterField The relation field in the master table
545 * @param string foreignTable The foreign table name
546 * @param string foreignField The relation field in the foreign table
547 * @return void
548 * @access private
549 * @see Table_Stats::__construct(),Relation_Stats::__construct()
551 private function _addRelation($masterTable, $masterField, $foreignTable, $foreignField, $showKeys)
553 if (!isset($this->tables[$masterTable])) {
554 $this->tables[$masterTable] = new Table_Stats($masterTable, $this->pageNumber, $showKeys);
556 if (!isset($this->tables[$foreignTable])) {
557 $this->tables[$foreignTable] = new Table_Stats($foreignTable, $this->pageNumber, $showKeys);
559 $this->_relations[] = new Relation_Stats($this->tables[$masterTable], $masterField, $this->tables[$foreignTable], $foreignField);
563 * Draws relation references
565 * connects master table's master field to
566 * foreign table's forein field.
568 * @param boolean changeColor Whether to use one color per relation or not
569 * @return void
570 * @access private
571 * @see Relation_Stats::relationDraw()
573 private function _drawRelations($changeColor)
575 foreach ($this->_relations as $relation) {
576 $relation->relationDraw($changeColor);
581 * Draws tables
584 * @param boolean changeColor Whether to show color for tables text or not
585 * @return void
586 * @access private
587 * @see Table_Stats::tableDraw()
589 private function _drawTables($changeColor)
591 foreach ($this->tables as $table) {
592 $table->tableDraw($changeColor);