Translation update done using Pootle.
[phpmyadmin/ammaryasirr.git] / libraries / schema / Visio_Relation_Schema.class.php
blobcfe4424a6e3dda659aa58646b208c13556b714bc
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 The font name
284 * @param integer fontSize The font size
285 * @global object The current Visio XML document
286 * @access private
287 * @see PMA_VISIO
289 function _setWidthTable($font,$fontSize)
291 global $visio;
296 * Sets the height of the table
298 * @access private
300 function _setHeightTable($fontSize)
302 $this->heightCell = $fontSize + 4;
303 $this->height = (count($this->fields) + 1) * $this->heightCell;
307 * draw the table
309 * @param boolean showColor Whether to display color
310 * @global object The current Visio XML document
311 * @access public
312 * @see PMA_VISIO
314 public function tableDraw($showColor)
316 global $visio;
317 //echo $this->_tableName.'<br />';
319 foreach ($this->fields as $field) {
320 $this->currentCell += $this->heightCell;
321 $showColor = 'none';
322 if ($showColor) {
323 if (in_array($field, $this->primary)) {
324 $showColor = '#0c0';
326 if ($field == $this->displayfield) {
327 $showColor = 'none';
330 // code here for drawing table diagrams
336 * Draws relation links
338 * @access public
339 * @see PMA_VISIO
341 class Relation_Stats
344 * Defines properties
346 public $xSrc, $ySrc;
347 public $srcDir ;
348 public $destDir;
349 public $xDest, $yDest;
350 public $wTick = 10;
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);
366 * [0] is x-left
367 * [1] is x-right
368 * [2] is y
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);
381 if ($d == $d1) {
382 $this->xSrc = $src_pos[0];
383 $this->srcDir = -1;
384 $this->xDest = $dest_pos[0];
385 $this->destDir = -1;
386 } elseif ($d == $d2) {
387 $this->xSrc = $src_pos[1];
388 $this->srcDir = 1;
389 $this->xDest = $dest_pos[0];
390 $this->destDir = -1;
391 } elseif ($d == $d3) {
392 $this->xSrc = $src_pos[0];
393 $this->srcDir = -1;
394 $this->xDest = $dest_pos[1];
395 $this->destDir = 1;
396 } else {
397 $this->xSrc = $src_pos[1];
398 $this->srcDir = 1;
399 $this->xDest = $dest_pos[1];
400 $this->destDir = 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
412 * @access private
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
427 * @access public
428 * @see PMA_VISIO
430 public function relationDraw($changeColor)
432 global $visio;
434 if ($changeColor) {
435 $listOfColors = array(
436 'red',
437 'grey',
438 'black',
439 'yellow',
440 'green',
441 'cyan',
442 ' orange'
444 shuffle($listOfColors);
445 $color = $listOfColors[0];
446 } else {
447 $color = 'black';
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
468 * to this class
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
480 * @return void
481 * @see PMA_VISIO,Table_Stats,Relation_Stats
483 function __construct()
485 global $visio,$db;
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');
510 if ($exist_rel) {
511 $seen_a_relation = true;
512 foreach ($exist_rel as $master_field => $rel) {
513 /* put the foreign table on the schema only if selected
514 * by the user
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);
531 exit();
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
541 * @return void
542 * @access private
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
563 * @return void
564 * @access private
565 * @see Relation_Stats::relationDraw()
567 private function _drawRelations($changeColor)
569 foreach ($this->_relations as $relation) {
570 $relation->relationDraw($changeColor);
575 * Draws tables
578 * @param boolean changeColor Whether to show color for tables text or not
579 * @return void
580 * @access private
581 * @see Table_Stats::tableDraw()
583 private function _drawTables($changeColor)
585 foreach ($this->tables as $table) {
586 $table->tableDraw($changeColor);