2 /* vim: set expandtab sw=4 ts=4 sts=4: */
8 include_once("Export_Relation_Schema.class.php");
11 * This Class inherits the XMLwriter class and
12 * helps in developing structure of MS Visio Schema Export
18 * @see http://php.net/manual/en/book.xmlwriter.php
21 class PMA_VISIO
extends XMLWriter
29 * The "PMA_VISIO" constructor
31 * Upon instantiation This starts writing the Visio XML .VDX document
34 * @see XMLWriter::openMemory(),XMLWriter::setIndent(),XMLWriter::startDocument()
36 function __construct()
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');
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.
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();
78 * @param string value sets the title text
82 function setTitle($value)
84 $this->title
= $value;
90 * @param string value sets the author
94 function setAuthor($value)
96 $this->author
= $value;
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
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>');
121 * Sets Visio XML .VDX Document Settings
123 * DocumentSettings tag contains elements that specify document settings.
127 * @see XMLWriter::startElement(),XMLWriter::endElement()
129 private function _documentSettings()
131 $this->startElement('DocumentSettings');
136 * Ends Visio XML Document
140 * @see XMLWriter::endElement(),XMLWriter::endDocument()
142 function endVisioDoc()
145 $this->endDocument();
149 * Output Visio XML .VDX Document for download
151 * @param string fileName name of the Visio XML document
154 * @see XMLWriter::flush()
156 function showOutput($fileName)
158 //if(ob_get_clean()){
161 header('Content-type: application/visio');
162 header('Content-Disposition: attachment; filename="'.$fileName.'.vdx"');
163 $output = $this->flush();
170 * Draws tables schema
179 private $_showInfo = false;
183 public $fields = array();
184 public $heightCell = 0;
185 public $currentCell = 0;
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
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));
218 * check to see if it will load all fields or only the foreign keys
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);
229 while ($row = PMA_DBI_fetch_row($result)) {
230 $this->fields
[] = $row[0];
234 $this->_showInfo
= $showInfo;
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
;
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
;
261 $this->displayfield
= PMA_getDisplayField($db, $tableName);
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
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
293 function _setWidthTable($font,$fontSize)
300 * Sets the height of the table
304 function _setHeightTable($fontSize)
306 $this->heightCell
= $fontSize +
4;
307 $this->height
= (count($this->fields
) +
1) * $this->heightCell
;
313 * @param boolean showColor Whether to display color
314 * @global object The current Visio XML document
318 public function tableDraw($showColor)
321 //echo $this->_tableName.'<br />';
323 foreach ($this->fields
as $field) {
324 $this->currentCell +
= $this->heightCell
;
327 if (in_array($field, $this->primary
)) {
330 if ($field == $this->displayfield
) {
334 // code here for drawing table diagrams
340 * Draws relation links
353 public $xDest, $yDest;
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);
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);
386 $this->xSrc
= $src_pos[0];
388 $this->xDest
= $dest_pos[0];
390 } elseif ($d == $d2) {
391 $this->xSrc
= $src_pos[1];
393 $this->xDest
= $dest_pos[0];
395 } elseif ($d == $d3) {
396 $this->xSrc
= $src_pos[0];
398 $this->xDest
= $dest_pos[1];
401 $this->xSrc
= $src_pos[1];
403 $this->xDest
= $dest_pos[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
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
434 public function relationDraw($changeColor)
439 $listOfColors = array(
448 shuffle($listOfColors);
449 $color = $listOfColors[0];
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
474 * @name Visio_Relation_Schema
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
487 * @see PMA_VISIO,Table_Stats,Relation_Stats
489 function __construct()
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');
517 $seen_a_relation = true;
518 foreach ($exist_rel as $master_field => $rel) {
519 /* put the foreign table on the schema only if selected
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
);
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
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
571 * @see Relation_Stats::relationDraw()
573 private function _drawRelations($changeColor)
575 foreach ($this->_relations
as $relation) {
576 $relation->relationDraw($changeColor);
584 * @param boolean changeColor Whether to show color for tables text or not
587 * @see Table_Stats::tableDraw()
589 private function _drawTables($changeColor)
591 foreach ($this->tables
as $table) {
592 $table->tableDraw($changeColor);