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
15 * @see http://php.net/manual/en/book.xmlwriter.php
18 class PMA_VISIO
extends XMLWriter
26 * The "PMA_VISIO" constructor
28 * Upon instantiation This starts writing the Visio XML .VDX document
31 * @see XMLWriter::openMemory(),XMLWriter::setIndent(),XMLWriter::startDocument()
33 function __construct()
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');
51 * Starts Visio XML .VDX Document
53 * Visio XML document starts by first initializing VisioDocument tag
54 * then DocumentProperties & DocumentSettings contains all the
55 * attributes that needed to define the document. Order of elements
56 * should be maintained while generating XML of Visio.
60 * @see XMLWriter::startElement(),XMLWriter::writeAttribute(),_documentProperties,_documentSettings
62 function startVisioDoc()
64 $this->startElement('VisioDocument');
65 $this->writeAttribute('xmlns', 'http://schemas.microsoft.com/visio/2003/core');
66 $this->writeAttribute('xmlns:vx', 'http://schemas.microsoft.com/visio/2006/extension');
67 $this->writeAttribute('xml:space', 'preserve');
68 $this->_documentProperties();
69 $this->_documentSettings();
75 * @param string value sets the title text
79 function setTitle($value)
81 $this->title
= $value;
87 * @param string value sets the author
91 function setAuthor($value)
93 $this->author
= $value;
97 * Sets Visio XML .VDX Document Properties
99 * DocumentProperties tag contains document property elements such as
100 the document's Title,Subject,Creator and templates tags
104 * @see XMLWriter::startElement(),XMLWriter::endElement(),XMLWriter::writeRaw()
106 private function _documentProperties()
108 $this->startElement('DocumentProperties');
109 $this->writeRaw('<Title>'.$this->title
.'</Title>');
110 $this->writeRaw('<Subject>'.$this->title
.'</Subject>');
111 $this->writeRaw('<Creator>'.$this->author
.'</Creator>');
112 $this->writeRaw('<Company>phpMyAdmin</Company>');
113 $this->writeRaw('<Template>c:\program files\microsoft office\office12\1033\DBMODL_U.VST</Template>');
118 * Sets Visio XML .VDX Document Settings
120 * DocumentSettings tag contains elements that specify document settings.
124 * @see XMLWriter::startElement(),XMLWriter::endElement()
126 private function _documentSettings()
128 $this->startElement('DocumentSettings');
133 * Ends Visio XML Document
137 * @see XMLWriter::endElement(),XMLWriter::endDocument()
139 function endVisioDoc()
142 $this->endDocument();
146 * Output Visio XML .VDX Document for download
148 * @param string fileName name of the Visio XML document
151 * @see XMLWriter::flush()
153 function showOutput($fileName)
155 //if(ob_get_clean()){
158 $output = $this->flush();
159 PMA_download_header($fileName . '.vdx', 'application/visio', strlen($output));
166 * Draws tables schema
175 private $_showInfo = false;
179 public $fields = array();
180 public $heightCell = 0;
181 public $currentCell = 0;
183 public $primary = array();
186 * The "Table_Stats" constructor
188 * @param string tableName The table name
189 * @param integer same_wide_width The max. with among tables
190 * @param boolean showKeys Whether to display keys or not
191 * @param boolean showInfo Whether to display table position or not
192 * @global object The current Visio XML document
193 * @global integer The current page number (from the
194 * $cfg['Servers'][$i]['table_coords'] table)
195 * @global array The relations settings
196 * @global string The current db name
198 * @see PMA_VISIO, Table_Stats::Table_Stats_setWidth,
199 Table_Stats::Table_Stats_setHeight
201 function __construct($tableName, $pageNumber, &$same_wide_width, $showKeys = false, $showInfo = false)
203 global $visio, $cfgRelation, $db;
205 $this->_tableName
= $tableName;
206 $sql = 'DESCRIBE ' . PMA_backquote($tableName);
207 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE
);
208 if (!$result ||
!PMA_DBI_num_rows($result)) {
209 $visio->dieSchema($pageNumber,"VISIO",sprintf(__('The %s table doesn\'t exist!'), $tableName));
214 * check to see if it will load all fields or only the foreign keys
218 $indexes = PMA_Index
::getFromTable($this->_tableName
, $db);
219 $all_columns = array();
220 foreach ($indexes as $index) {
221 $all_columns = array_merge($all_columns, array_flip(array_keys($index->getColumns())));
223 $this->fields
= array_keys($all_columns);
225 while ($row = PMA_DBI_fetch_row($result)) {
226 $this->fields
[] = $row[0];
230 $this->_showInfo
= $showInfo;
233 $this->_setHeightTable($fontSize);
235 // setWidth must me after setHeight, because title
236 // can include table height which changes table width
237 $this->_setWidthTable($font,$fontSize);
238 if ($same_wide_width < $this->width
) {
239 $same_wide_width = $this->width
;
243 $sql = 'SELECT x, y FROM '
244 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
245 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
246 . ' AND table_name = \'' . PMA_sqlAddSlashes($tableName) . '\''
247 . ' AND pdf_page_number = ' . $pageNumber;
248 $result = PMA_query_as_controluser($sql, false, PMA_DBI_QUERY_STORE
);
250 if (!$result ||
!PMA_DBI_num_rows($result)) {
251 $visio->dieSchema($pageNumber,"VISIO",sprintf(__('Please configure the coordinates for table %s'), $tableName));
253 list($this->x
, $this->y
) = PMA_DBI_fetch_row($result);
254 $this->x
= (double) $this->x
;
255 $this->y
= (double) $this->y
;
257 $this->displayfield
= PMA_getDisplayField($db, $tableName);
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'];
270 * Returns title of the current table,
271 * title can have the dimensions/co-ordinates of the table
275 private function _getTitle()
277 return ($this->_showInfo ?
sprintf('%.0f', $this->width
) . 'x' . sprintf('%.0f', $this->heightCell
) : '') . ' ' . $this->_tableName
;
281 * Sets the width of the table
283 * @param string font The font name
284 * @param integer fontSize The font size
285 * @global object The current Visio XML document
289 function _setWidthTable($font,$fontSize)
296 * Sets the height of the table
300 function _setHeightTable($fontSize)
302 $this->heightCell
= $fontSize +
4;
303 $this->height
= (count($this->fields
) +
1) * $this->heightCell
;
309 * @param boolean showColor Whether to display color
310 * @global object The current Visio XML document
314 public function tableDraw($showColor)
317 //echo $this->_tableName.'<br />';
319 foreach ($this->fields
as $field) {
320 $this->currentCell +
= $this->heightCell
;
323 if (in_array($field, $this->primary
)) {
326 if ($field == $this->displayfield
) {
330 // code here for drawing table diagrams
336 * Draws relation links
349 public $xDest, $yDest;
353 * The "Relation_Stats" constructor
355 * @param string master_table The master table name
356 * @param string master_field The relation field in the master table
357 * @param string foreign_table The foreign table name
358 * @param string foreigh_field The relation field in the foreign table
359 * @see Relation_Stats::_getXy
361 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
363 $src_pos = $this->_getXy($master_table, $master_field);
364 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
370 $src_left = $src_pos[0] - $this->wTick
;
371 $src_right = $src_pos[1] +
$this->wTick
;
372 $dest_left = $dest_pos[0] - $this->wTick
;
373 $dest_right = $dest_pos[1] +
$this->wTick
;
375 $d1 = abs($src_left - $dest_left);
376 $d2 = abs($src_right - $dest_left);
377 $d3 = abs($src_left - $dest_right);
378 $d4 = abs($src_right - $dest_right);
379 $d = min($d1, $d2, $d3, $d4);
382 $this->xSrc
= $src_pos[0];
384 $this->xDest
= $dest_pos[0];
386 } elseif ($d == $d2) {
387 $this->xSrc
= $src_pos[1];
389 $this->xDest
= $dest_pos[0];
391 } elseif ($d == $d3) {
392 $this->xSrc
= $src_pos[0];
394 $this->xDest
= $dest_pos[1];
397 $this->xSrc
= $src_pos[1];
399 $this->xDest
= $dest_pos[1];
402 $this->ySrc
= $src_pos[2];
403 $this->yDest
= $dest_pos[2];
407 * Gets arrows coordinates
409 * @param string table The current table name
410 * @param string column The relation column name
411 * @return array Arrows coordinates
414 function _getXy($table, $column)
416 $pos = array_search($column, $table->fields
);
417 // x_left, x_right, y
418 return array($table->x
, $table->x +
$table->width
, $table->y +
($pos +
1.5) * $table->heightCell
);
422 * draws relation links and arrows
423 * shows foreign key relations
425 * @param boolean changeColor Whether to use one color per relation or not
426 * @global object The current Visio XML document
430 public function relationDraw($changeColor)
435 $listOfColors = array(
444 shuffle($listOfColors);
445 $color = $listOfColors[0];
450 // code here for making connections b/w relation objects
455 * end of the "Relation_Stats" class
459 * Visio Relation Schema Class
461 * Purpose of this class is to generate the Visio XML .VDX Document
462 * which is used for representing the database diagrams in any version of MS Visio IDE.
463 * This class uses Software and Database Template and Database model diagram of Visio
464 * and with the combination of these objects actually helps in preparing Visio XML .VDX document.
466 * Visio XML is generated by using XMLWriter php extension and this class
467 * inherits Export_Relation_Schema class has common functionality added
470 * @name Visio_Relation_Schema
472 class PMA_Visio_Relation_Schema
extends PMA_Export_Relation_Schema
475 * The "PMA_Visio_Relation_Schema" constructor
477 * Upon instantiation This outputs the Visio XML document
478 * that user can download
481 * @see PMA_VISIO,Table_Stats,Relation_Stats
483 function __construct()
487 $this->setPageNumber($_POST['pdf_page_number']);
488 $this->setShowGrid(isset($_POST['show_grid']));
489 $this->setShowColor($_POST['show_color']);
490 $this->setShowKeys(isset($_POST['show_keys']));
491 $this->setOrientation(isset($_POST['orientation']));
492 $this->setPaper($_POST['paper']);
493 $this->setExportType($_POST['export_type']);
495 $visio = new PMA_VISIO();
496 $visio->setTitle(sprintf(__('Schema of the %s database - Page %s'), $db, $this->pageNumber
));
497 $visio->SetAuthor('phpMyAdmin ' . PMA_VERSION
);
498 $visio->startVisioDoc();
499 $alltables = $this->getAllTables($db,$this->pageNumber
);
501 foreach ($alltables as $table) {
502 if (! isset($this->tables
[$table])) {
503 $this->tables
[$table] = new Table_Stats($table, $this->pageNumber
, $this->showKeys
);
507 $seen_a_relation = false;
508 foreach ($alltables as $one_table) {
509 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
511 $seen_a_relation = true;
512 foreach ($exist_rel as $master_field => $rel) {
513 /* put the foreign table on the schema only if selected
515 * (do not use array_search() because we would have to
516 * to do a === false and this is not PHP3 compatible)
518 if (in_array($rel['foreign_table'], $alltables)) {
519 $this->_addRelation($one_table, $master_field, $rel['foreign_table'], $rel['foreign_field'],$this->showKeys
);
524 $this->_drawTables($this->showColor
);
526 if ($seen_a_relation) {
527 $this->_drawRelations($this->showColor
);
529 $visio->endVisioDoc();
530 $visio->showOutput($db.'-'.$this->pageNumber
);
535 * Defines relation objects
537 * @param string masterTable The master table name
538 * @param string masterField The relation field in the master table
539 * @param string foreignTable The foreign table name
540 * @param string foreignField The relation field in the foreign table
543 * @see Table_Stats::__construct(),Relation_Stats::__construct()
545 private function _addRelation($masterTable, $masterField, $foreignTable, $foreignField, $showKeys)
547 if (! isset($this->tables
[$masterTable])) {
548 $this->tables
[$masterTable] = new Table_Stats($masterTable, $this->pageNumber
, $showKeys);
550 if (! isset($this->tables
[$foreignTable])) {
551 $this->tables
[$foreignTable] = new Table_Stats($foreignTable, $this->pageNumber
, $showKeys);
553 $this->_relations
[] = new Relation_Stats($this->tables
[$masterTable], $masterField, $this->tables
[$foreignTable], $foreignField);
557 * Draws relation references
559 * connects master table's master field to
560 * foreign table's forein field.
562 * @param boolean changeColor Whether to use one color per relation or not
565 * @see Relation_Stats::relationDraw()
567 private function _drawRelations($changeColor)
569 foreach ($this->_relations
as $relation) {
570 $relation->relationDraw($changeColor);
578 * @param boolean changeColor Whether to show color for tables text or not
581 * @see Table_Stats::tableDraw()
583 private function _drawTables($changeColor)
585 foreach ($this->tables
as $table) {
586 $table->tableDraw($changeColor);