A few fixes for comments in Relation_Schema classes (they still require further work)
[phpmyadmin.git] / libraries / schema / Pdf_Relation_Schema.class.php
blobe74c14d126d97e550bca6d132cf06c1f56df8088
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 require_once './libraries/PDF.class.php';
12 /**
13 * Extends the "TCPDF" class and helps
14 * in developing the structure of PDF Schema Export
16 * @access public
17 * @see TCPDF
19 class PMA_Schema_PDF extends PMA_PDF
21 /**
22 * Defines properties
24 var $_xMin;
25 var $_yMin;
26 var $leftMargin = 10;
27 var $topMargin = 10;
28 var $scale;
29 var $PMA_links;
30 var $Outlines = array();
31 var $def_outlines;
32 var $widths;
33 private $_ff = PMA_PDF_FONT;
35 public function setCMargin($c_margin)
37 $this->cMargin = $c_margin;
40 /**
41 * Sets the scaling factor, defines minimum coordinates and margins
43 * @param float scale The scaling factor
44 * @param float _xMin The minimum X coordinate
45 * @param float _yMin The minimum Y coordinate
46 * @param float leftMargin The left margin
47 * @param float topMargin The top margin
48 * @access public
50 function PMA_PDF_setScale($scale = 1, $xMin = 0, $yMin = 0, $leftMargin = -1, $topMargin = -1)
52 $this->scale = $scale;
53 $this->_xMin = $xMin;
54 $this->_yMin = $yMin;
55 if ($this->leftMargin != -1) {
56 $this->leftMargin = $leftMargin;
58 if ($this->topMargin != -1) {
59 $this->topMargin = $topMargin;
63 /**
64 * Outputs a scaled cell
66 * @param float w The cell width
67 * @param float h The cell height
68 * @param string txt The text to output
69 * @param mixed border Whether to add borders or not
70 * @param integer ln Where to put the cursor once the output is done
71 * @param string align Align mode
72 * @param integer fill Whether to fill the cell with a color or not
73 * @access public
74 * @see TCPDF::Cell()
76 function PMA_PDF_cellScale($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $fill = 0, $link = '')
78 $h = $h / $this->scale;
79 $w = $w / $this->scale;
80 $this->Cell($w, $h, $txt, $border, $ln, $align, $fill, $link);
83 /**
84 * Draws a scaled line
86 * @param float x1 The horizontal position of the starting point
87 * @param float y1 The vertical position of the starting point
88 * @param float x2 The horizontal position of the ending point
89 * @param float y2 The vertical position of the ending point
90 * @access public
91 * @see TCPDF::Line()
93 function PMA_PDF_lineScale($x1, $y1, $x2, $y2)
95 $x1 = ($x1 - $this->_xMin) / $this->scale + $this->leftMargin;
96 $y1 = ($y1 - $this->_yMin) / $this->scale + $this->topMargin;
97 $x2 = ($x2 - $this->_xMin) / $this->scale + $this->leftMargin;
98 $y2 = ($y2 - $this->_yMin) / $this->scale + $this->topMargin;
99 $this->Line($x1, $y1, $x2, $y2);
103 * Sets x and y scaled positions
105 * @param float x The x position
106 * @param float y The y position
107 * @access public
108 * @see TCPDF::SetXY()
110 function PMA_PDF_setXyScale($x, $y)
112 $x = ($x - $this->_xMin) / $this->scale + $this->leftMargin;
113 $y = ($y - $this->_yMin) / $this->scale + $this->topMargin;
114 $this->SetXY($x, $y);
118 * Sets the X scaled positions
120 * @param float x The x position
121 * @access public
122 * @see TCPDF::SetX()
124 function PMA_PDF_setXScale($x)
126 $x = ($x - $this->_xMin) / $this->scale + $this->leftMargin;
127 $this->SetX($x);
131 * Sets the scaled font size
133 * @param float size The font size (in points)
134 * @access public
135 * @see TCPDF::SetFontSize()
137 function PMA_PDF_setFontSizeScale($size)
139 // Set font size in points
140 $size = $size / $this->scale;
141 $this->SetFontSize($size);
145 * Sets the scaled line width
147 * @param float $width The line width
148 * @access public
149 * @see TCPDF::SetLineWidth()
151 function PMA_PDF_setLineWidthScale($width)
153 $width = $width / $this->scale;
154 $this->SetLineWidth($width);
157 function Header()
159 // We only show this if we find something in the new pdf_pages table
161 // This function must be named "Header" to work with the TCPDF library
162 global $cfgRelation, $db, $pdf_page_number, $with_doc;
163 if ($with_doc) {
164 $test_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
165 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
166 . ' AND page_nr = \'' . $pdf_page_number . '\'';
167 $test_rs = PMA_query_as_controluser($test_query);
168 $pages = @PMA_DBI_fetch_assoc($test_rs);
169 $this->SetFont($this->_ff, 'B', 14);
170 $this->Cell(0, 6, ucfirst($pages['page_descr']), 'B', 1, 'C');
171 $this->SetFont($this->_ff, '');
172 $this->Ln();
177 * This function must be named "Footer" to work with the TCPDF library
179 function Footer()
181 global $with_doc;
182 if ($with_doc) {
183 parent::Footer();
187 function SetWidths($w)
189 // column widths
190 $this->widths = $w;
193 function Row($data, $links)
195 // line height
196 $nb = 0;
197 $data_cnt = count($data);
198 for ($i = 0;$i < $data_cnt;$i++)
199 $nb = max($nb, $this->NbLines($this->widths[$i], $data[$i]));
200 $il = $this->FontSize;
201 $h = ($il + 1) * $nb;
202 // page break if necessary
203 $this->CheckPageBreak($h);
204 // draw the cells
205 $data_cnt = count($data);
206 for ($i = 0;$i < $data_cnt;$i++) {
207 $w = $this->widths[$i];
208 // save current position
209 $x = $this->GetX();
210 $y = $this->GetY();
211 // draw the border
212 $this->Rect($x, $y, $w, $h);
213 if (isset($links[$i])) {
214 $this->Link($x, $y, $w, $h, $links[$i]);
216 // print text
217 $this->MultiCell($w, $il + 1, $data[$i], 0, 'L');
218 // go to right side
219 $this->SetXY($x + $w, $y);
221 // go to line
222 $this->Ln($h);
226 * Compute number of lines used by a multicell of width w
228 * @param int $w
229 * @param string $txt
230 * @return int
232 function NbLines($w, $txt)
234 $cw = &$this->CurrentFont['cw'];
235 if ($w == 0) {
236 $w = $this->w - $this->rMargin - $this->x;
238 $wmax = ($w-2 * $this->cMargin) * 1000 / $this->FontSize;
239 $s = str_replace("\r", '', $txt);
240 $nb = strlen($s);
241 if ($nb > 0 and $s[$nb-1] == "\n") {
242 $nb--;
244 $sep = -1;
245 $i = 0;
246 $j = 0;
247 $l = 0;
248 $nl = 1;
249 while ($i < $nb) {
250 $c = $s[$i];
251 if ($c == "\n") {
252 $i++;
253 $sep = -1;
254 $j = $i;
255 $l = 0;
256 $nl++;
257 continue;
259 if ($c == ' ') {
260 $sep = $i;
262 $l += isset($cw[ord($c)])?$cw[ord($c)]:0 ;
263 if ($l > $wmax) {
264 if ($sep == -1) {
265 if ($i == $j) {
266 $i++;
268 } else {
269 $i = $sep + 1;
271 $sep = -1;
272 $j = $i;
273 $l = 0;
274 $nl++;
275 } else {
276 $i++;
279 return $nl;
284 * Table preferences/statistics
286 * This class preserves the table co-ordinates,fields
287 * and helps in drawing/generating the Tables in PDF document.
289 * @name Table_Stats
290 * @see PMA_Schema_PDF
292 class Table_Stats
295 * Defines properties
297 private $_tableName;
298 private $_showInfo = false;
300 public $nb_fiels;
301 public $width = 0;
302 public $height;
303 public $fields = array();
304 public $heightCell = 6;
305 public $x, $y;
306 public $primary = array();
307 private $_ff = PMA_PDF_FONT;
310 * The "Table_Stats" constructor
312 * @param string table_name The table name
313 * @param integer fontSize The font size
314 * @param integer pageNumber The current page number (from the
315 * $cfg['Servers'][$i]['table_coords'] table)
316 * @param integer sameWideWidth The max. with among tables
317 * @param boolean showKeys Whether to display keys or not
318 * @param boolean showInfo Whether to display table position or not
319 * @global object The current PDF document
320 * @global array The relations settings
321 * @global string The current db name
322 * @see PMA_Schema_PDF, Table_Stats::Table_Stats_setWidth,
323 Table_Stats::Table_Stats_setHeight
325 function __construct($tableName, $fontSize, $pageNumber, &$sameWideWidth, $showKeys = false, $showInfo = false)
327 global $pdf, $cfgRelation, $db;
329 $this->_tableName = $tableName;
330 $sql = 'DESCRIBE ' . PMA_backquote($tableName);
331 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE);
332 if (!$result || !PMA_DBI_num_rows($result)) {
333 $pdf->Error(sprintf(__('The %s table doesn\'t exist!'), $tableName));
335 // load fields
336 //check to see if it will load all fields or only the foreign keys
337 if ($showKeys) {
338 $indexes = PMA_Index::getFromTable($this->_tableName, $db);
339 $all_columns = array();
340 foreach ($indexes as $index) {
341 $all_columns = array_merge($all_columns, array_flip(array_keys($index->getColumns())));
343 $this->fields = array_keys($all_columns);
344 } else {
345 while ($row = PMA_DBI_fetch_row($result)) {
346 $this->fields[] = $row[0];
350 $this->_showInfo = $showInfo;
351 $this->_setHeight();
353 * setWidth must me after setHeight, because title
354 * can include table height which changes table width
356 $this->_setWidth($fontSize);
357 if ($sameWideWidth < $this->width) {
358 $sameWideWidth = $this->width;
360 $sql = 'SELECT x, y FROM '
361 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
362 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
363 . ' AND table_name = \'' . PMA_sqlAddSlashes($tableName) . '\''
364 . ' AND pdf_page_number = ' . $pageNumber;
365 $result = PMA_query_as_controluser($sql, false, PMA_DBI_QUERY_STORE);
366 if (!$result || !PMA_DBI_num_rows($result)) {
367 $pdf->Error(sprintf(__('Please configure the coordinates for table %s'), $tableName));
369 list($this->x, $this->y) = PMA_DBI_fetch_row($result);
370 $this->x = (double) $this->x;
371 $this->y = (double) $this->y;
373 * displayfield
375 $this->displayfield = PMA_getDisplayField($db, $tableName);
377 * index
379 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tableName) . ';', null, PMA_DBI_QUERY_STORE);
380 if (PMA_DBI_num_rows($result) > 0) {
381 while ($row = PMA_DBI_fetch_assoc($result)) {
382 if ($row['Key_name'] == 'PRIMARY') {
383 $this->primary[] = $row['Column_name'];
390 * Returns title of the current table,
391 * title can have the dimensions of the table
393 * @return string
395 private function _getTitle()
397 return ($this->_showInfo ? sprintf('%.0f', $this->width) . 'x' . sprintf('%.0f', $this->height) : '') . ' ' . $this->_tableName;
401 * Sets the width of the table
403 * @param integer fontSize The font size
404 * @global object The current PDF document
405 * @access private
406 * @see PMA_Schema_PDF
408 private function _setWidth($fontSize)
410 global $pdf;
412 foreach ($this->fields as $field) {
413 $this->width = max($this->width, $pdf->GetStringWidth($field));
415 $this->width += $pdf->GetStringWidth(' ');
416 $pdf->SetFont($this->_ff, 'B', $fontSize);
418 * it is unknown what value must be added, because
419 * table title is affected by the tabe width value
421 while ($this->width < $pdf->GetStringWidth($this->_getTitle())) {
422 $this->width += 5;
424 $pdf->SetFont($this->_ff, '', $fontSize);
428 * Sets the height of the table
430 * @access private
432 private function _setHeight()
434 $this->height = (count($this->fields) + 1) * $this->heightCell;
438 * Do draw the table
440 * @param integer fontSize The font size
441 * @param boolean setColor Whether to display color
442 * @global object The current PDF document
443 * @access public
444 * @see PMA_Schema_PDF
446 public function tableDraw($fontSize, $withDoc, $setColor = 0)
448 global $pdf, $withDoc;
450 $pdf->PMA_PDF_setXyScale($this->x, $this->y);
451 $pdf->SetFont($this->_ff, 'B', $fontSize);
452 if ($setColor) {
453 $pdf->SetTextColor(200);
454 $pdf->SetFillColor(0, 0, 128);
456 if ($withDoc) {
457 $pdf->SetLink($pdf->PMA_links['RT'][$this->_tableName]['-'], -1);
458 } else {
459 $pdf->PMA_links['doc'][$this->_tableName]['-'] = '';
462 $pdf->PMA_PDF_cellScale($this->width, $this->heightCell, $this->_getTitle(), 1, 1, 'C', $setColor, $pdf->PMA_links['doc'][$this->_tableName]['-']);
463 $pdf->PMA_PDF_setXScale($this->x);
464 $pdf->SetFont($this->_ff, '', $fontSize);
465 $pdf->SetTextColor(0);
466 $pdf->SetFillColor(255);
468 foreach ($this->fields as $field) {
469 if ($setColor) {
470 if (in_array($field, $this->primary)) {
471 $pdf->SetFillColor(215, 121, 123);
473 if ($field == $this->displayfield) {
474 $pdf->SetFillColor(142, 159, 224);
477 if ($withDoc) {
478 $pdf->SetLink($pdf->PMA_links['RT'][$this->_tableName][$field], -1);
479 } else {
480 $pdf->PMA_links['doc'][$this->_tableName][$field] = '';
483 $pdf->PMA_PDF_cellScale($this->width, $this->heightCell, ' ' . $field, 1, 1, 'L', $setColor, $pdf->PMA_links['doc'][$this->_tableName][$field]);
484 $pdf->PMA_PDF_setXScale($this->x);
485 $pdf->SetFillColor(255);
487 /*if ($pdf->PageNo() > 1) {
488 $pdf->PMA_PDF_die(__('The scale factor is too small to fit the schema on one page'));
489 } */
494 * Relation preferences/statistics
496 * This class fetches the table master and foreign fields positions
497 * and helps in generating the Table references and then connects
498 * master table's master field to foreign table's foreign key
499 * in PDF document.
501 * @name Relation_Stats
502 * @see PMA_Schema_PDF::SetDrawColor,PMA_Schema_PDF::PMA_PDF_setLineWidthScale,PMA_Schema_PDF::PMA_PDF_lineScale
504 class Relation_Stats
507 * Defines properties
509 public $xSrc, $ySrc;
510 public $srcDir;
511 public $destDir;
512 public $xDest, $yDest;
513 public $wTick = 5;
516 * The "Relation_Stats" constructor
518 * @param string master_table The master table name
519 * @param string master_field The relation field in the master table
520 * @param string foreign_table The foreign table name
521 * @param string foreigh_field The relation field in the foreign table
522 * @see Relation_Stats::_getXy
524 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
526 $src_pos = $this->_getXy($master_table, $master_field);
527 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
529 * [0] is x-left
530 * [1] is x-right
531 * [2] is y
533 $src_left = $src_pos[0] - $this->wTick;
534 $src_right = $src_pos[1] + $this->wTick;
535 $dest_left = $dest_pos[0] - $this->wTick;
536 $dest_right = $dest_pos[1] + $this->wTick;
538 $d1 = abs($src_left - $dest_left);
539 $d2 = abs($src_right - $dest_left);
540 $d3 = abs($src_left - $dest_right);
541 $d4 = abs($src_right - $dest_right);
542 $d = min($d1, $d2, $d3, $d4);
544 if ($d == $d1) {
545 $this->xSrc = $src_pos[0];
546 $this->srcDir = -1;
547 $this->xDest = $dest_pos[0];
548 $this->destDir = -1;
549 } elseif ($d == $d2) {
550 $this->xSrc = $src_pos[1];
551 $this->srcDir = 1;
552 $this->xDest = $dest_pos[0];
553 $this->destDir = -1;
554 } elseif ($d == $d3) {
555 $this->xSrc = $src_pos[0];
556 $this->srcDir = -1;
557 $this->xDest = $dest_pos[1];
558 $this->destDir = 1;
559 } else {
560 $this->xSrc = $src_pos[1];
561 $this->srcDir = 1;
562 $this->xDest = $dest_pos[1];
563 $this->destDir = 1;
565 $this->ySrc = $src_pos[2];
566 $this->yDest = $dest_pos[2];
570 * Gets arrows coordinates
572 * @param string table The current table name
573 * @param string column The relation column name
574 * @return array Arrows coordinates
575 * @access private
577 private function _getXy($table, $column)
579 $pos = array_search($column, $table->fields);
580 // x_left, x_right, y
581 return array($table->x, $table->x + + $table->width, $table->y + ($pos + 1.5) * $table->heightCell);
585 * draws relation links and arrows
586 * shows foreign key relations
588 * @param boolean changeColor Whether to use one color per relation or not
589 * @param integer i The id of the link to draw
590 * @global object The current PDF document
591 * @access public
592 * @see PMA_Schema_PDF
594 public function relationDraw($changeColor, $i)
596 global $pdf;
598 if ($changeColor) {
599 $d = $i % 6;
600 $j = ($i - $d) / 6;
601 $j = $j % 4;
602 $j++;
603 $case = array(
604 array(1, 0, 0),
605 array(0, 1, 0),
606 array(0, 0, 1),
607 array(1, 1, 0),
608 array(1, 0, 1),
609 array(0, 1, 1)
611 list ($a, $b, $c) = $case[$d];
612 $e = (1 - ($j - 1) / 6);
613 $pdf->SetDrawColor($a * 255 * $e, $b * 255 * $e, $c * 255 * $e);
614 } else {
615 $pdf->SetDrawColor(0);
617 $pdf->PMA_PDF_setLineWidthScale(0.2);
618 $pdf->PMA_PDF_lineScale($this->xSrc, $this->ySrc, $this->xSrc + $this->srcDir * $this->wTick, $this->ySrc);
619 $pdf->PMA_PDF_lineScale($this->xDest + $this->destDir * $this->wTick, $this->yDest, $this->xDest, $this->yDest);
620 $pdf->PMA_PDF_setLineWidthScale(0.1);
621 $pdf->PMA_PDF_lineScale($this->xSrc + $this->srcDir * $this->wTick, $this->ySrc, $this->xDest + $this->destDir * $this->wTick, $this->yDest);
623 * Draws arrows ->
625 $root2 = 2 * sqrt(2);
626 $pdf->PMA_PDF_lineScale($this->xSrc + $this->srcDir * $this->wTick * 0.75, $this->ySrc, $this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick, $this->ySrc + $this->wTick / $root2);
627 $pdf->PMA_PDF_lineScale($this->xSrc + $this->srcDir * $this->wTick * 0.75, $this->ySrc, $this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick, $this->ySrc - $this->wTick / $root2);
629 $pdf->PMA_PDF_lineScale($this->xDest + $this->destDir * $this->wTick / 2, $this->yDest, $this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick, $this->yDest + $this->wTick / $root2);
630 $pdf->PMA_PDF_lineScale($this->xDest + $this->destDir * $this->wTick / 2, $this->yDest, $this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick, $this->yDest - $this->wTick / $root2);
631 $pdf->SetDrawColor(0);
636 * Pdf Relation Schema Class
638 * Purpose of this class is to generate the PDF Document. PDF is widely
639 * used format for documenting text,fonts,images and 3d vector graphics.
641 * This class inherits Export_Relation_Schema class has common functionality added
642 * to this class
644 * @name Pdf_Relation_Schema
646 class PMA_Pdf_Relation_Schema extends PMA_Export_Relation_Schema
649 * Defines properties
651 private $_tables = array();
652 private $_relations = array();
653 private $_ff = PMA_PDF_FONT;
654 private $_xMax = 0;
655 private $_yMax = 0;
656 private $scale;
657 private $_xMin = 100000;
658 private $_yMin = 100000;
659 private $topMargin = 10;
660 private $bottomMargin = 10;
661 private $leftMargin = 10;
662 private $rightMargin = 10;
663 private $_tablewidth;
666 * The "PMA_Pdf_Relation_Schema" constructor
668 * @global object The current PDF Schema document
669 * @global string The current db name
670 * @global array The relations settings
671 * @access private
672 * @see PMA_Schema_PDF
674 function __construct()
676 global $pdf,$db,$cfgRelation;
678 $this->setPageNumber($_POST['pdf_page_number']);
679 $this->setShowGrid(isset($_POST['show_grid']));
680 $this->setShowColor(isset($_POST['show_color']));
681 $this->setShowKeys(isset($_POST['show_keys']));
682 $this->setTableDimension(isset($_POST['show_table_dimension']));
683 $this->setAllTableSameWidth(isset($_POST['all_table_same_wide']));
684 $this->setWithDataDictionary($_POST['with_doc']);
685 $this->setOrientation($_POST['orientation']);
686 $this->setPaper($_POST['paper']);
687 $this->setExportType($_POST['export_type']);
689 // Initializes a new document
690 $pdf = new PMA_Schema_PDF($this->orientation, 'mm', $this->paper);
691 $pdf->SetTitle(sprintf(__('Schema of the %s database - Page %s'), $GLOBALS['db'], $this->pageNumber));
692 $pdf->setCMargin(0);
693 $pdf->Open();
694 $pdf->SetAutoPageBreak('auto');
695 $alltables = $this->getAllTables($db,$this->pageNumber);
697 if ($this->withDoc) {
698 $pdf->SetAutoPageBreak('auto', 15);
699 $pdf->setCMargin(1);
700 $this->dataDictionaryDoc($alltables);
701 $pdf->SetAutoPageBreak('auto');
702 $pdf->setCMargin(0);
705 $pdf->Addpage();
707 if ($this->withDoc) {
708 $pdf->SetLink($pdf->PMA_links['RT']['-'], -1);
709 $pdf->Bookmark(__('Relational schema'));
710 $pdf->SetAlias('{00}', $pdf->PageNo()) ;
711 $this->topMargin = 28;
712 $this->bottomMargin = 28;
715 /* snip */
716 foreach ($alltables as $table) {
717 if (! isset($this->tables[$table])) {
718 $this->tables[$table] = new Table_Stats($table, $this->_ff, $this->pageNumber, $this->_tablewidth, $this->showKeys, $this->tableDimension);
720 if ($this->sameWide) {
721 $this->tables[$table]->width = $this->_tablewidth;
723 $this->_setMinMax($this->tables[$table]);
726 // Defines the scale factor
727 $this->scale = ceil(
728 max(
729 ($this->_xMax - $this->_xMin) / ($pdf->getPageWidth() - $this->rightMargin - $this->leftMargin),
730 ($this->_yMax - $this->_yMin) / ($pdf->getPageHeight() - $this->topMargin - $this->bottomMargin))
731 * 100) / 100;
733 $pdf->PMA_PDF_setScale($this->scale, $this->_xMin, $this->_yMin, $this->leftMargin, $this->topMargin);
734 // Builds and save the PDF document
735 $pdf->PMA_PDF_setLineWidthScale(0.1);
737 if ($this->showGrid) {
738 $pdf->SetFontSize(10);
739 $this->_strokeGrid();
741 $pdf->PMA_PDF_setFontSizeScale(14);
742 // previous logic was checking master tables and foreign tables
743 // but I think that looping on every table of the pdf page as a master
744 // and finding its foreigns is OK (then we can support innodb)
745 $seen_a_relation = false;
746 foreach ($alltables as $one_table) {
747 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
748 if ($exist_rel) {
749 $seen_a_relation = true;
750 foreach ($exist_rel as $master_field => $rel) {
751 // put the foreign table on the schema only if selected
752 // by the user
753 // (do not use array_search() because we would have to
754 // to do a === false and this is not PHP3 compatible)
755 if (in_array($rel['foreign_table'], $alltables)) {
756 $this->_addRelation($one_table, $master_field, $rel['foreign_table'], $rel['foreign_field'], $this->tableDimension);
758 } // end while
759 } // end if
760 } // end while
762 if ($seen_a_relation) {
763 $this->_drawRelations($this->showColor);
765 $this->_drawTables($this->showColor);
766 $this->_showOutput($this->pageNumber);
767 exit();
771 * Sets X and Y minimum and maximum for a table cell
773 * @param string table The table name of which sets XY co-ordinates
774 * @access private
776 private function _setMinMax($table)
778 $this->_xMax = max($this->_xMax, $table->x + $table->width);
779 $this->_yMax = max($this->_yMax, $table->y + $table->height);
780 $this->_xMin = min($this->_xMin, $table->x);
781 $this->_yMin = min($this->_yMin, $table->y);
785 * Defines relation objects
787 * @param string master_table The master table name
788 * @param string master_field The relation field in the master table
789 * @param string foreign_table The foreign table name
790 * @param string foreign_field The relation field in the foreign table
791 * @param boolean show_info Whether to display table position or not
792 * @access private
793 * @see _setMinMax
795 private function _addRelation($masterTable, $masterField, $foreignTable, $foreignField, $showInfo)
797 if (! isset($this->tables[$masterTable])) {
798 $this->tables[$masterTable] = new Table_Stats($masterTable, $this->_ff, $this->pageNumber, $this->_tablewidth, false, $showInfo);
799 $this->_setMinMax($this->tables[$masterTable]);
801 if (! isset($this->tables[$foreignTable])) {
802 $this->tables[$foreignTable] = new Table_Stats($foreignTable, $this->_ff, $this->pageNumber, $this->_tablewidth, false, $showInfo);
803 $this->_setMinMax($this->tables[$foreignTable]);
805 $this->relations[] = new Relation_Stats($this->tables[$masterTable], $masterField, $this->tables[$foreignTable], $foreignField);
809 * Draws the grid
811 * @global object the current PMA_Schema_PDF instance
812 * @access private
813 * @see PMA_Schema_PDF
815 private function _strokeGrid()
817 global $pdf;
819 $gridSize = 10;
820 $labelHeight = 4;
821 $labelWidth = 5;
822 if ($this->withDoc) {
823 $topSpace = 6;
824 $bottomSpace = 15;
825 } else {
826 $topSpace = 0;
827 $bottomSpace = 0;
830 $pdf->SetMargins(0, 0);
831 $pdf->SetDrawColor(200, 200, 200);
832 // Draws horizontal lines
833 for ($l = 0; $l <= intval(($pdf->getPageHeight() - $topSpace - $bottomSpace) / $gridSize); $l++) {
834 $pdf->line(0, $l * $gridSize + $topSpace, $pdf->getPageWidth(), $l * $gridSize + $topSpace);
835 // Avoid duplicates
836 if ($l > 0 && $l <= intval(($pdf->getPageHeight() - $topSpace - $bottomSpace - $labelHeight) / $gridSize)) {
837 $pdf->SetXY(0, $l * $gridSize + $topSpace);
838 $label = (string) sprintf('%.0f', ($l * $gridSize + $topSpace - $this->topMargin) * $this->scale + $this->_yMin);
839 $pdf->Cell($labelWidth, $labelHeight, ' ' . $label);
840 } // end if
841 } // end for
842 // Draws vertical lines
843 for ($j = 0; $j <= intval($pdf->getPageWidth() / $gridSize); $j++) {
844 $pdf->line($j * $gridSize, $topSpace, $j * $gridSize, $pdf->getPageHeight() - $bottomSpace);
845 $pdf->SetXY($j * $gridSize, $topSpace);
846 $label = (string) sprintf('%.0f', ($j * $gridSize - $this->leftMargin) * $this->scale + $this->_xMin);
847 $pdf->Cell($labelWidth, $labelHeight, $label);
852 * Draws relation arrows
854 * @param boolean changeColor Whether to use one color per relation or not
855 * @access private
856 * @see Relation_Stats::relationdraw()
858 private function _drawRelations($changeColor)
860 $i = 0;
861 foreach ($this->relations as $relation) {
862 $relation->relationDraw($changeColor, $i);
863 $i++;
868 * Draws tables
870 * @param boolean changeColor Whether to display table position or not
871 * @access private
872 * @see Table_Stats::tableDraw()
874 private function _drawTables($changeColor = 0)
876 foreach ($this->tables as $table) {
877 $table->tableDraw($this->_ff, $this->withDoc, $changeColor);
882 * Ouputs the PDF document to a file
883 * or sends the output to browser
885 * @global object The current PDF document
886 * @global string The current database name
887 * @global integer The current page number (from the
888 * $cfg['Servers'][$i]['table_coords'] table)
889 * @access private
890 * @see PMA_Schema_PDF
892 private function _showOutput($pageNumber)
894 global $pdf, $db, $cfgRelation;
896 // Get the name of this pdfpage to use as filename
897 $_name_sql = 'SELECT page_descr FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
898 . ' WHERE page_nr = ' . $pageNumber;
899 $_name_rs = PMA_query_as_controluser($_name_sql);
900 if ($_name_rs) {
901 $_name_row = PMA_DBI_fetch_row($_name_rs);
902 $filename = $_name_row[0] . '.pdf';
904 if (empty($filename)) {
905 $filename = $pageNumber . '.pdf';
907 $pdf->Download($filename);
910 public function dataDictionaryDoc($alltables)
912 global $db, $pdf, $orientation, $paper;
913 // TOC
914 $pdf->addpage($GLOBALS['orientation']);
915 $pdf->Cell(0, 9, __('Table of contents'), 1, 0, 'C');
916 $pdf->Ln(15);
917 $i = 1;
918 foreach ($alltables as $table) {
919 $pdf->PMA_links['doc'][$table]['-'] = $pdf->AddLink();
920 $pdf->SetX(10);
921 // $pdf->Ln(1);
922 $pdf->Cell(0, 6, __('Page number:') . ' {' . sprintf("%02d", $i) . '}', 0, 0, 'R', 0, $pdf->PMA_links['doc'][$table]['-']);
923 $pdf->SetX(10);
924 $pdf->Cell(0, 6, $i . ' ' . $table, 0, 1, 'L', 0, $pdf->PMA_links['doc'][$table]['-']);
925 // $pdf->Ln(1);
926 $result = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';');
927 while ($row = PMA_DBI_fetch_assoc($result)) {
928 $pdf->SetX(20);
929 $field_name = $row['Field'];
930 $pdf->PMA_links['doc'][$table][$field_name] = $pdf->AddLink();
931 // $pdf->Cell(0, 6, $field_name,0,1,'L',0, $pdf->PMA_links['doc'][$table][$field_name]);
933 $lasttable = $table;
934 $i++;
936 $pdf->PMA_links['RT']['-'] = $pdf->AddLink();
937 $pdf->SetX(10);
938 $pdf->Cell(0, 6, __('Page number:') . ' {00}', 0, 0, 'R', 0, $pdf->PMA_links['RT']['-']);
939 $pdf->SetX(10);
940 $pdf->Cell(0, 6, $i . ' ' . __('Relational schema'), 0, 1, 'L', 0, $pdf->PMA_links['RT']['-']);
941 $z = 0;
942 foreach ($alltables as $table) {
943 $z++;
944 $pdf->SetAutoPageBreak(true, 15);
945 $pdf->addpage($GLOBALS['orientation']);
946 $pdf->Bookmark($table);
947 $pdf->SetAlias('{' . sprintf("%02d", $z) . '}', $pdf->PageNo()) ;
948 $pdf->PMA_links['RT'][$table]['-'] = $pdf->AddLink();
949 $pdf->SetLink($pdf->PMA_links['doc'][$table]['-'], -1);
950 $pdf->SetFont($this->_ff, 'B', 18);
951 $pdf->Cell(0, 8, $z . ' ' . $table, 1, 1, 'C', 0, $pdf->PMA_links['RT'][$table]['-']);
952 $pdf->SetFont($this->_ff, '', 8);
953 $pdf->ln();
955 $cfgRelation = PMA_getRelationsParam();
956 $comments = PMA_getComments($db, $table);
957 if ($cfgRelation['mimework']) {
958 $mime_map = PMA_getMIME($db, $table, true);
962 * Gets table informations
964 $showtable = PMA_Table::sGetStatusInfo($db, $table);
965 $num_rows = (isset($showtable['Rows']) ? $showtable['Rows'] : 0);
966 $show_comment = (isset($showtable['Comment']) ? $showtable['Comment'] : '');
967 $create_time = (isset($showtable['Create_time']) ? PMA_localisedDate(strtotime($showtable['Create_time'])) : '');
968 $update_time = (isset($showtable['Update_time']) ? PMA_localisedDate(strtotime($showtable['Update_time'])) : '');
969 $check_time = (isset($showtable['Check_time']) ? PMA_localisedDate(strtotime($showtable['Check_time'])) : '');
972 * Gets table keys and retains them
974 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
975 $primary = '';
976 $indexes = array();
977 $lastIndex = '';
978 $indexes_info = array();
979 $indexes_data = array();
980 $pk_array = array(); // will be use to emphasis prim. keys in the table
981 // view
982 while ($row = PMA_DBI_fetch_assoc($result)) {
983 // Backups the list of primary keys
984 if ($row['Key_name'] == 'PRIMARY') {
985 $primary .= $row['Column_name'] . ', ';
986 $pk_array[$row['Column_name']] = 1;
988 // Retains keys informations
989 if ($row['Key_name'] != $lastIndex) {
990 $indexes[] = $row['Key_name'];
991 $lastIndex = $row['Key_name'];
993 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
994 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
995 if (isset($row['Cardinality'])) {
996 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
998 // I don't know what does following column mean....
999 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
1000 $indexes_info[$row['Key_name']]['Comment'] = $row['Comment'];
1002 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
1003 if (isset($row['Sub_part'])) {
1004 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
1006 } // end while
1007 if ($result) {
1008 PMA_DBI_free_result($result);
1012 * Gets fields properties
1014 $columns = PMA_DBI_get_columns($db, $table);
1015 // Check if we can use Relations
1016 if (!empty($cfgRelation['relation'])) {
1017 // Find which tables are related with the current one and write it in
1018 // an array
1019 $res_rel = PMA_getForeigners($db, $table);
1021 if (count($res_rel) > 0) {
1022 $have_rel = true;
1023 } else {
1024 $have_rel = false;
1026 } else {
1027 $have_rel = false;
1028 } // end if
1031 * Displays the comments of the table if MySQL >= 3.23
1034 $break = false;
1035 if (!empty($show_comment)) {
1036 $pdf->Cell(0, 3, __('Table comments') . ' : ' . $show_comment, 0, 1);
1037 $break = true;
1040 if (!empty($create_time)) {
1041 $pdf->Cell(0, 3, __('Creation') . ': ' . $create_time, 0, 1);
1042 $break = true;
1045 if (!empty($update_time)) {
1046 $pdf->Cell(0, 3, __('Last update') . ': ' . $update_time, 0, 1);
1047 $break = true;
1050 if (!empty($check_time)) {
1051 $pdf->Cell(0, 3, __('Last check') . ': ' . $check_time, 0, 1);
1052 $break = true;
1055 if ($break == true) {
1056 $pdf->Cell(0, 3, '', 0, 1);
1057 $pdf->Ln();
1060 $pdf->SetFont($this->_ff, 'B');
1061 if (isset($orientation) && $orientation == 'L') {
1062 $pdf->Cell(25, 8, __('Column'), 1, 0, 'C');
1063 $pdf->Cell(20, 8, __('Type'), 1, 0, 'C');
1064 $pdf->Cell(20, 8, __('Attributes'), 1, 0, 'C');
1065 $pdf->Cell(10, 8, __('Null'), 1, 0, 'C');
1066 $pdf->Cell(20, 8, __('Default'), 1, 0, 'C');
1067 $pdf->Cell(25, 8, __('Extra'), 1, 0, 'C');
1068 $pdf->Cell(45, 8, __('Links to'), 1, 0, 'C');
1070 if ($paper == 'A4') {
1071 $comments_width = 67;
1072 } else {
1073 // this is really intended for 'letter'
1075 * @todo find optimal width for all formats
1077 $comments_width = 50;
1079 $pdf->Cell($comments_width, 8, __('Comments'), 1, 0, 'C');
1080 $pdf->Cell(45, 8, 'MIME', 1, 1, 'C');
1081 $pdf->SetWidths(array(25, 20, 20, 10, 20, 25, 45, $comments_width, 45));
1082 } else {
1083 $pdf->Cell(20, 8, __('Column'), 1, 0, 'C');
1084 $pdf->Cell(20, 8, __('Type'), 1, 0, 'C');
1085 $pdf->Cell(20, 8, __('Attributes'), 1, 0, 'C');
1086 $pdf->Cell(10, 8, __('Null'), 1, 0, 'C');
1087 $pdf->Cell(15, 8, __('Default'), 1, 0, 'C');
1088 $pdf->Cell(15, 8, __('Extra'), 1, 0, 'C');
1089 $pdf->Cell(30, 8, __('Links to'), 1, 0, 'C');
1090 $pdf->Cell(30, 8, __('Comments'), 1, 0, 'C');
1091 $pdf->Cell(30, 8, 'MIME', 1, 1, 'C');
1092 $pdf->SetWidths(array(20, 20, 20, 10, 15, 15, 30, 30, 30));
1094 $pdf->SetFont($this->_ff, '');
1096 foreach ($columns as $row) {
1097 $extracted_fieldspec = PMA_extractFieldSpec($row['Type']);
1098 $type = $extracted_fieldspec['print_type'];
1099 $attribute = $extracted_fieldspec['attribute'];
1100 if (! isset($row['Default'])) {
1101 if ($row['Null'] != '' && $row['Null'] != 'NO') {
1102 $row['Default'] = 'NULL';
1105 $field_name = $row['Field'];
1106 // $pdf->Ln();
1107 $pdf->PMA_links['RT'][$table][$field_name] = $pdf->AddLink();
1108 $pdf->Bookmark($field_name, 1, -1);
1109 $pdf->SetLink($pdf->PMA_links['doc'][$table][$field_name], -1);
1110 $pdf_row = array($field_name,
1111 $type,
1112 $attribute,
1113 ($row['Null'] == '' || $row['Null'] == 'NO') ? __('No') : __('Yes'),
1114 ((isset($row['Default'])) ? $row['Default'] : ''),
1115 $row['Extra'],
1116 ((isset($res_rel[$field_name])) ? $res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field'] : ''),
1117 ((isset($comments[$field_name])) ? $comments[$field_name] : ''),
1118 ((isset($mime_map) && isset($mime_map[$field_name])) ? str_replace('_', '/', $mime_map[$field_name]['mimetype']) : '')
1120 $links[0] = $pdf->PMA_links['RT'][$table][$field_name];
1121 if (isset($res_rel[$field_name]['foreign_table']) AND
1122 isset($res_rel[$field_name]['foreign_field']) AND
1123 isset($pdf->PMA_links['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']])
1126 $links[6] = $pdf->PMA_links['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']];
1127 } else {
1128 unset($links[6]);
1130 $pdf->Row($pdf_row, $links);
1131 } // end foreach
1132 $pdf->SetFont($this->_ff, '', 14);
1133 } //end each