2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Contributed by Maxime Delorme and merged by lem9
10 * Gets some core scripts
12 require_once './libraries/common.inc.php';
15 * Settings for relation stuff
17 require_once './libraries/relation.lib.php';
18 require_once './libraries/transformations.lib.php';
19 require_once './libraries/Index.class.php';
21 $cfgRelation = PMA_getRelationsParam();
24 * Now in ./libraries/relation.lib.php we check for all tables
25 * that we need, but if we don't find them we are quiet about it
26 * so people can work without.
27 * This page is absolutely useless if you didn't set up your tables
28 * correctly, so it is a good place to see which tables we can and
31 if (!$cfgRelation['pdfwork']) {
32 echo '<font color="red">' . $strError . '</font><br />' . "\n";
33 $url_to_goto = '<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">';
34 echo sprintf($strRelationNotWorking, $url_to_goto, '</a>') . "\n";
40 * @todo Make this configuratble (at least Sans/Serif).
42 define('PMA_PDF_FONT', 'DejaVuSans');
43 require_once './libraries/tcpdf/tcpdf.php';
46 * Extends the "FPDF" class and prepares the work
51 class PMA_PDF
extends TCPDF
{
53 * Defines private properties
61 var $Outlines = array();
66 public function getFh()
71 public function getFw()
76 public function setCMargin($c_margin)
78 $this->cMargin
= $c_margin;
81 function SetAlias($name, $value)
83 $this->Alias
[$name] = $value ;
88 if (count($this->Alias
) > 0) {
90 foreach ($this->Alias
AS $alias => $value) {
91 for ($n = 1;$n <= $nb;$n++
)
92 $this->pages
[$n]=str_replace($alias, $value, $this->pages
[$n]);
99 * Sets the scaling factor, defines minimum coordinates and margins
101 * @param double $ The scaling factor
102 * @param double $ The minimum X coordinate
103 * @param double $ The minimum Y coordinate
104 * @param double $ The left margin
105 * @param double $ The top margin
108 function PMA_PDF_setScale($scale = 1, $x_min = 0, $y_min = 0, $l_marg = -1, $t_marg = -1)
110 $this->scale
= $scale;
111 $this->x_min
= $x_min;
112 $this->y_min
= $y_min;
113 if ($this->l_marg
!= -1) {
114 $this->l_marg
= $l_marg;
116 if ($this->t_marg
!= -1) {
117 $this->t_marg
= $t_marg;
119 } // end of the "PMA_PDF_setScale" function
121 * Outputs a scaled cell
123 * @param double $ The cell width
124 * @param double $ The cell height
125 * @param string $ The text to output
126 * @param mixed $ Whether to add borders or not
127 * @param integer $ Where to put the cursor once the output is done
128 * @param string $ Align mode
129 * @param integer $ Whether to fill the cell with a color or not
133 function PMA_PDF_cellScale($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $fill = 0, $link = '')
135 $h = $h / $this->scale
;
136 $w = $w / $this->scale
;
137 $this->Cell($w, $h, $txt, $border, $ln, $align, $fill, $link);
138 } // end of the "PMA_PDF_cellScale" function
140 * Draws a scaled line
142 * @param double $ The horizontal position of the starting point
143 * @param double $ The vertical position of the starting point
144 * @param double $ The horizontal position of the ending point
145 * @param double $ The vertical position of the ending point
149 function PMA_PDF_lineScale($x1, $y1, $x2, $y2)
151 $x1 = ($x1 - $this->x_min
) / $this->scale +
$this->l_marg
;
152 $y1 = ($y1 - $this->y_min
) / $this->scale +
$this->t_marg
;
153 $x2 = ($x2 - $this->x_min
) / $this->scale +
$this->l_marg
;
154 $y2 = ($y2 - $this->y_min
) / $this->scale +
$this->t_marg
;
155 $this->Line($x1, $y1, $x2, $y2);
156 } // end of the "PMA_PDF_lineScale" function
158 * Sets x and y scaled positions
160 * @param double $ The x position
161 * @param double $ The y position
165 function PMA_PDF_setXyScale($x, $y)
167 $x = ($x - $this->x_min
) / $this->scale +
$this->l_marg
;
168 $y = ($y - $this->y_min
) / $this->scale +
$this->t_marg
;
169 $this->SetXY($x, $y);
170 } // end of the "PMA_PDF_setXyScale" function
172 * Sets the X scaled positions
174 * @param double $ The x position
178 function PMA_PDF_setXScale($x)
180 $x = ($x - $this->x_min
) / $this->scale +
$this->l_marg
;
182 } // end of the "PMA_PDF_setXScale" function
184 * Sets the scaled font size
186 * @param double $ The font size (in points)
188 * @see FPDF::SetFontSize()
190 function PMA_PDF_setFontSizeScale($size)
192 // Set font size in points
193 $size = $size / $this->scale
;
194 $this->SetFontSize($size);
195 } // end of the "PMA_PDF_setFontSizeScale" function
197 * Sets the scaled line width
199 * @param double $ The line width
201 * @see FPDF::SetLineWidth()
203 function PMA_PDF_setLineWidthScale($width)
205 $width = $width / $this->scale
;
206 $this->SetLineWidth($width);
207 } // end of the "PMA_PDF_setLineWidthScale" function
209 * Displays an error message
211 * @param string $ the error mesage
212 * @global array the PMA configuration array
213 * @global integer the current server id
214 * @global string the current language
215 * @global string the charset to convert to
216 * @global string the current database name
217 * @global string the current charset
218 * @global string the current text direction
219 * @global string a localized string
220 * @global string an other localized string
223 function PMA_PDF_die($error_message = '')
226 global $server, $lang, $convcharset, $db;
227 global $charset, $text_dir, $strRunning, $strDatabase;
229 require_once './libraries/header.inc.php';
231 echo '<p><strong>PDF - ' . $GLOBALS['strError'] . '</strong></p>' . "\n";
232 if (!empty($error_message)) {
233 $error_message = htmlspecialchars($error_message);
236 echo ' ' . $error_message . "\n";
239 echo '<a href="db_structure.php?' . PMA_generate_common_url($db)
240 . '">' . $GLOBALS['strBack'] . '</a>';
243 require_once './libraries/footer.inc.php';
244 } // end of the "PMA_PDF_die()" function
246 * Aliases the "Error()" function from the FPDF class to the
247 * "PMA_PDF_die()" one
249 * @param string $ the error mesage
253 function Error($error_message = '')
255 $this->PMA_PDF_die($error_message);
256 } // end of the "Error()" method
260 // We only show this if we find something in the new pdf_pages table
262 // This function must be named "Header" to work with the FPDF library
263 global $cfgRelation, $db, $pdf_page_number, $with_doc;
265 $test_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
266 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
267 . ' AND page_nr = \'' . $pdf_page_number . '\'';
268 $test_rs = PMA_query_as_cu($test_query);
269 $pages = @PMA_DBI_fetch_assoc
($test_rs);
270 $this->SetFont('', 'B', 14);
271 $this->Cell(0, 6, ucfirst($pages['page_descr']), 'B', 1, 'C');
272 $this->SetFont('', '');
278 // This function must be named "Footer" to work with the FPDF library
282 $this->SetFont('', '', 14);
283 $this->Cell(0, 6, $GLOBALS['strPageNumber'] . ' ' . $this->PageNo() . '/{nb}', 'T', 0, 'C');
284 $this->Cell(0, 6, PMA_localisedDate(), 0, 1, 'R');
288 function Bookmark($txt, $level = 0, $y = 0)
291 $this->Outlines
[0][] = $level;
292 $this->Outlines
[1][] = $txt;
293 $this->Outlines
[2][] = $this->page
;
297 $this->Outlines
[3][] = round($this->hPt
- $y * $this->k
, 2);
300 function _putbookmarks()
302 if (count($this->Outlines
) > 0) {
303 // Save object number
305 // Take the number of sub elements for an outline
306 $nb_outlines = sizeof($this->Outlines
[0]);
307 $first_level = array();
310 for ($i = 0; $i < $nb_outlines; $i++
) {
311 $level = $this->Outlines
[0][$i];
318 // Take the previous outline in the same level
319 while ($this->Outlines
[0][$cursor] > $level && $cursor > 0)
321 if ($this->Outlines
[0][$cursor] == $level) {
325 if ($i < $nb_outlines-1) {
327 while (isset($this->Outlines
[0][$cursor]) && $this->Outlines
[0][$cursor] > $level) {
328 // Take the immediate kid in level + 1
329 if ($this->Outlines
[0][$cursor] == $level +
1) {
336 // Take the next outline in the same level
337 while ($this->Outlines
[0][$cursor] > $level && ($cursor +
1 < sizeof($this->Outlines
[0])))
339 if ($this->Outlines
[0][$cursor] == $level) {
344 $parent[$level +
1] = $this->n
;
346 $first_level[] = $this->n
;
349 $this->_out('/Title (' . $this->Outlines
[1][$i] . ')');
350 $this->_out('/Parent ' . $parent[$level] . ' 0 R');
352 $this->_out('/Prev ' . ($memo_n +
$prev +
1) . ' 0 R');
355 $this->_out('/Next ' . ($this->n +
$next - $i) . ' 0 R');
357 $this->_out('/Dest [' . (1 +
(2 * $this->Outlines
[2][$i])) . ' 0 R /XYZ null ' . $this->Outlines
[3][$i] . ' null]');
359 $this->_out('/First ' . ($this->n +
1) . ' 0 R');
360 $this->_out('/Last ' . ($this->n +
$last - $i) . ' 0 R');
361 $this->_out('/Count -' . $kids);
364 $this->_out('endobj');
366 // First page of outlines
368 $this->def_outlines
= $this->n
;
370 $this->_out('/Type');
371 $this->_out('/Outlines');
372 $this->_out('/First ' . $first_level[0] . ' 0 R');
373 $this->_out('/Last ' . $first_level[sizeof($first_level)-1] . ' 0 R');
374 $this->_out('/Count ' . sizeof($first_level));
376 $this->_out('endobj');
380 function _putresources()
382 parent
::_putresources();
383 $this->_putbookmarks();
386 function _putcatalog()
388 parent
::_putcatalog();
389 if (count($this->Outlines
) > 0) {
390 $this->_out('/Outlines ' . $this->def_outlines
. ' 0 R');
391 $this->_out('/PageMode /UseOutlines');
394 function SetWidths($w)
400 function Row($data, $links)
404 $data_cnt = count($data);
405 for ($i = 0;$i < $data_cnt;$i++
)
406 $nb = max($nb, $this->NbLines($this->widths
[$i], $data[$i]));
407 $il = $this->FontSize
;
408 $h = ($il +
1) * $nb;
409 // page break if necessary
410 $this->CheckPageBreak($h);
412 $data_cnt = count($data);
413 for ($i = 0;$i < $data_cnt;$i++
) {
414 $w = $this->widths
[$i];
415 // save current position
419 $this->Rect($x, $y, $w, $h);
420 if (isset($links[$i])) {
421 $this->Link($x, $y, $w, $h, $links[$i]);
424 $this->MultiCell($w, $il +
1, $data[$i], 0, 'L');
426 $this->SetXY($x +
$w, $y);
432 function CheckPageBreak($h)
434 // if height h overflows, manual page break
435 if ($this->GetY() +
$h > $this->PageBreakTrigger
) {
436 $this->AddPage($this->CurOrientation
);
440 function NbLines($w, $txt)
442 // compute number of lines used by a multicell of width w
443 $cw = &$this->CurrentFont
['cw'];
445 $w = $this->w
- $this->rMargin
- $this->x
;
447 $wmax = ($w-2 * $this->cMargin
) * 1000 / $this->FontSize
;
448 $s = str_replace("\r", '', $txt);
450 if ($nb > 0 and $s[$nb-1] == "\n") {
471 $l +
= isset($cw[ord($c)])?
$cw[ord($c)]:0 ;
490 } // end of the "PMA_PDF" class
494 * Draws tables schema
501 * Defines private properties
507 var $fields = array();
508 var $height_cell = 6;
510 var $primary = array();
513 * Sets the width of the table
515 * @param integer $ The font size
516 * @global object The current PDF document
520 function PMA_RT_Table_setWidth($ff)
522 // this looks buggy to me... does it really work if
523 // there are fields that require wider cells than the name of the table?
526 foreach ($this->fields
AS $field) {
527 $this->width
= max($this->width
, $pdf->GetStringWidth($field));
529 $this->width +
= $pdf->GetStringWidth(' ');
530 $pdf->SetFont($ff, 'B');
531 $this->width
= max($this->width
, $pdf->GetStringWidth(' ' . $this->table_name
));
532 $pdf->SetFont($ff, '');
533 } // end of the "PMA_RT_Table_setWidth()" method
535 * Sets the height of the table
539 function PMA_RT_Table_setHeight()
541 $this->height
= (count($this->fields
) +
1) * $this->height_cell
;
542 } // end of the "PMA_RT_Table_setHeight()" method
546 * @param boolean $ Whether to display table position or not
547 * @param integer $ The font size
548 * @param boolean $ Whether to display color
549 * @param integer $ The max. with among tables
550 * @global object The current PDF document
554 function PMA_RT_Table_draw($show_info, $ff, $setcolor = 0)
556 global $pdf, $with_doc;
558 $pdf->PMA_PDF_setXyScale($this->x
, $this->y
);
559 $pdf->SetFont($ff, 'B');
561 $pdf->SetTextColor(200);
562 $pdf->SetFillColor(0, 0, 128);
565 $pdf->SetLink($pdf->PMA_links
['RT'][$this->table_name
]['-'], -1);
567 $pdf->PMA_links
['doc'][$this->table_name
]['-'] = '';
571 $pdf->PMA_PDF_cellScale($this->width
, $this->height_cell
, sprintf('%.0f', $this->width
) . 'x' . sprintf('%.0f', $this->height
) . ' ' . $this->table_name
, 1, 1, 'C', $setcolor, $pdf->PMA_links
['doc'][$this->table_name
]['-']);
573 $pdf->PMA_PDF_cellScale($this->width
, $this->height_cell
, $this->table_name
, 1, 1, 'C', $setcolor, $pdf->PMA_links
['doc'][$this->table_name
]['-']);
575 $pdf->PMA_PDF_setXScale($this->x
);
576 $pdf->SetFont($ff, '');
577 $pdf->SetTextColor(0);
578 $pdf->SetFillColor(255);
580 foreach ($this->fields
AS $field) {
582 // if (in_array($field, $this->primary)) {
584 if (in_array($field, $this->primary
)) {
585 $pdf->SetFillColor(215, 121, 123);
587 if ($field == $this->displayfield
) {
588 $pdf->SetFillColor(142, 159, 224);
592 $pdf->SetLink($pdf->PMA_links
['RT'][$this->table_name
][$field], -1);
594 $pdf->PMA_links
['doc'][$this->table_name
][$field] = '';
597 $pdf->PMA_PDF_cellScale($this->width
, $this->height_cell
, ' ' . $field, 1, 1, 'L', $setcolor, $pdf->PMA_links
['doc'][$this->table_name
][$field]);
598 $pdf->PMA_PDF_setXScale($this->x
);
599 $pdf->SetFillColor(255);
601 /*if ($pdf->PageNo() > 1) {
602 $pdf->PMA_PDF_die($GLOBALS['strScaleFactorSmall']);
604 } // end of the "PMA_RT_Table_draw()" method
606 * The "PMA_RT_Table" constructor
608 * @param string $ The table name
609 * @param integer $ The font size
610 * @param integer $ The max. with among tables
611 * @global object The current PDF document
612 * @global integer The current page number (from the
613 * $cfg['Servers'][$i]['table_coords'] table)
614 * @global array The relations settings
615 * @global string The current db name
617 * @see PMA_PDF, PMA_RT_Table::PMA_RT_Table_setWidth,
618 PMA_RT_Table::PMA_RT_Table_setHeight
620 function __construct($table_name, $ff, &$same_wide_width, $show_keys)
622 global $pdf, $pdf_page_number, $cfgRelation, $db;
624 $this->table_name
= $table_name;
625 $sql = 'DESCRIBE ' . PMA_backquote($table_name);
626 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE
);
627 if (!$result ||
!PMA_DBI_num_rows($result)) {
628 $pdf->PMA_PDF_die(sprintf($GLOBALS['strPdfInvalidTblName'], $table_name));
631 //check to see if it will load all fields or only the foreign keys
633 $indexes = PMA_Index
::getFromTable($this->table_name
, $db);
634 $all_columns = array();
635 foreach ($indexes as $index) {
636 $all_columns = array_merge($all_columns, array_flip(array_keys($index->getColumns())));
638 $this->fields
= array_keys($all_columns);
640 while ($row = PMA_DBI_fetch_row($result)) {
641 $this->fields
[] = $row[0];
645 $this->PMA_RT_Table_setWidth($ff);
646 $this->PMA_RT_Table_setHeight();
647 if ($same_wide_width < $this->width
) {
648 $same_wide_width = $this->width
;
651 $sql = 'SELECT x, y FROM '
652 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
653 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
654 . ' AND table_name = \'' . PMA_sqlAddslashes($table_name) . '\''
655 . ' AND pdf_page_number = ' . $pdf_page_number;
656 $result = PMA_query_as_cu($sql, false, PMA_DBI_QUERY_STORE
);
658 if (!$result ||
!PMA_DBI_num_rows($result)) {
659 $pdf->PMA_PDF_die(sprintf($GLOBALS['strConfigureTableCoord'], $table_name));
661 list($this->x
, $this->y
) = PMA_DBI_fetch_row($result);
662 $this->x
= (double) $this->x
;
663 $this->y
= (double) $this->y
;
665 $this->displayfield
= PMA_getDisplayField($db, $table_name);
667 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($table_name) . ';', null, PMA_DBI_QUERY_STORE
);
668 if (PMA_DBI_num_rows($result) > 0) {
669 while ($row = PMA_DBI_fetch_assoc($result)) {
670 if ($row['Key_name'] == 'PRIMARY') {
671 $this->primary
[] = $row['Column_name'];
675 } // end of the "PMA_RT_Table()" method
676 } // end class "PMA_RT_Table"
678 * Draws relation links
683 class PMA_RT_Relation
{
685 * Defines private properties
690 var $x_dest, $y_dest;
694 * Gets arrows coordinates
696 * @param string $ The current table name
697 * @param string $ The relation column name
698 * @return array Arrows coordinates
701 function PMA_RT_Relation_getXy($table, $column)
703 $pos = array_search($column, $table->fields
);
704 // x_left, x_right, y
705 return array($table->x
, $table->x + +
$table->width
, $table->y +
($pos +
1.5) * $table->height_cell
);
706 } // end of the "PMA_RT_Relation_getXy()" method
708 * Do draws relation links
710 * @param boolean $ Whether to use one color per relation or not
711 * @param integer $ The id of the link to draw
712 * @global object The current PDF document
716 function PMA_RT_Relation_draw($change_color, $i)
733 list ($a, $b, $c) = $case[$d];
734 $e = (1 - ($j - 1) / 6);
735 $pdf->SetDrawColor($a * 255 * $e, $b * 255 * $e, $c * 255 * $e);
737 $pdf->SetDrawColor(0);
738 } // end if... else...
739 $pdf->PMA_PDF_setLineWidthScale(0.2);
740 $pdf->PMA_PDF_lineScale($this->x_src
, $this->y_src
, $this->x_src +
$this->src_dir
* $this->w_tick
, $this->y_src
);
741 $pdf->PMA_PDF_lineScale($this->x_dest +
$this->dest_dir
* $this->w_tick
, $this->y_dest
, $this->x_dest
, $this->y_dest
);
742 $pdf->PMA_PDF_setLineWidthScale(0.1);
743 $pdf->PMA_PDF_lineScale($this->x_src +
$this->src_dir
* $this->w_tick
, $this->y_src
, $this->x_dest +
$this->dest_dir
* $this->w_tick
, $this->y_dest
);
745 $root2 = 2 * sqrt(2);
746 $pdf->PMA_PDF_lineScale($this->x_src +
$this->src_dir
* $this->w_tick
* 0.75, $this->y_src
, $this->x_src +
$this->src_dir
* (0.75 - 1 / $root2) * $this->w_tick
, $this->y_src +
$this->w_tick
/ $root2);
747 $pdf->PMA_PDF_lineScale($this->x_src +
$this->src_dir
* $this->w_tick
* 0.75, $this->y_src
, $this->x_src +
$this->src_dir
* (0.75 - 1 / $root2) * $this->w_tick
, $this->y_src
- $this->w_tick
/ $root2);
749 $pdf->PMA_PDF_lineScale($this->x_dest +
$this->dest_dir
* $this->w_tick
/ 2, $this->y_dest
, $this->x_dest +
$this->dest_dir
* (0.5 +
1 / $root2) * $this->w_tick
, $this->y_dest +
$this->w_tick
/ $root2);
750 $pdf->PMA_PDF_lineScale($this->x_dest +
$this->dest_dir
* $this->w_tick
/ 2, $this->y_dest
, $this->x_dest +
$this->dest_dir
* (0.5 +
1 / $root2) * $this->w_tick
, $this->y_dest
- $this->w_tick
/ $root2);
751 $pdf->SetDrawColor(0);
752 } // end of the "PMA_RT_Relation_draw()" method
754 * The "PMA_RT_Relation" constructor
756 * @param string $ The master table name
757 * @param string $ The relation field in the master table
758 * @param string $ The foreign table name
759 * @param string $ The relation field in the foreign table
761 * @see PMA_RT_Relation::PMA_RT_Relation_getXy
763 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
765 $src_pos = $this->PMA_RT_Relation_getXy($master_table, $master_field);
766 $dest_pos = $this->PMA_RT_Relation_getXy($foreign_table, $foreign_field);
767 $src_left = $src_pos[0] - $this->w_tick
;
768 $src_right = $src_pos[1] +
$this->w_tick
;
769 $dest_left = $dest_pos[0] - $this->w_tick
;
770 $dest_right = $dest_pos[1] +
$this->w_tick
;
772 $d1 = abs($src_left - $dest_left);
773 $d2 = abs($src_right - $dest_left);
774 $d3 = abs($src_left - $dest_right);
775 $d4 = abs($src_right - $dest_right);
776 $d = min($d1, $d2, $d3, $d4);
779 $this->x_src
= $src_pos[0];
781 $this->x_dest
= $dest_pos[0];
782 $this->dest_dir
= -1;
783 } elseif ($d == $d2) {
784 $this->x_src
= $src_pos[1];
786 $this->x_dest
= $dest_pos[0];
787 $this->dest_dir
= -1;
788 } elseif ($d == $d3) {
789 $this->x_src
= $src_pos[0];
791 $this->x_dest
= $dest_pos[1];
794 $this->x_src
= $src_pos[1];
796 $this->x_dest
= $dest_pos[1];
799 $this->y_src
= $src_pos[2];
800 $this->y_dest
= $dest_pos[2];
801 } // end of the "PMA_RT_Relation()" method
802 } // end of the "PMA_RT_Relation" class
804 * Draws and send the database schema
811 * Defines private properties
813 var $tables = array();
814 var $relations = array();
815 var $ff = PMA_PDF_FONT
;
829 * Sets X and Y minimum and maximum for a table cell
831 * @param string $ The table name
834 function PMA_RT_setMinMax($table)
836 $this->x_max
= max($this->x_max
, $table->x +
$table->width
);
837 $this->y_max
= max($this->y_max
, $table->y +
$table->height
);
838 $this->x_min
= min($this->x_min
, $table->x
);
839 $this->y_min
= min($this->y_min
, $table->y
);
840 } // end of the "PMA_RT_setMinMax()" method
842 * Defines relation objects
844 * @param string $ The master table name
845 * @param string $ The relation field in the master table
846 * @param string $ The foreign table name
847 * @param string $ The relation field in the foreign table
849 * @see PMA_RT_setMinMax
851 function PMA_RT_addRelation($master_table, $master_field, $foreign_table, $foreign_field)
853 if (!isset($this->tables
[$master_table])) {
854 $this->tables
[$master_table] = new PMA_RT_Table($master_table, $this->ff
, $this->tablewidth
);
855 $this->PMA_RT_setMinMax($this->tables
[$master_table]);
857 if (!isset($this->tables
[$foreign_table])) {
858 $this->tables
[$foreign_table] = new PMA_RT_Table($foreign_table, $this->ff
, $this->tablewidth
);
859 $this->PMA_RT_setMinMax($this->tables
[$foreign_table]);
861 $this->relations
[] = new PMA_RT_Relation($this->tables
[$master_table], $master_field, $this->tables
[$foreign_table], $foreign_field);
862 } // end of the "PMA_RT_addRelation()" method
866 * @global object the current PMA_PDF instance
870 function PMA_RT_strokeGrid()
874 $pdf->SetMargins(0, 0);
875 $pdf->SetDrawColor(200, 200, 200);
876 // Draws horizontal lines
877 for ($l = 0; $l < 21; $l++
) {
878 $pdf->line(0, $l * 10, $pdf->fh
, $l * 10);
881 $pdf->SetXY(0, $l * 10);
882 $label = (string) sprintf('%.0f', ($l * 10 - $this->t_marg
) * $this->scale +
$this->y_min
);
883 $pdf->Cell(5, 5, ' ' . $label);
886 // Draws vertical lines
887 for ($j = 0; $j < 30 ;$j++
) {
888 $pdf->line($j * 10, 0, $j * 10, $pdf->fw
);
889 $pdf->SetXY($j * 10, 0);
890 $label = (string) sprintf('%.0f', ($j * 10 - $this->l_marg
) * $this->scale +
$this->x_min
);
891 $pdf->Cell(5, 7, $label);
893 } // end of the "PMA_RT_strokeGrid()" method
895 * Draws relation arrows
897 * @param boolean $ Whether to use one color per relation or not
899 * @see PMA_RT_Relation::PMA_RT_Relation_draw()
901 function PMA_RT_drawRelations($change_color)
904 foreach ($this->relations
AS $relation) {
905 $relation->PMA_RT_Relation_draw($change_color, $i);
908 } // end of the "PMA_RT_drawRelations()" method
912 * @param boolean $ Whether to display table position or not
914 * @see PMA_RT_Table::PMA_RT_Table_draw()
916 function PMA_RT_drawTables($show_info, $draw_color = 0)
918 foreach ($this->tables
AS $table) {
919 $table->PMA_RT_Table_draw($show_info, $this->ff
, $draw_color);
921 } // end of the "PMA_RT_drawTables()" method
923 * Ouputs the PDF document to a file
925 * @global object The current PDF document
926 * @global string The current database name
927 * @global integer The current page number (from the
928 * $cfg['Servers'][$i]['table_coords'] table)
932 function PMA_RT_showRt()
934 global $pdf, $db, $pdf_page_number, $cfgRelation;
936 $pdf->SetFontSize(14);
937 $pdf->SetLineWidth(0.2);
938 $pdf->SetDisplayMode('fullpage');
939 // Get the name of this pdfpage to use as filename (Mike Beck)
940 $_name_sql = 'SELECT page_descr FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
941 . ' WHERE page_nr = ' . $pdf_page_number;
942 $_name_rs = PMA_query_as_cu($_name_sql);
944 $_name_row = PMA_DBI_fetch_row($_name_rs);
945 $filename = $_name_row[0] . '.pdf';
947 // i don't know if there is a chance for this to happen, but rather be on the safe side:
948 if (empty($filename)) {
949 $filename = $pdf_page_number . '.pdf';
951 // $pdf->Output($db . '_' . $filename, TRUE);
952 $pdf->Output($db . '_' . $filename, 'I'); // destination: Inline
953 } // end of the "PMA_RT_showRt()" method
955 * The "PMA_RT" constructor
957 * @param mixed $ The scaling factor
958 * @param integer $ The page number to draw (from the
959 * $cfg['Servers'][$i]['table_coords'] table)
960 * @param boolean $ Whether to display table position or not
961 * @param boolean $ Was originally whether to use one color per
962 * relation or not, now enables/disables color
963 * everywhere, due to some problems printing with color
964 * @param boolean $ Whether to draw grids or not
965 * @param boolean $ Whether all tables should have the same width or not
966 * @param boolean $ Wheter to show all field or only the keys
967 * @global object The current PDF document
968 * @global string The current db name
969 * @global array The relations settings
973 function __construct($which_rel, $show_info = 0, $change_color = 0, $show_grid = 0, $all_tab_same_wide = 0, $orientation = 'L', $paper = 'A4', $show_keys = 0)
975 global $pdf, $db, $cfgRelation, $with_doc;
977 $this->same_wide
= $all_tab_same_wide;
978 // Initializes a new document
979 $pdf = new PMA_PDF('L', 'mm', $paper);
980 $pdf->SetTitle(sprintf($GLOBALS['strPdfDbSchema'], $GLOBALS['db'], $which_rel));
983 $pdf->SetAuthor('phpMyAdmin ' . PMA_VERSION
);
984 $pdf->AliasNbPages();
985 $pdf->AddFont('DejaVuSans', '', 'dejavusans.php');
986 $pdf->AddFont('DejaVuSans', 'B', 'dejavusansb.php');
987 $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php');
988 $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserifb.php');
989 $this->ff
= PMA_PDF_FONT
;
990 $pdf->SetFont($this->ff
, '', 14);
991 $pdf->SetAutoPageBreak('auto');
992 // Gets tables on this page
993 $tab_sql = 'SELECT table_name FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
994 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
995 . ' AND pdf_page_number = ' . $which_rel;
996 $tab_rs = PMA_query_as_cu($tab_sql, null, PMA_DBI_QUERY_STORE
);
997 if (!$tab_rs ||
!PMA_DBI_num_rows($tab_rs) > 0) {
998 $pdf->PMA_PDF_die($GLOBALS['strPdfNoTables']);
1000 } while ($curr_table = @PMA_DBI_fetch_assoc
($tab_rs)) {
1001 $alltables[] = PMA_sqlAddslashes($curr_table['table_name']);
1002 // $intable = '\'' . implode('\', \'', $alltables) . '\'';
1006 $pdf->SetAutoPageBreak('auto', 15);
1007 $pdf->setCMargin(1);
1008 PMA_RT_DOC($alltables);
1009 $pdf->SetAutoPageBreak('auto');
1010 $pdf->setCMargin(0);
1016 $pdf->SetLink($pdf->PMA_links
['RT']['-'], -1);
1017 $pdf->Bookmark($GLOBALS['strRelationalSchema']);
1018 $pdf->SetAlias('{00}', $pdf->PageNo()) ;
1025 foreach ($alltables AS $table) {
1026 if (!isset($this->tables
[$table])) {
1027 $this->tables
[$table] = new PMA_RT_Table($table, $this->ff
, $this->tablewidth
, $show_keys);
1030 if ($this->same_wide
) {
1031 $this->tables
[$table]->width
= $this->tablewidth
;
1033 $this->PMA_RT_setMinMax($this->tables
[$table]);
1035 // Defines the scale factor
1036 $this->scale
= ceil(
1038 ($this->x_max
- $this->x_min
) / ($pdf->getFh() - $this->r_marg
- $this->l_marg
),
1039 ($this->y_max
- $this->y_min
) / ($pdf->getFw() - $this->t_marg
- $this->b_marg
))
1042 $pdf->PMA_PDF_setScale($this->scale
, $this->x_min
, $this->y_min
, $this->l_marg
, $this->t_marg
);
1043 // Builds and save the PDF document
1044 $pdf->PMA_PDF_setLineWidthScale(0.1);
1047 $pdf->SetFontSize(10);
1048 $this->PMA_RT_strokeGrid();
1050 $pdf->PMA_PDF_setFontSizeScale(14);
1051 // $sql = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
1052 // . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\' '
1053 // . ' AND foreign_db = \'' . PMA_sqlAddslashes($db) . '\' '
1054 // . ' AND master_table IN (' . $intable . ')'
1055 // . ' AND foreign_table IN (' . $intable . ')';
1056 // $result = PMA_query_as_cu($sql);
1059 // previous logic was checking master tables and foreign tables
1060 // but I think that looping on every table of the pdf page as a master
1061 // and finding its foreigns is OK (then we can support innodb)
1062 $seen_a_relation = false;
1063 foreach ($alltables AS $one_table) {
1064 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
1066 $seen_a_relation = true;
1067 foreach ($exist_rel AS $master_field => $rel) {
1068 // put the foreign table on the schema only if selected
1070 // (do not use array_search() because we would have to
1071 // to do a === FALSE and this is not PHP3 compatible)
1072 if (in_array($rel['foreign_table'], $alltables)) {
1073 $this->PMA_RT_addRelation($one_table, $master_field, $rel['foreign_table'], $rel['foreign_field']);
1078 // loic1: also show tables without relations
1079 // $norelations = TRUE;
1080 // if ($result && PMA_DBI_num_rows($result) > 0) {
1081 // $norelations = FALSE;
1082 // while ($row = PMA_DBI_fetch_assoc($result)) {
1083 // $this->PMA_RT_addRelation($row['master_table'], $row['master_field'], $row['foreign_table'], $row['foreign_field']);
1086 // if ($norelations == FALSE) {
1087 if ($seen_a_relation) {
1088 $this->PMA_RT_drawRelations($change_color);
1091 $this->PMA_RT_drawTables($show_info, $change_color);
1093 $this->PMA_RT_showRt();
1094 } // end of the "PMA_RT()" method
1095 } // end of the "PMA_RT" class
1097 function PMA_RT_DOC($alltables)
1099 global $db, $pdf, $orientation, $paper;
1101 $pdf->addpage($GLOBALS['orientation']);
1102 $pdf->Cell(0, 9, $GLOBALS['strTableOfContents'], 1, 0, 'C');
1105 foreach ($alltables AS $table) {
1106 $pdf->PMA_links
['doc'][$table]['-'] = $pdf->AddLink();
1109 $pdf->Cell(0, 6, $GLOBALS['strPageNumber'] . ' {' . sprintf("%02d", $i) . '}', 0, 0, 'R', 0, $pdf->PMA_links
['doc'][$table]['-']);
1111 $pdf->Cell(0, 6, $i . ' ' . $table, 0, 1, 'L', 0, $pdf->PMA_links
['doc'][$table]['-']);
1113 $result = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';');
1114 while ($row = PMA_DBI_fetch_assoc($result)) {
1116 $field_name = $row['Field'];
1117 $pdf->PMA_links
['doc'][$table][$field_name] = $pdf->AddLink();
1118 // $pdf->Cell(0, 6, $field_name,0,1,'L',0, $pdf->PMA_links['doc'][$table][$field_name]);
1120 $lasttable = $table;
1123 $pdf->PMA_links
['RT']['-'] = $pdf->AddLink();
1125 $pdf->Cell(0, 6, $GLOBALS['strPageNumber'] . ' {00}', 0, 0, 'R', 0, $pdf->PMA_links
['doc'][$lasttable]['-']);
1127 $pdf->Cell(0, 6, $i . ' ' . $GLOBALS['strRelationalSchema'], 0, 1, 'L', 0, $pdf->PMA_links
['RT']['-']);
1129 foreach ($alltables AS $table) {
1131 $pdf->addpage($GLOBALS['orientation']);
1132 $pdf->Bookmark($table);
1133 $pdf->SetAlias('{' . sprintf("%02d", $z) . '}', $pdf->PageNo()) ;
1134 $pdf->PMA_links
['RT'][$table]['-'] = $pdf->AddLink();
1135 $pdf->SetLink($pdf->PMA_links
['doc'][$table]['-'], -1);
1136 $pdf->SetFont('', 'B', 18);
1137 $pdf->Cell(0, 8, $z . ' ' . $table, 1, 1, 'C', 0, $pdf->PMA_links
['RT'][$table]['-']);
1138 $pdf->SetFont('', '', 8);
1141 $cfgRelation = PMA_getRelationsParam();
1142 $comments = PMA_getComments($db, $table);
1143 if ($cfgRelation['mimework']) {
1144 $mime_map = PMA_getMIME($db, $table, true);
1148 * Gets table informations
1150 $result = PMA_DBI_query('SHOW TABLE STATUS LIKE \'' . PMA_sqlAddslashes($table, true) . '\';', null, PMA_DBI_QUERY_STORE
);
1151 $showtable = PMA_DBI_fetch_assoc($result);
1152 $num_rows = (isset($showtable['Rows']) ?
$showtable['Rows'] : 0);
1153 $show_comment = (isset($showtable['Comment']) ?
$showtable['Comment'] : '');
1154 $create_time = (isset($showtable['Create_time']) ?
PMA_localisedDate(strtotime($showtable['Create_time'])) : '');
1155 $update_time = (isset($showtable['Update_time']) ?
PMA_localisedDate(strtotime($showtable['Update_time'])) : '');
1156 $check_time = (isset($showtable['Check_time']) ?
PMA_localisedDate(strtotime($showtable['Check_time'])) : '');
1158 PMA_DBI_free_result($result);
1162 * Gets table keys and retains them
1164 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
1168 $indexes_info = array();
1169 $indexes_data = array();
1170 $pk_array = array(); // will be use to emphasis prim. keys in the table
1172 while ($row = PMA_DBI_fetch_assoc($result)) {
1173 // Backups the list of primary keys
1174 if ($row['Key_name'] == 'PRIMARY') {
1175 $primary .= $row['Column_name'] . ', ';
1176 $pk_array[$row['Column_name']] = 1;
1178 // Retains keys informations
1179 if ($row['Key_name'] != $lastIndex) {
1180 $indexes[] = $row['Key_name'];
1181 $lastIndex = $row['Key_name'];
1183 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
1184 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
1185 if (isset($row['Cardinality'])) {
1186 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
1188 // I don't know what does following column mean....
1189 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
1190 $indexes_info[$row['Key_name']]['Comment'] = $row['Comment'];
1192 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
1193 if (isset($row['Sub_part'])) {
1194 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
1198 PMA_DBI_free_result($result);
1202 * Gets fields properties
1204 $result = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE
);
1205 $fields_cnt = PMA_DBI_num_rows($result);
1206 // Check if we can use Relations (Mike Beck)
1207 if (!empty($cfgRelation['relation'])) {
1208 // Find which tables are related with the current one and write it in
1210 $res_rel = PMA_getForeigners($db, $table);
1212 if (count($res_rel) > 0) {
1221 * Displays the comments of the table if MySQL >= 3.23
1225 if (!empty($show_comment)) {
1226 $pdf->Cell(0, 3, $GLOBALS['strTableComments'] . ' : ' . $show_comment, 0, 1);
1230 if (!empty($create_time)) {
1231 $pdf->Cell(0, 3, $GLOBALS['strStatCreateTime'] . ': ' . $create_time, 0, 1);
1235 if (!empty($update_time)) {
1236 $pdf->Cell(0, 3, $GLOBALS['strStatUpdateTime'] . ': ' . $update_time, 0, 1);
1240 if (!empty($check_time)) {
1241 $pdf->Cell(0, 3, $GLOBALS['strStatCheckTime'] . ': ' . $check_time, 0, 1);
1245 if ($break == true) {
1246 $pdf->Cell(0, 3, '', 0, 1);
1250 $pdf->SetFont('', 'B');
1251 if (isset($orientation) && $orientation == 'L') {
1252 $pdf->Cell(25, 8, ucfirst($GLOBALS['strField']), 1, 0, 'C');
1253 $pdf->Cell(20, 8, ucfirst($GLOBALS['strType']), 1, 0, 'C');
1254 $pdf->Cell(20, 8, ucfirst($GLOBALS['strAttr']), 1, 0, 'C');
1255 $pdf->Cell(10, 8, ucfirst($GLOBALS['strNull']), 1, 0, 'C');
1256 $pdf->Cell(20, 8, ucfirst($GLOBALS['strDefault']), 1, 0, 'C');
1257 $pdf->Cell(25, 8, ucfirst($GLOBALS['strExtra']), 1, 0, 'C');
1258 $pdf->Cell(45, 8, ucfirst($GLOBALS['strLinksTo']), 1, 0, 'C');
1260 if ($paper == 'A4') {
1261 $comments_width = 67;
1263 // this is really intended for 'letter'
1265 * @todo find optimal width for all formats
1267 $comments_width = 50;
1269 $pdf->Cell($comments_width, 8, ucfirst($GLOBALS['strComments']), 1, 0, 'C');
1270 $pdf->Cell(45, 8, 'MIME', 1, 1, 'C');
1271 $pdf->SetWidths(array(25, 20, 20, 10, 20, 25, 45, $comments_width, 45));
1273 $pdf->Cell(20, 8, ucfirst($GLOBALS['strField']), 1, 0, 'C');
1274 $pdf->Cell(20, 8, ucfirst($GLOBALS['strType']), 1, 0, 'C');
1275 $pdf->Cell(20, 8, ucfirst($GLOBALS['strAttr']), 1, 0, 'C');
1276 $pdf->Cell(10, 8, ucfirst($GLOBALS['strNull']), 1, 0, 'C');
1277 $pdf->Cell(15, 8, ucfirst($GLOBALS['strDefault']), 1, 0, 'C');
1278 $pdf->Cell(15, 8, ucfirst($GLOBALS['strExtra']), 1, 0, 'C');
1279 $pdf->Cell(30, 8, ucfirst($GLOBALS['strLinksTo']), 1, 0, 'C');
1280 $pdf->Cell(30, 8, ucfirst($GLOBALS['strComments']), 1, 0, 'C');
1281 $pdf->Cell(30, 8, 'MIME', 1, 1, 'C');
1282 $pdf->SetWidths(array(20, 20, 20, 10, 15, 15, 30, 30, 30));
1284 $pdf->SetFont('', '');
1286 while ($row = PMA_DBI_fetch_assoc($result)) {
1287 $type = $row['Type'];
1288 // reformat mysql query output - staybyte - 9. June 2001
1289 // loic1: set or enum types: slashes single quotes inside options
1290 if (preg_match('@^(set|enum)\((.+)\)$@i', $type, $tmp)) {
1291 $tmp[2] = substr(preg_replace("@([^,])''@", "\\1\\'", ',' . $tmp[2]), 1);
1292 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
1299 $type_nowrap = ' nowrap="nowrap"';
1300 $type = preg_replace('@BINARY@i', '', $type);
1301 $type = preg_replace('@ZEROFILL@i', '', $type);
1302 $type = preg_replace('@UNSIGNED@i', '', $type);
1307 $binary = stristr($row['Type'], 'BINARY');
1308 $unsigned = stristr($row['Type'], 'UNSIGNED');
1309 $zerofill = stristr($row['Type'], 'ZEROFILL');
1311 $strAttribute = ' ';
1313 $strAttribute = 'BINARY';
1316 $strAttribute = 'UNSIGNED';
1319 $strAttribute = 'UNSIGNED ZEROFILL';
1321 if (!isset($row['Default'])) {
1322 if ($row['Null'] != '' && $row['Null'] != 'NO') {
1323 $row['Default'] = 'NULL';
1326 $field_name = $row['Field'];
1328 $pdf->PMA_links
['RT'][$table][$field_name] = $pdf->AddLink();
1329 $pdf->Bookmark($field_name, 1, -1);
1330 $pdf->SetLink($pdf->PMA_links
['doc'][$table][$field_name], -1);
1331 $pdf_row = array($field_name,
1334 ($row['Null'] == '' ||
$row['Null'] == 'NO') ?
$GLOBALS['strNo'] : $GLOBALS['strYes'],
1335 ((isset($row['Default'])) ?
$row['Default'] : ''),
1337 ((isset($res_rel[$field_name])) ?
$res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field'] : ''),
1338 ((isset($comments[$field_name])) ?
$comments[$field_name] : ''),
1339 ((isset($mime_map) && isset($mime_map[$field_name])) ?
str_replace('_', '/', $mime_map[$field_name]['mimetype']) : '')
1341 $links[0] = $pdf->PMA_links
['RT'][$table][$field_name];
1342 if (isset($res_rel[$field_name]['foreign_table']) AND
1343 isset($res_rel[$field_name]['foreign_field']) AND
1344 isset($pdf->PMA_links
['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']])
1347 $links[6] = $pdf->PMA_links
['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']];
1351 $pdf->Row($pdf_row, $links);
1353 /*$pdf->Cell(20, 8, $field_name, 1, 0, 'L', 0, $pdf->PMA_links['RT'][$table][$field_name]);
1354 //echo ' ' . $field_name . ' ' . "\n";
1356 $pdf->Cell(20, 8, $type, 1, 0, 'L');
1357 $pdf->Cell(20, 8, $strAttribute, 1, 0, 'L');
1358 $pdf->Cell(15, 8, , 1, 0, 'L');
1359 $pdf->Cell(15, 8, ((isset($row['Default'])) ? $row['Default'] : ''),1,0,'L');
1360 $pdf->Cell(15, 8, $row['Extra'], 1, 0, 'L');
1362 if (isset($res_rel[$field_name])) {
1363 $pdf->Cell(30, 8, $res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field'],1,0,'L');
1366 if ($cfgRelation['commwork']) {
1367 if (isset($comments[$field_name])) {
1368 $pdf->Cell(0, 8, $comments[$field_name], 1, 0, 'L');
1372 $pdf->SetFont('', '', 14);
1373 PMA_DBI_free_result($result);
1375 } // end function PMA_RT_DOC
1380 if (!isset($pdf_page_number)) {
1381 $pdf_page_number = 1;
1384 $show_grid = (isset($show_grid) && $show_grid == 'on') ?
1 : 0;
1385 $show_color = (isset($show_color) && $show_color == 'on') ?
1 : 0;
1386 $show_table_dimension = (isset($show_table_dimension) && $show_table_dimension == 'on') ?
1 : 0;
1387 $all_tab_same_wide = (isset($all_tab_same_wide) && $all_tab_same_wide == 'on') ?
1 : 0;
1388 $with_doc = (isset($with_doc) && $with_doc == 'on') ?
1 : 0;
1389 $orientation = (isset($orientation) && $orientation == 'P') ?
'P' : 'L';
1390 $paper = isset($paper) ?
$paper : 'A4';
1391 $show_keys = (isset($show_keys) && $show_keys == 'on') ?
1 : 0;
1392 PMA_DBI_select_db($db);
1394 $rt = new PMA_RT($pdf_page_number, $show_table_dimension, $show_color, $show_grid, $all_tab_same_wide, $orientation, $paper, $show_keys);