2 /* vim: set expandtab sw=4 ts=4 sts=4: */
7 if (! defined('PHPMYADMIN')) {
11 require_once 'Export_Relation_Schema.class.php';
14 * This Class is EPS Library and
15 * helps in developing structure of EPS Schema Export
18 * @see http://php.net/manual/en/book.xmlwriter.php
25 public $stringCommands;
28 * The "PMA_EPS" constructor
30 * Upon instantiation This starts writing the EPS Document.
31 * %!PS-Adobe-3.0 EPSF-3.0 This is the MUST first comment to include
32 * it shows/tells that the Post Script document is purely under
33 * Document Structuring Convention [DSC] and is Compliant
34 * Encapsulated Post Script Document
39 function __construct()
41 $this->stringCommands
= "";
42 $this->stringCommands
.= "%!PS-Adobe-3.0 EPSF-3.0 \n";
48 * @param string $value sets the title text
54 function setTitle($value)
56 $this->stringCommands
.= '%%Title: ' . $value . "\n";
62 * @param string $value sets the author
68 function setAuthor($value)
70 $this->stringCommands
.= '%%Creator: ' . $value . "\n";
74 * Set document creation date
76 * @param string $value sets the date
82 function setDate($value)
84 $this->stringCommands
.= '%%CreationDate: ' . $value . "\n";
88 * Set document orientation
90 * @param string $value sets the author
96 function setOrientation($value)
98 $this->stringCommands
.= "%%PageOrder: Ascend \n";
100 $value = "Landscape";
101 $this->stringCommands
.= '%%Orientation: ' . $value . "\n";
104 $this->stringCommands
.= '%%Orientation: ' . $value . "\n";
106 $this->stringCommands
.= "%%EndComments \n";
107 $this->stringCommands
.= "%%Pages 1 \n";
108 $this->stringCommands
.= "%%BoundingBox: 72 150 144 170 \n";
112 * Set the font and size
114 * font can be set whenever needed in EPS
116 * @param string $value sets the font name e.g Arial
117 * @param integer $size sets the size of the font e.g 10
123 function setFont($value, $size)
125 $this->font
= $value;
126 $this->fontSize
= $size;
127 $this->stringCommands
.= "/" . $value . " findfont % Get the basic font\n";
128 $this->stringCommands
.= "" . $size . " scalefont % Scale the font to $size points\n";
129 $this->stringCommands
.= "setfont % Make it the current font\n";
135 * @return string return the font name e.g Arial
146 * @return string return the size of the font e.g 10
149 function getFontSize()
151 return $this->fontSize
;
157 * drawing the lines from x,y source to x,y destination and set the
158 * width of the line. lines helps in showing relationships of tables
160 * @param integer $x_from The x_from attribute defines the start
161 * left position of the element
162 * @param integer $y_from The y_from attribute defines the start
163 * right position of the element
164 * @param integer $x_to The x_to attribute defines the end
165 * left position of the element
166 * @param integer $y_to The y_to attribute defines the end
167 * right position of the element
168 * @param integer $lineWidth Sets the width of the line e.g 2
174 function line($x_from = 0, $y_from = 0, $x_to = 0, $y_to = 0, $lineWidth = 0)
176 $this->stringCommands
.= $lineWidth . " setlinewidth \n";
177 $this->stringCommands
.= $x_from . ' ' . $y_from . " moveto \n";
178 $this->stringCommands
.= $x_to . ' ' . $y_to . " lineto \n";
179 $this->stringCommands
.= "stroke \n";
185 * drawing the rectangle from x,y source to x,y destination and set the
186 * width of the line. rectangles drawn around the text shown of fields
188 * @param integer $x_from The x_from attribute defines the start
189 left position of the element
190 * @param integer $y_from The y_from attribute defines the start
191 right position of the element
192 * @param integer $x_to The x_to attribute defines the end
193 left position of the element
194 * @param integer $y_to The y_to attribute defines the end
195 right position of the element
196 * @param integer $lineWidth Sets the width of the line e.g 2
202 function rect($x_from, $y_from, $x_to, $y_to, $lineWidth)
204 $this->stringCommands
.= $lineWidth . " setlinewidth \n";
205 $this->stringCommands
.= "newpath \n";
206 $this->stringCommands
.= $x_from . " " . $y_from . " moveto \n";
207 $this->stringCommands
.= "0 " . $y_to . " rlineto \n";
208 $this->stringCommands
.= $x_to . " 0 rlineto \n";
209 $this->stringCommands
.= "0 -" . $y_to . " rlineto \n";
210 $this->stringCommands
.= "closepath \n";
211 $this->stringCommands
.= "stroke \n";
215 * Set the current point
217 * The moveto operator takes two numbers off the stack and treats
218 * them as x and y coordinates to which to move. The coordinates
219 * specified become the current point.
221 * @param integer $x The x attribute defines the left position of the element
222 * @param integer $y The y attribute defines the right position of the element
228 function moveTo($x, $y)
230 $this->stringCommands
.= $x . ' ' . $y . " moveto \n";
234 * Output/Display the text
236 * @param string $text The string to be displayed
244 $this->stringCommands
.= '(' . $text . ") show \n";
248 * Output the text at specified co-ordinates
250 * @param string $text String to be displayed
251 * @param integer $x X attribute defines the left position of the element
252 * @param integer $y Y attribute defines the right position of the element
258 function showXY($text, $x, $y)
260 $this->moveTo($x, $y);
265 * get width of string/text
267 * EPS text width is calcualted depending on font name
268 * and font size. It is very important to know the width of text
269 * because rectangle is drawn around it.
271 * This is a bit hardcore method. I didn't found any other better than this.
272 * if someone found better than this. would love to hear that method
274 * @param string $text string that width will be calculated
275 * @param integer $font name of the font like Arial,sans-serif etc
276 * @param integer $fontSize size of font
278 * @return integer width of the text
282 function getStringWidth($text,$font,$fontSize)
285 * Start by counting the width, giving each character a modifying value
288 $count = $count +
((strlen($text) - strlen(str_replace(array("i", "j", "l"), "", $text))) * 0.23);//ijl
289 $count = $count +
((strlen($text) - strlen(str_replace(array("f"), "", $text))) * 0.27);//f
290 $count = $count +
((strlen($text) - strlen(str_replace(array("t", "I"), "", $text))) * 0.28);//tI
291 $count = $count +
((strlen($text) - strlen(str_replace(array("r"), "", $text))) * 0.34);//r
292 $count = $count +
((strlen($text) - strlen(str_replace(array("1"), "", $text))) * 0.49);//1
293 $count = $count +
((strlen($text) - strlen(str_replace(array("c", "k", "s", "v", "x", "y", "z", "J"), "", $text))) * 0.5);//cksvxyzJ
294 $count = $count +
((strlen($text) - strlen(str_replace(array("a", "b", "d", "e", "g", "h", "n", "o", "p", "q", "u", "L", "0", "2", "3", "4", "5", "6", "7", "8", "9"), "", $text))) * 0.56);//abdeghnopquL023456789
295 $count = $count +
((strlen($text) - strlen(str_replace(array("F", "T", "Z"), "", $text))) * 0.61);//FTZ
296 $count = $count +
((strlen($text) - strlen(str_replace(array("A", "B", "E", "K", "P", "S", "V", "X", "Y"), "", $text))) * 0.67);//ABEKPSVXY
297 $count = $count +
((strlen($text) - strlen(str_replace(array("w", "C", "D", "H", "N", "R", "U"), "", $text))) * 0.73);//wCDHNRU
298 $count = $count +
((strlen($text) - strlen(str_replace(array("G", "O", "Q"), "", $text))) * 0.78);//GOQ
299 $count = $count +
((strlen($text) - strlen(str_replace(array("m", "M"), "", $text))) * 0.84);//mM
300 $count = $count +
((strlen($text) - strlen(str_replace("W", "", $text))) * .95);//W
301 $count = $count +
((strlen($text) - strlen(str_replace(" ", "", $text))) * .28);//" "
302 $text = str_replace(" ", "", $text);//remove the " "'s
303 $count = $count +
(strlen(preg_replace("/[a-z0-9]/i", "", $text)) * 0.3); //all other chrs
306 $font = strtolower($font);
309 * no modifier for arial and sans-serif
315 * .92 modifer for time, serif, brushscriptstd, and californian fb
319 case 'brushscriptstd':
320 case 'californian fb':
324 * 1.23 modifier for broadway
330 $textWidth = $count*$fontSize;
331 return ceil($textWidth*$modifier);
342 $this->stringCommands
.= "showpage \n";
346 * Output EPS Document for download
348 * @param string $fileName name of the eps document
354 function showOutput($fileName)
356 // if(ob_get_clean()){
359 $output = $this->stringCommands
;
360 PMA_Response
::getInstance()->disable();
361 PMA_downloadHeader($fileName . '.eps', 'image/x-eps', strlen($output));
367 * Table preferences/statistics
369 * This class preserves the table co-ordinates,fields
370 * and helps in drawing/generating the Tables in EPS.
382 private $_showInfo = false;
386 public $fields = array();
387 public $heightCell = 0;
388 public $currentCell = 0;
390 public $primary = array();
393 * The "Table_Stats" constructor
395 * @param string $tableName The table name
396 * @param string $font The font name
397 * @param integer $fontSize The font size
398 * @param integer $pageNumber Page number
399 * @param integer &$same_wide_width The max width among tables
400 * @param boolean $showKeys Whether to display keys or not
401 * @param boolean $showInfo Whether to display table position or not
403 * @global object The current eps document
404 * @global integer The current page number (from the
405 * $cfg['Servers'][$i]['table_coords'] table)
406 * @global array The relations settings
407 * @global string The current db name
410 * @see PMA_EPS, Table_Stats::Table_Stats_setWidth,
411 * Table_Stats::Table_Stats_setHeight
413 function __construct(
414 $tableName, $font, $fontSize, $pageNumber, &$same_wide_width,
415 $showKeys = false, $showInfo = false
417 global $eps, $cfgRelation, $db;
419 $this->_tableName
= $tableName;
420 $sql = 'DESCRIBE ' . PMA_Util
::backquote($tableName);
421 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE
);
422 if (! $result ||
! PMA_DBI_num_rows($result)) {
425 sprintf(__('The %s table doesn\'t exist!'), $tableName)
431 * check to see if it will load all fields or only the foreign keys
434 $indexes = PMA_Index
::getFromTable($this->_tableName
, $db);
435 $all_columns = array();
436 foreach ($indexes as $index) {
437 $all_columns = array_merge(
439 array_flip(array_keys($index->getColumns()))
442 $this->fields
= array_keys($all_columns);
444 while ($row = PMA_DBI_fetch_row($result)) {
445 $this->fields
[] = $row[0];
449 $this->_showInfo
= $showInfo;
452 $this->_setHeightTable($fontSize);
454 // setWidth must me after setHeight, because title
455 // can include table height which changes table width
456 $this->_setWidthTable($font, $fontSize);
457 if ($same_wide_width < $this->width
) {
458 $same_wide_width = $this->width
;
462 $sql = 'SELECT x, y FROM '
463 . PMA_Util
::backquote($GLOBALS['cfgRelation']['db']) . '.'
464 . PMA_Util
::backquote($cfgRelation['table_coords'])
465 . ' WHERE db_name = \'' . PMA_Util
::sqlAddSlashes($db) . '\''
466 . ' AND table_name = \'' . PMA_Util
::sqlAddSlashes($tableName) . '\''
467 . ' AND pdf_page_number = ' . $pageNumber;
468 $result = PMA_queryAsControlUser($sql, false, PMA_DBI_QUERY_STORE
);
470 if (! $result ||
! PMA_DBI_num_rows($result)) {
474 __('Please configure the coordinates for table %s'),
479 list($this->x
, $this->y
) = PMA_DBI_fetch_row($result);
480 $this->x
= (double) $this->x
;
481 $this->y
= (double) $this->y
;
483 $this->displayfield
= PMA_getDisplayField($db, $tableName);
485 $result = PMA_DBI_query(
486 'SHOW INDEX FROM ' . PMA_Util
::backquote($tableName) . ';',
487 null, PMA_DBI_QUERY_STORE
489 if (PMA_DBI_num_rows($result) > 0) {
490 while ($row = PMA_DBI_fetch_assoc($result)) {
491 if ($row['Key_name'] == 'PRIMARY') {
492 $this->primary
[] = $row['Column_name'];
499 * Returns title of the current table,
500 * title can have the dimensions/co-ordinates of the table
502 * @return string The relation/table name
505 private function _getTitle()
507 return ($this->_showInfo
508 ?
sprintf('%.0f', $this->width
) . 'x' . sprintf('%.0f', $this->heightCell
)
509 : '') . ' ' . $this->_tableName
;
513 * Sets the width of the table
515 * @param string $font The font name
516 * @param integer $fontSize The font size
518 * @global object The current eps document
525 private function _setWidthTable($font,$fontSize)
529 foreach ($this->fields
as $field) {
532 $eps->getStringWidth($field, $font, $fontSize)
535 $this->width +
= $eps->getStringWidth(' ', $font, $fontSize);
537 * it is unknown what value must be added, because
538 * table title is affected by the tabe width value
540 while ($this->width
< $eps->getStringWidth($this->_getTitle(), $font, $fontSize)) {
546 * Sets the height of the table
548 * @param integer $fontSize The font size
553 private function _setHeightTable($fontSize)
555 $this->heightCell
= $fontSize +
4;
556 $this->height
= (count($this->fields
) +
1) * $this->heightCell
;
562 * @param boolean $showColor Whether to display color
564 * @global object The current eps document
569 * @see PMA_EPS,PMA_EPS::line,PMA_EPS::rect
571 public function tableDraw($showColor)
574 //echo $this->_tableName.'<br />';
575 $eps->rect($this->x
, $this->y +
12, $this->width
, $this->heightCell
, 1);
576 $eps->showXY($this->_getTitle(), $this->x +
5, $this->y +
14);
577 foreach ($this->fields
as $field) {
578 $this->currentCell +
= $this->heightCell
;
581 if (in_array($field, $this->primary
)) {
584 if ($field == $this->displayfield
) {
589 $this->x
, $this->y +
12 +
$this->currentCell
,
590 $this->width
, $this->heightCell
, 1
592 $eps->showXY($field, $this->x +
5, $this->y +
14 +
$this->currentCell
);
598 * Relation preferences/statistics
600 * This class fetches the table master and foreign fields positions
601 * and helps in generating the Table references and then connects
602 * master table's master field to foreign table's foreign key
605 * @name Relation_Stats
616 public $xDest, $yDest;
620 * The "Relation_Stats" constructor
622 * @param string $master_table The master table name
623 * @param string $master_field The relation field in the master table
624 * @param string $foreign_table The foreign table name
625 * @param string $foreign_field The relation field in the foreign table
627 * @see Relation_Stats::_getXy
629 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
631 $src_pos = $this->_getXy($master_table, $master_field);
632 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
638 $src_left = $src_pos[0] - $this->wTick
;
639 $src_right = $src_pos[1] +
$this->wTick
;
640 $dest_left = $dest_pos[0] - $this->wTick
;
641 $dest_right = $dest_pos[1] +
$this->wTick
;
643 $d1 = abs($src_left - $dest_left);
644 $d2 = abs($src_right - $dest_left);
645 $d3 = abs($src_left - $dest_right);
646 $d4 = abs($src_right - $dest_right);
647 $d = min($d1, $d2, $d3, $d4);
650 $this->xSrc
= $src_pos[0];
652 $this->xDest
= $dest_pos[0];
654 } elseif ($d == $d2) {
655 $this->xSrc
= $src_pos[1];
657 $this->xDest
= $dest_pos[0];
659 } elseif ($d == $d3) {
660 $this->xSrc
= $src_pos[0];
662 $this->xDest
= $dest_pos[1];
665 $this->xSrc
= $src_pos[1];
667 $this->xDest
= $dest_pos[1];
670 $this->ySrc
= $src_pos[2] +
10;
671 $this->yDest
= $dest_pos[2] +
10;
675 * Gets arrows coordinates
677 * @param string $table The current table name
678 * @param string $column The relation column name
680 * @return array Arrows coordinates
684 private function _getXy($table, $column)
686 $pos = array_search($column, $table->fields
);
687 // x_left, x_right, y
690 $table->x +
$table->width
,
691 $table->y +
($pos +
1.5) * $table->heightCell
696 * draws relation links and arrows
697 * shows foreign key relations
699 * @param boolean $changeColor Whether to use one color per relation or not
701 * @global object The current EPS document
708 public function relationDraw($changeColor)
713 $listOfColors = array(
722 shuffle($listOfColors);
723 $color = $listOfColors[0];
727 // draw a line like -- to foreign field
731 $this->xSrc +
$this->srcDir
* $this->wTick
,
735 // draw a line like -- to master field
737 $this->xDest +
$this->destDir
* $this->wTick
,
743 // draw a line that connects to master field line and foreign field line
745 $this->xSrc +
$this->srcDir
* $this->wTick
,
747 $this->xDest +
$this->destDir
* $this->wTick
,
751 $root2 = 2 * sqrt(2);
753 $this->xSrc +
$this->srcDir
* $this->wTick
* 0.75,
755 $this->xSrc +
$this->srcDir
* (0.75 - 1 / $root2) * $this->wTick
,
756 $this->ySrc +
$this->wTick
/ $root2,
760 $this->xSrc +
$this->srcDir
* $this->wTick
* 0.75,
762 $this->xSrc +
$this->srcDir
* (0.75 - 1 / $root2) * $this->wTick
,
763 $this->ySrc
- $this->wTick
/ $root2,
767 $this->xDest +
$this->destDir
* $this->wTick
/ 2,
769 $this->xDest +
$this->destDir
* (0.5 +
1 / $root2) * $this->wTick
,
770 $this->yDest +
$this->wTick
/ $root2,
774 $this->xDest +
$this->destDir
* $this->wTick
/ 2,
776 $this->xDest +
$this->destDir
* (0.5 +
1 / $root2) * $this->wTick
,
777 $this->yDest
- $this->wTick
/ $root2,
783 * end of the "Relation_Stats" class
787 * EPS Relation Schema Class
789 * Purpose of this class is to generate the EPS Document
790 * which is used for representing the database diagrams.
791 * This class uses post script commands and with
792 * the combination of these commands actually helps in preparing EPS Document.
794 * This class inherits Export_Relation_Schema class has common functionality added
797 * @name Eps_Relation_Schema
799 class PMA_Eps_Relation_Schema
extends PMA_Export_Relation_Schema
801 private $_tables = array();
802 private $_relations = array();
805 * The "PMA_EPS_Relation_Schema" constructor
807 * Upon instantiation This starts writing the EPS document
808 * user will be prompted for download as .eps extension
813 function __construct()
817 $this->setPageNumber($_POST['pdf_page_number']);
818 $this->setShowColor(isset($_POST['show_color']));
819 $this->setShowKeys(isset($_POST['show_keys']));
820 $this->setTableDimension(isset($_POST['show_table_dimension']));
821 $this->setAllTablesSameWidth(isset($_POST['all_tables_same_width']));
822 $this->setOrientation($_POST['orientation']);
823 $this->setExportType($_POST['export_type']);
825 $eps = new PMA_EPS();
828 __('Schema of the %s database - Page %s'),
833 $eps->setAuthor('phpMyAdmin ' . PMA_VERSION
);
834 $eps->setDate(date("j F Y, g:i a"));
835 $eps->setOrientation($this->orientation
);
836 $eps->setFont('Verdana', '10');
838 $alltables = $this->getAllTables($db, $this->pageNumber
);
840 foreach ($alltables AS $table) {
841 if (! isset($this->_tables
[$table])) {
842 $this->_tables
[$table] = new Table_Stats(
843 $table, $eps->getFont(), $eps->getFontSize(), $this->pageNumber
,
844 $this->_tablewidth
, $this->showKeys
, $this->tableDimension
848 if ($this->sameWide
) {
849 $this->_tables
[$table]->width
= $this->_tablewidth
;
853 $seen_a_relation = false;
854 foreach ($alltables as $one_table) {
855 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
857 $seen_a_relation = true;
858 foreach ($exist_rel as $master_field => $rel) {
859 /* put the foreign table on the schema only if selected
861 * (do not use array_search() because we would have to
862 * to do a === false and this is not PHP3 compatible)
864 if (in_array($rel['foreign_table'], $alltables)) {
866 $one_table, $eps->getFont(), $eps->getFontSize(),
867 $master_field, $rel['foreign_table'],
868 $rel['foreign_field'], $this->tableDimension
874 if ($seen_a_relation) {
875 $this->_drawRelations($this->showColor
);
878 $this->_drawTables($this->showColor
);
880 $eps->showOutput($db.'-'.$this->pageNumber
);
885 * Defines relation objects
887 * @param string $masterTable The master table name
888 * @param string $font The font
889 * @param int $fontSize The font size
890 * @param string $masterField The relation field in the master table
891 * @param string $foreignTable The foreign table name
892 * @param string $foreignField The relation field in the foreign table
893 * @param boolean $showInfo Whether to display table position or not
898 * @see _setMinMax,Table_Stats::__construct(),Relation_Stats::__construct()
900 private function _addRelation(
901 $masterTable, $font, $fontSize, $masterField,
902 $foreignTable, $foreignField, $showInfo
904 if (! isset($this->_tables
[$masterTable])) {
905 $this->_tables
[$masterTable] = new Table_Stats(
906 $masterTable, $font, $fontSize, $this->pageNumber
,
907 $this->_tablewidth
, false, $showInfo
910 if (! isset($this->_tables
[$foreignTable])) {
911 $this->_tables
[$foreignTable] = new Table_Stats(
912 $foreignTable, $font, $fontSize, $this->pageNumber
,
913 $this->_tablewidth
, false, $showInfo
916 $this->_relations
[] = new Relation_Stats(
917 $this->_tables
[$masterTable], $masterField,
918 $this->_tables
[$foreignTable], $foreignField
923 * Draws relation arrows and lines connects master table's master field to
924 * foreign table's forein field
926 * @param boolean $changeColor Whether to use one color per relation or not
931 * @see Relation_Stats::relationDraw()
933 private function _drawRelations($changeColor)
935 foreach ($this->_relations
as $relation) {
936 $relation->relationDraw($changeColor);
943 * @param boolean $changeColor Whether to show color for primary fields or not
948 * @see Table_Stats::Table_Stats_tableDraw()
950 private function _drawTables($changeColor)
952 foreach ($this->_tables
as $table) {
953 $table->tableDraw($changeColor);