2 /* vim: set expandtab sw=4 ts=4 sts=4: */
9 include_once("Export_Relation_Schema.class.php");
12 * This Class inherits the XMLwriter class and
13 * helps in developing structure of MS Visio Schema Export
19 * @see http://php.net/manual/en/book.xmlwriter.php
22 class PMA_VISIO
extends XMLWriter
30 * The "PMA_VISIO" constructor
32 * Upon instantiation This starts writing the Visio XML .VDX document
35 * @see XMLWriter::openMemory(),XMLWriter::setIndent(),XMLWriter::startDocument()
37 function __construct()
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');
55 * Starts Visio XML .VDX Document
57 * Visio XML document starts by first initializing VisioDocument tag
58 * then DocumentProperties & DocumentSettings contains all the
59 * attributes that needed to define the document. Order of elements
60 * should be maintained while generating XML of Visio.
64 * @see XMLWriter::startElement(),XMLWriter::writeAttribute(),_documentProperties,_documentSettings
66 function startVisioDoc()
68 $this->startElement('VisioDocument');
69 $this->writeAttribute('xmlns', 'http://schemas.microsoft.com/visio/2003/core');
70 $this->writeAttribute('xmlns:vx', 'http://schemas.microsoft.com/visio/2006/extension');
71 $this->writeAttribute('xml:space', 'preserve');
72 $this->_documentProperties();
73 $this->_documentSettings();
79 * @param string value sets the title text
83 function setTitle($value)
85 $this->title
= $value;
91 * @param string value sets the author
95 function setAuthor($value)
97 $this->author
= $value;
101 * Sets Visio XML .VDX Document Properties
103 * DocumentProperties tag contains document property elements such as
104 the document's Title,Subject,Creator and templates tags
108 * @see XMLWriter::startElement(),XMLWriter::endElement(),XMLWriter::writeRaw()
110 private function _documentProperties()
112 $this->startElement('DocumentProperties');
113 $this->writeRaw('<Title>'.$this->title
.'</Title>');
114 $this->writeRaw('<Subject>'.$this->title
.'</Subject>');
115 $this->writeRaw('<Creator>'.$this->author
.'</Creator>');
116 $this->writeRaw('<Company>phpMyAdmin</Company>');
117 $this->writeRaw('<Template>c:\program files\microsoft office\office12\1033\DBMODL_U.VST</Template>');
122 * Sets Visio XML .VDX Document Settings
124 * DocumentSettings tag contains elements that specify document settings.
128 * @see XMLWriter::startElement(),XMLWriter::endElement()
130 private function _documentSettings()
132 $this->startElement('DocumentSettings');
137 * Ends Visio XML Document
141 * @see XMLWriter::endElement(),XMLWriter::endDocument()
143 function endVisioDoc()
146 $this->endDocument();
150 * Output Visio XML .VDX Document for download
152 * @param string fileName name of the Visio XML document
155 * @see XMLWriter::flush()
157 function showOutput($fileName)
159 //if(ob_get_clean()){
162 header('Content-type: application/visio');
163 header('Content-Disposition: attachment; filename="'.$fileName.'.vdx"');
164 $output = $this->flush();
171 * Draws tables schema
180 private $_showInfo = false;
184 public $fields = array();
185 public $heightCell = 0;
186 public $currentCell = 0;
188 public $primary = array();
191 * The "Table_Stats" constructor
193 * @param string tableName The table name
194 * @param integer same_wide_width The max. with among tables
195 * @param boolean showKeys Whether to display keys or not
196 * @param boolean showInfo Whether to display table position or not
197 * @global object The current Visio XML document
198 * @global integer The current page number (from the
199 * $cfg['Servers'][$i]['table_coords'] table)
200 * @global array The relations settings
201 * @global string The current db name
203 * @see PMA_VISIO, Table_Stats::Table_Stats_setWidth,
204 Table_Stats::Table_Stats_setHeight
206 function __construct($tableName, $pageNumber, &$same_wide_width, $showKeys = false, $showInfo = false)
208 global $visio, $cfgRelation, $db;
210 $this->_tableName
= $tableName;
211 $sql = 'DESCRIBE ' . PMA_backquote($tableName);
212 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE
);
213 if (!$result ||
!PMA_DBI_num_rows($result)) {
214 $visio->dieSchema($pageNumber,"VISIO",sprintf(__('The %s table doesn\'t exist!'), $tableName));
219 * check to see if it will load all fields or only the foreign keys
223 $indexes = PMA_Index
::getFromTable($this->_tableName
, $db);
224 $all_columns = array();
225 foreach ($indexes as $index) {
226 $all_columns = array_merge($all_columns, array_flip(array_keys($index->getColumns())));
228 $this->fields
= array_keys($all_columns);
230 while ($row = PMA_DBI_fetch_row($result)) {
231 $this->fields
[] = $row[0];
235 $this->_showInfo
= $showInfo;
238 $this->_setHeightTable($fontSize);
240 // setWidth must me after setHeight, because title
241 // can include table height which changes table width
242 $this->_setWidthTable($font,$fontSize);
243 if ($same_wide_width < $this->width
) {
244 $same_wide_width = $this->width
;
248 $sql = 'SELECT x, y FROM '
249 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
250 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
251 . ' AND table_name = \'' . PMA_sqlAddslashes($tableName) . '\''
252 . ' AND pdf_page_number = ' . $pageNumber;
253 $result = PMA_query_as_controluser($sql, false, PMA_DBI_QUERY_STORE
);
255 if (!$result ||
!PMA_DBI_num_rows($result)) {
256 $visio->dieSchema($pageNumber,"VISIO",sprintf(__('Please configure the coordinates for table %s'), $tableName));
258 list($this->x
, $this->y
) = PMA_DBI_fetch_row($result);
259 $this->x
= (double) $this->x
;
260 $this->y
= (double) $this->y
;
262 $this->displayfield
= PMA_getDisplayField($db, $tableName);
264 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tableName) . ';', null, PMA_DBI_QUERY_STORE
);
265 if (PMA_DBI_num_rows($result) > 0) {
266 while ($row = PMA_DBI_fetch_assoc($result)) {
267 if ($row['Key_name'] == 'PRIMARY') {
268 $this->primary
[] = $row['Column_name'];
275 * Returns title of the current table,
276 * title can have the dimensions/co-ordinates of the table
280 private function _getTitle()
282 return ($this->_showInfo ?
sprintf('%.0f', $this->width
) . 'x' . sprintf('%.0f', $this->heightCell
) : '') . ' ' . $this->_tableName
;
286 * Sets the width of the table
288 * @param string font The font name
289 * @param integer fontSize The font size
290 * @global object The current Visio XML document
294 function _setWidthTable($font,$fontSize)
301 * Sets the height of the table
305 function _setHeightTable($fontSize)
307 $this->heightCell
= $fontSize +
4;
308 $this->height
= (count($this->fields
) +
1) * $this->heightCell
;
314 * @param boolean showColor Whether to display color
315 * @global object The current Visio XML document
319 public function tableDraw($showColor)
322 //echo $this->_tableName.'<br />';
324 foreach ($this->fields
as $field) {
325 $this->currentCell +
= $this->heightCell
;
328 if (in_array($field, $this->primary
)) {
331 if ($field == $this->displayfield
) {
335 // code here for drawing table diagrams
341 * Draws relation links
354 public $xDest, $yDest;
358 * The "Relation_Stats" constructor
360 * @param string master_table The master table name
361 * @param string master_field The relation field in the master table
362 * @param string foreign_table The foreign table name
363 * @param string foreigh_field The relation field in the foreign table
364 * @see Relation_Stats::_getXy
366 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
368 $src_pos = $this->_getXy($master_table, $master_field);
369 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
375 $src_left = $src_pos[0] - $this->wTick
;
376 $src_right = $src_pos[1] +
$this->wTick
;
377 $dest_left = $dest_pos[0] - $this->wTick
;
378 $dest_right = $dest_pos[1] +
$this->wTick
;
380 $d1 = abs($src_left - $dest_left);
381 $d2 = abs($src_right - $dest_left);
382 $d3 = abs($src_left - $dest_right);
383 $d4 = abs($src_right - $dest_right);
384 $d = min($d1, $d2, $d3, $d4);
387 $this->xSrc
= $src_pos[0];
389 $this->xDest
= $dest_pos[0];
391 } elseif ($d == $d2) {
392 $this->xSrc
= $src_pos[1];
394 $this->xDest
= $dest_pos[0];
396 } elseif ($d == $d3) {
397 $this->xSrc
= $src_pos[0];
399 $this->xDest
= $dest_pos[1];
402 $this->xSrc
= $src_pos[1];
404 $this->xDest
= $dest_pos[1];
407 $this->ySrc
= $src_pos[2];
408 $this->yDest
= $dest_pos[2];
412 * Gets arrows coordinates
414 * @param string table The current table name
415 * @param string column The relation column name
416 * @return array Arrows coordinates
419 function _getXy($table, $column)
421 $pos = array_search($column, $table->fields
);
422 // x_left, x_right, y
423 return array($table->x
, $table->x +
$table->width
, $table->y +
($pos +
1.5) * $table->heightCell
);
427 * draws relation links and arrows
428 * shows foreign key relations
430 * @param boolean changeColor Whether to use one color per relation or not
431 * @global object The current Visio XML document
435 public function relationDraw($changeColor)
440 $listOfColors = array(
449 shuffle($listOfColors);
450 $color = $listOfColors[0];
455 // code here for making connections b/w relation objects
460 * end of the "Relation_Stats" class
464 * Visio Relation Schema Class
466 * Purpose of this class is to generate the Visio XML .VDX Document
467 * which is used for representing the database diagrams in any version of MS Visio IDE.
468 * This class uses Software and Database Template and Database model diagram of Visio
469 * and with the combination of these objects actually helps in preparing Visio XML .VDX document.
471 * Visio XML is generated by using XMLWriter php extension and this class
472 * inherits Export_Relation_Schema class has common functionality added
475 * @name Visio_Relation_Schema
479 class PMA_Visio_Relation_Schema
extends PMA_Export_Relation_Schema
482 * The "PMA_Visio_Relation_Schema" constructor
484 * Upon instantiation This outputs the Visio XML document
485 * that user can download
488 * @see PMA_VISIO,Table_Stats,Relation_Stats
490 function __construct()
494 $this->setPageNumber($_POST['pdf_page_number']);
495 $this->setShowGrid(isset($_POST['show_grid']));
496 $this->setShowColor($_POST['show_color']);
497 $this->setShowKeys(isset($_POST['show_keys']));
498 $this->setOrientation(isset($_POST['orientation']));
499 $this->setPaper($_POST['paper']);
500 $this->setExportType($_POST['export_type']);
502 $visio = new PMA_VISIO();
503 $visio->setTitle(sprintf(__('Schema of the %s database - Page %s'), $db, $this->pageNumber
));
504 $visio->SetAuthor('phpMyAdmin ' . PMA_VERSION
);
505 $visio->startVisioDoc();
506 $alltables = $this->getAllTables($db,$this->pageNumber
);
508 foreach ($alltables as $table) {
509 if (!isset($this->tables
[$table])) {
510 $this->tables
[$table] = new Table_Stats($table, $this->pageNumber
, $this->showKeys
);
514 $seen_a_relation = false;
515 foreach ($alltables as $one_table) {
516 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
518 $seen_a_relation = true;
519 foreach ($exist_rel as $master_field => $rel) {
520 /* put the foreign table on the schema only if selected
522 * (do not use array_search() because we would have to
523 * to do a === FALSE and this is not PHP3 compatible)
525 if (in_array($rel['foreign_table'], $alltables)) {
526 $this->_addRelation($one_table, $master_field, $rel['foreign_table'], $rel['foreign_field'],$this->showKeys
);
531 $this->_drawTables($this->showColor
);
533 if ($seen_a_relation) {
534 $this->_drawRelations($this->showColor
);
536 $visio->endVisioDoc();
537 $visio->showOutput($db.'-'.$this->pageNumber
);
542 * Defines relation objects
544 * @param string masterTable The master table name
545 * @param string masterField The relation field in the master table
546 * @param string foreignTable The foreign table name
547 * @param string foreignField The relation field in the foreign table
550 * @see Table_Stats::__construct(),Relation_Stats::__construct()
552 private function _addRelation($masterTable, $masterField, $foreignTable, $foreignField, $showKeys)
554 if (!isset($this->tables
[$masterTable])) {
555 $this->tables
[$masterTable] = new Table_Stats($masterTable, $this->pageNumber
, $showKeys);
557 if (!isset($this->tables
[$foreignTable])) {
558 $this->tables
[$foreignTable] = new Table_Stats($foreignTable, $this->pageNumber
, $showKeys);
560 $this->_relations
[] = new Relation_Stats($this->tables
[$masterTable], $masterField, $this->tables
[$foreignTable], $foreignField);
564 * Draws relation references
566 * connects master table's master field to
567 * foreign table's forein field.
569 * @param boolean changeColor Whether to use one color per relation or not
572 * @see Relation_Stats::relationDraw()
574 private function _drawRelations($changeColor)
576 foreach ($this->_relations
as $relation) {
577 $relation->relationDraw($changeColor);
585 * @param boolean changeColor Whether to show color for tables text or not
588 * @see Table_Stats::tableDraw()
590 private function _drawTables($changeColor)
592 foreach ($this->tables
as $table) {
593 $table->tableDraw($changeColor);