2 /* vim: set expandtab sw=4 ts=4 sts=4: */
9 include_once("Export_Relation_Schema.class.php");
12 * This Class is EPS Library and
13 * helps in developing structure of EPS Schema Export
19 * @see http://php.net/manual/en/book.xmlwriter.php
26 public $stringCommands;
29 * The "PMA_EPS" constructor
31 * Upon instantiation This starts writing the EPS Document.
32 * %!PS-Adobe-3.0 EPSF-3.0 This is the MUST first comment to include
33 * it shows/tells that the Post Script document is purely under
34 * Document Structuring Convention [DSC] and is Compliant
35 * Encapsulated Post Script Document
40 function __construct()
42 $this->stringCommands
= "";
43 $this->stringCommands
.= "%!PS-Adobe-3.0 EPSF-3.0 \n";
49 * @param string value sets the title text
53 function setTitle($value)
55 $this->stringCommands
.= '%%Title: ' . $value . "\n";
61 * @param string value sets the author
65 function setAuthor($value)
67 $this->stringCommands
.= '%%Creator: ' . $value . "\n";
71 * Set document creation date
73 * @param string value sets the date
77 function setDate($value)
79 $this->stringCommands
.= '%%CreationDate: ' . $value . "\n";
83 * Set document orientation
85 * @param string value sets the author
89 function setOrientation($value)
91 $this->stringCommands
.= "%%PageOrder: Ascend \n";
94 $this->stringCommands
.= '%%Orientation: ' . $value . "\n";
97 $this->stringCommands
.= '%%Orientation: ' . $value . "\n";
99 $this->stringCommands
.= "%%EndComments \n";
100 $this->stringCommands
.= "%%Pages 1 \n";
101 $this->stringCommands
.= "%%BoundingBox: 72 150 144 170 \n";
105 * Set the font and size
107 * font can be set whenever needed in EPS
109 * @param string value sets the font name e.g Arial
110 * @param integer value sets the size of the font e.g 10
114 function setFont($value,$size)
116 $this->font
= $value;
117 $this->fontSize
= $size;
118 $this->stringCommands
.= "/".$value." findfont % Get the basic font\n";
119 $this->stringCommands
.= "".$size." scalefont % Scale the font to $size points\n";
120 $this->stringCommands
.= "setfont % Make it the current font\n";
126 * @return string return the font name e.g Arial
137 * @return string return the size of the font e.g 10
140 function getFontSize()
142 return $this->fontSize
;
148 * drawing the lines from x,y source to x,y destination and set the
149 * width of the line. lines helps in showing relationships of tables
151 * @param integer x_from The x_from attribute defines the start
152 left position of the element
153 * @param integer y_from The y_from attribute defines the start
154 right position of the element
155 * @param integer x_to The x_to attribute defines the end
156 left position of the element
157 * @param integer y_to The y_to attribute defines the end
158 right position of the element
159 * @param integer lineWidth sets the width of the line e.g 2
163 function line($x_from=0, $y_from=0, $x_to=0, $y_to=0, $lineWidth=0)
165 $this->stringCommands
.= $lineWidth . " setlinewidth \n";
166 $this->stringCommands
.= $x_from . ' ' . $y_from . " moveto \n";
167 $this->stringCommands
.= $x_to . ' ' . $y_to . " lineto \n";
168 $this->stringCommands
.= "stroke \n";
174 * drawing the rectangle from x,y source to x,y destination and set the
175 * width of the line. rectangles drawn around the text shown of fields
177 * @param integer x_from The x_from attribute defines the start
178 left position of the element
179 * @param integer y_from The y_from attribute defines the start
180 right position of the element
181 * @param integer x_to The x_to attribute defines the end
182 left position of the element
183 * @param integer y_to The y_to attribute defines the end
184 right position of the element
185 * @param integer lineWidth sets the width of the line e.g 2
189 function rect($x_from, $y_from, $x_to, $y_to, $lineWidth)
191 $this->stringCommands
.= $lineWidth . " setlinewidth \n";
192 $this->stringCommands
.= "newpath \n";
193 $this->stringCommands
.= $x_from . " " . $y_from . " moveto \n";
194 $this->stringCommands
.= "0 " . $y_to . " rlineto \n";
195 $this->stringCommands
.= $x_to . " 0 rlineto \n";
196 $this->stringCommands
.= "0 -" . $y_to . " rlineto \n";
197 $this->stringCommands
.= "closepath \n";
198 $this->stringCommands
.= "stroke \n";
202 * Set the current point
204 * The moveto operator takes two numbers off the stack and treats
205 * them as x and y coordinates to which to move. The coordinates
206 * specified become the current point.
208 * @param integer x The x attribute defines the
209 left position of the element
210 * @param integer y The y attribute defines the
211 right position of the element
215 function moveTo($x, $y)
217 $this->stringCommands
.= $x . ' ' . $y . " moveto \n";
221 * Output/Display the text
223 * @param string text The string to be displayed
229 $this->stringCommands
.= '(' . $text . ") show \n";
233 * Output the text at specified co-ordinates
235 * @param string text The string to be displayed
236 * @param integer x The x attribute defines the
237 left position of the element
238 * @param integer y The y attribute defines the
239 right position of the element
243 function showXY($text, $x, $y)
245 $this->moveTo($x, $y);
250 * get width of string/text
252 * EPS text width is calcualted depending on font name
253 * and font size. It is very important to know the width of text
254 * because rectangle is drawn around it.
256 * This is a bit hardcore method. I didn't found any other better than this.
257 * if someone found better than this. would love to hear that method
259 * @param string text string that width will be calculated
260 * @param integer font name of the font like Arial,sans-serif etc
261 * @param integer fontSize size of font
262 * @return integer width of the text
265 function getStringWidth($text,$font,$fontSize)
268 * Start by counting the width, giving each character a modifying value
271 $count = $count +
((strlen($text) - strlen(str_replace(array("i","j","l"),"",$text)))*0.23);//ijl
272 $count = $count +
((strlen($text) - strlen(str_replace(array("f"),"",$text)))*0.27);//f
273 $count = $count +
((strlen($text) - strlen(str_replace(array("t","I"),"",$text)))*0.28);//tI
274 $count = $count +
((strlen($text) - strlen(str_replace(array("r"),"",$text)))*0.34);//r
275 $count = $count +
((strlen($text) - strlen(str_replace(array("1"),"",$text)))*0.49);//1
276 $count = $count +
((strlen($text) - strlen(str_replace(array("c","k","s","v","x","y","z","J"),"",$text)))*0.5);//cksvxyzJ
277 $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
278 $count = $count +
((strlen($text) - strlen(str_replace(array("F","T","Z"),"",$text)))*0.61);//FTZ
279 $count = $count +
((strlen($text) - strlen(str_replace(array("A","B","E","K","P","S","V","X","Y"),"",$text)))*0.67);//ABEKPSVXY
280 $count = $count +
((strlen($text) - strlen(str_replace(array("w","C","D","H","N","R","U"),"",$text)))*0.73);//wCDHNRU
281 $count = $count +
((strlen($text) - strlen(str_replace(array("G","O","Q"),"",$text)))*0.78);//GOQ
282 $count = $count +
((strlen($text) - strlen(str_replace(array("m","M"),"",$text)))*0.84);//mM
283 $count = $count +
((strlen($text) - strlen(str_replace("W","",$text)))*.95);//W
284 $count = $count +
((strlen($text) - strlen(str_replace(" ","",$text)))*.28);//" "
285 $text = str_replace(" ","",$text);//remove the " "'s
286 $count = $count +
(strlen(preg_replace("/[a-z0-9]/i","",$text))*0.3); //all other chrs
289 $font = strtolower($font);
292 * no modifier for arial and sans-serif
298 * .92 modifer for time, serif, brushscriptstd, and californian fb
302 case 'brushscriptstd':
303 case 'californian fb':
307 * 1.23 modifier for broadway
313 $textWidth = $count*$fontSize;
314 return ceil($textWidth*$modifier);
325 $this->stringCommands
.= "showpage \n";
329 * Output EPS Document for download
331 * @param string fileName name of the eps document
335 function showOutput($fileName)
337 // if(ob_get_clean()){
340 header('Content-type: image/x-eps');
341 header('Content-Disposition: attachment; filename="'.$fileName.'.eps"');
342 $output = $this->stringCommands
;
348 * Table preferences/statistics
350 * This class preserves the table co-ordinates,fields
351 * and helps in drawing/generating the Tables in EPS.
365 private $_showInfo = false;
369 public $fields = array();
370 public $heightCell = 0;
371 public $currentCell = 0;
373 public $primary = array();
376 * The "Table_Stats" constructor
378 * @param string tableName The table name
379 * @param string font The font name
380 * @param integer fontSize The font size
381 * @param integer same_wide_width The max width among tables
382 * @param boolean showKeys Whether to display keys or not
383 * @param boolean showInfo Whether to display table position or not
384 * @global object The current eps document
385 * @global integer The current page number (from the
386 * $cfg['Servers'][$i]['table_coords'] table)
387 * @global array The relations settings
388 * @global string The current db name
390 * @see PMA_EPS, Table_Stats::Table_Stats_setWidth,
391 Table_Stats::Table_Stats_setHeight
393 function __construct($tableName, $font, $fontSize, $pageNumber, &$same_wide_width, $showKeys = false, $showInfo = false)
395 global $eps, $cfgRelation, $db;
397 $this->_tableName
= $tableName;
398 $sql = 'DESCRIBE ' . PMA_backquote($tableName);
399 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE
);
400 if (!$result ||
!PMA_DBI_num_rows($result)) {
401 $eps->dieSchema($pageNumber,"EPS",sprintf(__('The %s table doesn\'t exist!'), $tableName));
406 * check to see if it will load all fields or only the foreign keys
409 $indexes = PMA_Index
::getFromTable($this->_tableName
, $db);
410 $all_columns = array();
411 foreach ($indexes as $index) {
412 $all_columns = array_merge($all_columns, array_flip(array_keys($index->getColumns())));
414 $this->fields
= array_keys($all_columns);
416 while ($row = PMA_DBI_fetch_row($result)) {
417 $this->fields
[] = $row[0];
421 $this->_showInfo
= $showInfo;
424 $this->_setHeightTable($fontSize);
426 // setWidth must me after setHeight, because title
427 // can include table height which changes table width
428 $this->_setWidthTable($font,$fontSize);
429 if ($same_wide_width < $this->width
) {
430 $same_wide_width = $this->width
;
434 $sql = 'SELECT x, y FROM '
435 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
436 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
437 . ' AND table_name = \'' . PMA_sqlAddslashes($tableName) . '\''
438 . ' AND pdf_page_number = ' . $pageNumber;
439 $result = PMA_query_as_controluser($sql, false, PMA_DBI_QUERY_STORE
);
441 if (!$result ||
!PMA_DBI_num_rows($result)) {
442 $eps->dieSchema($pageNumber,"EPS",sprintf(__('Please configure the coordinates for table %s'), $tableName));
444 list($this->x
, $this->y
) = PMA_DBI_fetch_row($result);
445 $this->x
= (double) $this->x
;
446 $this->y
= (double) $this->y
;
448 $this->displayfield
= PMA_getDisplayField($db, $tableName);
450 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tableName) . ';', null, PMA_DBI_QUERY_STORE
);
451 if (PMA_DBI_num_rows($result) > 0) {
452 while ($row = PMA_DBI_fetch_assoc($result)) {
453 if ($row['Key_name'] == 'PRIMARY') {
454 $this->primary
[] = $row['Column_name'];
461 * Returns title of the current table,
462 * title can have the dimensions/co-ordinates of the table
464 * @return string The relation/table name
467 private function _getTitle()
469 return ($this->_showInfo ?
sprintf('%.0f', $this->width
) . 'x' . sprintf('%.0f', $this->heightCell
) : '') . ' ' . $this->_tableName
;
473 * Sets the width of the table
475 * @param string font The font name
476 * @param integer fontSize The font size
477 * @global object The current eps document
482 private function _setWidthTable($font,$fontSize)
486 foreach ($this->fields
as $field) {
487 $this->width
= max($this->width
, $eps->getStringWidth($field,$font,$fontSize));
489 $this->width +
= $eps->getStringWidth(' ',$font,$fontSize);
491 * it is unknown what value must be added, because
492 * table title is affected by the tabe width value
494 while ($this->width
< $eps->getStringWidth($this->_getTitle(),$font,$fontSize)) {
500 * Sets the height of the table
502 * @param integer fontSize The font size
506 private function _setHeightTable($fontSize)
508 $this->heightCell
= $fontSize +
4;
509 $this->height
= (count($this->fields
) +
1) * $this->heightCell
;
515 * @param boolean showColor Whether to display color
516 * @global object The current eps document
519 * @see PMA_EPS,PMA_EPS::line,PMA_EPS::rect
521 public function tableDraw($showColor)
524 //echo $this->_tableName.'<br />';
525 $eps->rect($this->x
,$this->y +
12,
526 $this->width
,$this->heightCell
,
529 $eps->showXY($this->_getTitle(),$this->x +
5,$this->y +
14);
530 foreach ($this->fields
as $field) {
531 $this->currentCell +
= $this->heightCell
;
534 if (in_array($field, $this->primary
)) {
537 if ($field == $this->displayfield
) {
541 $eps->rect($this->x
,$this->y +
12 +
$this->currentCell
,
542 $this->width
, $this->heightCell
,1);
543 $eps->showXY($field, $this->x +
5, $this->y +
14 +
$this->currentCell
);
549 * Relation preferences/statistics
551 * This class fetches the table master and foreign fields positions
552 * and helps in generating the Table references and then connects
553 * master table's master field to foreign table's foreign key
556 * @name Relation_Stats
569 public $xDest, $yDest;
573 * The "Relation_Stats" constructor
575 * @param string master_table The master table name
576 * @param string master_field The relation field in the master table
577 * @param string foreign_table The foreign table name
578 * @param string foreigh_field The relation field in the foreign table
579 * @see Relation_Stats::_getXy
581 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
583 $src_pos = $this->_getXy($master_table, $master_field);
584 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
590 $src_left = $src_pos[0] - $this->wTick
;
591 $src_right = $src_pos[1] +
$this->wTick
;
592 $dest_left = $dest_pos[0] - $this->wTick
;
593 $dest_right = $dest_pos[1] +
$this->wTick
;
595 $d1 = abs($src_left - $dest_left);
596 $d2 = abs($src_right - $dest_left);
597 $d3 = abs($src_left - $dest_right);
598 $d4 = abs($src_right - $dest_right);
599 $d = min($d1, $d2, $d3, $d4);
602 $this->xSrc
= $src_pos[0];
604 $this->xDest
= $dest_pos[0];
606 } elseif ($d == $d2) {
607 $this->xSrc
= $src_pos[1];
609 $this->xDest
= $dest_pos[0];
611 } elseif ($d == $d3) {
612 $this->xSrc
= $src_pos[0];
614 $this->xDest
= $dest_pos[1];
617 $this->xSrc
= $src_pos[1];
619 $this->xDest
= $dest_pos[1];
622 $this->ySrc
= $src_pos[2] +
10;
623 $this->yDest
= $dest_pos[2] +
10;
627 * Gets arrows coordinates
629 * @param string table The current table name
630 * @param string column The relation column name
631 * @return array Arrows coordinates
634 private function _getXy($table, $column)
636 $pos = array_search($column, $table->fields
);
637 // x_left, x_right, y
638 return array($table->x
, $table->x +
$table->width
, $table->y +
($pos +
1.5) * $table->heightCell
);
642 * draws relation links and arrows
643 * shows foreign key relations
645 * @param boolean changeColor Whether to use one color per relation or not
646 * @global object The current EPS document
650 public function relationDraw($changeColor)
655 $listOfColors = array(
664 shuffle($listOfColors);
665 $color = $listOfColors[0];
669 // draw a line like -- to foreign field
670 $eps->line($this->xSrc
,$this->ySrc
,
671 $this->xSrc +
$this->srcDir
* $this->wTick
,$this->ySrc
,
674 // draw a line like -- to master field
675 $eps->line($this->xDest +
$this->destDir
* $this->wTick
, $this->yDest
,
676 $this->xDest
, $this->yDest
,
679 // draw a line that connects to master field line and foreign field line
680 $eps->line($this->xSrc +
$this->srcDir
* $this->wTick
,$this->ySrc
,
681 $this->xDest +
$this->destDir
* $this->wTick
, $this->yDest
,
684 $root2 = 2 * sqrt(2);
685 $eps->line($this->xSrc +
$this->srcDir
* $this->wTick
* 0.75, $this->ySrc
,
686 $this->xSrc +
$this->srcDir
* (0.75 - 1 / $root2) * $this->wTick
,
687 $this->ySrc +
$this->wTick
/ $root2 ,
690 $eps->line($this->xSrc +
$this->srcDir
* $this->wTick
* 0.75, $this->ySrc
,
691 $this->xSrc +
$this->srcDir
* (0.75 - 1 / $root2) * $this->wTick
,
692 $this->ySrc
- $this->wTick
/ $root2 ,
695 $eps->line($this->xDest +
$this->destDir
* $this->wTick
/ 2 , $this->yDest
,
696 $this->xDest +
$this->destDir
* (0.5 +
1 / $root2) * $this->wTick
,
697 $this->yDest +
$this->wTick
/ $root2 ,
699 $eps->line($this->xDest +
$this->destDir
* $this->wTick
/ 2 ,
700 $this->yDest
, $this->xDest +
$this->destDir
* (0.5 +
1 / $root2) * $this->wTick
,
701 $this->yDest
- $this->wTick
/ $root2 ,
707 * end of the "Relation_Stats" class
711 * EPS Relation Schema Class
713 * Purpose of this class is to generate the EPS Document
714 * which is used for representing the database diagrams.
715 * This class uses post script commands and with
716 * the combination of these commands actually helps in preparing EPS Document.
718 * This class inherits Export_Relation_Schema class has common functionality added
721 * @name Eps_Relation_Schema
725 class PMA_Eps_Relation_Schema
extends PMA_Export_Relation_Schema
727 private $tables = array();
728 private $_relations = array();
731 * The "PMA_EPS_Relation_Schema" constructor
733 * Upon instantiation This starts writing the EPS document
734 * user will be prompted for download as .eps extension
739 function __construct()
743 $this->setPageNumber($_POST['pdf_page_number']);
744 $this->setShowColor(isset($_POST['show_color']));
745 $this->setShowKeys(isset($_POST['show_keys']));
746 $this->setTableDimension(isset($_POST['show_table_dimension']));
747 $this->setAllTableSameWidth(isset($_POST['all_table_same_wide']));
748 $this->setOrientation($_POST['orientation']);
749 $this->setExportType($_POST['export_type']);
751 $eps = new PMA_EPS();
752 $eps->setTitle(sprintf(__('Schema of the %s database - Page %s'), $db, $this->pageNumber
));
753 $eps->setAuthor('phpMyAdmin ' . PMA_VERSION
);
754 $eps->setDate(date("j F Y, g:i a"));
755 $eps->setOrientation($this->orientation
);
756 $eps->setFont('Verdana','10');
760 $alltables = $this->getAllTables($db,$this->pageNumber
);
762 foreach ($alltables AS $table) {
763 if (!isset($this->tables
[$table])) {
764 $this->tables
[$table] = new Table_Stats($table,$eps->getFont(),$eps->getFontSize(), $this->pageNumber
, $this->_tablewidth
, $this->showKeys
, $this->tableDimension
);
767 if ($this->sameWide
) {
768 $this->tables
[$table]->width
= $this->_tablewidth
;
772 $seen_a_relation = false;
773 foreach ($alltables as $one_table) {
774 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
776 $seen_a_relation = true;
777 foreach ($exist_rel as $master_field => $rel) {
778 /* put the foreign table on the schema only if selected
780 * (do not use array_search() because we would have to
781 * to do a === FALSE and this is not PHP3 compatible)
783 if (in_array($rel['foreign_table'], $alltables)) {
784 $this->_addRelation($one_table,$eps->getFont(),$eps->getFontSize(), $master_field, $rel['foreign_table'], $rel['foreign_field'], $this->tableDimension
);
789 if ($seen_a_relation) {
790 $this->_drawRelations($this->showColor
);
793 $this->_drawTables($this->showColor
);
795 $eps->showOutput($db.'-'.$this->pageNumber
);
800 * Defines relation objects
802 * @param string masterTable The master table name
803 * @param string masterField The relation field in the master table
804 * @param string foreignTable The foreign table name
805 * @param string foreignField The relation field in the foreign table
806 * @param boolean showInfo Whether to display table position or not
809 * @see _setMinMax,Table_Stats::__construct(),Relation_Stats::__construct()
811 private function _addRelation($masterTable,$font,$fontSize, $masterField, $foreignTable, $foreignField, $showInfo)
813 if (!isset($this->tables
[$masterTable])) {
814 $this->tables
[$masterTable] = new Table_Stats($masterTable, $font, $fontSize, $this->pageNumber
, $this->_tablewidth
, false, $showInfo);
816 if (!isset($this->tables
[$foreignTable])) {
817 $this->tables
[$foreignTable] = new Table_Stats($foreignTable,$font,$fontSize,$this->pageNumber
, $this->_tablewidth
, false, $showInfo);
819 $this->_relations
[] = new Relation_Stats($this->tables
[$masterTable], $masterField, $this->tables
[$foreignTable], $foreignField);
823 * Draws relation arrows and lines
824 * connects master table's master field to
825 * foreign table's forein field
827 * @param boolean changeColor Whether to use one color per relation or not
830 * @see Relation_Stats::relationDraw()
832 private function _drawRelations($changeColor)
834 foreach ($this->_relations
as $relation) {
835 $relation->relationDraw($changeColor);
842 * @param boolean changeColor Whether to show color for primary fields or not
845 * @see Table_Stats::Table_Stats_tableDraw()
847 private function _drawTables($changeColor)
849 foreach ($this->tables
as $table) {
850 $table->tableDraw($changeColor);