Translation update done using Pootle.
[phpmyadmin/dkf.git] / libraries / schema / Visio_Relation_Schema.class.php
blobd4aaa2f214441e1a38f484bf2d49f32a537aac7a
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @version $Id$
6 * @package phpMyAdmin
7 */
9 include_once("Export_Relation_Schema.class.php");
11 /**
12 * This Class inherits the XMLwriter class and
13 * helps in developing structure of MS Visio Schema Export
15 * @name PMA_VISIO
16 * @copyright
17 * @license
18 * @access public
19 * @see http://php.net/manual/en/book.xmlwriter.php
22 class PMA_VISIO extends XMLWriter
24 public $title;
25 public $author;
26 public $font;
27 public $fontSize;
29 /**
30 * The "PMA_VISIO" constructor
32 * Upon instantiation This starts writing the Visio XML .VDX document
34 * @return void
35 * @see XMLWriter::openMemory(),XMLWriter::setIndent(),XMLWriter::startDocument()
37 function __construct()
39 $this->openMemory();
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');
54 /**
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.
62 * @return void
63 * @access public
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();
76 /**
77 * Set document title
79 * @param string value sets the title text
80 * @return void
81 * @access public
83 function setTitle($value)
85 $this->title = $value;
88 /**
89 * Set document author
91 * @param string value sets the author
92 * @return void
93 * @access public
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
106 * @return void
107 * @access private
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>');
118 $this->endElement();
122 * Sets Visio XML .VDX Document Settings
124 * DocumentSettings tag contains elements that specify document settings.
126 * @return void
127 * @access private
128 * @see XMLWriter::startElement(),XMLWriter::endElement()
130 private function _documentSettings()
132 $this->startElement('DocumentSettings');
133 $this->endElement();
137 * Ends Visio XML Document
139 * @return void
140 * @access public
141 * @see XMLWriter::endElement(),XMLWriter::endDocument()
143 function endVisioDoc()
145 $this->endElement();
146 $this->endDocument();
150 * Output Visio XML .VDX Document for download
152 * @param string fileName name of the Visio XML document
153 * @return void
154 * @access public
155 * @see XMLWriter::flush()
157 function showOutput($fileName)
159 //if(ob_get_clean()){
160 //ob_end_clean();
162 header('Content-type: application/visio');
163 header('Content-Disposition: attachment; filename="'.$fileName.'.vdx"');
164 $output = $this->flush();
165 print $output;
171 * Draws tables schema
173 class Table_Stats
176 * Defines properties
179 private $_tableName;
180 private $_showInfo = false;
182 public $width = 0;
183 public $height;
184 public $fields = array();
185 public $heightCell = 0;
186 public $currentCell = 0;
187 public $x, $y;
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
202 * @access private
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));
218 * load fields
219 * check to see if it will load all fields or only the foreign keys
222 if ($showKeys) {
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);
229 } else {
230 while ($row = PMA_DBI_fetch_row($result)) {
231 $this->fields[] = $row[0];
235 $this->_showInfo = $showInfo;
237 // height and width
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;
247 // x and y
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;
261 // displayfield
262 $this->displayfield = PMA_getDisplayField($db, $tableName);
263 // index
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
278 * @access private
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
291 * @access private
292 * @see PMA_VISIO
294 function _setWidthTable($font,$fontSize)
296 global $visio;
301 * Sets the height of the table
303 * @access private
305 function _setHeightTable($fontSize)
307 $this->heightCell = $fontSize + 4;
308 $this->height = (count($this->fields) + 1) * $this->heightCell;
312 * draw the table
314 * @param boolean showColor Whether to display color
315 * @global object The current Visio XML document
316 * @access public
317 * @see PMA_VISIO
319 public function tableDraw($showColor)
321 global $visio;
322 //echo $this->_tableName.'<br />';
324 foreach ($this->fields as $field) {
325 $this->currentCell += $this->heightCell;
326 $showColor = 'none';
327 if ($showColor) {
328 if (in_array($field, $this->primary)) {
329 $showColor = '#0c0';
331 if ($field == $this->displayfield) {
332 $showColor = 'none';
335 // code here for drawing table diagrams
341 * Draws relation links
343 * @access public
344 * @see PMA_VISIO
346 class Relation_Stats
349 * Defines properties
351 public $xSrc, $ySrc;
352 public $srcDir ;
353 public $destDir;
354 public $xDest, $yDest;
355 public $wTick = 10;
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);
371 * [0] is x-left
372 * [1] is x-right
373 * [2] is y
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);
386 if ($d == $d1) {
387 $this->xSrc = $src_pos[0];
388 $this->srcDir = -1;
389 $this->xDest = $dest_pos[0];
390 $this->destDir = -1;
391 } elseif ($d == $d2) {
392 $this->xSrc = $src_pos[1];
393 $this->srcDir = 1;
394 $this->xDest = $dest_pos[0];
395 $this->destDir = -1;
396 } elseif ($d == $d3) {
397 $this->xSrc = $src_pos[0];
398 $this->srcDir = -1;
399 $this->xDest = $dest_pos[1];
400 $this->destDir = 1;
401 } else {
402 $this->xSrc = $src_pos[1];
403 $this->srcDir = 1;
404 $this->xDest = $dest_pos[1];
405 $this->destDir = 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
417 * @access private
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
432 * @access public
433 * @see PMA_VISIO
435 public function relationDraw($changeColor)
437 global $visio;
439 if ($changeColor) {
440 $listOfColors = array(
441 'red',
442 'grey',
443 'black',
444 'yellow',
445 'green',
446 'cyan',
447 ' orange'
449 shuffle($listOfColors);
450 $color = $listOfColors[0];
451 } else {
452 $color = 'black';
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
473 * to this class
475 * @name Visio_Relation_Schema
476 * @copyright
477 * @license
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
487 * @return void
488 * @see PMA_VISIO,Table_Stats,Relation_Stats
490 function __construct()
492 global $visio,$db;
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');
517 if ($exist_rel) {
518 $seen_a_relation = true;
519 foreach ($exist_rel as $master_field => $rel) {
520 /* put the foreign table on the schema only if selected
521 * by the user
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);
538 exit();
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
548 * @return void
549 * @access private
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
570 * @return void
571 * @access private
572 * @see Relation_Stats::relationDraw()
574 private function _drawRelations($changeColor)
576 foreach ($this->_relations as $relation) {
577 $relation->relationDraw($changeColor);
582 * Draws tables
585 * @param boolean changeColor Whether to show color for tables text or not
586 * @return void
587 * @access private
588 * @see Table_Stats::tableDraw()
590 private function _drawTables($changeColor)
592 foreach ($this->tables as $table) {
593 $table->tableDraw($changeColor);