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 getH()
49 public function getW()
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($this->_ff
, 'B', 14);
229 $this->Cell(0, 6, ucfirst($pages['page_descr']), 'B', 1, 'C');
230 $this->SetFont($this->_ff
, '');
237 // This function must be named "Footer" to work with the TCPDF library
241 $this->SetFont($this->_ff
, '', 14);
242 $this->Cell(0, 6, __('Page number:') . ' ' . $this->getAliasNumPage() . '/' . $this->getAliasNbPages(), 'T', 0, 'C');
243 $this->Cell(0, 6, PMA_localisedDate(), 0, 1, 'R');
248 function Bookmark($txt, $level = 0, $y = 0, $page = '')
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 SetWidths($w)
352 function Row($data, $links)
356 $data_cnt = count($data);
357 for ($i = 0;$i < $data_cnt;$i++
)
358 $nb = max($nb, $this->NbLines($this->widths
[$i], $data[$i]));
359 $il = $this->FontSize
;
360 $h = ($il +
1) * $nb;
361 // page break if necessary
362 $this->CheckPageBreak($h);
364 $data_cnt = count($data);
365 for ($i = 0;$i < $data_cnt;$i++
) {
366 $w = $this->widths
[$i];
367 // save current position
371 $this->Rect($x, $y, $w, $h);
372 if (isset($links[$i])) {
373 $this->Link($x, $y, $w, $h, $links[$i]);
376 $this->MultiCell($w, $il +
1, $data[$i], 0, 'L');
378 $this->SetXY($x +
$w, $y);
384 function NbLines($w, $txt)
386 // compute number of lines used by a multicell of width w
387 $cw = &$this->CurrentFont
['cw'];
389 $w = $this->w
- $this->rMargin
- $this->x
;
391 $wmax = ($w-2 * $this->cMargin
) * 1000 / $this->FontSize
;
392 $s = str_replace("\r", '', $txt);
394 if ($nb > 0 and $s[$nb-1] == "\n") {
415 $l +
= isset($cw[ord($c)])?
$cw[ord($c)]:0 ;
437 * Table preferences/statistics
439 * This class preserves the table co-ordinates,fields
440 * and helps in drawing/generating the Tables in PDF document.
453 private $_showInfo = false;
458 public $fields = array();
459 public $heightCell = 6;
461 public $primary = array();
464 * The "Table_Stats" constructor
466 * @param string table_name The table name
467 * @param integer fontSize The font size
468 * @param integer pageNumber The current page number (from the
469 * $cfg['Servers'][$i]['table_coords'] table)
470 * @param integer sameWideWidth The max. with among tables
471 * @param boolean showKeys Whether to display keys or not
472 * @param boolean showInfo Whether to display table position or not
473 * @global object The current PDF document
474 * @global array The relations settings
475 * @global string The current db name
476 * @see PMA_PDF, Table_Stats::Table_Stats_setWidth,
477 Table_Stats::Table_Stats_setHeight
479 function __construct($tableName, $fontSize, $pageNumber, &$sameWideWidth, $showKeys = false, $showInfo = false)
481 global $pdf, $cfgRelation, $db;
483 $this->_tableName
= $tableName;
484 $sql = 'DESCRIBE ' . PMA_backquote($tableName);
485 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE
);
486 if (!$result ||
!PMA_DBI_num_rows($result)) {
487 $pdf->Error(sprintf(__('The %s table doesn\'t exist!'), $tableName));
490 //check to see if it will load all fields or only the foreign keys
492 $indexes = PMA_Index
::getFromTable($this->_tableName
, $db);
493 $all_columns = array();
494 foreach ($indexes as $index) {
495 $all_columns = array_merge($all_columns, array_flip(array_keys($index->getColumns())));
497 $this->fields
= array_keys($all_columns);
499 while ($row = PMA_DBI_fetch_row($result)) {
500 $this->fields
[] = $row[0];
504 $this->_showInfo
= $showInfo;
507 * setWidth must me after setHeight, because title
508 * can include table height which changes table width
510 $this->_setWidth($fontSize);
511 if ($sameWideWidth < $this->width
) {
512 $sameWideWidth = $this->width
;
514 $sql = 'SELECT x, y FROM '
515 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
516 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
517 . ' AND table_name = \'' . PMA_sqlAddslashes($tableName) . '\''
518 . ' AND pdf_page_number = ' . $pageNumber;
519 $result = PMA_query_as_controluser($sql, false, PMA_DBI_QUERY_STORE
);
520 if (!$result ||
!PMA_DBI_num_rows($result)) {
521 $pdf->Error(sprintf(__('Please configure the coordinates for table %s'), $tableName));
523 list($this->x
, $this->y
) = PMA_DBI_fetch_row($result);
524 $this->x
= (double) $this->x
;
525 $this->y
= (double) $this->y
;
529 $this->displayfield
= PMA_getDisplayField($db, $tableName);
533 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tableName) . ';', null, PMA_DBI_QUERY_STORE
);
534 if (PMA_DBI_num_rows($result) > 0) {
535 while ($row = PMA_DBI_fetch_assoc($result)) {
536 if ($row['Key_name'] == 'PRIMARY') {
537 $this->primary
[] = $row['Column_name'];
544 * Returns title of the current table,
545 * title can have the dimensions of the table
549 private function _getTitle()
551 return ($this->_showInfo ?
sprintf('%.0f', $this->width
) . 'x' . sprintf('%.0f', $this->height
) : '') . ' ' . $this->_tableName
;
555 * Sets the width of the table
557 * @param integer fontSize The font size
558 * @global object The current PDF document
562 function _setWidth($fontSize)
566 foreach ($this->fields
as $field) {
567 $this->width
= max($this->width
, $pdf->GetStringWidth($field));
569 $this->width +
= $pdf->GetStringWidth(' ');
570 $pdf->SetFont($this->_ff
, 'B', $fontSize);
572 * it is unknown what value must be added, because
573 * table title is affected by the tabe width value
575 while ($this->width
< $pdf->GetStringWidth($this->_getTitle())) {
578 $pdf->SetFont($this->_ff
, '', $fontSize);
582 * Sets the height of the table
586 private function _setHeight()
588 $this->height
= (count($this->fields
) +
1) * $this->heightCell
;
594 * @param integer fontSize The font size
595 * @param boolean setColor Whether to display color
596 * @global object The current PDF document
600 public function tableDraw($fontSize, $withDoc, $setColor = 0)
602 global $pdf, $withDoc;
604 $pdf->PMA_PDF_setXyScale($this->x
, $this->y
);
605 $pdf->SetFont($this->_ff
, 'B', $fontSize);
607 $pdf->SetTextColor(200);
608 $pdf->SetFillColor(0, 0, 128);
611 $pdf->SetLink($pdf->PMA_links
['RT'][$this->_tableName
]['-'], -1);
613 $pdf->PMA_links
['doc'][$this->_tableName
]['-'] = '';
616 $pdf->PMA_PDF_cellScale($this->width
, $this->heightCell
, $this->_getTitle(), 1, 1, 'C', $setColor, $pdf->PMA_links
['doc'][$this->_tableName
]['-']);
617 $pdf->PMA_PDF_setXScale($this->x
);
618 $pdf->SetFont($this->_ff
, '', $fontSize);
619 $pdf->SetTextColor(0);
620 $pdf->SetFillColor(255);
622 foreach ($this->fields
as $field) {
624 if (in_array($field, $this->primary
)) {
625 $pdf->SetFillColor(215, 121, 123);
627 if ($field == $this->displayfield
) {
628 $pdf->SetFillColor(142, 159, 224);
632 $pdf->SetLink($pdf->PMA_links
['RT'][$this->_tableName
][$field], -1);
634 $pdf->PMA_links
['doc'][$this->_tableName
][$field] = '';
637 $pdf->PMA_PDF_cellScale($this->width
, $this->heightCell
, ' ' . $field, 1, 1, 'L', $setColor, $pdf->PMA_links
['doc'][$this->_tableName
][$field]);
638 $pdf->PMA_PDF_setXScale($this->x
);
639 $pdf->SetFillColor(255);
641 /*if ($pdf->PageNo() > 1) {
642 $pdf->PMA_PDF_die(__('The scale factor is too small to fit the schema on one page'));
648 * Relation preferences/statistics
650 * This class fetches the table master and foreign fields positions
651 * and helps in generating the Table references and then connects
652 * master table's master field to foreign table's foreign key
655 * @name Relation_Stats
658 * @see PMA_PDF::SetDrawColor,PMA_PDF::PMA_PDF_setLineWidthScale,PMA_PDF::PMA_PDF_lineScale
668 public $xDest, $yDest;
672 * The "Relation_Stats" constructor
674 * @param string master_table The master table name
675 * @param string master_field The relation field in the master table
676 * @param string foreign_table The foreign table name
677 * @param string foreigh_field The relation field in the foreign table
678 * @see Relation_Stats::_getXy
680 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
682 $src_pos = $this->_getXy($master_table, $master_field);
683 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
689 $src_left = $src_pos[0] - $this->wTick
;
690 $src_right = $src_pos[1] +
$this->wTick
;
691 $dest_left = $dest_pos[0] - $this->wTick
;
692 $dest_right = $dest_pos[1] +
$this->wTick
;
694 $d1 = abs($src_left - $dest_left);
695 $d2 = abs($src_right - $dest_left);
696 $d3 = abs($src_left - $dest_right);
697 $d4 = abs($src_right - $dest_right);
698 $d = min($d1, $d2, $d3, $d4);
701 $this->xSrc
= $src_pos[0];
703 $this->xDest
= $dest_pos[0];
705 } elseif ($d == $d2) {
706 $this->xSrc
= $src_pos[1];
708 $this->xDest
= $dest_pos[0];
710 } elseif ($d == $d3) {
711 $this->xSrc
= $src_pos[0];
713 $this->xDest
= $dest_pos[1];
716 $this->xSrc
= $src_pos[1];
718 $this->xDest
= $dest_pos[1];
721 $this->ySrc
= $src_pos[2];
722 $this->yDest
= $dest_pos[2];
726 * Gets arrows coordinates
728 * @param string table The current table name
729 * @param string column The relation column name
730 * @return array Arrows coordinates
733 private function _getXy($table, $column)
735 $pos = array_search($column, $table->fields
);
736 // x_left, x_right, y
737 return array($table->x
, $table->x + +
$table->width
, $table->y +
($pos +
1.5) * $table->heightCell
);
741 * draws relation links and arrows
742 * shows foreign key relations
744 * @param boolean changeColor Whether to use one color per relation or not
745 * @param integer i The id of the link to draw
746 * @global object The current PDF document
750 public function relationDraw($changeColor, $i)
767 list ($a, $b, $c) = $case[$d];
768 $e = (1 - ($j - 1) / 6);
769 $pdf->SetDrawColor($a * 255 * $e, $b * 255 * $e, $c * 255 * $e);
771 $pdf->SetDrawColor(0);
773 $pdf->PMA_PDF_setLineWidthScale(0.2);
774 $pdf->PMA_PDF_lineScale($this->xSrc
, $this->ySrc
, $this->xSrc +
$this->srcDir
* $this->wTick
, $this->ySrc
);
775 $pdf->PMA_PDF_lineScale($this->xDest +
$this->destDir
* $this->wTick
, $this->yDest
, $this->xDest
, $this->yDest
);
776 $pdf->PMA_PDF_setLineWidthScale(0.1);
777 $pdf->PMA_PDF_lineScale($this->xSrc +
$this->srcDir
* $this->wTick
, $this->ySrc
, $this->xDest +
$this->destDir
* $this->wTick
, $this->yDest
);
781 $root2 = 2 * sqrt(2);
782 $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);
783 $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);
785 $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);
786 $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);
787 $pdf->SetDrawColor(0);
792 * Pdf Relation Schema Class
794 * Purpose of this class is to generate the PDF Document. PDF is widely
795 * used format for documenting text,fonts,images and 3d vector graphics.
797 * This class inherits Export_Relation_Schema class has common functionality added
800 * @name Pdf_Relation_Schema
804 class PMA_Pdf_Relation_Schema
extends PMA_Export_Relation_Schema
809 private $_tables = array();
810 private $_relations = array();
811 private $_ff = PMA_PDF_FONT
;
815 private $_xMin = 100000;
816 private $_yMin = 100000;
817 private $topMargin = 10;
818 private $bottomMargin = 10;
819 private $leftMargin = 10;
820 private $rightMargin = 10;
821 private $_tablewidth;
824 * The "PMA_Pdf_Relation_Schema" constructor
826 * @global object The current PDF Schema document
827 * @global string The current db name
828 * @global array The relations settings
832 function __construct()
834 global $pdf,$db,$cfgRelation;
836 $this->setPageNumber($_POST['pdf_page_number']);
837 $this->setShowGrid(isset($_POST['show_grid']));
838 $this->setShowColor(isset($_POST['show_color']));
839 $this->setShowKeys(isset($_POST['show_keys']));
840 $this->setTableDimension(isset($_POST['show_table_dimension']));
841 $this->setAllTableSameWidth(isset($_POST['all_table_same_wide']));
842 $this->setWithDataDictionary($_POST['with_doc']);
843 $this->setOrientation($_POST['orientation']);
844 $this->setPaper($_POST['paper']);
845 $this->setExportType($_POST['export_type']);
847 // Initializes a new document
848 $pdf = new PMA_PDF($this->orientation
, 'mm', $this->paper
);
849 $pdf->SetTitle(sprintf(__('Schema of the %s database - Page %s'), $GLOBALS['db'], $this->pageNumber
));
852 $pdf->SetAuthor('phpMyAdmin ' . PMA_VERSION
);
853 $pdf->AliasNbPages();
854 $pdf->AddFont('DejaVuSans', '', 'dejavusans.php');
855 $pdf->AddFont('DejaVuSans', 'B', 'dejavusansb.php');
856 $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php');
857 $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserifb.php');
858 $pdf->SetFont($this->_ff
, '', 14);
859 $pdf->setFooterFont(array($this->_ff
, '', 14));
860 $pdf->SetAutoPageBreak('auto');
861 $alltables = $this->getAllTables($db,$this->pageNumber
);
863 if ($this->withDoc
) {
864 $pdf->SetAutoPageBreak('auto', 15);
866 $this->dataDictionaryDoc($alltables);
867 $pdf->SetAutoPageBreak('auto');
873 if ($this->withDoc
) {
874 $pdf->SetLink($pdf->PMA_links
['RT']['-'], -1);
875 $pdf->Bookmark(__('Relational schema'));
876 $pdf->SetAlias('{00}', $pdf->PageNo()) ;
877 $this->topMargin
= 28;
878 $this->bottomMargin
= 28;
882 foreach ($alltables as $table) {
883 if (!isset($this->tables
[$table])) {
884 $this->tables
[$table] = new Table_Stats($table, $this->_ff
, $this->pageNumber
, $this->_tablewidth
, $this->showKeys
, $this->tableDimension
);
886 if ($this->sameWide
) {
887 $this->tables
[$table]->width
= $this->_tablewidth
;
889 $this->_setMinMax($this->tables
[$table]);
892 // Defines the scale factor
895 ($this->_xMax
- $this->_xMin
) / ($pdf->getW() - $this->rightMargin
- $this->leftMargin
),
896 ($this->_yMax
- $this->_yMin
) / ($pdf->getH() - $this->topMargin
- $this->bottomMargin
))
899 $pdf->PMA_PDF_setScale($this->scale
, $this->_xMin
, $this->_yMin
, $this->leftMargin
, $this->topMargin
);
900 // Builds and save the PDF document
901 $pdf->PMA_PDF_setLineWidthScale(0.1);
903 if ($this->showGrid
) {
904 $pdf->SetFontSize(10);
905 $this->_strokeGrid();
907 $pdf->PMA_PDF_setFontSizeScale(14);
908 // previous logic was checking master tables and foreign tables
909 // but I think that looping on every table of the pdf page as a master
910 // and finding its foreigns is OK (then we can support innodb)
911 $seen_a_relation = false;
912 foreach ($alltables as $one_table) {
913 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
915 $seen_a_relation = true;
916 foreach ($exist_rel as $master_field => $rel) {
917 // put the foreign table on the schema only if selected
919 // (do not use array_search() because we would have to
920 // to do a === FALSE and this is not PHP3 compatible)
921 if (in_array($rel['foreign_table'], $alltables)) {
922 $this->_addRelation($one_table, $master_field, $rel['foreign_table'], $rel['foreign_field'], $this->tableDimension
);
928 if ($seen_a_relation) {
929 $this->_drawRelations($this->showColor
);
931 $this->_drawTables($this->showColor
);
932 $this->_showOutput($this->pageNumber
);
937 * Sets X and Y minimum and maximum for a table cell
939 * @param string table The table name of which sets XY co-ordinates
942 private function _setMinMax($table)
944 $this->_xMax
= max($this->_xMax
, $table->x +
$table->width
);
945 $this->_yMax
= max($this->_yMax
, $table->y +
$table->height
);
946 $this->_xMin
= min($this->_xMin
, $table->x
);
947 $this->_yMin
= min($this->_yMin
, $table->y
);
951 * Defines relation objects
953 * @param string master_table The master table name
954 * @param string master_field The relation field in the master table
955 * @param string foreign_table The foreign table name
956 * @param string foreign_field The relation field in the foreign table
957 * @param boolean show_info Whether to display table position or not
961 private function _addRelation($masterTable, $masterField, $foreignTable, $foreignField, $showInfo)
963 if (!isset($this->tables
[$masterTable])) {
964 $this->tables
[$masterTable] = new Table_Stats($masterTable, $this->_ff
, $this->pageNumber
, $this->_tablewidth
, false, $showInfo);
965 $this->_setMinMax($this->tables
[$masterTable]);
967 if (!isset($this->tables
[$foreignTable])) {
968 $this->tables
[$foreignTable] = new Table_Stats($foreignTable, $this->_ff
, $this->pageNumber
, $this->_tablewidth
, false, $showInfo);
969 $this->_setMinMax($this->tables
[$foreignTable]);
971 $this->relations
[] = new Relation_Stats($this->tables
[$masterTable], $masterField, $this->tables
[$foreignTable], $foreignField);
977 * @global object the current PMA_PDF instance
981 private function _strokeGrid()
988 if ($this->withDoc
) {
996 $pdf->SetMargins(0, 0);
997 $pdf->SetDrawColor(200, 200, 200);
998 // Draws horizontal lines
999 for ($l = 0; $l <= intval(($pdf->getH() - $topSpace - $bottomSpace) / $gridSize); $l++
) {
1000 $pdf->line(0, $l * $gridSize +
$topSpace, $pdf->getW(), $l * $gridSize +
$topSpace);
1002 if ($l > 0 && $l <= intval(($pdf->getH() - $topSpace - $bottomSpace - $labelHeight) / $gridSize)) {
1003 $pdf->SetXY(0, $l * $gridSize +
$topSpace);
1004 $label = (string) sprintf('%.0f', ($l * $gridSize +
$topSpace - $this->topMargin
) * $this->scale +
$this->_yMin
);
1005 $pdf->Cell($labelWidth, $labelHeight, ' ' . $label);
1008 // Draws vertical lines
1009 for ($j = 0; $j <= intval($pdf->getW() / $gridSize); $j++
) {
1010 $pdf->line($j * $gridSize, $topSpace, $j * $gridSize, $pdf->getH() - $bottomSpace);
1011 $pdf->SetXY($j * $gridSize, $topSpace);
1012 $label = (string) sprintf('%.0f', ($j * $gridSize - $this->leftMargin
) * $this->scale +
$this->_xMin
);
1013 $pdf->Cell($labelWidth, $labelHeight, $label);
1018 * Draws relation arrows
1020 * @param boolean changeColor Whether to use one color per relation or not
1022 * @see Relation_Stats::relationdraw()
1024 private function _drawRelations($changeColor)
1027 foreach ($this->relations
as $relation) {
1028 $relation->relationDraw($changeColor, $i);
1036 * @param boolean changeColor Whether to display table position or not
1038 * @see Table_Stats::tableDraw()
1040 private function _drawTables($changeColor = 0)
1042 foreach ($this->tables
as $table) {
1043 $table->tableDraw($this->_ff
, $this->withDoc
, $changeColor);
1048 * Ouputs the PDF document to a file
1049 * or sends the output to browser
1051 * @global object The current PDF document
1052 * @global string The current database name
1053 * @global integer The current page number (from the
1054 * $cfg['Servers'][$i]['table_coords'] table)
1058 private function _showOutput($pageNumber)
1060 global $pdf, $db, $cfgRelation;
1062 $pdf->SetFontSize(14);
1063 $pdf->SetLineWidth(0.2);
1064 $pdf->SetDisplayMode('fullpage');
1065 // Get the name of this pdfpage to use as filename (Mike Beck)
1066 $_name_sql = 'SELECT page_descr FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
1067 . ' WHERE page_nr = ' . $pageNumber;
1068 $_name_rs = PMA_query_as_controluser($_name_sql);
1070 $_name_row = PMA_DBI_fetch_row($_name_rs);
1071 $filename = $_name_row[0] . '.pdf';
1073 if (empty($filename)) {
1074 $filename = $pageNumber . '.pdf';
1076 // instead of $pdf->Output():
1077 $pdfData = $pdf->getPDFData();
1078 header('Content-Type: application/pdf');
1079 header('Content-Length: '.strlen($pdfData).'');
1080 header('Content-disposition: attachment; filename="'.$filename.'"');
1084 public function dataDictionaryDoc($alltables)
1086 global $db, $pdf, $orientation, $paper;
1088 $pdf->addpage($GLOBALS['orientation']);
1089 $pdf->Cell(0, 9, __('Table of contents'), 1, 0, 'C');
1092 foreach ($alltables as $table) {
1093 $pdf->PMA_links
['doc'][$table]['-'] = $pdf->AddLink();
1096 $pdf->Cell(0, 6, __('Page number:') . ' {' . sprintf("%02d", $i +
1) . '}', 0, 0, 'R', 0, $pdf->PMA_links
['doc'][$table]['-']);
1098 $pdf->Cell(0, 6, $i . ' ' . $table, 0, 1, 'L', 0, $pdf->PMA_links
['doc'][$table]['-']);
1100 $result = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';');
1101 while ($row = PMA_DBI_fetch_assoc($result)) {
1103 $field_name = $row['Field'];
1104 $pdf->PMA_links
['doc'][$table][$field_name] = $pdf->AddLink();
1105 // $pdf->Cell(0, 6, $field_name,0,1,'L',0, $pdf->PMA_links['doc'][$table][$field_name]);
1107 $lasttable = $table;
1110 $pdf->PMA_links
['RT']['-'] = $pdf->AddLink();
1112 $pdf->Cell(0, 6, __('Page number:') . ' {' . sprintf("%02d", $i +
1) . '}', 0, 0, 'R', 0, $pdf->PMA_links
['RT']['-']);
1114 $pdf->Cell(0, 6, $i . ' ' . __('Relational schema'), 0, 1, 'L', 0, $pdf->PMA_links
['RT']['-']);
1116 foreach ($alltables as $table) {
1118 $pdf->SetAutoPageBreak(true, 15);
1119 $pdf->addpage($GLOBALS['orientation']);
1120 $pdf->Bookmark($table);
1121 $pdf->SetAlias('{' . sprintf("%02d", $z) . '}', $pdf->PageNo()) ;
1122 $pdf->PMA_links
['RT'][$table]['-'] = $pdf->AddLink();
1123 $pdf->SetLink($pdf->PMA_links
['doc'][$table]['-'], -1);
1124 $pdf->SetFont($this->_ff
, 'B', 18);
1125 $pdf->Cell(0, 8, $z . ' ' . $table, 1, 1, 'C', 0, $pdf->PMA_links
['RT'][$table]['-']);
1126 $pdf->SetFont($this->_ff
, '', 8);
1129 $cfgRelation = PMA_getRelationsParam();
1130 $comments = PMA_getComments($db, $table);
1131 if ($cfgRelation['mimework']) {
1132 $mime_map = PMA_getMIME($db, $table, true);
1136 * Gets table informations
1138 $showtable = PMA_Table
::sGetStatusInfo($db, $table);
1139 $num_rows = (isset($showtable['Rows']) ?
$showtable['Rows'] : 0);
1140 $show_comment = (isset($showtable['Comment']) ?
$showtable['Comment'] : '');
1141 $create_time = (isset($showtable['Create_time']) ?
PMA_localisedDate(strtotime($showtable['Create_time'])) : '');
1142 $update_time = (isset($showtable['Update_time']) ?
PMA_localisedDate(strtotime($showtable['Update_time'])) : '');
1143 $check_time = (isset($showtable['Check_time']) ?
PMA_localisedDate(strtotime($showtable['Check_time'])) : '');
1146 * Gets table keys and retains them
1148 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
1152 $indexes_info = array();
1153 $indexes_data = array();
1154 $pk_array = array(); // will be use to emphasis prim. keys in the table
1156 while ($row = PMA_DBI_fetch_assoc($result)) {
1157 // Backups the list of primary keys
1158 if ($row['Key_name'] == 'PRIMARY') {
1159 $primary .= $row['Column_name'] . ', ';
1160 $pk_array[$row['Column_name']] = 1;
1162 // Retains keys informations
1163 if ($row['Key_name'] != $lastIndex) {
1164 $indexes[] = $row['Key_name'];
1165 $lastIndex = $row['Key_name'];
1167 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
1168 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
1169 if (isset($row['Cardinality'])) {
1170 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
1172 // I don't know what does following column mean....
1173 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
1174 $indexes_info[$row['Key_name']]['Comment'] = $row['Comment'];
1176 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
1177 if (isset($row['Sub_part'])) {
1178 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
1182 PMA_DBI_free_result($result);
1186 * Gets fields properties
1188 $result = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE
);
1189 $fields_cnt = PMA_DBI_num_rows($result);
1190 // Check if we can use Relations (Mike Beck)
1191 if (!empty($cfgRelation['relation'])) {
1192 // Find which tables are related with the current one and write it in
1194 $res_rel = PMA_getForeigners($db, $table);
1196 if (count($res_rel) > 0) {
1206 * Displays the comments of the table if MySQL >= 3.23
1210 if (!empty($show_comment)) {
1211 $pdf->Cell(0, 3, __('Table comments') . ' : ' . $show_comment, 0, 1);
1215 if (!empty($create_time)) {
1216 $pdf->Cell(0, 3, __('Creation') . ': ' . $create_time, 0, 1);
1220 if (!empty($update_time)) {
1221 $pdf->Cell(0, 3, __('Last update') . ': ' . $update_time, 0, 1);
1225 if (!empty($check_time)) {
1226 $pdf->Cell(0, 3, __('Last check') . ': ' . $check_time, 0, 1);
1230 if ($break == true) {
1231 $pdf->Cell(0, 3, '', 0, 1);
1235 $pdf->SetFont($this->_ff
, 'B');
1236 if (isset($orientation) && $orientation == 'L') {
1237 $pdf->Cell(25, 8, ucfirst(__('Column')), 1, 0, 'C');
1238 $pdf->Cell(20, 8, ucfirst(__('Type')), 1, 0, 'C');
1239 $pdf->Cell(20, 8, ucfirst(__('Attributes')), 1, 0, 'C');
1240 $pdf->Cell(10, 8, ucfirst(__('Null')), 1, 0, 'C');
1241 $pdf->Cell(20, 8, ucfirst(__('Default')), 1, 0, 'C');
1242 $pdf->Cell(25, 8, ucfirst(__('Extra')), 1, 0, 'C');
1243 $pdf->Cell(45, 8, ucfirst(__('Links to')), 1, 0, 'C');
1245 if ($paper == 'A4') {
1246 $comments_width = 67;
1248 // this is really intended for 'letter'
1250 * @todo find optimal width for all formats
1252 $comments_width = 50;
1254 $pdf->Cell($comments_width, 8, ucfirst(__('Comments')), 1, 0, 'C');
1255 $pdf->Cell(45, 8, 'MIME', 1, 1, 'C');
1256 $pdf->SetWidths(array(25, 20, 20, 10, 20, 25, 45, $comments_width, 45));
1258 $pdf->Cell(20, 8, ucfirst(__('Column')), 1, 0, 'C');
1259 $pdf->Cell(20, 8, ucfirst(__('Type')), 1, 0, 'C');
1260 $pdf->Cell(20, 8, ucfirst(__('Attributes')), 1, 0, 'C');
1261 $pdf->Cell(10, 8, ucfirst(__('Null')), 1, 0, 'C');
1262 $pdf->Cell(15, 8, ucfirst(__('Default')), 1, 0, 'C');
1263 $pdf->Cell(15, 8, ucfirst(__('Extra')), 1, 0, 'C');
1264 $pdf->Cell(30, 8, ucfirst(__('Links to')), 1, 0, 'C');
1265 $pdf->Cell(30, 8, ucfirst(__('Comments')), 1, 0, 'C');
1266 $pdf->Cell(30, 8, 'MIME', 1, 1, 'C');
1267 $pdf->SetWidths(array(20, 20, 20, 10, 15, 15, 30, 30, 30));
1269 $pdf->SetFont($this->_ff
, '');
1271 while ($row = PMA_DBI_fetch_assoc($result)) {
1272 $type = $row['Type'];
1273 // reformat mysql query output
1274 // set or enum types: slashes single quotes inside options
1275 if (preg_match('@^(set|enum)\((.+)\)$@i', $type, $tmp)) {
1276 $tmp[2] = substr(preg_replace("@([^,])''@", "\\1\\'", ',' . $tmp[2]), 1);
1277 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
1284 $type_nowrap = ' nowrap="nowrap"';
1285 $type = preg_replace('@BINARY@i', '', $type);
1286 $type = preg_replace('@ZEROFILL@i', '', $type);
1287 $type = preg_replace('@UNSIGNED@i', '', $type);
1292 $binary = stristr($row['Type'], 'BINARY');
1293 $unsigned = stristr($row['Type'], 'UNSIGNED');
1294 $zerofill = stristr($row['Type'], 'ZEROFILL');
1298 $attribute = 'BINARY';
1301 $attribute = 'UNSIGNED';
1304 $attribute = 'UNSIGNED ZEROFILL';
1306 if (!isset($row['Default'])) {
1307 if ($row['Null'] != '' && $row['Null'] != 'NO') {
1308 $row['Default'] = 'NULL';
1311 $field_name = $row['Field'];
1313 $pdf->PMA_links
['RT'][$table][$field_name] = $pdf->AddLink();
1314 $pdf->Bookmark($field_name, 1, -1);
1315 $pdf->SetLink($pdf->PMA_links
['doc'][$table][$field_name], -1);
1316 $pdf_row = array($field_name,
1319 ($row['Null'] == '' ||
$row['Null'] == 'NO') ?
__('No') : __('Yes'),
1320 ((isset($row['Default'])) ?
$row['Default'] : ''),
1322 ((isset($res_rel[$field_name])) ?
$res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field'] : ''),
1323 ((isset($comments[$field_name])) ?
$comments[$field_name] : ''),
1324 ((isset($mime_map) && isset($mime_map[$field_name])) ?
str_replace('_', '/', $mime_map[$field_name]['mimetype']) : '')
1326 $links[0] = $pdf->PMA_links
['RT'][$table][$field_name];
1327 if (isset($res_rel[$field_name]['foreign_table']) AND
1328 isset($res_rel[$field_name]['foreign_field']) AND
1329 isset($pdf->PMA_links
['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']])
1332 $links[6] = $pdf->PMA_links
['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']];
1336 $pdf->Row($pdf_row, $links);
1338 $pdf->SetFont($this->_ff
, '', 14);
1339 PMA_DBI_free_result($result);