A few fixes for comments in Relation_Schema classes (they still require further work)
[phpmyadmin.git] / libraries / schema / Visio_Relation_Schema.class.php
blobcf7bab266adcba95fe7ec28ac34293be90fdffa8
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 * @access public
15 * @see http://php.net/manual/en/book.xmlwriter.php
18 class PMA_VISIO extends XMLWriter
20 public $title;
21 public $author;
22 public $font;
23 public $fontSize;
25 /**
26 * The "PMA_VISIO" constructor
28 * Upon instantiation This starts writing the Visio XML .VDX document
30 * @return void
31 * @see XMLWriter::openMemory(),XMLWriter::setIndent(),XMLWriter::startDocument()
33 function __construct()
35 $this->openMemory();
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');
50 /**
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.
58 * @return void
59 * @access public
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();
72 /**
73 * Set document title
75 * @param string value sets the title text
76 * @return void
77 * @access public
79 function setTitle($value)
81 $this->title = $value;
84 /**
85 * Set document author
87 * @param string value sets the author
88 * @return void
89 * @access public
91 function setAuthor($value)
93 $this->author = $value;
96 /**
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
102 * @return void
103 * @access private
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>');
114 $this->endElement();
118 * Sets Visio XML .VDX Document Settings
120 * DocumentSettings tag contains elements that specify document settings.
122 * @return void
123 * @access private
124 * @see XMLWriter::startElement(),XMLWriter::endElement()
126 private function _documentSettings()
128 $this->startElement('DocumentSettings');
129 $this->endElement();
133 * Ends Visio XML Document
135 * @return void
136 * @access public
137 * @see XMLWriter::endElement(),XMLWriter::endDocument()
139 function endVisioDoc()
141 $this->endElement();
142 $this->endDocument();
146 * Output Visio XML .VDX Document for download
148 * @param string fileName name of the Visio XML document
149 * @return void
150 * @access public
151 * @see XMLWriter::flush()
153 function showOutput($fileName)
155 //if(ob_get_clean()){
156 //ob_end_clean();
158 $output = $this->flush();
159 PMA_download_header($fileName . '.vdx', 'application/visio', strlen($output));
160 print $output;
166 * Draws tables schema
168 class Table_Stats
171 * Defines properties
174 private $_tableName;
175 private $_showInfo = false;
177 public $width = 0;
178 public $height;
179 public $fields = array();
180 public $heightCell = 0;
181 public $currentCell = 0;
182 public $x, $y;
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
197 * @access private
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));
213 * load fields
214 * check to see if it will load all fields or only the foreign keys
217 if ($showKeys) {
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);
224 } else {
225 while ($row = PMA_DBI_fetch_row($result)) {
226 $this->fields[] = $row[0];
230 $this->_showInfo = $showInfo;
232 // height and width
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;
242 // x and y
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;
256 // displayfield
257 $this->displayfield = PMA_getDisplayField($db, $tableName);
258 // index
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
273 * @access private
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 font name
284 * @param integer $fontSize font size
285 * @global object The current Visio XML document
286 * @see PMA_VISIO
288 private function _setWidthTable($font,$fontSize)
290 global $visio;
295 * Sets the height of the table
297 * @access private
299 function _setHeightTable($fontSize)
301 $this->heightCell = $fontSize + 4;
302 $this->height = (count($this->fields) + 1) * $this->heightCell;
306 * draw the table
308 * @param boolean showColor Whether to display color
309 * @global object The current Visio XML document
310 * @access public
311 * @see PMA_VISIO
313 public function tableDraw($showColor)
315 global $visio;
316 //echo $this->_tableName.'<br />';
318 foreach ($this->fields as $field) {
319 $this->currentCell += $this->heightCell;
320 $showColor = 'none';
321 if ($showColor) {
322 if (in_array($field, $this->primary)) {
323 $showColor = '#0c0';
325 if ($field == $this->displayfield) {
326 $showColor = 'none';
329 // code here for drawing table diagrams
335 * Draws relation links
337 * @access public
338 * @see PMA_VISIO
340 class Relation_Stats
343 * Defines properties
345 public $xSrc, $ySrc;
346 public $srcDir ;
347 public $destDir;
348 public $xDest, $yDest;
349 public $wTick = 10;
352 * The "Relation_Stats" constructor
354 * @param string master_table The master table name
355 * @param string master_field The relation field in the master table
356 * @param string foreign_table The foreign table name
357 * @param string foreigh_field The relation field in the foreign table
358 * @see Relation_Stats::_getXy
360 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
362 $src_pos = $this->_getXy($master_table, $master_field);
363 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
365 * [0] is x-left
366 * [1] is x-right
367 * [2] is y
369 $src_left = $src_pos[0] - $this->wTick;
370 $src_right = $src_pos[1] + $this->wTick;
371 $dest_left = $dest_pos[0] - $this->wTick;
372 $dest_right = $dest_pos[1] + $this->wTick;
374 $d1 = abs($src_left - $dest_left);
375 $d2 = abs($src_right - $dest_left);
376 $d3 = abs($src_left - $dest_right);
377 $d4 = abs($src_right - $dest_right);
378 $d = min($d1, $d2, $d3, $d4);
380 if ($d == $d1) {
381 $this->xSrc = $src_pos[0];
382 $this->srcDir = -1;
383 $this->xDest = $dest_pos[0];
384 $this->destDir = -1;
385 } elseif ($d == $d2) {
386 $this->xSrc = $src_pos[1];
387 $this->srcDir = 1;
388 $this->xDest = $dest_pos[0];
389 $this->destDir = -1;
390 } elseif ($d == $d3) {
391 $this->xSrc = $src_pos[0];
392 $this->srcDir = -1;
393 $this->xDest = $dest_pos[1];
394 $this->destDir = 1;
395 } else {
396 $this->xSrc = $src_pos[1];
397 $this->srcDir = 1;
398 $this->xDest = $dest_pos[1];
399 $this->destDir = 1;
401 $this->ySrc = $src_pos[2];
402 $this->yDest = $dest_pos[2];
406 * Gets arrows coordinates
408 * @param string table The current table name
409 * @param string column The relation column name
410 * @return array Arrows coordinates
411 * @access private
413 function _getXy($table, $column)
415 $pos = array_search($column, $table->fields);
416 // x_left, x_right, y
417 return array($table->x, $table->x + $table->width, $table->y + ($pos + 1.5) * $table->heightCell);
421 * draws relation links and arrows
422 * shows foreign key relations
424 * @param boolean changeColor Whether to use one color per relation or not
425 * @global object The current Visio XML document
426 * @access public
427 * @see PMA_VISIO
429 public function relationDraw($changeColor)
431 global $visio;
433 if ($changeColor) {
434 $listOfColors = array(
435 'red',
436 'grey',
437 'black',
438 'yellow',
439 'green',
440 'cyan',
441 ' orange'
443 shuffle($listOfColors);
444 $color = $listOfColors[0];
445 } else {
446 $color = 'black';
449 // code here for making connections b/w relation objects
454 * end of the "Relation_Stats" class
458 * Visio Relation Schema Class
460 * Purpose of this class is to generate the Visio XML .VDX Document
461 * which is used for representing the database diagrams in any version of MS Visio IDE.
462 * This class uses Software and Database Template and Database model diagram of Visio
463 * and with the combination of these objects actually helps in preparing Visio XML .VDX document.
465 * Visio XML is generated by using XMLWriter php extension and this class
466 * inherits Export_Relation_Schema class has common functionality added
467 * to this class
469 * @name Visio_Relation_Schema
471 class PMA_Visio_Relation_Schema extends PMA_Export_Relation_Schema
474 * The "PMA_Visio_Relation_Schema" constructor
476 * Upon instantiation This outputs the Visio XML document
477 * that user can download
479 * @return void
480 * @see PMA_VISIO,Table_Stats,Relation_Stats
482 function __construct()
484 global $visio,$db;
486 $this->setPageNumber($_POST['pdf_page_number']);
487 $this->setShowGrid(isset($_POST['show_grid']));
488 $this->setShowColor($_POST['show_color']);
489 $this->setShowKeys(isset($_POST['show_keys']));
490 $this->setOrientation(isset($_POST['orientation']));
491 $this->setPaper($_POST['paper']);
492 $this->setExportType($_POST['export_type']);
494 $visio = new PMA_VISIO();
495 $visio->setTitle(sprintf(__('Schema of the %s database - Page %s'), $db, $this->pageNumber));
496 $visio->SetAuthor('phpMyAdmin ' . PMA_VERSION);
497 $visio->startVisioDoc();
498 $alltables = $this->getAllTables($db,$this->pageNumber);
500 foreach ($alltables as $table) {
501 if (! isset($this->tables[$table])) {
502 $this->tables[$table] = new Table_Stats($table, $this->pageNumber, $this->showKeys);
506 $seen_a_relation = false;
507 foreach ($alltables as $one_table) {
508 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
509 if ($exist_rel) {
510 $seen_a_relation = true;
511 foreach ($exist_rel as $master_field => $rel) {
512 /* put the foreign table on the schema only if selected
513 * by the user
514 * (do not use array_search() because we would have to
515 * to do a === false and this is not PHP3 compatible)
517 if (in_array($rel['foreign_table'], $alltables)) {
518 $this->_addRelation($one_table, $master_field, $rel['foreign_table'], $rel['foreign_field'],$this->showKeys);
523 $this->_drawTables($this->showColor);
525 if ($seen_a_relation) {
526 $this->_drawRelations($this->showColor);
528 $visio->endVisioDoc();
529 $visio->showOutput($db.'-'.$this->pageNumber);
530 exit();
534 * Defines relation objects
536 * @param string masterTable The master table name
537 * @param string masterField The relation field in the master table
538 * @param string foreignTable The foreign table name
539 * @param string foreignField The relation field in the foreign table
540 * @return void
541 * @access private
542 * @see Table_Stats::__construct(),Relation_Stats::__construct()
544 private function _addRelation($masterTable, $masterField, $foreignTable, $foreignField, $showKeys)
546 if (! isset($this->tables[$masterTable])) {
547 $this->tables[$masterTable] = new Table_Stats($masterTable, $this->pageNumber, $showKeys);
549 if (! isset($this->tables[$foreignTable])) {
550 $this->tables[$foreignTable] = new Table_Stats($foreignTable, $this->pageNumber, $showKeys);
552 $this->_relations[] = new Relation_Stats($this->tables[$masterTable], $masterField, $this->tables[$foreignTable], $foreignField);
556 * Draws relation references
558 * connects master table's master field to
559 * foreign table's forein field.
561 * @param boolean changeColor Whether to use one color per relation or not
562 * @return void
563 * @access private
564 * @see Relation_Stats::relationDraw()
566 private function _drawRelations($changeColor)
568 foreach ($this->_relations as $relation) {
569 $relation->relationDraw($changeColor);
574 * Draws tables
577 * @param boolean changeColor Whether to show color for tables text or not
578 * @return void
579 * @access private
580 * @see Table_Stats::tableDraw()
582 private function _drawTables($changeColor)
584 foreach ($this->tables as $table) {
585 $table->tableDraw($changeColor);