Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / schema / Pdf_Relation_Schema.class.php
blob5bd36eb5a7faae93158a5dab8d5e6a87226a9eb5
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * PDF schema handling
6 * @package PhpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 require_once 'Export_Relation_Schema.class.php';
13 require_once './libraries/PDF.class.php';
15 /**
16 * Extends the "TCPDF" class and helps
17 * in developing the structure of PDF Schema Export
19 * @access public
20 * @package PhpMyAdmin
21 * @see TCPDF
23 class PMA_Schema_PDF extends PMA_PDF
25 /**
26 * Defines properties
28 var $_xMin;
29 var $_yMin;
30 var $leftMargin = 10;
31 var $topMargin = 10;
32 var $scale;
33 var $PMA_links;
34 var $Outlines = array();
35 var $def_outlines;
36 var $widths;
37 private $_ff = PMA_PDF_FONT;
39 /**
40 * Sets the value for margins
42 * @param float $c_margin margin
44 * @return void
46 public function setCMargin($c_margin)
48 $this->cMargin = $c_margin;
51 /**
52 * Sets the scaling factor, defines minimum coordinates and margins
54 * @param float $scale The scaling factor
55 * @param float $xMin The minimum X coordinate
56 * @param float $yMin The minimum Y coordinate
57 * @param float $leftMargin The left margin
58 * @param float $topMargin The top margin
60 * @access public
62 * @return void
64 function setScale($scale = 1, $xMin = 0, $yMin = 0,
65 $leftMargin = -1, $topMargin = -1
66 ) {
67 $this->scale = $scale;
68 $this->_xMin = $xMin;
69 $this->_yMin = $yMin;
70 if ($this->leftMargin != -1) {
71 $this->leftMargin = $leftMargin;
73 if ($this->topMargin != -1) {
74 $this->topMargin = $topMargin;
78 /**
79 * Outputs a scaled cell
81 * @param float $w The cell width
82 * @param float $h The cell height
83 * @param string $txt The text to output
84 * @param mixed $border Whether to add borders or not
85 * @param integer $ln Where to put the cursor once the output is done
86 * @param string $align Align mode
87 * @param integer $fill Whether to fill the cell with a color or not
88 * @param string $link Link
90 * @access public
92 * @return void
94 * @see TCPDF::Cell()
96 function cellScale($w, $h = 0, $txt = '', $border = 0, $ln = 0,
97 $align = '', $fill = 0, $link = ''
98 ) {
99 $h = $h / $this->scale;
100 $w = $w / $this->scale;
101 $this->Cell($w, $h, $txt, $border, $ln, $align, $fill, $link);
105 * Draws a scaled line
107 * @param float $x1 The horizontal position of the starting point
108 * @param float $y1 The vertical position of the starting point
109 * @param float $x2 The horizontal position of the ending point
110 * @param float $y2 The vertical position of the ending point
112 * @access public
114 * @return void
116 * @see TCPDF::Line()
118 function lineScale($x1, $y1, $x2, $y2)
120 $x1 = ($x1 - $this->_xMin) / $this->scale + $this->leftMargin;
121 $y1 = ($y1 - $this->_yMin) / $this->scale + $this->topMargin;
122 $x2 = ($x2 - $this->_xMin) / $this->scale + $this->leftMargin;
123 $y2 = ($y2 - $this->_yMin) / $this->scale + $this->topMargin;
124 $this->Line($x1, $y1, $x2, $y2);
128 * Sets x and y scaled positions
130 * @param float $x The x position
131 * @param float $y The y position
133 * @access public
135 * @return void
137 * @see TCPDF::SetXY()
139 function setXyScale($x, $y)
141 $x = ($x - $this->_xMin) / $this->scale + $this->leftMargin;
142 $y = ($y - $this->_yMin) / $this->scale + $this->topMargin;
143 $this->SetXY($x, $y);
147 * Sets the X scaled positions
149 * @param float $x The x position
151 * @access public
153 * @return void
155 * @see TCPDF::SetX()
157 function setXScale($x)
159 $x = ($x - $this->_xMin) / $this->scale + $this->leftMargin;
160 $this->SetX($x);
164 * Sets the scaled font size
166 * @param float $size The font size (in points)
168 * @access public
170 * @return void
172 * @see TCPDF::SetFontSize()
174 function setFontSizeScale($size)
176 // Set font size in points
177 $size = $size / $this->scale;
178 $this->SetFontSize($size);
182 * Sets the scaled line width
184 * @param float $width The line width
186 * @access public
188 * @return void
190 * @see TCPDF::SetLineWidth()
192 function setLineWidthScale($width)
194 $width = $width / $this->scale;
195 $this->SetLineWidth($width);
199 * This method is used to render the page header.
201 * @return void
203 * @see TCPDF::Header()
205 function Header()
207 // We only show this if we find something in the new pdf_pages table
209 // This function must be named "Header" to work with the TCPDF library
210 global $cfgRelation, $db, $pdf_page_number, $with_doc;
211 if ($with_doc) {
212 $test_query = 'SELECT * FROM '
213 . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.'
214 . PMA_Util::backquote($cfgRelation['pdf_pages'])
215 . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\''
216 . ' AND page_nr = \'' . $pdf_page_number . '\'';
217 $test_rs = PMA_queryAsControlUser($test_query);
218 $pages = @PMA_DBI_fetch_assoc($test_rs);
219 $this->SetFont($this->_ff, 'B', 14);
220 $this->Cell(0, 6, ucfirst($pages['page_descr']), 'B', 1, 'C');
221 $this->SetFont($this->_ff, '');
222 $this->Ln();
227 * This function must be named "Footer" to work with the TCPDF library
229 * @return void
231 * @see PMA_PDF::Footer()
233 function Footer()
235 global $with_doc;
236 if ($with_doc) {
237 parent::Footer();
242 * Sets widths
244 * @param array $w array of widths
246 * @return void
248 function SetWidths($w)
250 // column widths
251 $this->widths = $w;
255 * Generates table row.
257 * @param array $data Data for table
258 * @param array $links Links for table cells
260 * @return void
262 function Row($data, $links)
264 // line height
265 $nb = 0;
266 $data_cnt = count($data);
267 for ($i = 0;$i < $data_cnt;$i++) {
268 $nb = max($nb, $this->NbLines($this->widths[$i], $data[$i]));
270 $il = $this->FontSize;
271 $h = ($il + 1) * $nb;
272 // page break if necessary
273 $this->CheckPageBreak($h);
274 // draw the cells
275 $data_cnt = count($data);
276 for ($i = 0;$i < $data_cnt;$i++) {
277 $w = $this->widths[$i];
278 // save current position
279 $x = $this->GetX();
280 $y = $this->GetY();
281 // draw the border
282 $this->Rect($x, $y, $w, $h);
283 if (isset($links[$i])) {
284 $this->Link($x, $y, $w, $h, $links[$i]);
286 // print text
287 $this->MultiCell($w, $il + 1, $data[$i], 0, 'L');
288 // go to right side
289 $this->SetXY($x + $w, $y);
291 // go to line
292 $this->Ln($h);
296 * Compute number of lines used by a multicell of width w
298 * @param int $w width
299 * @param string $txt text
301 * @return int
303 function NbLines($w, $txt)
305 $cw = &$this->CurrentFont['cw'];
306 if ($w == 0) {
307 $w = $this->w - $this->rMargin - $this->x;
309 $wmax = ($w-2 * $this->cMargin) * 1000 / $this->FontSize;
310 $s = str_replace("\r", '', $txt);
311 $nb = strlen($s);
312 if ($nb > 0 and $s[$nb-1] == "\n") {
313 $nb--;
315 $sep = -1;
316 $i = 0;
317 $j = 0;
318 $l = 0;
319 $nl = 1;
320 while ($i < $nb) {
321 $c = $s[$i];
322 if ($c == "\n") {
323 $i++;
324 $sep = -1;
325 $j = $i;
326 $l = 0;
327 $nl++;
328 continue;
330 if ($c == ' ') {
331 $sep = $i;
333 $l += isset($cw[ord($c)])?$cw[ord($c)]:0 ;
334 if ($l > $wmax) {
335 if ($sep == -1) {
336 if ($i == $j) {
337 $i++;
339 } else {
340 $i = $sep + 1;
342 $sep = -1;
343 $j = $i;
344 $l = 0;
345 $nl++;
346 } else {
347 $i++;
350 return $nl;
355 * Table preferences/statistics
357 * This class preserves the table co-ordinates,fields
358 * and helps in drawing/generating the Tables in PDF document.
360 * @name Table_Stats
361 * @package PhpMyAdmin
362 * @see PMA_Schema_PDF
364 class Table_Stats
367 * Defines properties
369 private $_tableName;
370 private $_showInfo = false;
372 public $nb_fiels;
373 public $width = 0;
374 public $height;
375 public $fields = array();
376 public $heightCell = 6;
377 public $x, $y;
378 public $primary = array();
379 private $_ff = PMA_PDF_FONT;
382 * The "Table_Stats" constructor
384 * @param string $tableName The table name
385 * @param integer $fontSize The font size
386 * @param integer $pageNumber The current page number (from the
387 * $cfg['Servers'][$i]['table_coords'] table)
388 * @param integer &$sameWideWidth The max. with among tables
389 * @param boolean $showKeys Whether to display keys or not
390 * @param boolean $showInfo Whether to display table position or not
392 * @global object The current PDF document
393 * @global array The relations settings
394 * @global string The current db name
396 * @return void
398 * @see PMA_Schema_PDF, Table_Stats::Table_Stats_setWidth,
399 * Table_Stats::Table_Stats_setHeight
401 function __construct($tableName, $fontSize, $pageNumber, &$sameWideWidth,
402 $showKeys = false, $showInfo = false
404 global $pdf, $cfgRelation, $db;
406 $this->_tableName = $tableName;
407 $sql = 'DESCRIBE ' . PMA_Util::backquote($tableName);
408 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE);
409 if (! $result || ! PMA_DBI_num_rows($result)) {
410 $pdf->Error(sprintf(__('The %s table doesn\'t exist!'), $tableName));
412 // load fields
413 //check to see if it will load all fields or only the foreign keys
414 if ($showKeys) {
415 $indexes = PMA_Index::getFromTable($this->_tableName, $db);
416 $all_columns = array();
417 foreach ($indexes as $index) {
418 $all_columns = array_merge(
419 $all_columns,
420 array_flip(array_keys($index->getColumns()))
423 $this->fields = array_keys($all_columns);
424 } else {
425 while ($row = PMA_DBI_fetch_row($result)) {
426 $this->fields[] = $row[0];
430 $this->_showInfo = $showInfo;
431 $this->_setHeight();
433 * setWidth must me after setHeight, because title
434 * can include table height which changes table width
436 $this->_setWidth($fontSize);
437 if ($sameWideWidth < $this->width) {
438 $sameWideWidth = $this->width;
440 $sql = 'SELECT x, y FROM '
441 . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.'
442 . PMA_Util::backquote($cfgRelation['table_coords'])
443 . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\''
444 . ' AND table_name = \'' . PMA_Util::sqlAddSlashes($tableName) . '\''
445 . ' AND pdf_page_number = ' . $pageNumber;
446 $result = PMA_queryAsControlUser($sql, false, PMA_DBI_QUERY_STORE);
447 if (! $result || ! PMA_DBI_num_rows($result)) {
448 $pdf->Error(
449 sprintf(
450 __('Please configure the coordinates for table %s'),
451 $tableName
455 list($this->x, $this->y) = PMA_DBI_fetch_row($result);
456 $this->x = (double) $this->x;
457 $this->y = (double) $this->y;
459 * displayfield
461 $this->displayfield = PMA_getDisplayField($db, $tableName);
463 * index
465 $result = PMA_DBI_query(
466 'SHOW INDEX FROM ' . PMA_Util::backquote($tableName) . ';',
467 null, PMA_DBI_QUERY_STORE
469 if (PMA_DBI_num_rows($result) > 0) {
470 while ($row = PMA_DBI_fetch_assoc($result)) {
471 if ($row['Key_name'] == 'PRIMARY') {
472 $this->primary[] = $row['Column_name'];
479 * Returns title of the current table,
480 * title can have the dimensions of the table
482 * @return string
484 private function _getTitle()
486 $ret = '';
487 if ($this->_showInfo) {
488 $ret = sprintf('%.0fx%0.f', $this->width, $this->height);
490 return $ret . ' ' . $this->_tableName;
494 * Sets the width of the table
496 * @param integer $fontSize The font size
498 * @global object The current PDF document
500 * @access private
502 * @return void
504 * @see PMA_Schema_PDF
506 private function _setWidth($fontSize)
508 global $pdf;
510 foreach ($this->fields as $field) {
511 $this->width = max($this->width, $pdf->GetStringWidth($field));
513 $this->width += $pdf->GetStringWidth(' ');
514 $pdf->SetFont($this->_ff, 'B', $fontSize);
516 * it is unknown what value must be added, because
517 * table title is affected by the tabe width value
519 while ($this->width < $pdf->GetStringWidth($this->_getTitle())) {
520 $this->width += 5;
522 $pdf->SetFont($this->_ff, '', $fontSize);
526 * Sets the height of the table
528 * @return void
530 * @access private
532 private function _setHeight()
534 $this->height = (count($this->fields) + 1) * $this->heightCell;
538 * Do draw the table
540 * @param integer $fontSize The font size
541 * @param boolean $withDoc Whether to include links to documentation
542 * @param boolean $setColor Whether to display color
544 * @global object The current PDF document
546 * @access public
548 * @return void
550 * @see PMA_Schema_PDF
552 public function tableDraw($fontSize, $withDoc, $setColor = 0)
554 global $pdf, $withDoc;
556 $pdf->setXyScale($this->x, $this->y);
557 $pdf->SetFont($this->_ff, 'B', $fontSize);
558 if ($setColor) {
559 $pdf->SetTextColor(200);
560 $pdf->SetFillColor(0, 0, 128);
562 if ($withDoc) {
563 $pdf->SetLink($pdf->PMA_links['RT'][$this->_tableName]['-'], -1);
564 } else {
565 $pdf->PMA_links['doc'][$this->_tableName]['-'] = '';
568 $pdf->cellScale(
569 $this->width,
570 $this->heightCell,
571 $this->_getTitle(),
574 'C',
575 $setColor,
576 $pdf->PMA_links['doc'][$this->_tableName]['-']
578 $pdf->setXScale($this->x);
579 $pdf->SetFont($this->_ff, '', $fontSize);
580 $pdf->SetTextColor(0);
581 $pdf->SetFillColor(255);
583 foreach ($this->fields as $field) {
584 if ($setColor) {
585 if (in_array($field, $this->primary)) {
586 $pdf->SetFillColor(215, 121, 123);
588 if ($field == $this->displayfield) {
589 $pdf->SetFillColor(142, 159, 224);
592 if ($withDoc) {
593 $pdf->SetLink($pdf->PMA_links['RT'][$this->_tableName][$field], -1);
594 } else {
595 $pdf->PMA_links['doc'][$this->_tableName][$field] = '';
598 $pdf->cellScale(
599 $this->width,
600 $this->heightCell,
601 ' ' . $field,
604 'L',
605 $setColor,
606 $pdf->PMA_links['doc'][$this->_tableName][$field]
608 $pdf->setXScale($this->x);
609 $pdf->SetFillColor(255);
615 * Relation preferences/statistics
617 * This class fetches the table master and foreign fields positions
618 * and helps in generating the Table references and then connects
619 * master table's master field to foreign table's foreign key
620 * in PDF document.
622 * @name Relation_Stats
623 * @package PhpMyAdmin
624 * @see PMA_Schema_PDF::SetDrawColor, PMA_Schema_PDF::setLineWidthScale,
625 * PMA_Schema_PDF::lineScale
627 class Relation_Stats
630 * Defines properties
632 public $xSrc, $ySrc;
633 public $srcDir;
634 public $destDir;
635 public $xDest, $yDest;
636 public $wTick = 5;
639 * The "Relation_Stats" constructor
641 * @param string $master_table The master table name
642 * @param string $master_field The relation field in the master table
643 * @param string $foreign_table The foreign table name
644 * @param string $foreign_field The relation field in the foreign table
646 * @return void
648 * @see Relation_Stats::_getXy
650 function __construct($master_table, $master_field, $foreign_table,
651 $foreign_field
653 $src_pos = $this->_getXy($master_table, $master_field);
654 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
656 * [0] is x-left
657 * [1] is x-right
658 * [2] is y
660 $src_left = $src_pos[0] - $this->wTick;
661 $src_right = $src_pos[1] + $this->wTick;
662 $dest_left = $dest_pos[0] - $this->wTick;
663 $dest_right = $dest_pos[1] + $this->wTick;
665 $d1 = abs($src_left - $dest_left);
666 $d2 = abs($src_right - $dest_left);
667 $d3 = abs($src_left - $dest_right);
668 $d4 = abs($src_right - $dest_right);
669 $d = min($d1, $d2, $d3, $d4);
671 if ($d == $d1) {
672 $this->xSrc = $src_pos[0];
673 $this->srcDir = -1;
674 $this->xDest = $dest_pos[0];
675 $this->destDir = -1;
676 } elseif ($d == $d2) {
677 $this->xSrc = $src_pos[1];
678 $this->srcDir = 1;
679 $this->xDest = $dest_pos[0];
680 $this->destDir = -1;
681 } elseif ($d == $d3) {
682 $this->xSrc = $src_pos[0];
683 $this->srcDir = -1;
684 $this->xDest = $dest_pos[1];
685 $this->destDir = 1;
686 } else {
687 $this->xSrc = $src_pos[1];
688 $this->srcDir = 1;
689 $this->xDest = $dest_pos[1];
690 $this->destDir = 1;
692 $this->ySrc = $src_pos[2];
693 $this->yDest = $dest_pos[2];
697 * Gets arrows coordinates
699 * @param string $table The current table name
700 * @param string $column The relation column name
702 * @return array Arrows coordinates
704 * @access private
706 private function _getXy($table, $column)
708 $pos = array_search($column, $table->fields);
709 // x_left, x_right, y
710 return array(
711 $table->x,
712 $table->x + $table->width,
713 $table->y + ($pos + 1.5) * $table->heightCell
718 * draws relation links and arrows shows foreign key relations
720 * @param boolean $changeColor Whether to use one color per relation or not
721 * @param integer $i The id of the link to draw
723 * @global object The current PDF document
725 * @access public
727 * @return void
729 * @see PMA_Schema_PDF
731 public function relationDraw($changeColor, $i)
733 global $pdf;
735 if ($changeColor) {
736 $d = $i % 6;
737 $j = ($i - $d) / 6;
738 $j = $j % 4;
739 $j++;
740 $case = array(
741 array(1, 0, 0),
742 array(0, 1, 0),
743 array(0, 0, 1),
744 array(1, 1, 0),
745 array(1, 0, 1),
746 array(0, 1, 1)
748 list ($a, $b, $c) = $case[$d];
749 $e = (1 - ($j - 1) / 6);
750 $pdf->SetDrawColor($a * 255 * $e, $b * 255 * $e, $c * 255 * $e);
751 } else {
752 $pdf->SetDrawColor(0);
754 $pdf->setLineWidthScale(0.2);
755 $pdf->lineScale(
756 $this->xSrc,
757 $this->ySrc,
758 $this->xSrc + $this->srcDir * $this->wTick,
759 $this->ySrc
761 $pdf->lineScale(
762 $this->xDest + $this->destDir * $this->wTick,
763 $this->yDest,
764 $this->xDest,
765 $this->yDest
767 $pdf->setLineWidthScale(0.1);
768 $pdf->lineScale(
769 $this->xSrc + $this->srcDir * $this->wTick,
770 $this->ySrc,
771 $this->xDest + $this->destDir * $this->wTick,
772 $this->yDest
775 * Draws arrows ->
777 $root2 = 2 * sqrt(2);
778 $pdf->lineScale(
779 $this->xSrc + $this->srcDir * $this->wTick * 0.75,
780 $this->ySrc,
781 $this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick,
782 $this->ySrc + $this->wTick / $root2
784 $pdf->lineScale(
785 $this->xSrc + $this->srcDir * $this->wTick * 0.75,
786 $this->ySrc,
787 $this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick,
788 $this->ySrc - $this->wTick / $root2
791 $pdf->lineScale(
792 $this->xDest + $this->destDir * $this->wTick / 2,
793 $this->yDest,
794 $this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick,
795 $this->yDest + $this->wTick / $root2
797 $pdf->lineScale(
798 $this->xDest + $this->destDir * $this->wTick / 2,
799 $this->yDest,
800 $this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick,
801 $this->yDest - $this->wTick / $root2
803 $pdf->SetDrawColor(0);
808 * Pdf Relation Schema Class
810 * Purpose of this class is to generate the PDF Document. PDF is widely
811 * used format for documenting text,fonts,images and 3d vector graphics.
813 * This class inherits Export_Relation_Schema class has common functionality added
814 * to this class
816 * @name Pdf_Relation_Schema
817 * @package PhpMyAdmin
819 class PMA_Pdf_Relation_Schema extends PMA_Export_Relation_Schema
822 * Defines properties
824 private $_tables = array();
825 private $_ff = PMA_PDF_FONT;
826 private $_xMax = 0;
827 private $_yMax = 0;
828 private $_scale;
829 private $_xMin = 100000;
830 private $_yMin = 100000;
831 private $_topMargin = 10;
832 private $_bottomMargin = 10;
833 private $_leftMargin = 10;
834 private $_rightMargin = 10;
835 private $_tablewidth;
838 * The "PMA_Pdf_Relation_Schema" constructor
840 * @global object The current PDF Schema document
841 * @global string The current db name
842 * @global array The relations settings
843 * @access private
844 * @see PMA_Schema_PDF
846 function __construct()
848 global $pdf, $db;
850 $this->setPageNumber($_POST['pdf_page_number']);
851 $this->setShowGrid(isset($_POST['show_grid']));
852 $this->setShowColor(isset($_POST['show_color']));
853 $this->setShowKeys(isset($_POST['show_keys']));
854 $this->setTableDimension(isset($_POST['show_table_dimension']));
855 $this->setAllTablesSameWidth(isset($_POST['all_tables_same_width']));
856 $this->setWithDataDictionary($_POST['with_doc']);
857 $this->setOrientation($_POST['orientation']);
858 $this->setPaper($_POST['paper']);
859 $this->setExportType($_POST['export_type']);
861 // Initializes a new document
862 $pdf = new PMA_Schema_PDF($this->orientation, 'mm', $this->paper);
863 $pdf->SetTitle(
864 sprintf(
865 __('Schema of the %s database - Page %s'),
866 $GLOBALS['db'],
867 $this->pageNumber
870 $pdf->setCMargin(0);
871 $pdf->Open();
872 $pdf->SetAutoPageBreak('auto');
873 $alltables = $this->getAllTables($db, $this->pageNumber);
875 if ($this->withDoc) {
876 $pdf->SetAutoPageBreak('auto', 15);
877 $pdf->setCMargin(1);
878 $this->dataDictionaryDoc($alltables);
879 $pdf->SetAutoPageBreak('auto');
880 $pdf->setCMargin(0);
883 $pdf->Addpage();
885 if ($this->withDoc) {
886 $pdf->SetLink($pdf->PMA_links['RT']['-'], -1);
887 $pdf->Bookmark(__('Relational schema'));
888 $pdf->SetAlias('{00}', $pdf->PageNo());
889 $this->_topMargin = 28;
890 $this->_bottomMargin = 28;
893 /* snip */
894 foreach ($alltables as $table) {
895 if (! isset($this->_tables[$table])) {
896 $this->_tables[$table] = new Table_Stats(
897 $table,
898 null,
899 $this->pageNumber,
900 $this->_tablewidth,
901 $this->showKeys,
902 $this->tableDimension
905 if ($this->sameWide) {
906 $this->_tables[$table]->width = $this->_tablewidth;
908 $this->_setMinMax($this->_tables[$table]);
911 // Defines the scale factor
912 $this->_scale = ceil(
913 max(
914 ($this->_xMax - $this->_xMin)
915 / ($pdf->getPageWidth() - $this->_rightMargin - $this->_leftMargin),
916 ($this->_yMax - $this->_yMin)
917 / ($pdf->getPageHeight() - $this->_topMargin - $this->_bottomMargin)
918 ) * 100
919 ) / 100;
921 $pdf->setScale(
922 $this->_scale,
923 $this->_xMin,
924 $this->_yMin,
925 $this->_leftMargin,
926 $this->_topMargin
928 // Builds and save the PDF document
929 $pdf->setLineWidthScale(0.1);
931 if ($this->showGrid) {
932 $pdf->SetFontSize(10);
933 $this->_strokeGrid();
935 $pdf->setFontSizeScale(14);
936 // previous logic was checking master tables and foreign tables
937 // but I think that looping on every table of the pdf page as a master
938 // and finding its foreigns is OK (then we can support innodb)
939 $seen_a_relation = false;
940 foreach ($alltables as $one_table) {
941 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
942 if ($exist_rel) {
943 $seen_a_relation = true;
944 foreach ($exist_rel as $master_field => $rel) {
945 // put the foreign table on the schema only if selected
946 // by the user
947 // (do not use array_search() because we would have to
948 // to do a === false and this is not PHP3 compatible)
949 if (in_array($rel['foreign_table'], $alltables)) {
950 $this->_addRelation(
951 $one_table,
952 $master_field,
953 $rel['foreign_table'],
954 $rel['foreign_field'],
955 $this->tableDimension
958 } // end while
959 } // end if
960 } // end while
962 if ($seen_a_relation) {
963 $this->_drawRelations($this->showColor);
965 $this->_drawTables($this->showColor);
966 $this->_showOutput($this->pageNumber);
967 exit();
971 * Sets X and Y minimum and maximum for a table cell
973 * @param string $table The table name of which sets XY co-ordinates
975 * @return void
977 * @access private
979 private function _setMinMax($table)
981 $this->_xMax = max($this->_xMax, $table->x + $table->width);
982 $this->_yMax = max($this->_yMax, $table->y + $table->height);
983 $this->_xMin = min($this->_xMin, $table->x);
984 $this->_yMin = min($this->_yMin, $table->y);
988 * Defines relation objects
990 * @param string $masterTable The master table name
991 * @param string $masterField The relation field in the master table
992 * @param string $foreignTable The foreign table name
993 * @param string $foreignField The relation field in the foreign table
994 * @param boolean $showInfo Whether to display table position or not
996 * @access private
998 * @return void
1000 * @see _setMinMax
1002 private function _addRelation($masterTable, $masterField, $foreignTable,
1003 $foreignField, $showInfo
1005 if (! isset($this->_tables[$masterTable])) {
1006 $this->_tables[$masterTable] = new Table_Stats(
1007 $masterTable, null, $this->pageNumber,
1008 $this->_tablewidth, false, $showInfo
1010 $this->_setMinMax($this->_tables[$masterTable]);
1012 if (! isset($this->_tables[$foreignTable])) {
1013 $this->_tables[$foreignTable] = new Table_Stats(
1014 $foreignTable, null, $this->pageNumber,
1015 $this->_tablewidth, false, $showInfo
1017 $this->_setMinMax($this->_tables[$foreignTable]);
1019 $this->relations[] = new Relation_Stats(
1020 $this->_tables[$masterTable], $masterField,
1021 $this->_tables[$foreignTable], $foreignField
1026 * Draws the grid
1028 * @global object the current PMA_Schema_PDF instance
1030 * @access private
1032 * @return void
1034 * @see PMA_Schema_PDF
1036 private function _strokeGrid()
1038 global $pdf;
1040 $gridSize = 10;
1041 $labelHeight = 4;
1042 $labelWidth = 5;
1043 if ($this->withDoc) {
1044 $topSpace = 6;
1045 $bottomSpace = 15;
1046 } else {
1047 $topSpace = 0;
1048 $bottomSpace = 0;
1051 $pdf->SetMargins(0, 0);
1052 $pdf->SetDrawColor(200, 200, 200);
1053 // Draws horizontal lines
1054 for ($l = 0; $l <= intval(($pdf->getPageHeight() - $topSpace - $bottomSpace) / $gridSize); $l++) {
1055 $pdf->line(
1056 0, $l * $gridSize + $topSpace,
1057 $pdf->getPageWidth(), $l * $gridSize + $topSpace
1059 // Avoid duplicates
1060 if ($l > 0
1061 && $l <= intval(($pdf->getPageHeight() - $topSpace - $bottomSpace - $labelHeight) / $gridSize)
1063 $pdf->SetXY(0, $l * $gridSize + $topSpace);
1064 $label = (string) sprintf(
1065 '%.0f',
1066 ($l * $gridSize + $topSpace - $this->_topMargin)
1067 * $this->_scale + $this->_yMin
1069 $pdf->Cell($labelWidth, $labelHeight, ' ' . $label);
1070 } // end if
1071 } // end for
1072 // Draws vertical lines
1073 for ($j = 0; $j <= intval($pdf->getPageWidth() / $gridSize); $j++) {
1074 $pdf->line(
1075 $j * $gridSize,
1076 $topSpace,
1077 $j * $gridSize,
1078 $pdf->getPageHeight() - $bottomSpace
1080 $pdf->SetXY($j * $gridSize, $topSpace);
1081 $label = (string) sprintf(
1082 '%.0f',
1083 ($j * $gridSize - $this->_leftMargin) * $this->_scale + $this->_xMin
1085 $pdf->Cell($labelWidth, $labelHeight, $label);
1090 * Draws relation arrows
1092 * @param boolean $changeColor Whether to use one color per relation or not
1094 * @access private
1096 * @return void
1098 * @see Relation_Stats::relationdraw()
1100 private function _drawRelations($changeColor)
1102 $i = 0;
1103 foreach ($this->relations as $relation) {
1104 $relation->relationDraw($changeColor, $i);
1105 $i++;
1110 * Draws tables
1112 * @param boolean $changeColor Whether to display table position or not
1114 * @access private
1116 * @return void
1118 * @see Table_Stats::tableDraw()
1120 private function _drawTables($changeColor = 0)
1122 foreach ($this->_tables as $table) {
1123 $table->tableDraw(null, $this->withDoc, $changeColor);
1128 * Ouputs the PDF document to a file
1129 * or sends the output to browser
1131 * @param integer $pageNumber page number
1133 * @global object The current PDF document
1134 * @global string The current database name
1135 * @global integer The current page number (from the
1136 * $cfg['Servers'][$i]['table_coords'] table)
1137 * @access private
1139 * @return void
1141 * @see PMA_Schema_PDF
1143 private function _showOutput($pageNumber)
1145 global $pdf, $cfgRelation;
1147 // Get the name of this pdfpage to use as filename
1148 $_name_sql = 'SELECT page_descr FROM '
1149 . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.'
1150 . PMA_Util::backquote($cfgRelation['pdf_pages'])
1151 . ' WHERE page_nr = ' . $pageNumber;
1152 $_name_rs = PMA_queryAsControlUser($_name_sql);
1153 if ($_name_rs) {
1154 $_name_row = PMA_DBI_fetch_row($_name_rs);
1155 $filename = $_name_row[0] . '.pdf';
1157 if (empty($filename)) {
1158 $filename = $pageNumber . '.pdf';
1160 $pdf->Download($filename);
1164 * Generates data dictionary pages.
1166 * @param bool $alltables Tables to document.
1168 * @return void
1170 public function dataDictionaryDoc($alltables)
1172 global $db, $pdf, $orientation, $paper;
1173 // TOC
1174 $pdf->addpage($_POST['orientation']);
1175 $pdf->Cell(0, 9, __('Table of contents'), 1, 0, 'C');
1176 $pdf->Ln(15);
1177 $i = 1;
1178 foreach ($alltables as $table) {
1179 $pdf->PMA_links['doc'][$table]['-'] = $pdf->AddLink();
1180 $pdf->SetX(10);
1181 // $pdf->Ln(1);
1182 $pdf->Cell(
1183 0, 6, __('Page number:') . ' {' . sprintf("%02d", $i) . '}', 0, 0,
1184 'R', 0, $pdf->PMA_links['doc'][$table]['-']
1186 $pdf->SetX(10);
1187 $pdf->Cell(
1188 0, 6, $i . ' ' . $table, 0, 1,
1189 'L', 0, $pdf->PMA_links['doc'][$table]['-']
1191 // $pdf->Ln(1);
1192 $fields = PMA_DBI_get_columns($GLOBALS['db'], $table);
1193 foreach ($fields as $row) {
1194 $pdf->SetX(20);
1195 $field_name = $row['Field'];
1196 $pdf->PMA_links['doc'][$table][$field_name] = $pdf->AddLink();
1197 //$pdf->Cell(
1198 // 0, 6, $field_name, 0, 1,
1199 // 'L', 0, $pdf->PMA_links['doc'][$table][$field_name]
1200 //);
1202 $i++;
1204 $pdf->PMA_links['RT']['-'] = $pdf->AddLink();
1205 $pdf->SetX(10);
1206 $pdf->Cell(
1207 0, 6, __('Page number:') . ' {00}', 0, 0,
1208 'R', 0, $pdf->PMA_links['RT']['-']
1210 $pdf->SetX(10);
1211 $pdf->Cell(
1212 0, 6, $i . ' ' . __('Relational schema'), 0, 1,
1213 'L', 0, $pdf->PMA_links['RT']['-']
1215 $z = 0;
1216 foreach ($alltables as $table) {
1217 $z++;
1218 $pdf->SetAutoPageBreak(true, 15);
1219 $pdf->addpage($_POST['orientation']);
1220 $pdf->Bookmark($table);
1221 $pdf->SetAlias('{' . sprintf("%02d", $z) . '}', $pdf->PageNo());
1222 $pdf->PMA_links['RT'][$table]['-'] = $pdf->AddLink();
1223 $pdf->SetLink($pdf->PMA_links['doc'][$table]['-'], -1);
1224 $pdf->SetFont($this->_ff, 'B', 18);
1225 $pdf->Cell(
1226 0, 8, $z . ' ' . $table, 1, 1,
1227 'C', 0, $pdf->PMA_links['RT'][$table]['-']
1229 $pdf->SetFont($this->_ff, '', 8);
1230 $pdf->ln();
1232 $cfgRelation = PMA_getRelationsParam();
1233 $comments = PMA_getComments($db, $table);
1234 if ($cfgRelation['mimework']) {
1235 $mime_map = PMA_getMIME($db, $table, true);
1239 * Gets table informations
1241 $showtable = PMA_Table::sGetStatusInfo($db, $table);
1242 $show_comment = isset($showtable['Comment'])
1243 ? $showtable['Comment']
1244 : '';
1245 $create_time = isset($showtable['Create_time'])
1246 ? PMA_Util::localisedDate(
1247 strtotime($showtable['Create_time'])
1249 : '';
1250 $update_time = isset($showtable['Update_time'])
1251 ? PMA_Util::localisedDate(
1252 strtotime($showtable['Update_time'])
1254 : '';
1255 $check_time = isset($showtable['Check_time'])
1256 ? PMA_Util::localisedDate(
1257 strtotime($showtable['Check_time'])
1259 : '';
1262 * Gets table keys and retains them
1264 $result = PMA_DBI_query(
1265 'SHOW KEYS FROM ' . PMA_Util::backquote($table) . ';'
1267 $primary = '';
1268 $indexes = array();
1269 $lastIndex = '';
1270 $indexes_info = array();
1271 $indexes_data = array();
1272 $pk_array = array(); // will be use to emphasis prim. keys in the table
1273 // view
1274 while ($row = PMA_DBI_fetch_assoc($result)) {
1275 // Backups the list of primary keys
1276 if ($row['Key_name'] == 'PRIMARY') {
1277 $primary .= $row['Column_name'] . ', ';
1278 $pk_array[$row['Column_name']] = 1;
1280 // Retains keys informations
1281 if ($row['Key_name'] != $lastIndex) {
1282 $indexes[] = $row['Key_name'];
1283 $lastIndex = $row['Key_name'];
1285 $indexes_info[$row['Key_name']]['Sequences'][]
1286 = $row['Seq_in_index'];
1287 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
1288 if (isset($row['Cardinality'])) {
1289 $indexes_info[$row['Key_name']]['Cardinality']
1290 = $row['Cardinality'];
1292 // I don't know what does following column mean....
1293 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
1294 $indexes_info[$row['Key_name']]['Comment'] = $row['Comment'];
1296 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name']
1297 = $row['Column_name'];
1298 if (isset($row['Sub_part'])) {
1299 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part']
1300 = $row['Sub_part'];
1302 } // end while
1303 if ($result) {
1304 PMA_DBI_free_result($result);
1308 * Gets fields properties
1310 $columns = PMA_DBI_get_columns($db, $table);
1311 // Check if we can use Relations
1312 if (!empty($cfgRelation['relation'])) {
1313 // Find which tables are related with the current one and write it in
1314 // an array
1315 $res_rel = PMA_getForeigners($db, $table);
1317 if (count($res_rel) > 0) {
1318 $have_rel = true;
1319 } else {
1320 $have_rel = false;
1322 } else {
1323 $have_rel = false;
1324 } // end if
1327 * Displays the comments of the table if MySQL >= 3.23
1330 $break = false;
1331 if (! empty($show_comment)) {
1332 $pdf->Cell(0, 3, __('Table comments') . ' : ' . $show_comment, 0, 1);
1333 $break = true;
1336 if (! empty($create_time)) {
1337 $pdf->Cell(0, 3, __('Creation') . ': ' . $create_time, 0, 1);
1338 $break = true;
1341 if (! empty($update_time)) {
1342 $pdf->Cell(0, 3, __('Last update') . ': ' . $update_time, 0, 1);
1343 $break = true;
1346 if (! empty($check_time)) {
1347 $pdf->Cell(0, 3, __('Last check') . ': ' . $check_time, 0, 1);
1348 $break = true;
1351 if ($break == true) {
1352 $pdf->Cell(0, 3, '', 0, 1);
1353 $pdf->Ln();
1356 $pdf->SetFont($this->_ff, 'B');
1357 if (isset($orientation) && $orientation == 'L') {
1358 $pdf->Cell(25, 8, __('Column'), 1, 0, 'C');
1359 $pdf->Cell(20, 8, __('Type'), 1, 0, 'C');
1360 $pdf->Cell(20, 8, __('Attributes'), 1, 0, 'C');
1361 $pdf->Cell(10, 8, __('Null'), 1, 0, 'C');
1362 $pdf->Cell(20, 8, __('Default'), 1, 0, 'C');
1363 $pdf->Cell(25, 8, __('Extra'), 1, 0, 'C');
1364 $pdf->Cell(45, 8, __('Links to'), 1, 0, 'C');
1366 if ($paper == 'A4') {
1367 $comments_width = 67;
1368 } else {
1369 // this is really intended for 'letter'
1371 * @todo find optimal width for all formats
1373 $comments_width = 50;
1375 $pdf->Cell($comments_width, 8, __('Comments'), 1, 0, 'C');
1376 $pdf->Cell(45, 8, 'MIME', 1, 1, 'C');
1377 $pdf->SetWidths(
1378 array(25, 20, 20, 10, 20, 25, 45, $comments_width, 45)
1380 } else {
1381 $pdf->Cell(20, 8, __('Column'), 1, 0, 'C');
1382 $pdf->Cell(20, 8, __('Type'), 1, 0, 'C');
1383 $pdf->Cell(20, 8, __('Attributes'), 1, 0, 'C');
1384 $pdf->Cell(10, 8, __('Null'), 1, 0, 'C');
1385 $pdf->Cell(15, 8, __('Default'), 1, 0, 'C');
1386 $pdf->Cell(15, 8, __('Extra'), 1, 0, 'C');
1387 $pdf->Cell(30, 8, __('Links to'), 1, 0, 'C');
1388 $pdf->Cell(30, 8, __('Comments'), 1, 0, 'C');
1389 $pdf->Cell(30, 8, 'MIME', 1, 1, 'C');
1390 $pdf->SetWidths(array(20, 20, 20, 10, 15, 15, 30, 30, 30));
1392 $pdf->SetFont($this->_ff, '');
1394 foreach ($columns as $row) {
1395 $extracted_columnspec
1396 = PMA_Util::extractColumnSpec($row['Type']);
1397 $type = $extracted_columnspec['print_type'];
1398 $attribute = $extracted_columnspec['attribute'];
1399 if (! isset($row['Default'])) {
1400 if ($row['Null'] != '' && $row['Null'] != 'NO') {
1401 $row['Default'] = 'NULL';
1404 $field_name = $row['Field'];
1405 // $pdf->Ln();
1406 $pdf->PMA_links['RT'][$table][$field_name] = $pdf->AddLink();
1407 $pdf->Bookmark($field_name, 1, -1);
1408 $pdf->SetLink($pdf->PMA_links['doc'][$table][$field_name], -1);
1409 $pdf_row = array(
1410 $field_name,
1411 $type,
1412 $attribute,
1413 ($row['Null'] == '' || $row['Null'] == 'NO')
1414 ? __('No')
1415 : __('Yes'),
1416 (isset($row['Default']) ? $row['Default'] : ''),
1417 $row['Extra'],
1418 (isset($res_rel[$field_name])
1419 ? $res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field']
1420 : ''),
1421 (isset($comments[$field_name])
1422 ? $comments[$field_name]
1423 : ''),
1424 (isset($mime_map) && isset($mime_map[$field_name])
1425 ? str_replace('_', '/', $mime_map[$field_name]['mimetype'])
1426 : '')
1428 $links[0] = $pdf->PMA_links['RT'][$table][$field_name];
1429 if (isset($res_rel[$field_name]['foreign_table'])
1430 && isset($res_rel[$field_name]['foreign_field'])
1431 && isset($pdf->PMA_links['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']])
1433 $links[6] = $pdf->PMA_links['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']];
1434 } else {
1435 unset($links[6]);
1437 $pdf->Row($pdf_row, $links);
1438 } // end foreach
1439 $pdf->SetFont($this->_ff, '', 14);
1440 } //end each