Merge remote-tracking branch 'origin/master' into drizzle
[phpmyadmin/crack.git] / libraries / schema / Pdf_Relation_Schema.class.php
blob610837572bb7c0032913c2fe8464c5d5997a1d94
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @package phpMyAdmin
6 */
8 require_once 'Export_Relation_Schema.class.php';
9 require_once './libraries/PDF.class.php';
11 /**
12 * Extends the "TCPDF" class and helps
13 * in developing the structure of PDF Schema Export
15 * @access public
16 * @see TCPDF
18 class PMA_Schema_PDF extends PMA_PDF
20 /**
21 * Defines properties
23 var $_xMin;
24 var $_yMin;
25 var $leftMargin = 10;
26 var $topMargin = 10;
27 var $scale;
28 var $PMA_links;
29 var $Outlines = array();
30 var $def_outlines;
31 var $widths;
32 private $_ff = PMA_PDF_FONT;
34 /**
35 * Sets the value for margins
37 * @param float $c_margin margin
39 * @return nothing
41 public function setCMargin($c_margin)
43 $this->cMargin = $c_margin;
46 /**
47 * Sets the scaling factor, defines minimum coordinates and margins
49 * @param float $scale The scaling factor
50 * @param float $xMin The minimum X coordinate
51 * @param float $yMin The minimum Y coordinate
52 * @param float $leftMargin The left margin
53 * @param float $topMargin The top margin
55 * @access public
57 * @return nothing
59 function PMA_PDF_setScale($scale = 1, $xMin = 0, $yMin = 0, $leftMargin = -1, $topMargin = -1)
61 $this->scale = $scale;
62 $this->_xMin = $xMin;
63 $this->_yMin = $yMin;
64 if ($this->leftMargin != -1) {
65 $this->leftMargin = $leftMargin;
67 if ($this->topMargin != -1) {
68 $this->topMargin = $topMargin;
72 /**
73 * Outputs a scaled cell
75 * @param float $w The cell width
76 * @param float $h The cell height
77 * @param string $txt The text to output
78 * @param mixed $border Whether to add borders or not
79 * @param integer $ln Where to put the cursor once the output is done
80 * @param string $align Align mode
81 * @param integer $fill Whether to fill the cell with a color or not
82 * @param string $link Link
84 * @access public
86 * @return nothing
88 * @see TCPDF::Cell()
90 function PMA_PDF_cellScale($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $fill = 0, $link = '')
92 $h = $h / $this->scale;
93 $w = $w / $this->scale;
94 $this->Cell($w, $h, $txt, $border, $ln, $align, $fill, $link);
97 /**
98 * Draws a scaled line
100 * @param float $x1 The horizontal position of the starting point
101 * @param float $y1 The vertical position of the starting point
102 * @param float $x2 The horizontal position of the ending point
103 * @param float $y2 The vertical position of the ending point
105 * @access public
107 * @return nothing
109 * @see TCPDF::Line()
111 function PMA_PDF_lineScale($x1, $y1, $x2, $y2)
113 $x1 = ($x1 - $this->_xMin) / $this->scale + $this->leftMargin;
114 $y1 = ($y1 - $this->_yMin) / $this->scale + $this->topMargin;
115 $x2 = ($x2 - $this->_xMin) / $this->scale + $this->leftMargin;
116 $y2 = ($y2 - $this->_yMin) / $this->scale + $this->topMargin;
117 $this->Line($x1, $y1, $x2, $y2);
121 * Sets x and y scaled positions
123 * @param float $x The x position
124 * @param float $y The y position
126 * @access public
128 * @return nothing
130 * @see TCPDF::SetXY()
132 function PMA_PDF_setXyScale($x, $y)
134 $x = ($x - $this->_xMin) / $this->scale + $this->leftMargin;
135 $y = ($y - $this->_yMin) / $this->scale + $this->topMargin;
136 $this->SetXY($x, $y);
140 * Sets the X scaled positions
142 * @param float $x The x position
144 * @access public
146 * @return nothing
148 * @see TCPDF::SetX()
150 function PMA_PDF_setXScale($x)
152 $x = ($x - $this->_xMin) / $this->scale + $this->leftMargin;
153 $this->SetX($x);
157 * Sets the scaled font size
159 * @param float $size The font size (in points)
161 * @access public
163 * @return nothing
165 * @see TCPDF::SetFontSize()
167 function PMA_PDF_setFontSizeScale($size)
169 // Set font size in points
170 $size = $size / $this->scale;
171 $this->SetFontSize($size);
175 * Sets the scaled line width
177 * @param float $width The line width
179 * @access public
181 * @return nothing
183 * @see TCPDF::SetLineWidth()
185 function PMA_PDF_setLineWidthScale($width)
187 $width = $width / $this->scale;
188 $this->SetLineWidth($width);
192 * This method is used to render the page header.
194 * @return nothing
196 * @see TCPDF::Header()
198 function Header()
200 // We only show this if we find something in the new pdf_pages table
202 // This function must be named "Header" to work with the TCPDF library
203 global $cfgRelation, $db, $pdf_page_number, $with_doc;
204 if ($with_doc) {
205 $test_query = 'SELECT * FROM '
206 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.'
207 . PMA_backquote($cfgRelation['pdf_pages'])
208 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
209 . ' AND page_nr = \'' . $pdf_page_number . '\'';
210 $test_rs = PMA_query_as_controluser($test_query);
211 $pages = @PMA_DBI_fetch_assoc($test_rs);
212 $this->SetFont($this->_ff, 'B', 14);
213 $this->Cell(0, 6, ucfirst($pages['page_descr']), 'B', 1, 'C');
214 $this->SetFont($this->_ff, '');
215 $this->Ln();
220 * This function must be named "Footer" to work with the TCPDF library
222 * @return nothing
224 * @see PMA_PDF::Footer()
226 function Footer()
228 global $with_doc;
229 if ($with_doc) {
230 parent::Footer();
235 * Sets widths
237 * @param array $w array of widths
239 * @return nothing
241 function SetWidths($w)
243 // column widths
244 $this->widths = $w;
247 function Row($data, $links)
249 // line height
250 $nb = 0;
251 $data_cnt = count($data);
252 for ($i = 0;$i < $data_cnt;$i++) {
253 $nb = max($nb, $this->NbLines($this->widths[$i], $data[$i]));
255 $il = $this->FontSize;
256 $h = ($il + 1) * $nb;
257 // page break if necessary
258 $this->CheckPageBreak($h);
259 // draw the cells
260 $data_cnt = count($data);
261 for ($i = 0;$i < $data_cnt;$i++) {
262 $w = $this->widths[$i];
263 // save current position
264 $x = $this->GetX();
265 $y = $this->GetY();
266 // draw the border
267 $this->Rect($x, $y, $w, $h);
268 if (isset($links[$i])) {
269 $this->Link($x, $y, $w, $h, $links[$i]);
271 // print text
272 $this->MultiCell($w, $il + 1, $data[$i], 0, 'L');
273 // go to right side
274 $this->SetXY($x + $w, $y);
276 // go to line
277 $this->Ln($h);
281 * Compute number of lines used by a multicell of width w
283 * @param int $w width
284 * @param string $txt text
286 * @return int
288 function NbLines($w, $txt)
290 $cw = &$this->CurrentFont['cw'];
291 if ($w == 0) {
292 $w = $this->w - $this->rMargin - $this->x;
294 $wmax = ($w-2 * $this->cMargin) * 1000 / $this->FontSize;
295 $s = str_replace("\r", '', $txt);
296 $nb = strlen($s);
297 if ($nb > 0 and $s[$nb-1] == "\n") {
298 $nb--;
300 $sep = -1;
301 $i = 0;
302 $j = 0;
303 $l = 0;
304 $nl = 1;
305 while ($i < $nb) {
306 $c = $s[$i];
307 if ($c == "\n") {
308 $i++;
309 $sep = -1;
310 $j = $i;
311 $l = 0;
312 $nl++;
313 continue;
315 if ($c == ' ') {
316 $sep = $i;
318 $l += isset($cw[ord($c)])?$cw[ord($c)]:0 ;
319 if ($l > $wmax) {
320 if ($sep == -1) {
321 if ($i == $j) {
322 $i++;
324 } else {
325 $i = $sep + 1;
327 $sep = -1;
328 $j = $i;
329 $l = 0;
330 $nl++;
331 } else {
332 $i++;
335 return $nl;
340 * Table preferences/statistics
342 * This class preserves the table co-ordinates,fields
343 * and helps in drawing/generating the Tables in PDF document.
345 * @name Table_Stats
346 * @see PMA_Schema_PDF
348 class Table_Stats
351 * Defines properties
353 private $_tableName;
354 private $_showInfo = false;
356 public $nb_fiels;
357 public $width = 0;
358 public $height;
359 public $fields = array();
360 public $heightCell = 6;
361 public $x, $y;
362 public $primary = array();
363 private $_ff = PMA_PDF_FONT;
366 * The "Table_Stats" constructor
368 * @param string $tableName The table name
369 * @param integer $fontSize The font size
370 * @param integer $pageNumber The current page number (from the
371 * $cfg['Servers'][$i]['table_coords'] table)
372 * @param integer &$sameWideWidth The max. with among tables
373 * @param boolean $showKeys Whether to display keys or not
374 * @param boolean $showInfo Whether to display table position or not
376 * @global object The current PDF document
377 * @global array The relations settings
378 * @global string The current db name
380 * @return nothing
382 * @see PMA_Schema_PDF, Table_Stats::Table_Stats_setWidth,
383 * Table_Stats::Table_Stats_setHeight
385 function __construct($tableName, $fontSize, $pageNumber, &$sameWideWidth, $showKeys = false, $showInfo = false)
387 global $pdf, $cfgRelation, $db;
389 $this->_tableName = $tableName;
390 $sql = 'DESCRIBE ' . PMA_backquote($tableName);
391 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE);
392 if (! $result || ! PMA_DBI_num_rows($result)) {
393 $pdf->Error(sprintf(__('The %s table doesn\'t exist!'), $tableName));
395 // load fields
396 //check to see if it will load all fields or only the foreign keys
397 if ($showKeys) {
398 $indexes = PMA_Index::getFromTable($this->_tableName, $db);
399 $all_columns = array();
400 foreach ($indexes as $index) {
401 $all_columns = array_merge(
402 $all_columns,
403 array_flip(array_keys($index->getColumns()))
406 $this->fields = array_keys($all_columns);
407 } else {
408 while ($row = PMA_DBI_fetch_row($result)) {
409 $this->fields[] = $row[0];
413 $this->_showInfo = $showInfo;
414 $this->_setHeight();
416 * setWidth must me after setHeight, because title
417 * can include table height which changes table width
419 $this->_setWidth($fontSize);
420 if ($sameWideWidth < $this->width) {
421 $sameWideWidth = $this->width;
423 $sql = 'SELECT x, y FROM '
424 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.'
425 . PMA_backquote($cfgRelation['table_coords'])
426 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
427 . ' AND table_name = \'' . PMA_sqlAddSlashes($tableName) . '\''
428 . ' AND pdf_page_number = ' . $pageNumber;
429 $result = PMA_query_as_controluser($sql, false, PMA_DBI_QUERY_STORE);
430 if (! $result || ! PMA_DBI_num_rows($result)) {
431 $pdf->Error(
432 sprintf(
433 __('Please configure the coordinates for table %s'),
434 $tableName
438 list($this->x, $this->y) = PMA_DBI_fetch_row($result);
439 $this->x = (double) $this->x;
440 $this->y = (double) $this->y;
442 * displayfield
444 $this->displayfield = PMA_getDisplayField($db, $tableName);
446 * index
448 $result = PMA_DBI_query(
449 'SHOW INDEX FROM ' . PMA_backquote($tableName) . ';',
450 null, PMA_DBI_QUERY_STORE
452 if (PMA_DBI_num_rows($result) > 0) {
453 while ($row = PMA_DBI_fetch_assoc($result)) {
454 if ($row['Key_name'] == 'PRIMARY') {
455 $this->primary[] = $row['Column_name'];
462 * Returns title of the current table,
463 * title can have the dimensions of the table
465 * @return string
467 private function _getTitle()
469 return ($this->_showInfo ? sprintf('%.0f', $this->width) . 'x' . sprintf('%.0f', $this->height) : '') . ' ' . $this->_tableName;
473 * Sets the width of the table
475 * @param integer $fontSize The font size
477 * @global object The current PDF document
479 * @access private
481 * @return nothing
483 * @see PMA_Schema_PDF
485 private function _setWidth($fontSize)
487 global $pdf;
489 foreach ($this->fields as $field) {
490 $this->width = max($this->width, $pdf->GetStringWidth($field));
492 $this->width += $pdf->GetStringWidth(' ');
493 $pdf->SetFont($this->_ff, 'B', $fontSize);
495 * it is unknown what value must be added, because
496 * table title is affected by the tabe width value
498 while ($this->width < $pdf->GetStringWidth($this->_getTitle())) {
499 $this->width += 5;
501 $pdf->SetFont($this->_ff, '', $fontSize);
505 * Sets the height of the table
507 * @return nothing
509 * @access private
511 private function _setHeight()
513 $this->height = (count($this->fields) + 1) * $this->heightCell;
517 * Do draw the table
519 * @param integer $fontSize The font size
520 * @param boolean $withDoc
521 * @param boolean $setColor Whether to display color
523 * @global object The current PDF document
525 * @access public
527 * @return nothing
529 * @see PMA_Schema_PDF
531 public function tableDraw($fontSize, $withDoc, $setColor = 0)
533 global $pdf, $withDoc;
535 $pdf->PMA_PDF_setXyScale($this->x, $this->y);
536 $pdf->SetFont($this->_ff, 'B', $fontSize);
537 if ($setColor) {
538 $pdf->SetTextColor(200);
539 $pdf->SetFillColor(0, 0, 128);
541 if ($withDoc) {
542 $pdf->SetLink($pdf->PMA_links['RT'][$this->_tableName]['-'], -1);
543 } else {
544 $pdf->PMA_links['doc'][$this->_tableName]['-'] = '';
547 $pdf->PMA_PDF_cellScale(
548 $this->width,
549 $this->heightCell,
550 $this->_getTitle(),
553 'C',
554 $setColor,
555 $pdf->PMA_links['doc'][$this->_tableName]['-']
557 $pdf->PMA_PDF_setXScale($this->x);
558 $pdf->SetFont($this->_ff, '', $fontSize);
559 $pdf->SetTextColor(0);
560 $pdf->SetFillColor(255);
562 foreach ($this->fields as $field) {
563 if ($setColor) {
564 if (in_array($field, $this->primary)) {
565 $pdf->SetFillColor(215, 121, 123);
567 if ($field == $this->displayfield) {
568 $pdf->SetFillColor(142, 159, 224);
571 if ($withDoc) {
572 $pdf->SetLink($pdf->PMA_links['RT'][$this->_tableName][$field], -1);
573 } else {
574 $pdf->PMA_links['doc'][$this->_tableName][$field] = '';
577 $pdf->PMA_PDF_cellScale(
578 $this->width,
579 $this->heightCell,
580 ' ' . $field,
583 'L',
584 $setColor,
585 $pdf->PMA_links['doc'][$this->_tableName][$field]
587 $pdf->PMA_PDF_setXScale($this->x);
588 $pdf->SetFillColor(255);
590 /*if ($pdf->PageNo() > 1) {
591 $pdf->PMA_PDF_die(
592 __('The scale factor is too small to fit the schema on one page')
594 } */
599 * Relation preferences/statistics
601 * This class fetches the table master and foreign fields positions
602 * and helps in generating the Table references and then connects
603 * master table's master field to foreign table's foreign key
604 * in PDF document.
606 * @name Relation_Stats
607 * @see PMA_Schema_PDF::SetDrawColor, PMA_Schema_PDF::PMA_PDF_setLineWidthScale,
608 * PMA_Schema_PDF::PMA_PDF_lineScale
610 class Relation_Stats
613 * Defines properties
615 public $xSrc, $ySrc;
616 public $srcDir;
617 public $destDir;
618 public $xDest, $yDest;
619 public $wTick = 5;
622 * The "Relation_Stats" constructor
624 * @param string $master_table The master table name
625 * @param string $master_field The relation field in the master table
626 * @param string $foreign_table The foreign table name
627 * @param string $foreign_field The relation field in the foreign table
629 * @return nothing
631 * @see Relation_Stats::_getXy
633 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
635 $src_pos = $this->_getXy($master_table, $master_field);
636 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
638 * [0] is x-left
639 * [1] is x-right
640 * [2] is y
642 $src_left = $src_pos[0] - $this->wTick;
643 $src_right = $src_pos[1] + $this->wTick;
644 $dest_left = $dest_pos[0] - $this->wTick;
645 $dest_right = $dest_pos[1] + $this->wTick;
647 $d1 = abs($src_left - $dest_left);
648 $d2 = abs($src_right - $dest_left);
649 $d3 = abs($src_left - $dest_right);
650 $d4 = abs($src_right - $dest_right);
651 $d = min($d1, $d2, $d3, $d4);
653 if ($d == $d1) {
654 $this->xSrc = $src_pos[0];
655 $this->srcDir = -1;
656 $this->xDest = $dest_pos[0];
657 $this->destDir = -1;
658 } elseif ($d == $d2) {
659 $this->xSrc = $src_pos[1];
660 $this->srcDir = 1;
661 $this->xDest = $dest_pos[0];
662 $this->destDir = -1;
663 } elseif ($d == $d3) {
664 $this->xSrc = $src_pos[0];
665 $this->srcDir = -1;
666 $this->xDest = $dest_pos[1];
667 $this->destDir = 1;
668 } else {
669 $this->xSrc = $src_pos[1];
670 $this->srcDir = 1;
671 $this->xDest = $dest_pos[1];
672 $this->destDir = 1;
674 $this->ySrc = $src_pos[2];
675 $this->yDest = $dest_pos[2];
679 * Gets arrows coordinates
681 * @param string $table The current table name
682 * @param string $column The relation column name
684 * @return array Arrows coordinates
686 * @access private
688 private function _getXy($table, $column)
690 $pos = array_search($column, $table->fields);
691 // x_left, x_right, y
692 return array($table->x, $table->x + + $table->width, $table->y + ($pos + 1.5) * $table->heightCell);
696 * draws relation links and arrows shows foreign key relations
698 * @param boolean $changeColor Whether to use one color per relation or not
699 * @param integer $i The id of the link to draw
701 * @global object The current PDF document
703 * @access public
705 * @return nothing
707 * @see PMA_Schema_PDF
709 public function relationDraw($changeColor, $i)
711 global $pdf;
713 if ($changeColor) {
714 $d = $i % 6;
715 $j = ($i - $d) / 6;
716 $j = $j % 4;
717 $j++;
718 $case = array(
719 array(1, 0, 0),
720 array(0, 1, 0),
721 array(0, 0, 1),
722 array(1, 1, 0),
723 array(1, 0, 1),
724 array(0, 1, 1)
726 list ($a, $b, $c) = $case[$d];
727 $e = (1 - ($j - 1) / 6);
728 $pdf->SetDrawColor($a * 255 * $e, $b * 255 * $e, $c * 255 * $e);
729 } else {
730 $pdf->SetDrawColor(0);
732 $pdf->PMA_PDF_setLineWidthScale(0.2);
733 $pdf->PMA_PDF_lineScale(
734 $this->xSrc,
735 $this->ySrc,
736 $this->xSrc + $this->srcDir * $this->wTick,
737 $this->ySrc
739 $pdf->PMA_PDF_lineScale(
740 $this->xDest + $this->destDir * $this->wTick,
741 $this->yDest,
742 $this->xDest,
743 $this->yDest
745 $pdf->PMA_PDF_setLineWidthScale(0.1);
746 $pdf->PMA_PDF_lineScale(
747 $this->xSrc + $this->srcDir * $this->wTick,
748 $this->ySrc,
749 $this->xDest + $this->destDir * $this->wTick,
750 $this->yDest
753 * Draws arrows ->
755 $root2 = 2 * sqrt(2);
756 $pdf->PMA_PDF_lineScale(
757 $this->xSrc + $this->srcDir * $this->wTick * 0.75,
758 $this->ySrc,
759 $this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick,
760 $this->ySrc + $this->wTick / $root2
762 $pdf->PMA_PDF_lineScale(
763 $this->xSrc + $this->srcDir * $this->wTick * 0.75,
764 $this->ySrc,
765 $this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick,
766 $this->ySrc - $this->wTick / $root2
769 $pdf->PMA_PDF_lineScale(
770 $this->xDest + $this->destDir * $this->wTick / 2,
771 $this->yDest,
772 $this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick,
773 $this->yDest + $this->wTick / $root2
775 $pdf->PMA_PDF_lineScale(
776 $this->xDest + $this->destDir * $this->wTick / 2,
777 $this->yDest,
778 $this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick,
779 $this->yDest - $this->wTick / $root2
781 $pdf->SetDrawColor(0);
786 * Pdf Relation Schema Class
788 * Purpose of this class is to generate the PDF Document. PDF is widely
789 * used format for documenting text,fonts,images and 3d vector graphics.
791 * This class inherits Export_Relation_Schema class has common functionality added
792 * to this class
794 * @name Pdf_Relation_Schema
796 class PMA_Pdf_Relation_Schema extends PMA_Export_Relation_Schema
799 * Defines properties
801 private $_ff = PMA_PDF_FONT;
802 private $_xMax = 0;
803 private $_yMax = 0;
804 private $scale;
805 private $_xMin = 100000;
806 private $_yMin = 100000;
807 private $topMargin = 10;
808 private $bottomMargin = 10;
809 private $leftMargin = 10;
810 private $rightMargin = 10;
811 private $_tablewidth;
814 * The "PMA_Pdf_Relation_Schema" constructor
816 * @global object The current PDF Schema document
817 * @global string The current db name
818 * @global array The relations settings
819 * @access private
820 * @see PMA_Schema_PDF
822 function __construct()
824 global $pdf, $db;
826 $this->setPageNumber($_POST['pdf_page_number']);
827 $this->setShowGrid(isset($_POST['show_grid']));
828 $this->setShowColor(isset($_POST['show_color']));
829 $this->setShowKeys(isset($_POST['show_keys']));
830 $this->setTableDimension(isset($_POST['show_table_dimension']));
831 $this->setAllTableSameWidth(isset($_POST['all_table_same_wide']));
832 $this->setWithDataDictionary($_POST['with_doc']);
833 $this->setOrientation($_POST['orientation']);
834 $this->setPaper($_POST['paper']);
835 $this->setExportType($_POST['export_type']);
837 // Initializes a new document
838 $pdf = new PMA_Schema_PDF($this->orientation, 'mm', $this->paper);
839 $pdf->SetTitle(
840 sprintf(
841 __('Schema of the %s database - Page %s'),
842 $GLOBALS['db'],
843 $this->pageNumber
846 $pdf->setCMargin(0);
847 $pdf->Open();
848 $pdf->SetAutoPageBreak('auto');
849 $alltables = $this->getAllTables($db, $this->pageNumber);
851 if ($this->withDoc) {
852 $pdf->SetAutoPageBreak('auto', 15);
853 $pdf->setCMargin(1);
854 $this->dataDictionaryDoc($alltables);
855 $pdf->SetAutoPageBreak('auto');
856 $pdf->setCMargin(0);
859 $pdf->Addpage();
861 if ($this->withDoc) {
862 $pdf->SetLink($pdf->PMA_links['RT']['-'], -1);
863 $pdf->Bookmark(__('Relational schema'));
864 $pdf->SetAlias('{00}', $pdf->PageNo());
865 $this->topMargin = 28;
866 $this->bottomMargin = 28;
869 /* snip */
870 foreach ($alltables as $table) {
871 if (! isset($this->tables[$table])) {
872 $this->tables[$table] = new Table_Stats(
873 $table, $this->_ff,
874 $this->pageNumber,
875 $this->_tablewidth,
876 $this->showKeys,
877 $this->tableDimension
880 if ($this->sameWide) {
881 $this->tables[$table]->width = $this->_tablewidth;
883 $this->_setMinMax($this->tables[$table]);
886 // Defines the scale factor
887 $this->scale = ceil(
888 max(
889 ($this->_xMax - $this->_xMin) / ($pdf->getPageWidth() - $this->rightMargin - $this->leftMargin),
890 ($this->_yMax - $this->_yMin) / ($pdf->getPageHeight() - $this->topMargin - $this->bottomMargin)
891 ) * 100
892 ) / 100;
894 $pdf->PMA_PDF_setScale(
895 $this->scale,
896 $this->_xMin,
897 $this->_yMin,
898 $this->leftMargin,
899 $this->topMargin
901 // Builds and save the PDF document
902 $pdf->PMA_PDF_setLineWidthScale(0.1);
904 if ($this->showGrid) {
905 $pdf->SetFontSize(10);
906 $this->_strokeGrid();
908 $pdf->PMA_PDF_setFontSizeScale(14);
909 // previous logic was checking master tables and foreign tables
910 // but I think that looping on every table of the pdf page as a master
911 // and finding its foreigns is OK (then we can support innodb)
912 $seen_a_relation = false;
913 foreach ($alltables as $one_table) {
914 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
915 if ($exist_rel) {
916 $seen_a_relation = true;
917 foreach ($exist_rel as $master_field => $rel) {
918 // put the foreign table on the schema only if selected
919 // by the user
920 // (do not use array_search() because we would have to
921 // to do a === false and this is not PHP3 compatible)
922 if (in_array($rel['foreign_table'], $alltables)) {
923 $this->_addRelation(
924 $one_table,
925 $master_field,
926 $rel['foreign_table'],
927 $rel['foreign_field'],
928 $this->tableDimension
931 } // end while
932 } // end if
933 } // end while
935 if ($seen_a_relation) {
936 $this->_drawRelations($this->showColor);
938 $this->_drawTables($this->showColor);
939 $this->_showOutput($this->pageNumber);
940 exit();
944 * Sets X and Y minimum and maximum for a table cell
946 * @param string $table The table name of which sets XY co-ordinates
948 * @return nothing
950 * @access private
952 private function _setMinMax($table)
954 $this->_xMax = max($this->_xMax, $table->x + $table->width);
955 $this->_yMax = max($this->_yMax, $table->y + $table->height);
956 $this->_xMin = min($this->_xMin, $table->x);
957 $this->_yMin = min($this->_yMin, $table->y);
961 * Defines relation objects
963 * @param string $masterTable The master table name
964 * @param string $masterField The relation field in the master table
965 * @param string $foreignTable The foreign table name
966 * @param string $foreignField The relation field in the foreign table
967 * @param boolean $showInfo Whether to display table position or not
969 * @access private
971 * @return nothing
973 * @see _setMinMax
975 private function _addRelation($masterTable, $masterField, $foreignTable, $foreignField, $showInfo)
977 if (! isset($this->tables[$masterTable])) {
978 $this->tables[$masterTable] = new Table_Stats(
979 $masterTable, $this->_ff, $this->pageNumber,
980 $this->_tablewidth, false, $showInfo
982 $this->_setMinMax($this->tables[$masterTable]);
984 if (! isset($this->tables[$foreignTable])) {
985 $this->tables[$foreignTable] = new Table_Stats(
986 $foreignTable, $this->_ff, $this->pageNumber,
987 $this->_tablewidth, false, $showInfo
989 $this->_setMinMax($this->tables[$foreignTable]);
991 $this->relations[] = new Relation_Stats(
992 $this->tables[$masterTable], $masterField,
993 $this->tables[$foreignTable], $foreignField
998 * Draws the grid
1000 * @global object the current PMA_Schema_PDF instance
1002 * @access private
1004 * @return nothing
1006 * @see PMA_Schema_PDF
1008 private function _strokeGrid()
1010 global $pdf;
1012 $gridSize = 10;
1013 $labelHeight = 4;
1014 $labelWidth = 5;
1015 if ($this->withDoc) {
1016 $topSpace = 6;
1017 $bottomSpace = 15;
1018 } else {
1019 $topSpace = 0;
1020 $bottomSpace = 0;
1023 $pdf->SetMargins(0, 0);
1024 $pdf->SetDrawColor(200, 200, 200);
1025 // Draws horizontal lines
1026 for ($l = 0; $l <= intval(($pdf->getPageHeight() - $topSpace - $bottomSpace) / $gridSize); $l++) {
1027 $pdf->line(
1028 0, $l * $gridSize + $topSpace,
1029 $pdf->getPageWidth(), $l * $gridSize + $topSpace
1031 // Avoid duplicates
1032 if ($l > 0
1033 && $l <= intval(($pdf->getPageHeight() - $topSpace - $bottomSpace - $labelHeight) / $gridSize)
1035 $pdf->SetXY(0, $l * $gridSize + $topSpace);
1036 $label = (string) sprintf(
1037 '%.0f',
1038 ($l * $gridSize + $topSpace - $this->topMargin) * $this->scale + $this->_yMin
1040 $pdf->Cell($labelWidth, $labelHeight, ' ' . $label);
1041 } // end if
1042 } // end for
1043 // Draws vertical lines
1044 for ($j = 0; $j <= intval($pdf->getPageWidth() / $gridSize); $j++) {
1045 $pdf->line(
1046 $j * $gridSize,
1047 $topSpace,
1048 $j * $gridSize,
1049 $pdf->getPageHeight() - $bottomSpace
1051 $pdf->SetXY($j * $gridSize, $topSpace);
1052 $label = (string) sprintf(
1053 '%.0f',
1054 ($j * $gridSize - $this->leftMargin) * $this->scale + $this->_xMin
1056 $pdf->Cell($labelWidth, $labelHeight, $label);
1061 * Draws relation arrows
1063 * @param boolean $changeColor Whether to use one color per relation or not
1065 * @access private
1067 * @return nothing
1069 * @see Relation_Stats::relationdraw()
1071 private function _drawRelations($changeColor)
1073 $i = 0;
1074 foreach ($this->relations as $relation) {
1075 $relation->relationDraw($changeColor, $i);
1076 $i++;
1081 * Draws tables
1083 * @param boolean $changeColor Whether to display table position or not
1085 * @access private
1087 * @return nothing
1089 * @see Table_Stats::tableDraw()
1091 private function _drawTables($changeColor = 0)
1093 foreach ($this->tables as $table) {
1094 $table->tableDraw($this->_ff, $this->withDoc, $changeColor);
1099 * Ouputs the PDF document to a file
1100 * or sends the output to browser
1102 * @param integer $pageNumber page number
1104 * @global object The current PDF document
1105 * @global string The current database name
1106 * @global integer The current page number (from the
1107 * $cfg['Servers'][$i]['table_coords'] table)
1108 * @access private
1110 * @return nothing
1112 * @see PMA_Schema_PDF
1114 private function _showOutput($pageNumber)
1116 global $pdf, $cfgRelation;
1118 // Get the name of this pdfpage to use as filename
1119 $_name_sql = 'SELECT page_descr FROM '
1120 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.'
1121 . PMA_backquote($cfgRelation['pdf_pages'])
1122 . ' WHERE page_nr = ' . $pageNumber;
1123 $_name_rs = PMA_query_as_controluser($_name_sql);
1124 if ($_name_rs) {
1125 $_name_row = PMA_DBI_fetch_row($_name_rs);
1126 $filename = $_name_row[0] . '.pdf';
1128 if (empty($filename)) {
1129 $filename = $pageNumber . '.pdf';
1131 $pdf->Download($filename);
1134 public function dataDictionaryDoc($alltables)
1136 global $db, $pdf, $orientation, $paper;
1137 // TOC
1138 $pdf->addpage($GLOBALS['orientation']);
1139 $pdf->Cell(0, 9, __('Table of contents'), 1, 0, 'C');
1140 $pdf->Ln(15);
1141 $i = 1;
1142 foreach ($alltables as $table) {
1143 $pdf->PMA_links['doc'][$table]['-'] = $pdf->AddLink();
1144 $pdf->SetX(10);
1145 // $pdf->Ln(1);
1146 $pdf->Cell(
1147 0, 6, __('Page number:') . ' {' . sprintf("%02d", $i) . '}', 0, 0,
1148 'R', 0, $pdf->PMA_links['doc'][$table]['-']
1150 $pdf->SetX(10);
1151 $pdf->Cell(
1152 0, 6, $i . ' ' . $table, 0, 1,
1153 'L', 0, $pdf->PMA_links['doc'][$table]['-']
1155 // $pdf->Ln(1);
1156 $fields = PMA_DBI_get_columns($GLOBALS['db'], $table);
1157 foreach ($fields as $row) {
1158 $pdf->SetX(20);
1159 $field_name = $row['Field'];
1160 $pdf->PMA_links['doc'][$table][$field_name] = $pdf->AddLink();
1161 //$pdf->Cell(
1162 // 0, 6, $field_name, 0, 1,
1163 // 'L', 0, $pdf->PMA_links['doc'][$table][$field_name]
1164 //);
1166 $i++;
1168 $pdf->PMA_links['RT']['-'] = $pdf->AddLink();
1169 $pdf->SetX(10);
1170 $pdf->Cell(
1171 0, 6, __('Page number:') . ' {00}', 0, 0,
1172 'R', 0, $pdf->PMA_links['RT']['-']
1174 $pdf->SetX(10);
1175 $pdf->Cell(
1176 0, 6, $i . ' ' . __('Relational schema'), 0, 1,
1177 'L', 0, $pdf->PMA_links['RT']['-']
1179 $z = 0;
1180 foreach ($alltables as $table) {
1181 $z++;
1182 $pdf->SetAutoPageBreak(true, 15);
1183 $pdf->addpage($GLOBALS['orientation']);
1184 $pdf->Bookmark($table);
1185 $pdf->SetAlias('{' . sprintf("%02d", $z) . '}', $pdf->PageNo());
1186 $pdf->PMA_links['RT'][$table]['-'] = $pdf->AddLink();
1187 $pdf->SetLink($pdf->PMA_links['doc'][$table]['-'], -1);
1188 $pdf->SetFont($this->_ff, 'B', 18);
1189 $pdf->Cell(
1190 0, 8, $z . ' ' . $table, 1, 1,
1191 'C', 0, $pdf->PMA_links['RT'][$table]['-']
1193 $pdf->SetFont($this->_ff, '', 8);
1194 $pdf->ln();
1196 $cfgRelation = PMA_getRelationsParam();
1197 $comments = PMA_getComments($db, $table);
1198 if ($cfgRelation['mimework']) {
1199 $mime_map = PMA_getMIME($db, $table, true);
1203 * Gets table informations
1205 $showtable = PMA_Table::sGetStatusInfo($db, $table);
1206 $show_comment = isset($showtable['Comment'])
1207 ? $showtable['Comment']
1208 : '';
1209 $create_time = isset($showtable['Create_time'])
1210 ? PMA_localisedDate(strtotime($showtable['Create_time']))
1211 : '';
1212 $update_time = isset($showtable['Update_time'])
1213 ? PMA_localisedDate(strtotime($showtable['Update_time']))
1214 : '';
1215 $check_time = isset($showtable['Check_time'])
1216 ? PMA_localisedDate(strtotime($showtable['Check_time']))
1217 : '';
1220 * Gets table keys and retains them
1222 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
1223 $primary = '';
1224 $indexes = array();
1225 $lastIndex = '';
1226 $indexes_info = array();
1227 $indexes_data = array();
1228 $pk_array = array(); // will be use to emphasis prim. keys in the table
1229 // view
1230 while ($row = PMA_DBI_fetch_assoc($result)) {
1231 // Backups the list of primary keys
1232 if ($row['Key_name'] == 'PRIMARY') {
1233 $primary .= $row['Column_name'] . ', ';
1234 $pk_array[$row['Column_name']] = 1;
1236 // Retains keys informations
1237 if ($row['Key_name'] != $lastIndex) {
1238 $indexes[] = $row['Key_name'];
1239 $lastIndex = $row['Key_name'];
1241 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
1242 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
1243 if (isset($row['Cardinality'])) {
1244 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
1246 // I don't know what does following column mean....
1247 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
1248 $indexes_info[$row['Key_name']]['Comment'] = $row['Comment'];
1250 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
1251 if (isset($row['Sub_part'])) {
1252 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
1254 } // end while
1255 if ($result) {
1256 PMA_DBI_free_result($result);
1260 * Gets fields properties
1262 $columns = PMA_DBI_get_columns($db, $table);
1263 // Check if we can use Relations
1264 if (!empty($cfgRelation['relation'])) {
1265 // Find which tables are related with the current one and write it in
1266 // an array
1267 $res_rel = PMA_getForeigners($db, $table);
1269 if (count($res_rel) > 0) {
1270 $have_rel = true;
1271 } else {
1272 $have_rel = false;
1274 } else {
1275 $have_rel = false;
1276 } // end if
1279 * Displays the comments of the table if MySQL >= 3.23
1282 $break = false;
1283 if (! empty($show_comment)) {
1284 $pdf->Cell(0, 3, __('Table comments') . ' : ' . $show_comment, 0, 1);
1285 $break = true;
1288 if (! empty($create_time)) {
1289 $pdf->Cell(0, 3, __('Creation') . ': ' . $create_time, 0, 1);
1290 $break = true;
1293 if (! empty($update_time)) {
1294 $pdf->Cell(0, 3, __('Last update') . ': ' . $update_time, 0, 1);
1295 $break = true;
1298 if (! empty($check_time)) {
1299 $pdf->Cell(0, 3, __('Last check') . ': ' . $check_time, 0, 1);
1300 $break = true;
1303 if ($break == true) {
1304 $pdf->Cell(0, 3, '', 0, 1);
1305 $pdf->Ln();
1308 $pdf->SetFont($this->_ff, 'B');
1309 if (isset($orientation) && $orientation == 'L') {
1310 $pdf->Cell(25, 8, __('Column'), 1, 0, 'C');
1311 $pdf->Cell(20, 8, __('Type'), 1, 0, 'C');
1312 $pdf->Cell(20, 8, __('Attributes'), 1, 0, 'C');
1313 $pdf->Cell(10, 8, __('Null'), 1, 0, 'C');
1314 $pdf->Cell(20, 8, __('Default'), 1, 0, 'C');
1315 $pdf->Cell(25, 8, __('Extra'), 1, 0, 'C');
1316 $pdf->Cell(45, 8, __('Links to'), 1, 0, 'C');
1318 if ($paper == 'A4') {
1319 $comments_width = 67;
1320 } else {
1321 // this is really intended for 'letter'
1323 * @todo find optimal width for all formats
1325 $comments_width = 50;
1327 $pdf->Cell($comments_width, 8, __('Comments'), 1, 0, 'C');
1328 $pdf->Cell(45, 8, 'MIME', 1, 1, 'C');
1329 $pdf->SetWidths(array(25, 20, 20, 10, 20, 25, 45, $comments_width, 45));
1330 } else {
1331 $pdf->Cell(20, 8, __('Column'), 1, 0, 'C');
1332 $pdf->Cell(20, 8, __('Type'), 1, 0, 'C');
1333 $pdf->Cell(20, 8, __('Attributes'), 1, 0, 'C');
1334 $pdf->Cell(10, 8, __('Null'), 1, 0, 'C');
1335 $pdf->Cell(15, 8, __('Default'), 1, 0, 'C');
1336 $pdf->Cell(15, 8, __('Extra'), 1, 0, 'C');
1337 $pdf->Cell(30, 8, __('Links to'), 1, 0, 'C');
1338 $pdf->Cell(30, 8, __('Comments'), 1, 0, 'C');
1339 $pdf->Cell(30, 8, 'MIME', 1, 1, 'C');
1340 $pdf->SetWidths(array(20, 20, 20, 10, 15, 15, 30, 30, 30));
1342 $pdf->SetFont($this->_ff, '');
1344 foreach ($columns as $row) {
1345 $extracted_fieldspec = PMA_extractFieldSpec($row['Type']);
1346 $type = $extracted_fieldspec['print_type'];
1347 $attribute = $extracted_fieldspec['attribute'];
1348 if (! isset($row['Default'])) {
1349 if ($row['Null'] != '' && $row['Null'] != 'NO') {
1350 $row['Default'] = 'NULL';
1353 $field_name = $row['Field'];
1354 // $pdf->Ln();
1355 $pdf->PMA_links['RT'][$table][$field_name] = $pdf->AddLink();
1356 $pdf->Bookmark($field_name, 1, -1);
1357 $pdf->SetLink($pdf->PMA_links['doc'][$table][$field_name], -1);
1358 $pdf_row = array(
1359 $field_name,
1360 $type,
1361 $attribute,
1362 ($row['Null'] == '' || $row['Null'] == 'NO') ? __('No') : __('Yes'),
1363 (isset($row['Default']) ? $row['Default'] : ''),
1364 $row['Extra'],
1365 (isset($res_rel[$field_name])
1366 ? $res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field']
1367 : ''),
1368 (isset($comments[$field_name])
1369 ? $comments[$field_name]
1370 : ''),
1371 (isset($mime_map) && isset($mime_map[$field_name])
1372 ? str_replace('_', '/', $mime_map[$field_name]['mimetype'])
1373 : '')
1375 $links[0] = $pdf->PMA_links['RT'][$table][$field_name];
1376 if (isset($res_rel[$field_name]['foreign_table'])
1377 AND isset($res_rel[$field_name]['foreign_field'])
1378 AND isset($pdf->PMA_links['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']])
1380 $links[6] = $pdf->PMA_links['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']];
1381 } else {
1382 unset($links[6]);
1384 $pdf->Row($pdf_row, $links);
1385 } // end foreach
1386 $pdf->SetFont($this->_ff, '', 14);
1387 } //end each