2 /* vim: set expandtab sw=4 ts=4 sts=4: */
8 include_once("Export_Relation_Schema.class.php");
13 * @todo Make this configuratble (at least Sans/Serif).
15 define('PMA_PDF_FONT', 'DejaVuSans');
16 require_once './libraries/tcpdf/tcpdf.php';
19 * Extends the "TCPDF" class and helps
20 * in developing the structure of PDF Schema Export
28 class PMA_PDF
extends TCPDF
39 var $Outlines = array();
44 public function getFh()
49 public function getFw()
54 public function setCMargin($c_margin)
56 $this->cMargin
= $c_margin;
59 function SetAlias($name, $value)
61 $this->Alias
[$name] = $value ;
66 if (count($this->Alias
) > 0) {
68 foreach ($this->Alias
as $alias => $value) {
69 for ($n = 1;$n <= $nb;$n++
)
70 $this->pages
[$n]=str_replace($alias, $value, $this->pages
[$n]);
76 // added because tcpdf for PHP 5 has a protected $buffer
77 public function getBuffer()
82 public function getState()
88 * Sets the scaling factor, defines minimum coordinates and margins
90 * @param float scale The scaling factor
91 * @param float _xMin The minimum X coordinate
92 * @param float _yMin The minimum Y coordinate
93 * @param float leftMargin The left margin
94 * @param float topMargin The top margin
97 function PMA_PDF_setScale($scale = 1, $xMin = 0, $yMin = 0, $leftMargin = -1, $topMargin = -1)
99 $this->scale
= $scale;
100 $this->_xMin
= $xMin;
101 $this->_yMin
= $yMin;
102 if ($this->leftMargin
!= -1) {
103 $this->leftMargin
= $leftMargin;
105 if ($this->topMargin
!= -1) {
106 $this->topMargin
= $topMargin;
111 * Outputs a scaled cell
113 * @param float w The cell width
114 * @param float h The cell height
115 * @param string txt The text to output
116 * @param mixed border Whether to add borders or not
117 * @param integer ln Where to put the cursor once the output is done
118 * @param string align Align mode
119 * @param integer fill Whether to fill the cell with a color or not
123 function PMA_PDF_cellScale($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $fill = 0, $link = '')
125 $h = $h / $this->scale
;
126 $w = $w / $this->scale
;
127 $this->Cell($w, $h, $txt, $border, $ln, $align, $fill, $link);
131 * Draws a scaled line
133 * @param float x1 The horizontal position of the starting point
134 * @param float y1 The vertical position of the starting point
135 * @param float x2 The horizontal position of the ending point
136 * @param float y2 The vertical position of the ending point
140 function PMA_PDF_lineScale($x1, $y1, $x2, $y2)
142 $x1 = ($x1 - $this->_xMin
) / $this->scale +
$this->leftMargin
;
143 $y1 = ($y1 - $this->_yMin
) / $this->scale +
$this->topMargin
;
144 $x2 = ($x2 - $this->_xMin
) / $this->scale +
$this->leftMargin
;
145 $y2 = ($y2 - $this->_yMin
) / $this->scale +
$this->topMargin
;
146 $this->Line($x1, $y1, $x2, $y2);
150 * Sets x and y scaled positions
152 * @param float x The x position
153 * @param float y The y position
155 * @see TCPDF::SetXY()
157 function PMA_PDF_setXyScale($x, $y)
159 $x = ($x - $this->_xMin
) / $this->scale +
$this->leftMargin
;
160 $y = ($y - $this->_yMin
) / $this->scale +
$this->topMargin
;
161 $this->SetXY($x, $y);
165 * Sets the X scaled positions
167 * @param float x The x position
171 function PMA_PDF_setXScale($x)
173 $x = ($x - $this->_xMin
) / $this->scale +
$this->leftMargin
;
178 * Sets the scaled font size
180 * @param float size The font size (in points)
182 * @see TCPDF::SetFontSize()
184 function PMA_PDF_setFontSizeScale($size)
186 // Set font size in points
187 $size = $size / $this->scale
;
188 $this->SetFontSize($size);
192 * Sets the scaled line width
194 * @param float width The line width
196 * @see TCPDF::SetLineWidth()
198 function PMA_PDF_setLineWidthScale($width)
200 $width = $width / $this->scale
;
201 $this->SetLineWidth($width);
205 * Displays an error message
207 * @param string error_message the error mesage
209 * @see PMA_Export_Relation_Schema::dieSchema
211 function Error($error_message = '')
213 PMA_Export_Relation_Schema
::dieSchema($error_message);
218 // We only show this if we find something in the new pdf_pages table
220 // This function must be named "Header" to work with the TCPDF library
221 global $cfgRelation, $db, $pdf_page_number, $with_doc;
223 $test_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
224 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
225 . ' AND page_nr = \'' . $pdf_page_number . '\'';
226 $test_rs = PMA_query_as_controluser($test_query);
227 $pages = @PMA_DBI_fetch_assoc
($test_rs);
228 $this->SetFont('', 'B', 14);
229 $this->Cell(0, 6, ucfirst($pages['page_descr']), 'B', 1, 'C');
230 $this->SetFont('', '');
237 // This function must be named "Footer" to work with the TCPDF library
241 $this->SetFont('', '', 14);
242 $this->Cell(0, 6, __('Page number:') . ' ' . $this->PageNo() . '/{nb}', 'T', 0, 'C');
243 $this->Cell(0, 6, PMA_localisedDate(), 0, 1, 'R');
248 function Bookmark($txt, $level = 0, $y = 0)
251 $this->Outlines
[0][] = $level;
252 $this->Outlines
[1][] = $txt;
253 $this->Outlines
[2][] = $this->page
;
257 $this->Outlines
[3][] = round($this->hPt
- $y * $this->k
, 2);
260 function _putbookmarks()
262 if (count($this->Outlines
) > 0) {
263 // Save object number
265 // Take the number of sub elements for an outline
266 $nb_outlines = sizeof($this->Outlines
[0]);
267 $first_level = array();
270 for ($i = 0; $i < $nb_outlines; $i++
) {
271 $level = $this->Outlines
[0][$i];
278 // Take the previous outline in the same level
279 while ($this->Outlines
[0][$cursor] > $level && $cursor > 0)
281 if ($this->Outlines
[0][$cursor] == $level) {
285 if ($i < $nb_outlines-1) {
287 while (isset($this->Outlines
[0][$cursor]) && $this->Outlines
[0][$cursor] > $level) {
288 // Take the immediate kid in level + 1
289 if ($this->Outlines
[0][$cursor] == $level +
1) {
296 // Take the next outline in the same level
297 while ($this->Outlines
[0][$cursor] > $level && ($cursor +
1 < sizeof($this->Outlines
[0])))
299 if ($this->Outlines
[0][$cursor] == $level) {
304 $parent[$level +
1] = $this->n
;
306 $first_level[] = $this->n
;
309 $this->_out('/Title (' . $this->Outlines
[1][$i] . ')');
310 $this->_out('/Parent ' . $parent[$level] . ' 0 R');
312 $this->_out('/Prev ' . ($memo_n +
$prev +
1) . ' 0 R');
315 $this->_out('/Next ' . ($this->n +
$next - $i) . ' 0 R');
317 $this->_out('/Dest [' . (1 +
(2 * $this->Outlines
[2][$i])) . ' 0 R /XYZ null ' . $this->Outlines
[3][$i] . ' null]');
319 $this->_out('/First ' . ($this->n +
1) . ' 0 R');
320 $this->_out('/Last ' . ($this->n +
$last - $i) . ' 0 R');
321 $this->_out('/Count -' . $kids);
324 $this->_out('endobj');
326 // First page of outlines
328 $this->def_outlines
= $this->n
;
330 $this->_out('/Type');
331 $this->_out('/Outlines');
332 $this->_out('/First ' . $first_level[0] . ' 0 R');
333 $this->_out('/Last ' . $first_level[sizeof($first_level)-1] . ' 0 R');
334 $this->_out('/Count ' . sizeof($first_level));
336 $this->_out('endobj');
340 function _putresources()
342 parent
::_putresources();
343 $this->_putbookmarks();
346 function _putcatalog()
348 parent
::_putcatalog();
349 if (count($this->Outlines
) > 0) {
350 $this->_out('/Outlines ' . $this->def_outlines
. ' 0 R');
351 $this->_out('/PageMode /UseOutlines');
355 function SetWidths($w)
361 function Row($data, $links)
365 $data_cnt = count($data);
366 for ($i = 0;$i < $data_cnt;$i++
)
367 $nb = max($nb, $this->NbLines($this->widths
[$i], $data[$i]));
368 $il = $this->FontSize
;
369 $h = ($il +
1) * $nb;
370 // page break if necessary
371 $this->CheckPageBreak($h);
373 $data_cnt = count($data);
374 for ($i = 0;$i < $data_cnt;$i++
) {
375 $w = $this->widths
[$i];
376 // save current position
380 $this->Rect($x, $y, $w, $h);
381 if (isset($links[$i])) {
382 $this->Link($x, $y, $w, $h, $links[$i]);
385 $this->MultiCell($w, $il +
1, $data[$i], 0, 'L');
387 $this->SetXY($x +
$w, $y);
393 function CheckPageBreak($h)
395 // if height h overflows, manual page break
396 if ($this->GetY() +
$h > $this->PageBreakTrigger
) {
397 $this->AddPage($this->CurOrientation
);
401 function NbLines($w, $txt)
403 // compute number of lines used by a multicell of width w
404 $cw = &$this->CurrentFont
['cw'];
406 $w = $this->w
- $this->rMargin
- $this->x
;
408 $wmax = ($w-2 * $this->cMargin
) * 1000 / $this->FontSize
;
409 $s = str_replace("\r", '', $txt);
411 if ($nb > 0 and $s[$nb-1] == "\n") {
432 $l +
= isset($cw[ord($c)])?
$cw[ord($c)]:0 ;
454 * Table preferences/statistics
456 * This class preserves the table co-ordinates,fields
457 * and helps in drawing/generating the Tables in PDF document.
470 private $_showInfo = false;
475 public $fields = array();
476 public $heightCell = 6;
478 public $primary = array();
481 * The "Table_Stats" constructor
483 * @param string table_name The table name
484 * @param integer fontSize The font size
485 * @param integer pageNumber The current page number (from the
486 * $cfg['Servers'][$i]['table_coords'] table)
487 * @param integer sameWideWidth The max. with among tables
488 * @param boolean showKeys Whether to display keys or not
489 * @param boolean showInfo Whether to display table position or not
490 * @global object The current PDF document
491 * @global array The relations settings
492 * @global string The current db name
493 * @see PMA_PDF, Table_Stats::Table_Stats_setWidth,
494 Table_Stats::Table_Stats_setHeight
496 function __construct($tableName, $fontSize, $pageNumber, &$sameWideWidth, $showKeys = false, $showInfo = false)
498 global $pdf, $cfgRelation, $db;
500 $this->_tableName
= $tableName;
501 $sql = 'DESCRIBE ' . PMA_backquote($tableName);
502 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE
);
503 if (!$result ||
!PMA_DBI_num_rows($result)) {
504 $pdf->Error(sprintf(__('The %s table doesn\'t exist!'), $tableName));
507 //check to see if it will load all fields or only the foreign keys
509 $indexes = PMA_Index
::getFromTable($this->_tableName
, $db);
510 $all_columns = array();
511 foreach ($indexes as $index) {
512 $all_columns = array_merge($all_columns, array_flip(array_keys($index->getColumns())));
514 $this->fields
= array_keys($all_columns);
516 while ($row = PMA_DBI_fetch_row($result)) {
517 $this->fields
[] = $row[0];
521 $this->_showInfo
= $showInfo;
524 * setWidth must me after setHeight, because title
525 * can include table height which changes table width
527 $this->_setWidth($fontSize);
528 if ($sameWideWidth < $this->width
) {
529 $sameWideWidth = $this->width
;
531 $sql = 'SELECT x, y FROM '
532 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
533 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
534 . ' AND table_name = \'' . PMA_sqlAddslashes($tableName) . '\''
535 . ' AND pdf_page_number = ' . $pageNumber;
536 $result = PMA_query_as_controluser($sql, false, PMA_DBI_QUERY_STORE
);
537 if (!$result ||
!PMA_DBI_num_rows($result)) {
538 $pdf->Error(sprintf(__('Please configure the coordinates for table %s'), $tableName));
540 list($this->x
, $this->y
) = PMA_DBI_fetch_row($result);
541 $this->x
= (double) $this->x
;
542 $this->y
= (double) $this->y
;
546 $this->displayfield
= PMA_getDisplayField($db, $tableName);
550 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tableName) . ';', null, PMA_DBI_QUERY_STORE
);
551 if (PMA_DBI_num_rows($result) > 0) {
552 while ($row = PMA_DBI_fetch_assoc($result)) {
553 if ($row['Key_name'] == 'PRIMARY') {
554 $this->primary
[] = $row['Column_name'];
561 * Returns title of the current table,
562 * title can have the dimensions of the table
566 private function _getTitle()
568 return ($this->_showInfo ?
sprintf('%.0f', $this->width
) . 'x' . sprintf('%.0f', $this->height
) : '') . ' ' . $this->_tableName
;
572 * Sets the width of the table
574 * @param integer fontSize The font size
575 * @global object The current PDF document
579 function _setWidth($fontSize)
583 foreach ($this->fields
as $field) {
584 $this->width
= max($this->width
, $pdf->GetStringWidth($field));
586 $this->width +
= $pdf->GetStringWidth(' ');
587 $pdf->SetFont($fontSize, 'B');
589 * it is unknown what value must be added, because
590 * table title is affected by the tabe width value
592 while ($this->width
< $pdf->GetStringWidth($this->_getTitle())) {
595 $pdf->SetFont($fontSize, '');
599 * Sets the height of the table
603 private function _setHeight()
605 $this->height
= (count($this->fields
) +
1) * $this->heightCell
;
611 * @param integer fontSize The font size
612 * @param boolean setColor Whether to display color
613 * @global object The current PDF document
617 public function tableDraw($fontSize, $withDoc, $setColor = 0)
619 global $pdf, $withDoc;
621 $pdf->PMA_PDF_setXyScale($this->x
, $this->y
);
622 $pdf->SetFont($fontSize, 'B');
624 $pdf->SetTextColor(200);
625 $pdf->SetFillColor(0, 0, 128);
628 $pdf->SetLink($pdf->PMA_links
['RT'][$this->_tableName
]['-'], -1);
630 $pdf->PMA_links
['doc'][$this->_tableName
]['-'] = '';
633 $pdf->PMA_PDF_cellScale($this->width
, $this->heightCell
, $this->_getTitle(), 1, 1, 'C', $setColor, $pdf->PMA_links
['doc'][$this->_tableName
]['-']);
634 $pdf->PMA_PDF_setXScale($this->x
);
635 $pdf->SetFont($fontSize, '');
636 $pdf->SetTextColor(0);
637 $pdf->SetFillColor(255);
639 foreach ($this->fields
as $field) {
641 if (in_array($field, $this->primary
)) {
642 $pdf->SetFillColor(215, 121, 123);
644 if ($field == $this->displayfield
) {
645 $pdf->SetFillColor(142, 159, 224);
649 $pdf->SetLink($pdf->PMA_links
['RT'][$this->_tableName
][$field], -1);
651 $pdf->PMA_links
['doc'][$this->_tableName
][$field] = '';
654 $pdf->PMA_PDF_cellScale($this->width
, $this->heightCell
, ' ' . $field, 1, 1, 'L', $setColor, $pdf->PMA_links
['doc'][$this->_tableName
][$field]);
655 $pdf->PMA_PDF_setXScale($this->x
);
656 $pdf->SetFillColor(255);
658 /*if ($pdf->PageNo() > 1) {
659 $pdf->PMA_PDF_die(__('The scale factor is too small to fit the schema on one page'));
665 * Relation preferences/statistics
667 * This class fetches the table master and foreign fields positions
668 * and helps in generating the Table references and then connects
669 * master table's master field to foreign table's foreign key
672 * @name Relation_Stats
675 * @see PMA_PDF::SetDrawColor,PMA_PDF::PMA_PDF_setLineWidthScale,PMA_PDF::PMA_PDF_lineScale
685 public $xDest, $yDest;
689 * The "Relation_Stats" constructor
691 * @param string master_table The master table name
692 * @param string master_field The relation field in the master table
693 * @param string foreign_table The foreign table name
694 * @param string foreigh_field The relation field in the foreign table
695 * @see Relation_Stats::_getXy
697 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
699 $src_pos = $this->_getXy($master_table, $master_field);
700 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
706 $src_left = $src_pos[0] - $this->wTick
;
707 $src_right = $src_pos[1] +
$this->wTick
;
708 $dest_left = $dest_pos[0] - $this->wTick
;
709 $dest_right = $dest_pos[1] +
$this->wTick
;
711 $d1 = abs($src_left - $dest_left);
712 $d2 = abs($src_right - $dest_left);
713 $d3 = abs($src_left - $dest_right);
714 $d4 = abs($src_right - $dest_right);
715 $d = min($d1, $d2, $d3, $d4);
718 $this->xSrc
= $src_pos[0];
720 $this->xDest
= $dest_pos[0];
722 } elseif ($d == $d2) {
723 $this->xSrc
= $src_pos[1];
725 $this->xDest
= $dest_pos[0];
727 } elseif ($d == $d3) {
728 $this->xSrc
= $src_pos[0];
730 $this->xDest
= $dest_pos[1];
733 $this->xSrc
= $src_pos[1];
735 $this->xDest
= $dest_pos[1];
738 $this->ySrc
= $src_pos[2];
739 $this->yDest
= $dest_pos[2];
743 * Gets arrows coordinates
745 * @param string table The current table name
746 * @param string column The relation column name
747 * @return array Arrows coordinates
750 private function _getXy($table, $column)
752 $pos = array_search($column, $table->fields
);
753 // x_left, x_right, y
754 return array($table->x
, $table->x + +
$table->width
, $table->y +
($pos +
1.5) * $table->heightCell
);
758 * draws relation links and arrows
759 * shows foreign key relations
761 * @param boolean changeColor Whether to use one color per relation or not
762 * @param integer i The id of the link to draw
763 * @global object The current PDF document
767 public function relationDraw($changeColor, $i)
784 list ($a, $b, $c) = $case[$d];
785 $e = (1 - ($j - 1) / 6);
786 $pdf->SetDrawColor($a * 255 * $e, $b * 255 * $e, $c * 255 * $e);
788 $pdf->SetDrawColor(0);
790 $pdf->PMA_PDF_setLineWidthScale(0.2);
791 $pdf->PMA_PDF_lineScale($this->xSrc
, $this->ySrc
, $this->xSrc +
$this->srcDir
* $this->wTick
, $this->ySrc
);
792 $pdf->PMA_PDF_lineScale($this->xDest +
$this->destDir
* $this->wTick
, $this->yDest
, $this->xDest
, $this->yDest
);
793 $pdf->PMA_PDF_setLineWidthScale(0.1);
794 $pdf->PMA_PDF_lineScale($this->xSrc +
$this->srcDir
* $this->wTick
, $this->ySrc
, $this->xDest +
$this->destDir
* $this->wTick
, $this->yDest
);
798 $root2 = 2 * sqrt(2);
799 $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);
800 $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);
802 $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);
803 $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);
804 $pdf->SetDrawColor(0);
809 * Pdf Relation Schema Class
811 * Purpose of this class is to generate the PDF Document. PDF is widely
812 * used format for documenting text,fonts,images and 3d vector graphics.
814 * This class inherits Export_Relation_Schema class has common functionality added
817 * @name Pdf_Relation_Schema
821 class PMA_Pdf_Relation_Schema
extends PMA_Export_Relation_Schema
826 private $_tables = array();
827 private $_relations = array();
828 private $_ff = PMA_PDF_FONT
;
832 private $_xMin = 100000;
833 private $_yMin = 100000;
834 private $topMargin = 10;
835 private $bottomMargin = 10;
836 private $leftMargin = 10;
837 private $rightMargin = 10;
838 private $_tablewidth;
841 * The "PMA_Pdf_Relation_Schema" constructor
843 * @global object The current PDF Schema document
844 * @global string The current db name
845 * @global array The relations settings
849 function __construct()
851 global $pdf,$db,$cfgRelation;
853 $this->setPageNumber($_POST['pdf_page_number']);
854 $this->setShowGrid(isset($_POST['show_grid']));
855 $this->setShowColor(isset($_POST['show_color']));
856 $this->setShowKeys(isset($_POST['show_keys']));
857 $this->setTableDimension(isset($_POST['show_table_dimension']));
858 $this->setAllTableSameWidth(isset($_POST['all_table_same_wide']));
859 $this->setWithDataDictionary($_POST['with_doc']);
860 $this->setOrientation($_POST['orientation']);
861 $this->setPaper($_POST['paper']);
862 $this->setExportType($_POST['export_type']);
864 // Initializes a new document
865 $pdf = new PMA_PDF('L', 'mm', $this->paper
);
866 $pdf->SetTitle(sprintf(__('Schema of the %s database - Page %s'), $GLOBALS['db'], $this->pageNumber
));
869 $pdf->SetAuthor('phpMyAdmin ' . PMA_VERSION
);
870 $pdf->AliasNbPages();
871 $pdf->AddFont('DejaVuSans', '', 'dejavusans.php');
872 $pdf->AddFont('DejaVuSans', 'B', 'dejavusansb.php');
873 $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php');
874 $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserifb.php');
875 $pdf->SetFont($this->_ff
, '', 14);
876 $pdf->SetAutoPageBreak('auto');
877 $alltables = $this->getAllTables($db,$this->pageNumber
);
879 if ($this->withDoc
) {
880 $pdf->SetAutoPageBreak('auto', 15);
882 $this->dataDictionaryDoc($alltables);
883 $pdf->SetAutoPageBreak('auto');
889 if ($this->withDoc
) {
890 $pdf->SetLink($pdf->PMA_links
['RT']['-'], -1);
891 $pdf->Bookmark(__('Relational schema'));
892 $pdf->SetAlias('{00}', $pdf->PageNo()) ;
893 $this->topMargin
= 28;
894 $this->bottomMargin
= 28;
898 foreach ($alltables as $table) {
899 if (!isset($this->tables
[$table])) {
900 $this->tables
[$table] = new Table_Stats($table, $this->_ff
, $this->pageNumber
, $this->_tablewidth
, $this->showKeys
, $this->tableDimension
);
902 if ($this->sameWide
) {
903 $this->tables
[$table]->width
= $this->_tablewidth
;
905 $this->_setMinMax($this->tables
[$table]);
908 // Defines the scale factor
911 ($this->_xMax
- $this->_xMin
) / ($pdf->getFh() - $this->rightMargin
- $this->leftMargin
),
912 ($this->_yMax
- $this->_yMin
) / ($pdf->getFw() - $this->topMargin
- $this->bottomMargin
))
915 $pdf->PMA_PDF_setScale($this->scale
, $this->_xMin
, $this->_yMin
, $this->leftMargin
, $this->topMargin
);
916 // Builds and save the PDF document
917 $pdf->PMA_PDF_setLineWidthScale(0.1);
919 if ($this->showGrid
) {
920 $pdf->SetFontSize(10);
921 $this->_strokeGrid();
923 $pdf->PMA_PDF_setFontSizeScale(14);
924 // previous logic was checking master tables and foreign tables
925 // but I think that looping on every table of the pdf page as a master
926 // and finding its foreigns is OK (then we can support innodb)
927 $seen_a_relation = false;
928 foreach ($alltables as $one_table) {
929 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
931 $seen_a_relation = true;
932 foreach ($exist_rel as $master_field => $rel) {
933 // put the foreign table on the schema only if selected
935 // (do not use array_search() because we would have to
936 // to do a === FALSE and this is not PHP3 compatible)
937 if (in_array($rel['foreign_table'], $alltables)) {
938 $this->_addRelation($one_table, $master_field, $rel['foreign_table'], $rel['foreign_field'], $this->tableDimension
);
944 if ($seen_a_relation) {
945 $this->_drawRelations($this->showColor
);
947 $this->_drawTables($this->showColor
);
948 $this->_showOutput($this->pageNumber
);
953 * Sets X and Y minimum and maximum for a table cell
955 * @param string table The table name of which sets XY co-ordinates
958 private function _setMinMax($table)
960 $this->_xMax
= max($this->_xMax
, $table->x +
$table->width
);
961 $this->_yMax
= max($this->_yMax
, $table->y +
$table->height
);
962 $this->_xMin
= min($this->_xMin
, $table->x
);
963 $this->_yMin
= min($this->_yMin
, $table->y
);
967 * Defines relation objects
969 * @param string master_table The master table name
970 * @param string master_field The relation field in the master table
971 * @param string foreign_table The foreign table name
972 * @param string foreign_field The relation field in the foreign table
973 * @param boolean show_info Whether to display table position or not
977 private function _addRelation($masterTable, $masterField, $foreignTable, $foreignField, $showInfo)
979 if (!isset($this->tables
[$masterTable])) {
980 $this->tables
[$masterTable] = new Table_Stats($masterTable, $this->_ff
, $this->pageNumber
, $this->_tablewidth
, false, $showInfo);
981 $this->_setMinMax($this->tables
[$masterTable]);
983 if (!isset($this->tables
[$foreignTable])) {
984 $this->tables
[$foreignTable] = new Table_Stats($foreignTable, $this->_ff
, $this->pageNumber
, $this->_tablewidth
, false, $showInfo);
985 $this->_setMinMax($this->tables
[$foreignTable]);
987 $this->relations
[] = new Relation_Stats($this->tables
[$masterTable], $masterField, $this->tables
[$foreignTable], $foreignField);
993 * @global object the current PMA_PDF instance
997 private function _strokeGrid()
1001 $pdf->SetMargins(0, 0);
1002 $pdf->SetDrawColor(200, 200, 200);
1003 // Draws horizontal lines
1004 for ($l = 0; $l < 21; $l++
) {
1005 $pdf->line(0, $l * 10, $pdf->getFh(), $l * 10);
1008 $pdf->SetXY(0, $l * 10);
1009 $label = (string) sprintf('%.0f', ($l * 10 - $this->topMargin
) * $this->scale +
$this->_yMin
);
1010 $pdf->Cell(5, 5, ' ' . $label);
1013 // Draws vertical lines
1014 for ($j = 0; $j < 30 ;$j++
) {
1015 $pdf->line($j * 10, 0, $j * 10, $pdf->getFw());
1016 $pdf->SetXY($j * 10, 0);
1017 $label = (string) sprintf('%.0f', ($j * 10 - $this->leftMargin
) * $this->scale +
$this->_xMin
);
1018 $pdf->Cell(5, 7, $label);
1023 * Draws relation arrows
1025 * @param boolean changeColor Whether to use one color per relation or not
1027 * @see Relation_Stats::relationdraw()
1029 private function _drawRelations($changeColor)
1032 foreach ($this->relations
as $relation) {
1033 $relation->relationDraw($changeColor, $i);
1041 * @param boolean changeColor Whether to display table position or not
1043 * @see Table_Stats::tableDraw()
1045 private function _drawTables($changeColor = 0)
1047 foreach ($this->tables
as $table) {
1048 $table->tableDraw($this->_ff
, $this->withDoc
, $changeColor);
1053 * Ouputs the PDF document to a file
1054 * or sends the output to browser
1056 * @global object The current PDF document
1057 * @global string The current database name
1058 * @global integer The current page number (from the
1059 * $cfg['Servers'][$i]['table_coords'] table)
1063 private function _showOutput($pageNumber)
1065 global $pdf, $db, $cfgRelation;
1067 $pdf->SetFontSize(14);
1068 $pdf->SetLineWidth(0.2);
1069 $pdf->SetDisplayMode('fullpage');
1070 // Get the name of this pdfpage to use as filename (Mike Beck)
1071 $_name_sql = 'SELECT page_descr FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
1072 . ' WHERE page_nr = ' . $pageNumber;
1073 $_name_rs = PMA_query_as_controluser($_name_sql);
1075 $_name_row = PMA_DBI_fetch_row($_name_rs);
1076 $filename = $_name_row[0] . '.pdf';
1078 if (empty($filename)) {
1079 $filename = $pageNumber . '.pdf';
1081 // instead of $pdf->Output():
1082 $pdfData = $pdf->getPDFData();
1083 header('Content-Type: application/pdf');
1084 header('Content-Length: '.strlen($pdfData).'');
1085 header('Content-disposition: attachment; filename="'.$filename.'"');
1089 public function dataDictionaryDoc($alltables)
1091 global $db, $pdf, $orientation, $paper;
1093 $pdf->addpage($GLOBALS['orientation']);
1094 $pdf->Cell(0, 9, __('Table of contents'), 1, 0, 'C');
1097 foreach ($alltables as $table) {
1098 $pdf->PMA_links
['doc'][$table]['-'] = $pdf->AddLink();
1101 $pdf->Cell(0, 6, __('Page number:') . ' {' . sprintf("%02d", $i +
1) . '}', 0, 0, 'R', 0, $pdf->PMA_links
['doc'][$table]['-']);
1103 $pdf->Cell(0, 6, $i . ' ' . $table, 0, 1, 'L', 0, $pdf->PMA_links
['doc'][$table]['-']);
1105 $result = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';');
1106 while ($row = PMA_DBI_fetch_assoc($result)) {
1108 $field_name = $row['Field'];
1109 $pdf->PMA_links
['doc'][$table][$field_name] = $pdf->AddLink();
1110 // $pdf->Cell(0, 6, $field_name,0,1,'L',0, $pdf->PMA_links['doc'][$table][$field_name]);
1112 $lasttable = $table;
1115 $pdf->PMA_links
['RT']['-'] = $pdf->AddLink();
1117 $pdf->Cell(0, 6, __('Page number:') . ' {' . sprintf("%02d", $i +
1) . '}', 0, 0, 'R', 0, $pdf->PMA_links
['doc'][$lasttable]['-']);
1119 $pdf->Cell(0, 6, $i +
1 . ' ' . __('Relational schema'), 0, 1, 'L', 0, $pdf->PMA_links
['RT']['-']);
1121 foreach ($alltables as $table) {
1123 $pdf->addpage($GLOBALS['orientation']);
1124 $pdf->Bookmark($table);
1125 $pdf->SetAlias('{' . sprintf("%02d", $z) . '}', $pdf->PageNo()) ;
1126 $pdf->PMA_links
['RT'][$table]['-'] = $pdf->AddLink();
1127 $pdf->SetLink($pdf->PMA_links
['doc'][$table]['-'], -1);
1128 $pdf->SetFont('', 'B', 18);
1129 $pdf->Cell(0, 8, $z . ' ' . $table, 1, 1, 'C', 0, $pdf->PMA_links
['RT'][$table]['-']);
1130 $pdf->SetFont('', '', 8);
1133 $cfgRelation = PMA_getRelationsParam();
1134 $comments = PMA_getComments($db, $table);
1135 if ($cfgRelation['mimework']) {
1136 $mime_map = PMA_getMIME($db, $table, true);
1140 * Gets table informations
1142 $showtable = PMA_Table
::sGetStatusInfo($db, $table);
1143 $num_rows = (isset($showtable['Rows']) ?
$showtable['Rows'] : 0);
1144 $show_comment = (isset($showtable['Comment']) ?
$showtable['Comment'] : '');
1145 $create_time = (isset($showtable['Create_time']) ?
PMA_localisedDate(strtotime($showtable['Create_time'])) : '');
1146 $update_time = (isset($showtable['Update_time']) ?
PMA_localisedDate(strtotime($showtable['Update_time'])) : '');
1147 $check_time = (isset($showtable['Check_time']) ?
PMA_localisedDate(strtotime($showtable['Check_time'])) : '');
1150 * Gets table keys and retains them
1152 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
1156 $indexes_info = array();
1157 $indexes_data = array();
1158 $pk_array = array(); // will be use to emphasis prim. keys in the table
1160 while ($row = PMA_DBI_fetch_assoc($result)) {
1161 // Backups the list of primary keys
1162 if ($row['Key_name'] == 'PRIMARY') {
1163 $primary .= $row['Column_name'] . ', ';
1164 $pk_array[$row['Column_name']] = 1;
1166 // Retains keys informations
1167 if ($row['Key_name'] != $lastIndex) {
1168 $indexes[] = $row['Key_name'];
1169 $lastIndex = $row['Key_name'];
1171 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
1172 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
1173 if (isset($row['Cardinality'])) {
1174 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
1176 // I don't know what does following column mean....
1177 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
1178 $indexes_info[$row['Key_name']]['Comment'] = $row['Comment'];
1180 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
1181 if (isset($row['Sub_part'])) {
1182 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
1186 PMA_DBI_free_result($result);
1190 * Gets fields properties
1192 $result = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE
);
1193 $fields_cnt = PMA_DBI_num_rows($result);
1194 // Check if we can use Relations (Mike Beck)
1195 if (!empty($cfgRelation['relation'])) {
1196 // Find which tables are related with the current one and write it in
1198 $res_rel = PMA_getForeigners($db, $table);
1200 if (count($res_rel) > 0) {
1210 * Displays the comments of the table if MySQL >= 3.23
1214 if (!empty($show_comment)) {
1215 $pdf->Cell(0, 3, __('Table comments') . ' : ' . $show_comment, 0, 1);
1219 if (!empty($create_time)) {
1220 $pdf->Cell(0, 3, __('Creation') . ': ' . $create_time, 0, 1);
1224 if (!empty($update_time)) {
1225 $pdf->Cell(0, 3, __('Last update') . ': ' . $update_time, 0, 1);
1229 if (!empty($check_time)) {
1230 $pdf->Cell(0, 3, __('Last check') . ': ' . $check_time, 0, 1);
1234 if ($break == true) {
1235 $pdf->Cell(0, 3, '', 0, 1);
1239 $pdf->SetFont('', 'B');
1240 if (isset($orientation) && $orientation == 'L') {
1241 $pdf->Cell(25, 8, ucfirst(__('Column')), 1, 0, 'C');
1242 $pdf->Cell(20, 8, ucfirst(__('Type')), 1, 0, 'C');
1243 $pdf->Cell(20, 8, ucfirst(__('Attributes')), 1, 0, 'C');
1244 $pdf->Cell(10, 8, ucfirst(__('Null')), 1, 0, 'C');
1245 $pdf->Cell(20, 8, ucfirst(__('Default')), 1, 0, 'C');
1246 $pdf->Cell(25, 8, ucfirst(__('Extra')), 1, 0, 'C');
1247 $pdf->Cell(45, 8, ucfirst(__('Links to')), 1, 0, 'C');
1249 if ($paper == 'A4') {
1250 $comments_width = 67;
1252 // this is really intended for 'letter'
1254 * @todo find optimal width for all formats
1256 $comments_width = 50;
1258 $pdf->Cell($comments_width, 8, ucfirst(__('Comments')), 1, 0, 'C');
1259 $pdf->Cell(45, 8, 'MIME', 1, 1, 'C');
1260 $pdf->SetWidths(array(25, 20, 20, 10, 20, 25, 45, $comments_width, 45));
1262 $pdf->Cell(20, 8, ucfirst(__('Column')), 1, 0, 'C');
1263 $pdf->Cell(20, 8, ucfirst(__('Type')), 1, 0, 'C');
1264 $pdf->Cell(20, 8, ucfirst(__('Attributes')), 1, 0, 'C');
1265 $pdf->Cell(10, 8, ucfirst(__('Null')), 1, 0, 'C');
1266 $pdf->Cell(15, 8, ucfirst(__('Default')), 1, 0, 'C');
1267 $pdf->Cell(15, 8, ucfirst(__('Extra')), 1, 0, 'C');
1268 $pdf->Cell(30, 8, ucfirst(__('Links to')), 1, 0, 'C');
1269 $pdf->Cell(30, 8, ucfirst(__('Comments')), 1, 0, 'C');
1270 $pdf->Cell(30, 8, 'MIME', 1, 1, 'C');
1271 $pdf->SetWidths(array(20, 20, 20, 10, 15, 15, 30, 30, 30));
1273 $pdf->SetFont('', '');
1275 while ($row = PMA_DBI_fetch_assoc($result)) {
1276 $type = $row['Type'];
1277 // reformat mysql query output
1278 // set or enum types: slashes single quotes inside options
1279 if (preg_match('@^(set|enum)\((.+)\)$@i', $type, $tmp)) {
1280 $tmp[2] = substr(preg_replace("@([^,])''@", "\\1\\'", ',' . $tmp[2]), 1);
1281 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
1288 $type_nowrap = ' nowrap="nowrap"';
1289 $type = preg_replace('@BINARY@i', '', $type);
1290 $type = preg_replace('@ZEROFILL@i', '', $type);
1291 $type = preg_replace('@UNSIGNED@i', '', $type);
1296 $binary = stristr($row['Type'], 'BINARY');
1297 $unsigned = stristr($row['Type'], 'UNSIGNED');
1298 $zerofill = stristr($row['Type'], 'ZEROFILL');
1302 $attribute = 'BINARY';
1305 $attribute = 'UNSIGNED';
1308 $attribute = 'UNSIGNED ZEROFILL';
1310 if (!isset($row['Default'])) {
1311 if ($row['Null'] != '' && $row['Null'] != 'NO') {
1312 $row['Default'] = 'NULL';
1315 $field_name = $row['Field'];
1317 $pdf->PMA_links
['RT'][$table][$field_name] = $pdf->AddLink();
1318 $pdf->Bookmark($field_name, 1, -1);
1319 $pdf->SetLink($pdf->PMA_links
['doc'][$table][$field_name], -1);
1320 $pdf_row = array($field_name,
1323 ($row['Null'] == '' ||
$row['Null'] == 'NO') ?
__('No') : __('Yes'),
1324 ((isset($row['Default'])) ?
$row['Default'] : ''),
1326 ((isset($res_rel[$field_name])) ?
$res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field'] : ''),
1327 ((isset($comments[$field_name])) ?
$comments[$field_name] : ''),
1328 ((isset($mime_map) && isset($mime_map[$field_name])) ?
str_replace('_', '/', $mime_map[$field_name]['mimetype']) : '')
1330 $links[0] = $pdf->PMA_links
['RT'][$table][$field_name];
1331 if (isset($res_rel[$field_name]['foreign_table']) AND
1332 isset($res_rel[$field_name]['foreign_field']) AND
1333 isset($pdf->PMA_links
['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']])
1336 $links[6] = $pdf->PMA_links
['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']];
1340 $pdf->Row($pdf_row, $links);
1342 $pdf->SetFont('', '', 14);
1343 PMA_DBI_free_result($result);