2 /* vim: set expandtab sw=4 ts=4 sts=4: */
8 require_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
17 class PMA_VISIO
extends XMLWriter
25 * The "PMA_VISIO" constructor
27 * Upon instantiation This starts writing the Visio XML .VDX document
30 * @see XMLWriter::openMemory(),XMLWriter::setIndent(),XMLWriter::startDocument()
32 function __construct()
36 * Set indenting using three spaces,
37 * so output is formatted
40 $this->setIndent(true);
41 $this->setIndentString(' ');
43 * Create the XML document
46 $this->startDocument('1.0', 'UTF-8');
50 * Starts Visio XML .VDX Document
52 * Visio XML document starts by first initializing VisioDocument tag
53 * then DocumentProperties & DocumentSettings contains all the
54 * attributes that needed to define the document. Order of elements
55 * should be maintained while generating XML of Visio.
59 * @see XMLWriter::startElement(), XMLWriter::writeAttribute(),
60 * _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 title text
80 function setTitle($value)
82 $this->title
= $value;
88 * @param string $value the author
93 function setAuthor($value)
95 $this->author
= $value;
99 * Sets Visio XML .VDX Document Properties
101 * DocumentProperties tag contains document property elements such as
102 the document's Title,Subject,Creator and templates tags
106 * @see XMLWriter::startElement(),XMLWriter::endElement(),XMLWriter::writeRaw()
108 private function _documentProperties()
110 $this->startElement('DocumentProperties');
111 $this->writeRaw('<Title>'.$this->title
.'</Title>');
112 $this->writeRaw('<Subject>'.$this->title
.'</Subject>');
113 $this->writeRaw('<Creator>'.$this->author
.'</Creator>');
114 $this->writeRaw('<Company>phpMyAdmin</Company>');
115 $this->writeRaw('<Template>c:\program files\microsoft office\office12\1033\DBMODL_U.VST</Template>');
120 * Sets Visio XML .VDX Document Settings
122 * DocumentSettings tag contains elements that specify document settings.
126 * @see XMLWriter::startElement(),XMLWriter::endElement()
128 private function _documentSettings()
130 $this->startElement('DocumentSettings');
135 * Ends Visio XML Document
139 * @see XMLWriter::endElement(),XMLWriter::endDocument()
141 function endVisioDoc()
144 $this->endDocument();
148 * Output Visio XML .VDX Document for download
150 * @param string $fileName name of the Visio XML document
154 * @see XMLWriter::flush()
156 function showOutput($fileName)
158 //if(ob_get_clean()){
161 $output = $this->flush();
162 PMA_download_header($fileName . '.vdx', 'application/visio', strlen($output));
169 * Draws tables schema
178 private $_showInfo = false;
182 public $fields = array();
183 public $heightCell = 0;
184 public $currentCell = 0;
186 public $primary = array();
189 * The "Table_Stats" constructor
191 * @param string $tableName The table name
192 * @param integer $pageNumber Page number
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
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
205 * @see PMA_VISIO, Table_Stats::Table_Stats_setWidth,
206 * Table_Stats::Table_Stats_setHeight
208 function __construct($tableName, $pageNumber, &$same_wide_width, $showKeys = false, $showInfo = false)
210 global $visio, $cfgRelation, $db;
212 $this->_tableName
= $tableName;
213 $sql = 'DESCRIBE ' . PMA_backquote($tableName);
214 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE
);
215 if (!$result ||
!PMA_DBI_num_rows($result)) {
219 sprintf(__('The %s table doesn\'t exist!'), $tableName)
225 * check to see if it will load all fields or only the foreign keys
229 $indexes = PMA_Index
::getFromTable($this->_tableName
, $db);
230 $all_columns = array();
231 foreach ($indexes as $index) {
232 $all_columns = array_merge(
234 array_flip(array_keys($index->getColumns()))
237 $this->fields
= array_keys($all_columns);
239 while ($row = PMA_DBI_fetch_row($result)) {
240 $this->fields
[] = $row[0];
244 $this->_showInfo
= $showInfo;
247 $this->_setHeightTable($fontSize);
249 // setWidth must me after setHeight, because title
250 // can include table height which changes table width
251 $this->_setWidthTable($font, $fontSize);
252 if ($same_wide_width < $this->width
) {
253 $same_wide_width = $this->width
;
257 $sql = 'SELECT x, y FROM '
258 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.'
259 . PMA_backquote($cfgRelation['table_coords'])
260 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
261 . ' AND table_name = \'' . PMA_sqlAddSlashes($tableName) . '\''
262 . ' AND pdf_page_number = ' . $pageNumber;
263 $result = PMA_query_as_controluser($sql, false, PMA_DBI_QUERY_STORE
);
265 if (!$result ||
!PMA_DBI_num_rows($result)) {
270 __('Please configure the coordinates for table %s'),
275 list($this->x
, $this->y
) = PMA_DBI_fetch_row($result);
276 $this->x
= (double) $this->x
;
277 $this->y
= (double) $this->y
;
279 $this->displayfield
= PMA_getDisplayField($db, $tableName);
281 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tableName) . ';', null, PMA_DBI_QUERY_STORE
);
282 if (PMA_DBI_num_rows($result) > 0) {
283 while ($row = PMA_DBI_fetch_assoc($result)) {
284 if ($row['Key_name'] == 'PRIMARY') {
285 $this->primary
[] = $row['Column_name'];
292 * Returns title of the current table,
293 * title can have the dimensions/co-ordinates of the table
298 private function _getTitle()
300 return ($this->_showInfo
301 ?
sprintf('%.0f', $this->width
) . 'x' . sprintf('%.0f', $this->heightCell
)
302 : '') . ' ' . $this->_tableName
;
306 * Sets the width of the table
308 * @param string $font font name
309 * @param integer $fontSize font size
311 * @global object The current Visio XML document
316 private function _setWidthTable($font,$fontSize)
322 * Sets the height of the table
324 * @param integer $fontSize font size
329 function _setHeightTable($fontSize)
331 $this->heightCell
= $fontSize +
4;
332 $this->height
= (count($this->fields
) +
1) * $this->heightCell
;
338 * @param boolean $showColor Whether to display color
340 * @global object The current Visio XML document
346 public function tableDraw($showColor)
349 //echo $this->_tableName.'<br />';
351 foreach ($this->fields
as $field) {
352 $this->currentCell +
= $this->heightCell
;
355 if (in_array($field, $this->primary
)) {
358 if ($field == $this->displayfield
) {
362 // code here for drawing table diagrams
368 * Draws relation links
381 public $xDest, $yDest;
385 * The "Relation_Stats" constructor
387 * @param string $master_table The master table name
388 * @param string $master_field The relation field in the master table
389 * @param string $foreign_table The foreign table name
390 * @param string $foreign_field The relation field in the foreign table
393 * @see Relation_Stats::_getXy
395 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
397 $src_pos = $this->_getXy($master_table, $master_field);
398 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
404 $src_left = $src_pos[0] - $this->wTick
;
405 $src_right = $src_pos[1] +
$this->wTick
;
406 $dest_left = $dest_pos[0] - $this->wTick
;
407 $dest_right = $dest_pos[1] +
$this->wTick
;
409 $d1 = abs($src_left - $dest_left);
410 $d2 = abs($src_right - $dest_left);
411 $d3 = abs($src_left - $dest_right);
412 $d4 = abs($src_right - $dest_right);
413 $d = min($d1, $d2, $d3, $d4);
416 $this->xSrc
= $src_pos[0];
418 $this->xDest
= $dest_pos[0];
420 } elseif ($d == $d2) {
421 $this->xSrc
= $src_pos[1];
423 $this->xDest
= $dest_pos[0];
425 } elseif ($d == $d3) {
426 $this->xSrc
= $src_pos[0];
428 $this->xDest
= $dest_pos[1];
431 $this->xSrc
= $src_pos[1];
433 $this->xDest
= $dest_pos[1];
436 $this->ySrc
= $src_pos[2];
437 $this->yDest
= $dest_pos[2];
441 * Gets arrows coordinates
443 * @param string $table The current table name
444 * @param string $column The relation column name
446 * @return array Arrows coordinates
449 function _getXy($table, $column)
451 $pos = array_search($column, $table->fields
);
452 // x_left, x_right, y
455 $table->x +
$table->width
,
456 $table->y +
($pos +
1.5) * $table->heightCell
461 * draws relation links and arrows shows foreign key relations
463 * @param boolean $changeColor Whether to use one color per relation or not
465 * @global object The current Visio XML document
471 public function relationDraw($changeColor)
476 $listOfColors = array(
485 shuffle($listOfColors);
486 $color = $listOfColors[0];
491 // code here for making connections b/w relation objects
495 * end of the "Relation_Stats" class
499 * Visio Relation Schema Class
501 * Purpose of this class is to generate the Visio XML .VDX Document which is used
502 * for representing the database diagrams in any version of MS Visio IDE.
503 * This class uses Software and Database Template and Database model diagram of
504 * Visio and with the combination of these objects actually helps in preparing
505 * Visio XML .VDX document.
507 * Visio XML is generated by using XMLWriter php extension and this class
508 * inherits Export_Relation_Schema class has common functionality added
511 * @name Visio_Relation_Schema
513 class PMA_Visio_Relation_Schema
extends PMA_Export_Relation_Schema
516 * The "PMA_Visio_Relation_Schema" constructor
518 * Upon instantiation This outputs the Visio XML document
519 * that user can download
522 * @see PMA_VISIO,Table_Stats,Relation_Stats
524 function __construct()
528 $this->setPageNumber($_POST['pdf_page_number']);
529 $this->setShowGrid(isset($_POST['show_grid']));
530 $this->setShowColor($_POST['show_color']);
531 $this->setShowKeys(isset($_POST['show_keys']));
532 $this->setOrientation(isset($_POST['orientation']));
533 $this->setPaper($_POST['paper']);
534 $this->setExportType($_POST['export_type']);
536 $visio = new PMA_VISIO();
537 $visio->setTitle(sprintf(__('Schema of the %s database - Page %s'), $db, $this->pageNumber
));
538 $visio->SetAuthor('phpMyAdmin ' . PMA_VERSION
);
539 $visio->startVisioDoc();
540 $alltables = $this->getAllTables($db, $this->pageNumber
);
542 foreach ($alltables as $table) {
543 if (! isset($this->tables
[$table])) {
544 $this->tables
[$table] = new Table_Stats($table, $this->pageNumber
, $this->showKeys
);
548 $seen_a_relation = false;
549 foreach ($alltables as $one_table) {
550 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
552 $seen_a_relation = true;
553 foreach ($exist_rel as $master_field => $rel) {
554 /* put the foreign table on the schema only if selected
556 * (do not use array_search() because we would have to
557 * to do a === false and this is not PHP3 compatible)
559 if (in_array($rel['foreign_table'], $alltables)) {
563 $rel['foreign_table'],
564 $rel['foreign_field'],
571 $this->_drawTables($this->showColor
);
573 if ($seen_a_relation) {
574 $this->_drawRelations($this->showColor
);
576 $visio->endVisioDoc();
577 $visio->showOutput($db.'-'.$this->pageNumber
);
582 * Defines relation objects
584 * @param string $masterTable The master table name
585 * @param string $masterField The relation field in the master table
586 * @param string $foreignTable The foreign table name
587 * @param string $foreignField The relation field in the foreign table
588 * @param boolean $showKeys Whether to display keys or not
592 * @see Table_Stats::__construct(), Relation_Stats::__construct()
594 private function _addRelation($masterTable, $masterField, $foreignTable, $foreignField, $showKeys)
596 if (! isset($this->tables
[$masterTable])) {
597 $this->tables
[$masterTable] = new Table_Stats(
598 $masterTable, $this->pageNumber
, $showKeys
601 if (! isset($this->tables
[$foreignTable])) {
602 $this->tables
[$foreignTable] = new Table_Stats(
603 $foreignTable, $this->pageNumber
, $showKeys
606 $this->_relations
[] = new Relation_Stats(
607 $this->tables
[$masterTable], $masterField,
608 $this->tables
[$foreignTable], $foreignField
613 * Draws relation references
614 * connects master table's master field to foreign table's forein field.
616 * @param boolean $changeColor Whether to use one color per relation or not
620 * @see Relation_Stats::relationDraw()
622 private function _drawRelations($changeColor)
624 foreach ($this->_relations
as $relation) {
625 $relation->relationDraw($changeColor);
632 * @param boolean $changeColor Whether to show color for tables text or not
636 * @see Table_Stats::tableDraw()
638 private function _drawTables($changeColor)
640 foreach ($this->tables
as $table) {
641 $table->tableDraw($changeColor);