Refresh .po files
[phpmyadmin.git] / libraries / schema / Visio_Relation_Schema.class.php
blob0b889e60a5df4034011ae29a9b42ee1841e4e877
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @package PhpMyAdmin
6 */
8 require_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
17 class PMA_VISIO extends XMLWriter
19 public $title;
20 public $author;
21 public $font;
22 public $fontSize;
24 /**
25 * The "PMA_VISIO" constructor
27 * Upon instantiation This starts writing the Visio XML .VDX document
29 * @return void
30 * @see XMLWriter::openMemory(),XMLWriter::setIndent(),XMLWriter::startDocument()
32 function __construct()
34 $this->openMemory();
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');
49 /**
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.
57 * @return void
58 * @access public
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();
72 /**
73 * Set document title
75 * @param string $value title text
77 * @return void
78 * @access public
80 function setTitle($value)
82 $this->title = $value;
85 /**
86 * Set document author
88 * @param string $value the author
90 * @return void
91 * @access public
93 function setAuthor($value)
95 $this->author = $value;
98 /**
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
104 * @return void
105 * @access private
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>');
116 $this->endElement();
120 * Sets Visio XML .VDX Document Settings
122 * DocumentSettings tag contains elements that specify document settings.
124 * @return void
125 * @access private
126 * @see XMLWriter::startElement(),XMLWriter::endElement()
128 private function _documentSettings()
130 $this->startElement('DocumentSettings');
131 $this->endElement();
135 * Ends Visio XML Document
137 * @return void
138 * @access public
139 * @see XMLWriter::endElement(),XMLWriter::endDocument()
141 function endVisioDoc()
143 $this->endElement();
144 $this->endDocument();
148 * Output Visio XML .VDX Document for download
150 * @param string $fileName name of the Visio XML document
152 * @return void
153 * @access public
154 * @see XMLWriter::flush()
156 function showOutput($fileName)
158 //if(ob_get_clean()){
159 //ob_end_clean();
161 $output = $this->flush();
162 PMA_download_header($fileName . '.vdx', 'application/visio', strlen($output));
163 print $output;
169 * Draws tables schema
171 class Table_Stats
174 * Defines properties
177 private $_tableName;
178 private $_showInfo = false;
180 public $width = 0;
181 public $height;
182 public $fields = array();
183 public $heightCell = 0;
184 public $currentCell = 0;
185 public $x, $y;
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
203 * @return void
204 * @access private
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)) {
216 $visio->dieSchema(
217 $pageNumber,
218 "VISIO",
219 sprintf(__('The %s table doesn\'t exist!'), $tableName)
224 * load fields
225 * check to see if it will load all fields or only the foreign keys
228 if ($showKeys) {
229 $indexes = PMA_Index::getFromTable($this->_tableName, $db);
230 $all_columns = array();
231 foreach ($indexes as $index) {
232 $all_columns = array_merge(
233 $all_columns,
234 array_flip(array_keys($index->getColumns()))
237 $this->fields = array_keys($all_columns);
238 } else {
239 while ($row = PMA_DBI_fetch_row($result)) {
240 $this->fields[] = $row[0];
244 $this->_showInfo = $showInfo;
246 // height and width
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;
256 // x and y
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)) {
266 $visio->dieSchema(
267 $pageNumber,
268 "VISIO",
269 sprintf(
270 __('Please configure the coordinates for table %s'),
271 $tableName
275 list($this->x, $this->y) = PMA_DBI_fetch_row($result);
276 $this->x = (double) $this->x;
277 $this->y = (double) $this->y;
278 // displayfield
279 $this->displayfield = PMA_getDisplayField($db, $tableName);
280 // index
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
295 * @return the title
296 * @access private
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
313 * @return void
314 * @see PMA_VISIO
316 private function _setWidthTable($font,$fontSize)
318 global $visio;
322 * Sets the height of the table
324 * @param integer $fontSize font size
326 * @return void
327 * @access private
329 function _setHeightTable($fontSize)
331 $this->heightCell = $fontSize + 4;
332 $this->height = (count($this->fields) + 1) * $this->heightCell;
336 * draw the table
338 * @param boolean $showColor Whether to display color
340 * @global object The current Visio XML document
342 * @return void
343 * @access public
344 * @see PMA_VISIO
346 public function tableDraw($showColor)
348 global $visio;
349 //echo $this->_tableName.'<br />';
351 foreach ($this->fields as $field) {
352 $this->currentCell += $this->heightCell;
353 $showColor = 'none';
354 if ($showColor) {
355 if (in_array($field, $this->primary)) {
356 $showColor = '#0c0';
358 if ($field == $this->displayfield) {
359 $showColor = 'none';
362 // code here for drawing table diagrams
368 * Draws relation links
370 * @access public
371 * @see PMA_VISIO
373 class Relation_Stats
376 * Defines properties
378 public $xSrc, $ySrc;
379 public $srcDir ;
380 public $destDir;
381 public $xDest, $yDest;
382 public $wTick = 10;
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
392 * @return void
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);
400 * [0] is x-left
401 * [1] is x-right
402 * [2] is y
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);
415 if ($d == $d1) {
416 $this->xSrc = $src_pos[0];
417 $this->srcDir = -1;
418 $this->xDest = $dest_pos[0];
419 $this->destDir = -1;
420 } elseif ($d == $d2) {
421 $this->xSrc = $src_pos[1];
422 $this->srcDir = 1;
423 $this->xDest = $dest_pos[0];
424 $this->destDir = -1;
425 } elseif ($d == $d3) {
426 $this->xSrc = $src_pos[0];
427 $this->srcDir = -1;
428 $this->xDest = $dest_pos[1];
429 $this->destDir = 1;
430 } else {
431 $this->xSrc = $src_pos[1];
432 $this->srcDir = 1;
433 $this->xDest = $dest_pos[1];
434 $this->destDir = 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
447 * @access private
449 function _getXy($table, $column)
451 $pos = array_search($column, $table->fields);
452 // x_left, x_right, y
453 return array(
454 $table->x,
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
467 * @return void
468 * @access public
469 * @see PMA_VISIO
471 public function relationDraw($changeColor)
473 global $visio;
475 if ($changeColor) {
476 $listOfColors = array(
477 'red',
478 'grey',
479 'black',
480 'yellow',
481 'green',
482 'cyan',
483 'orange'
485 shuffle($listOfColors);
486 $color = $listOfColors[0];
487 } else {
488 $color = 'black';
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
509 * to this class
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
521 * @return void
522 * @see PMA_VISIO,Table_Stats,Relation_Stats
524 function __construct()
526 global $visio,$db;
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');
551 if ($exist_rel) {
552 $seen_a_relation = true;
553 foreach ($exist_rel as $master_field => $rel) {
554 /* put the foreign table on the schema only if selected
555 * by the user
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)) {
560 $this->_addRelation(
561 $one_table,
562 $master_field,
563 $rel['foreign_table'],
564 $rel['foreign_field'],
565 $this->showKeys
571 $this->_drawTables($this->showColor);
573 if ($seen_a_relation) {
574 $this->_drawRelations($this->showColor);
576 $visio->endVisioDoc();
577 $visio->showOutput($db.'-'.$this->pageNumber);
578 exit();
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
590 * @return void
591 * @access private
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
618 * @return void
619 * @access private
620 * @see Relation_Stats::relationDraw()
622 private function _drawRelations($changeColor)
624 foreach ($this->_relations as $relation) {
625 $relation->relationDraw($changeColor);
630 * Draws tables
632 * @param boolean $changeColor Whether to show color for tables text or not
634 * @return void
635 * @access private
636 * @see Table_Stats::tableDraw()
638 private function _drawTables($changeColor)
640 foreach ($this->tables as $table) {
641 $table->tableDraw($changeColor);