Merge remote-tracking branch 'origin/master' into drizzle
[phpmyadmin.git] / libraries / schema / Pdf_Relation_Schema.class.php
blobdbca46ca77c1df8103e880c57a29b93d30ab1de8
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 function NbLines($w, $txt)
230 $cw = &$this->CurrentFont['cw'];
231 if ($w == 0) {
232 $w = $this->w - $this->rMargin - $this->x;
234 $wmax = ($w-2 * $this->cMargin) * 1000 / $this->FontSize;
235 $s = str_replace("\r", '', $txt);
236 $nb = strlen($s);
237 if ($nb > 0 and $s[$nb-1] == "\n") {
238 $nb--;
240 $sep = -1;
241 $i = 0;
242 $j = 0;
243 $l = 0;
244 $nl = 1;
245 while ($i < $nb) {
246 $c = $s[$i];
247 if ($c == "\n") {
248 $i++;
249 $sep = -1;
250 $j = $i;
251 $l = 0;
252 $nl++;
253 continue;
255 if ($c == ' ') {
256 $sep = $i;
258 $l += isset($cw[ord($c)])?$cw[ord($c)]:0 ;
259 if ($l > $wmax) {
260 if ($sep == -1) {
261 if ($i == $j) {
262 $i++;
264 } else {
265 $i = $sep + 1;
267 $sep = -1;
268 $j = $i;
269 $l = 0;
270 $nl++;
271 } else {
272 $i++;
275 return $nl;
280 * Table preferences/statistics
282 * This class preserves the table co-ordinates,fields
283 * and helps in drawing/generating the Tables in PDF document.
285 * @name Table_Stats
286 * @see PMA_Schema_PDF
288 class Table_Stats
291 * Defines properties
293 private $_tableName;
294 private $_showInfo = false;
296 public $nb_fiels;
297 public $width = 0;
298 public $height;
299 public $fields = array();
300 public $heightCell = 6;
301 public $x, $y;
302 public $primary = array();
303 private $_ff = PMA_PDF_FONT;
306 * The "Table_Stats" constructor
308 * @param string table_name The table name
309 * @param integer fontSize The font size
310 * @param integer pageNumber The current page number (from the
311 * $cfg['Servers'][$i]['table_coords'] table)
312 * @param integer sameWideWidth The max. with among tables
313 * @param boolean showKeys Whether to display keys or not
314 * @param boolean showInfo Whether to display table position or not
315 * @global object The current PDF document
316 * @global array The relations settings
317 * @global string The current db name
318 * @see PMA_Schema_PDF, Table_Stats::Table_Stats_setWidth,
319 Table_Stats::Table_Stats_setHeight
321 function __construct($tableName, $fontSize, $pageNumber, &$sameWideWidth, $showKeys = false, $showInfo = false)
323 global $pdf, $cfgRelation, $db;
325 $this->_tableName = $tableName;
326 $sql = 'DESCRIBE ' . PMA_backquote($tableName);
327 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE);
328 if (!$result || !PMA_DBI_num_rows($result)) {
329 $pdf->Error(sprintf(__('The %s table doesn\'t exist!'), $tableName));
331 // load fields
332 //check to see if it will load all fields or only the foreign keys
333 if ($showKeys) {
334 $indexes = PMA_Index::getFromTable($this->_tableName, $db);
335 $all_columns = array();
336 foreach ($indexes as $index) {
337 $all_columns = array_merge($all_columns, array_flip(array_keys($index->getColumns())));
339 $this->fields = array_keys($all_columns);
340 } else {
341 while ($row = PMA_DBI_fetch_row($result)) {
342 $this->fields[] = $row[0];
346 $this->_showInfo = $showInfo;
347 $this->_setHeight();
349 * setWidth must me after setHeight, because title
350 * can include table height which changes table width
352 $this->_setWidth($fontSize);
353 if ($sameWideWidth < $this->width) {
354 $sameWideWidth = $this->width;
356 $sql = 'SELECT x, y FROM '
357 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
358 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
359 . ' AND table_name = \'' . PMA_sqlAddSlashes($tableName) . '\''
360 . ' AND pdf_page_number = ' . $pageNumber;
361 $result = PMA_query_as_controluser($sql, false, PMA_DBI_QUERY_STORE);
362 if (!$result || !PMA_DBI_num_rows($result)) {
363 $pdf->Error(sprintf(__('Please configure the coordinates for table %s'), $tableName));
365 list($this->x, $this->y) = PMA_DBI_fetch_row($result);
366 $this->x = (double) $this->x;
367 $this->y = (double) $this->y;
369 * displayfield
371 $this->displayfield = PMA_getDisplayField($db, $tableName);
373 * index
375 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tableName) . ';', null, PMA_DBI_QUERY_STORE);
376 if (PMA_DBI_num_rows($result) > 0) {
377 while ($row = PMA_DBI_fetch_assoc($result)) {
378 if ($row['Key_name'] == 'PRIMARY') {
379 $this->primary[] = $row['Column_name'];
386 * Returns title of the current table,
387 * title can have the dimensions of the table
389 * @access private
391 private function _getTitle()
393 return ($this->_showInfo ? sprintf('%.0f', $this->width) . 'x' . sprintf('%.0f', $this->height) : '') . ' ' . $this->_tableName;
397 * Sets the width of the table
399 * @param integer fontSize The font size
400 * @global object The current PDF document
401 * @access private
402 * @see PMA_Schema_PDF
404 function _setWidth($fontSize)
406 global $pdf;
408 foreach ($this->fields as $field) {
409 $this->width = max($this->width, $pdf->GetStringWidth($field));
411 $this->width += $pdf->GetStringWidth(' ');
412 $pdf->SetFont($this->_ff, 'B', $fontSize);
414 * it is unknown what value must be added, because
415 * table title is affected by the tabe width value
417 while ($this->width < $pdf->GetStringWidth($this->_getTitle())) {
418 $this->width += 5;
420 $pdf->SetFont($this->_ff, '', $fontSize);
424 * Sets the height of the table
426 * @access private
428 private function _setHeight()
430 $this->height = (count($this->fields) + 1) * $this->heightCell;
434 * Do draw the table
436 * @param integer fontSize The font size
437 * @param boolean setColor Whether to display color
438 * @global object The current PDF document
439 * @access public
440 * @see PMA_Schema_PDF
442 public function tableDraw($fontSize, $withDoc, $setColor = 0)
444 global $pdf, $withDoc;
446 $pdf->PMA_PDF_setXyScale($this->x, $this->y);
447 $pdf->SetFont($this->_ff, 'B', $fontSize);
448 if ($setColor) {
449 $pdf->SetTextColor(200);
450 $pdf->SetFillColor(0, 0, 128);
452 if ($withDoc) {
453 $pdf->SetLink($pdf->PMA_links['RT'][$this->_tableName]['-'], -1);
454 } else {
455 $pdf->PMA_links['doc'][$this->_tableName]['-'] = '';
458 $pdf->PMA_PDF_cellScale($this->width, $this->heightCell, $this->_getTitle(), 1, 1, 'C', $setColor, $pdf->PMA_links['doc'][$this->_tableName]['-']);
459 $pdf->PMA_PDF_setXScale($this->x);
460 $pdf->SetFont($this->_ff, '', $fontSize);
461 $pdf->SetTextColor(0);
462 $pdf->SetFillColor(255);
464 foreach ($this->fields as $field) {
465 if ($setColor) {
466 if (in_array($field, $this->primary)) {
467 $pdf->SetFillColor(215, 121, 123);
469 if ($field == $this->displayfield) {
470 $pdf->SetFillColor(142, 159, 224);
473 if ($withDoc) {
474 $pdf->SetLink($pdf->PMA_links['RT'][$this->_tableName][$field], -1);
475 } else {
476 $pdf->PMA_links['doc'][$this->_tableName][$field] = '';
479 $pdf->PMA_PDF_cellScale($this->width, $this->heightCell, ' ' . $field, 1, 1, 'L', $setColor, $pdf->PMA_links['doc'][$this->_tableName][$field]);
480 $pdf->PMA_PDF_setXScale($this->x);
481 $pdf->SetFillColor(255);
483 /*if ($pdf->PageNo() > 1) {
484 $pdf->PMA_PDF_die(__('The scale factor is too small to fit the schema on one page'));
485 } */
490 * Relation preferences/statistics
492 * This class fetches the table master and foreign fields positions
493 * and helps in generating the Table references and then connects
494 * master table's master field to foreign table's foreign key
495 * in PDF document.
497 * @name Relation_Stats
498 * @see PMA_Schema_PDF::SetDrawColor,PMA_Schema_PDF::PMA_PDF_setLineWidthScale,PMA_Schema_PDF::PMA_PDF_lineScale
500 class Relation_Stats
503 * Defines properties
505 public $xSrc, $ySrc;
506 public $srcDir;
507 public $destDir;
508 public $xDest, $yDest;
509 public $wTick = 5;
512 * The "Relation_Stats" constructor
514 * @param string master_table The master table name
515 * @param string master_field The relation field in the master table
516 * @param string foreign_table The foreign table name
517 * @param string foreigh_field The relation field in the foreign table
518 * @see Relation_Stats::_getXy
520 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
522 $src_pos = $this->_getXy($master_table, $master_field);
523 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
525 * [0] is x-left
526 * [1] is x-right
527 * [2] is y
529 $src_left = $src_pos[0] - $this->wTick;
530 $src_right = $src_pos[1] + $this->wTick;
531 $dest_left = $dest_pos[0] - $this->wTick;
532 $dest_right = $dest_pos[1] + $this->wTick;
534 $d1 = abs($src_left - $dest_left);
535 $d2 = abs($src_right - $dest_left);
536 $d3 = abs($src_left - $dest_right);
537 $d4 = abs($src_right - $dest_right);
538 $d = min($d1, $d2, $d3, $d4);
540 if ($d == $d1) {
541 $this->xSrc = $src_pos[0];
542 $this->srcDir = -1;
543 $this->xDest = $dest_pos[0];
544 $this->destDir = -1;
545 } elseif ($d == $d2) {
546 $this->xSrc = $src_pos[1];
547 $this->srcDir = 1;
548 $this->xDest = $dest_pos[0];
549 $this->destDir = -1;
550 } elseif ($d == $d3) {
551 $this->xSrc = $src_pos[0];
552 $this->srcDir = -1;
553 $this->xDest = $dest_pos[1];
554 $this->destDir = 1;
555 } else {
556 $this->xSrc = $src_pos[1];
557 $this->srcDir = 1;
558 $this->xDest = $dest_pos[1];
559 $this->destDir = 1;
561 $this->ySrc = $src_pos[2];
562 $this->yDest = $dest_pos[2];
566 * Gets arrows coordinates
568 * @param string table The current table name
569 * @param string column The relation column name
570 * @return array Arrows coordinates
571 * @access private
573 private function _getXy($table, $column)
575 $pos = array_search($column, $table->fields);
576 // x_left, x_right, y
577 return array($table->x, $table->x + + $table->width, $table->y + ($pos + 1.5) * $table->heightCell);
581 * draws relation links and arrows
582 * shows foreign key relations
584 * @param boolean changeColor Whether to use one color per relation or not
585 * @param integer i The id of the link to draw
586 * @global object The current PDF document
587 * @access public
588 * @see PMA_Schema_PDF
590 public function relationDraw($changeColor, $i)
592 global $pdf;
594 if ($changeColor) {
595 $d = $i % 6;
596 $j = ($i - $d) / 6;
597 $j = $j % 4;
598 $j++;
599 $case = array(
600 array(1, 0, 0),
601 array(0, 1, 0),
602 array(0, 0, 1),
603 array(1, 1, 0),
604 array(1, 0, 1),
605 array(0, 1, 1)
607 list ($a, $b, $c) = $case[$d];
608 $e = (1 - ($j - 1) / 6);
609 $pdf->SetDrawColor($a * 255 * $e, $b * 255 * $e, $c * 255 * $e);
610 } else {
611 $pdf->SetDrawColor(0);
613 $pdf->PMA_PDF_setLineWidthScale(0.2);
614 $pdf->PMA_PDF_lineScale($this->xSrc, $this->ySrc, $this->xSrc + $this->srcDir * $this->wTick, $this->ySrc);
615 $pdf->PMA_PDF_lineScale($this->xDest + $this->destDir * $this->wTick, $this->yDest, $this->xDest, $this->yDest);
616 $pdf->PMA_PDF_setLineWidthScale(0.1);
617 $pdf->PMA_PDF_lineScale($this->xSrc + $this->srcDir * $this->wTick, $this->ySrc, $this->xDest + $this->destDir * $this->wTick, $this->yDest);
619 * Draws arrows ->
621 $root2 = 2 * sqrt(2);
622 $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);
623 $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);
625 $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);
626 $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);
627 $pdf->SetDrawColor(0);
632 * Pdf Relation Schema Class
634 * Purpose of this class is to generate the PDF Document. PDF is widely
635 * used format for documenting text,fonts,images and 3d vector graphics.
637 * This class inherits Export_Relation_Schema class has common functionality added
638 * to this class
640 * @name Pdf_Relation_Schema
642 class PMA_Pdf_Relation_Schema extends PMA_Export_Relation_Schema
645 * Defines properties
647 private $_tables = array();
648 private $_relations = array();
649 private $_ff = PMA_PDF_FONT;
650 private $_xMax = 0;
651 private $_yMax = 0;
652 private $scale;
653 private $_xMin = 100000;
654 private $_yMin = 100000;
655 private $topMargin = 10;
656 private $bottomMargin = 10;
657 private $leftMargin = 10;
658 private $rightMargin = 10;
659 private $_tablewidth;
662 * The "PMA_Pdf_Relation_Schema" constructor
664 * @global object The current PDF Schema document
665 * @global string The current db name
666 * @global array The relations settings
667 * @access private
668 * @see PMA_Schema_PDF
670 function __construct()
672 global $pdf,$db,$cfgRelation;
674 $this->setPageNumber($_POST['pdf_page_number']);
675 $this->setShowGrid(isset($_POST['show_grid']));
676 $this->setShowColor(isset($_POST['show_color']));
677 $this->setShowKeys(isset($_POST['show_keys']));
678 $this->setTableDimension(isset($_POST['show_table_dimension']));
679 $this->setAllTableSameWidth(isset($_POST['all_table_same_wide']));
680 $this->setWithDataDictionary($_POST['with_doc']);
681 $this->setOrientation($_POST['orientation']);
682 $this->setPaper($_POST['paper']);
683 $this->setExportType($_POST['export_type']);
685 // Initializes a new document
686 $pdf = new PMA_Schema_PDF($this->orientation, 'mm', $this->paper);
687 $pdf->SetTitle(sprintf(__('Schema of the %s database - Page %s'), $GLOBALS['db'], $this->pageNumber));
688 $pdf->setCMargin(0);
689 $pdf->Open();
690 $pdf->SetAutoPageBreak('auto');
691 $alltables = $this->getAllTables($db,$this->pageNumber);
693 if ($this->withDoc) {
694 $pdf->SetAutoPageBreak('auto', 15);
695 $pdf->setCMargin(1);
696 $this->dataDictionaryDoc($alltables);
697 $pdf->SetAutoPageBreak('auto');
698 $pdf->setCMargin(0);
701 $pdf->Addpage();
703 if ($this->withDoc) {
704 $pdf->SetLink($pdf->PMA_links['RT']['-'], -1);
705 $pdf->Bookmark(__('Relational schema'));
706 $pdf->SetAlias('{00}', $pdf->PageNo()) ;
707 $this->topMargin = 28;
708 $this->bottomMargin = 28;
711 /* snip */
712 foreach ($alltables as $table) {
713 if (! isset($this->tables[$table])) {
714 $this->tables[$table] = new Table_Stats($table, $this->_ff, $this->pageNumber, $this->_tablewidth, $this->showKeys, $this->tableDimension);
716 if ($this->sameWide) {
717 $this->tables[$table]->width = $this->_tablewidth;
719 $this->_setMinMax($this->tables[$table]);
722 // Defines the scale factor
723 $this->scale = ceil(
724 max(
725 ($this->_xMax - $this->_xMin) / ($pdf->getPageWidth() - $this->rightMargin - $this->leftMargin),
726 ($this->_yMax - $this->_yMin) / ($pdf->getPageHeight() - $this->topMargin - $this->bottomMargin))
727 * 100) / 100;
729 $pdf->PMA_PDF_setScale($this->scale, $this->_xMin, $this->_yMin, $this->leftMargin, $this->topMargin);
730 // Builds and save the PDF document
731 $pdf->PMA_PDF_setLineWidthScale(0.1);
733 if ($this->showGrid) {
734 $pdf->SetFontSize(10);
735 $this->_strokeGrid();
737 $pdf->PMA_PDF_setFontSizeScale(14);
738 // previous logic was checking master tables and foreign tables
739 // but I think that looping on every table of the pdf page as a master
740 // and finding its foreigns is OK (then we can support innodb)
741 $seen_a_relation = false;
742 foreach ($alltables as $one_table) {
743 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
744 if ($exist_rel) {
745 $seen_a_relation = true;
746 foreach ($exist_rel as $master_field => $rel) {
747 // put the foreign table on the schema only if selected
748 // by the user
749 // (do not use array_search() because we would have to
750 // to do a === false and this is not PHP3 compatible)
751 if (in_array($rel['foreign_table'], $alltables)) {
752 $this->_addRelation($one_table, $master_field, $rel['foreign_table'], $rel['foreign_field'], $this->tableDimension);
754 } // end while
755 } // end if
756 } // end while
758 if ($seen_a_relation) {
759 $this->_drawRelations($this->showColor);
761 $this->_drawTables($this->showColor);
762 $this->_showOutput($this->pageNumber);
763 exit();
767 * Sets X and Y minimum and maximum for a table cell
769 * @param string table The table name of which sets XY co-ordinates
770 * @access private
772 private function _setMinMax($table)
774 $this->_xMax = max($this->_xMax, $table->x + $table->width);
775 $this->_yMax = max($this->_yMax, $table->y + $table->height);
776 $this->_xMin = min($this->_xMin, $table->x);
777 $this->_yMin = min($this->_yMin, $table->y);
781 * Defines relation objects
783 * @param string master_table The master table name
784 * @param string master_field The relation field in the master table
785 * @param string foreign_table The foreign table name
786 * @param string foreign_field The relation field in the foreign table
787 * @param boolean show_info Whether to display table position or not
788 * @access private
789 * @see _setMinMax
791 private function _addRelation($masterTable, $masterField, $foreignTable, $foreignField, $showInfo)
793 if (! isset($this->tables[$masterTable])) {
794 $this->tables[$masterTable] = new Table_Stats($masterTable, $this->_ff, $this->pageNumber, $this->_tablewidth, false, $showInfo);
795 $this->_setMinMax($this->tables[$masterTable]);
797 if (! isset($this->tables[$foreignTable])) {
798 $this->tables[$foreignTable] = new Table_Stats($foreignTable, $this->_ff, $this->pageNumber, $this->_tablewidth, false, $showInfo);
799 $this->_setMinMax($this->tables[$foreignTable]);
801 $this->relations[] = new Relation_Stats($this->tables[$masterTable], $masterField, $this->tables[$foreignTable], $foreignField);
805 * Draws the grid
807 * @global object the current PMA_Schema_PDF instance
808 * @access private
809 * @see PMA_Schema_PDF
811 private function _strokeGrid()
813 global $pdf;
815 $gridSize = 10;
816 $labelHeight = 4;
817 $labelWidth = 5;
818 if ($this->withDoc) {
819 $topSpace = 6;
820 $bottomSpace = 15;
821 } else {
822 $topSpace = 0;
823 $bottomSpace = 0;
826 $pdf->SetMargins(0, 0);
827 $pdf->SetDrawColor(200, 200, 200);
828 // Draws horizontal lines
829 for ($l = 0; $l <= intval(($pdf->getPageHeight() - $topSpace - $bottomSpace) / $gridSize); $l++) {
830 $pdf->line(0, $l * $gridSize + $topSpace, $pdf->getPageWidth(), $l * $gridSize + $topSpace);
831 // Avoid duplicates
832 if ($l > 0 && $l <= intval(($pdf->getPageHeight() - $topSpace - $bottomSpace - $labelHeight) / $gridSize)) {
833 $pdf->SetXY(0, $l * $gridSize + $topSpace);
834 $label = (string) sprintf('%.0f', ($l * $gridSize + $topSpace - $this->topMargin) * $this->scale + $this->_yMin);
835 $pdf->Cell($labelWidth, $labelHeight, ' ' . $label);
836 } // end if
837 } // end for
838 // Draws vertical lines
839 for ($j = 0; $j <= intval($pdf->getPageWidth() / $gridSize); $j++) {
840 $pdf->line($j * $gridSize, $topSpace, $j * $gridSize, $pdf->getPageHeight() - $bottomSpace);
841 $pdf->SetXY($j * $gridSize, $topSpace);
842 $label = (string) sprintf('%.0f', ($j * $gridSize - $this->leftMargin) * $this->scale + $this->_xMin);
843 $pdf->Cell($labelWidth, $labelHeight, $label);
848 * Draws relation arrows
850 * @param boolean changeColor Whether to use one color per relation or not
851 * @access private
852 * @see Relation_Stats::relationdraw()
854 private function _drawRelations($changeColor)
856 $i = 0;
857 foreach ($this->relations as $relation) {
858 $relation->relationDraw($changeColor, $i);
859 $i++;
864 * Draws tables
866 * @param boolean changeColor Whether to display table position or not
867 * @access private
868 * @see Table_Stats::tableDraw()
870 private function _drawTables($changeColor = 0)
872 foreach ($this->tables as $table) {
873 $table->tableDraw($this->_ff, $this->withDoc, $changeColor);
878 * Ouputs the PDF document to a file
879 * or sends the output to browser
881 * @global object The current PDF document
882 * @global string The current database name
883 * @global integer The current page number (from the
884 * $cfg['Servers'][$i]['table_coords'] table)
885 * @access private
886 * @see PMA_Schema_PDF
888 private function _showOutput($pageNumber)
890 global $pdf, $db, $cfgRelation;
892 // Get the name of this pdfpage to use as filename
893 $_name_sql = 'SELECT page_descr FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
894 . ' WHERE page_nr = ' . $pageNumber;
895 $_name_rs = PMA_query_as_controluser($_name_sql);
896 if ($_name_rs) {
897 $_name_row = PMA_DBI_fetch_row($_name_rs);
898 $filename = $_name_row[0] . '.pdf';
900 if (empty($filename)) {
901 $filename = $pageNumber . '.pdf';
903 $pdf->Download($filename);
906 public function dataDictionaryDoc($alltables)
908 global $db, $pdf, $orientation, $paper;
909 // TOC
910 $pdf->addpage($GLOBALS['orientation']);
911 $pdf->Cell(0, 9, __('Table of contents'), 1, 0, 'C');
912 $pdf->Ln(15);
913 $i = 1;
914 foreach ($alltables as $table) {
915 $pdf->PMA_links['doc'][$table]['-'] = $pdf->AddLink();
916 $pdf->SetX(10);
917 // $pdf->Ln(1);
918 $pdf->Cell(0, 6, __('Page number:') . ' {' . sprintf("%02d", $i) . '}', 0, 0, 'R', 0, $pdf->PMA_links['doc'][$table]['-']);
919 $pdf->SetX(10);
920 $pdf->Cell(0, 6, $i . ' ' . $table, 0, 1, 'L', 0, $pdf->PMA_links['doc'][$table]['-']);
921 // $pdf->Ln(1);
922 $result = PMA_DBI_query(PMA_DBI_get_columns_sql($db, $table), null, PMA_DBI_QUERY_STORE);
923 while ($row = PMA_DBI_fetch_assoc($result)) {
924 $pdf->SetX(20);
925 $field_name = $row['Field'];
926 $pdf->PMA_links['doc'][$table][$field_name] = $pdf->AddLink();
927 // $pdf->Cell(0, 6, $field_name,0,1,'L',0, $pdf->PMA_links['doc'][$table][$field_name]);
929 $lasttable = $table;
930 $i++;
932 $pdf->PMA_links['RT']['-'] = $pdf->AddLink();
933 $pdf->SetX(10);
934 $pdf->Cell(0, 6, __('Page number:') . ' {00}', 0, 0, 'R', 0, $pdf->PMA_links['RT']['-']);
935 $pdf->SetX(10);
936 $pdf->Cell(0, 6, $i . ' ' . __('Relational schema'), 0, 1, 'L', 0, $pdf->PMA_links['RT']['-']);
937 $z = 0;
938 foreach ($alltables as $table) {
939 $z++;
940 $pdf->SetAutoPageBreak(true, 15);
941 $pdf->addpage($GLOBALS['orientation']);
942 $pdf->Bookmark($table);
943 $pdf->SetAlias('{' . sprintf("%02d", $z) . '}', $pdf->PageNo()) ;
944 $pdf->PMA_links['RT'][$table]['-'] = $pdf->AddLink();
945 $pdf->SetLink($pdf->PMA_links['doc'][$table]['-'], -1);
946 $pdf->SetFont($this->_ff, 'B', 18);
947 $pdf->Cell(0, 8, $z . ' ' . $table, 1, 1, 'C', 0, $pdf->PMA_links['RT'][$table]['-']);
948 $pdf->SetFont($this->_ff, '', 8);
949 $pdf->ln();
951 $cfgRelation = PMA_getRelationsParam();
952 $comments = PMA_getComments($db, $table);
953 if ($cfgRelation['mimework']) {
954 $mime_map = PMA_getMIME($db, $table, true);
958 * Gets table informations
960 $showtable = PMA_Table::sGetStatusInfo($db, $table);
961 $num_rows = (isset($showtable['Rows']) ? $showtable['Rows'] : 0);
962 $show_comment = (isset($showtable['Comment']) ? $showtable['Comment'] : '');
963 $create_time = (isset($showtable['Create_time']) ? PMA_localisedDate(strtotime($showtable['Create_time'])) : '');
964 $update_time = (isset($showtable['Update_time']) ? PMA_localisedDate(strtotime($showtable['Update_time'])) : '');
965 $check_time = (isset($showtable['Check_time']) ? PMA_localisedDate(strtotime($showtable['Check_time'])) : '');
968 * Gets table keys and retains them
970 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
971 $primary = '';
972 $indexes = array();
973 $lastIndex = '';
974 $indexes_info = array();
975 $indexes_data = array();
976 $pk_array = array(); // will be use to emphasis prim. keys in the table
977 // view
978 while ($row = PMA_DBI_fetch_assoc($result)) {
979 // Backups the list of primary keys
980 if ($row['Key_name'] == 'PRIMARY') {
981 $primary .= $row['Column_name'] . ', ';
982 $pk_array[$row['Column_name']] = 1;
984 // Retains keys informations
985 if ($row['Key_name'] != $lastIndex) {
986 $indexes[] = $row['Key_name'];
987 $lastIndex = $row['Key_name'];
989 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
990 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
991 if (isset($row['Cardinality'])) {
992 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
994 // I don't know what does following column mean....
995 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
996 $indexes_info[$row['Key_name']]['Comment'] = $row['Comment'];
998 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
999 if (isset($row['Sub_part'])) {
1000 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
1002 } // end while
1003 if ($result) {
1004 PMA_DBI_free_result($result);
1008 * Gets fields properties
1010 $columns = PMA_DBI_get_columns($db, $table);
1011 // Check if we can use Relations
1012 if (!empty($cfgRelation['relation'])) {
1013 // Find which tables are related with the current one and write it in
1014 // an array
1015 $res_rel = PMA_getForeigners($db, $table);
1017 if (count($res_rel) > 0) {
1018 $have_rel = true;
1019 } else {
1020 $have_rel = false;
1022 } else {
1023 $have_rel = false;
1024 } // end if
1027 * Displays the comments of the table if MySQL >= 3.23
1030 $break = false;
1031 if (!empty($show_comment)) {
1032 $pdf->Cell(0, 3, __('Table comments') . ' : ' . $show_comment, 0, 1);
1033 $break = true;
1036 if (!empty($create_time)) {
1037 $pdf->Cell(0, 3, __('Creation') . ': ' . $create_time, 0, 1);
1038 $break = true;
1041 if (!empty($update_time)) {
1042 $pdf->Cell(0, 3, __('Last update') . ': ' . $update_time, 0, 1);
1043 $break = true;
1046 if (!empty($check_time)) {
1047 $pdf->Cell(0, 3, __('Last check') . ': ' . $check_time, 0, 1);
1048 $break = true;
1051 if ($break == true) {
1052 $pdf->Cell(0, 3, '', 0, 1);
1053 $pdf->Ln();
1056 $pdf->SetFont($this->_ff, 'B');
1057 if (isset($orientation) && $orientation == 'L') {
1058 $pdf->Cell(25, 8, __('Column'), 1, 0, 'C');
1059 $pdf->Cell(20, 8, __('Type'), 1, 0, 'C');
1060 $pdf->Cell(20, 8, __('Attributes'), 1, 0, 'C');
1061 $pdf->Cell(10, 8, __('Null'), 1, 0, 'C');
1062 $pdf->Cell(20, 8, __('Default'), 1, 0, 'C');
1063 $pdf->Cell(25, 8, __('Extra'), 1, 0, 'C');
1064 $pdf->Cell(45, 8, __('Links to'), 1, 0, 'C');
1066 if ($paper == 'A4') {
1067 $comments_width = 67;
1068 } else {
1069 // this is really intended for 'letter'
1071 * @todo find optimal width for all formats
1073 $comments_width = 50;
1075 $pdf->Cell($comments_width, 8, __('Comments'), 1, 0, 'C');
1076 $pdf->Cell(45, 8, 'MIME', 1, 1, 'C');
1077 $pdf->SetWidths(array(25, 20, 20, 10, 20, 25, 45, $comments_width, 45));
1078 } else {
1079 $pdf->Cell(20, 8, __('Column'), 1, 0, 'C');
1080 $pdf->Cell(20, 8, __('Type'), 1, 0, 'C');
1081 $pdf->Cell(20, 8, __('Attributes'), 1, 0, 'C');
1082 $pdf->Cell(10, 8, __('Null'), 1, 0, 'C');
1083 $pdf->Cell(15, 8, __('Default'), 1, 0, 'C');
1084 $pdf->Cell(15, 8, __('Extra'), 1, 0, 'C');
1085 $pdf->Cell(30, 8, __('Links to'), 1, 0, 'C');
1086 $pdf->Cell(30, 8, __('Comments'), 1, 0, 'C');
1087 $pdf->Cell(30, 8, 'MIME', 1, 1, 'C');
1088 $pdf->SetWidths(array(20, 20, 20, 10, 15, 15, 30, 30, 30));
1090 $pdf->SetFont($this->_ff, '');
1092 foreach ($columns as $row) {
1093 $extracted_fieldspec = PMA_extractFieldSpec($row['Type']);
1094 $type = $extracted_fieldspec['print_type'];
1095 $attribute = $extracted_fieldspec['attribute'];
1096 if (! isset($row['Default'])) {
1097 if ($row['Null'] != '' && $row['Null'] != 'NO') {
1098 $row['Default'] = 'NULL';
1101 $field_name = $row['Field'];
1102 // $pdf->Ln();
1103 $pdf->PMA_links['RT'][$table][$field_name] = $pdf->AddLink();
1104 $pdf->Bookmark($field_name, 1, -1);
1105 $pdf->SetLink($pdf->PMA_links['doc'][$table][$field_name], -1);
1106 $pdf_row = array($field_name,
1107 $type,
1108 $attribute,
1109 ($row['Null'] == '' || $row['Null'] == 'NO') ? __('No') : __('Yes'),
1110 ((isset($row['Default'])) ? $row['Default'] : ''),
1111 $row['Extra'],
1112 ((isset($res_rel[$field_name])) ? $res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field'] : ''),
1113 ((isset($comments[$field_name])) ? $comments[$field_name] : ''),
1114 ((isset($mime_map) && isset($mime_map[$field_name])) ? str_replace('_', '/', $mime_map[$field_name]['mimetype']) : '')
1116 $links[0] = $pdf->PMA_links['RT'][$table][$field_name];
1117 if (isset($res_rel[$field_name]['foreign_table']) AND
1118 isset($res_rel[$field_name]['foreign_field']) AND
1119 isset($pdf->PMA_links['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']])
1122 $links[6] = $pdf->PMA_links['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']];
1123 } else {
1124 unset($links[6]);
1126 $pdf->Row($pdf_row, $links);
1127 } // end foreach
1128 $pdf->SetFont($this->_ff, '', 14);
1129 } //end each