2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Contributed by Maxime Delorme and merged by lem9
11 * Gets some core scripts
13 require_once './libraries/common.inc.php';
16 * Settings for relation stuff
18 require_once './libraries/relation.lib.php';
19 require_once './libraries/transformations.lib.php';
20 require_once './libraries/Index.class.php';
22 $cfgRelation = PMA_getRelationsParam();
25 * Now in ./libraries/relation.lib.php we check for all tables
26 * that we need, but if we don't find them we are quiet about it
27 * so people can work without.
28 * This page is absolutely useless if you didn't set up your tables
29 * correctly, so it is a good place to see which tables we can and
32 if (!$cfgRelation['pdfwork']) {
33 echo '<font color="red">' . $strError . '</font><br />' . "\n";
34 $url_to_goto = '<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">';
35 echo sprintf($strRelationNotWorking, $url_to_goto, '</a>') . "\n";
41 * @todo Make this configuratble (at least Sans/Serif).
43 define('PMA_PDF_FONT', 'DejaVuSans');
44 require_once './libraries/tcpdf/tcpdf.php';
47 * Extends the "FPDF" class and prepares the work
52 class PMA_PDF
extends TCPDF
{
54 * Defines private properties
62 var $Outlines = array();
67 public function getFh()
72 public function getFw()
77 public function setCMargin($c_margin)
79 $this->cMargin
= $c_margin;
82 function SetAlias($name, $value)
84 $this->Alias
[$name] = $value ;
89 if (count($this->Alias
) > 0) {
91 foreach ($this->Alias
AS $alias => $value) {
92 for ($n = 1;$n <= $nb;$n++
)
93 $this->pages
[$n]=str_replace($alias, $value, $this->pages
[$n]);
100 * Sets the scaling factor, defines minimum coordinates and margins
102 * @param double $ The scaling factor
103 * @param double $ The minimum X coordinate
104 * @param double $ The minimum Y coordinate
105 * @param double $ The left margin
106 * @param double $ The top margin
109 function PMA_PDF_setScale($scale = 1, $x_min = 0, $y_min = 0, $l_marg = -1, $t_marg = -1)
111 $this->scale
= $scale;
112 $this->x_min
= $x_min;
113 $this->y_min
= $y_min;
114 if ($this->l_marg
!= -1) {
115 $this->l_marg
= $l_marg;
117 if ($this->t_marg
!= -1) {
118 $this->t_marg
= $t_marg;
120 } // end of the "PMA_PDF_setScale" function
122 * Outputs a scaled cell
124 * @param double $ The cell width
125 * @param double $ The cell height
126 * @param string $ The text to output
127 * @param mixed $ Whether to add borders or not
128 * @param integer $ Where to put the cursor once the output is done
129 * @param string $ Align mode
130 * @param integer $ Whether to fill the cell with a color or not
134 function PMA_PDF_cellScale($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $fill = 0, $link = '')
136 $h = $h / $this->scale
;
137 $w = $w / $this->scale
;
138 $this->Cell($w, $h, $txt, $border, $ln, $align, $fill, $link);
139 } // end of the "PMA_PDF_cellScale" function
141 * Draws a scaled line
143 * @param double $ The horizontal position of the starting point
144 * @param double $ The vertical position of the starting point
145 * @param double $ The horizontal position of the ending point
146 * @param double $ The vertical position of the ending point
150 function PMA_PDF_lineScale($x1, $y1, $x2, $y2)
152 $x1 = ($x1 - $this->x_min
) / $this->scale +
$this->l_marg
;
153 $y1 = ($y1 - $this->y_min
) / $this->scale +
$this->t_marg
;
154 $x2 = ($x2 - $this->x_min
) / $this->scale +
$this->l_marg
;
155 $y2 = ($y2 - $this->y_min
) / $this->scale +
$this->t_marg
;
156 $this->Line($x1, $y1, $x2, $y2);
157 } // end of the "PMA_PDF_lineScale" function
159 * Sets x and y scaled positions
161 * @param double $ The x position
162 * @param double $ The y position
166 function PMA_PDF_setXyScale($x, $y)
168 $x = ($x - $this->x_min
) / $this->scale +
$this->l_marg
;
169 $y = ($y - $this->y_min
) / $this->scale +
$this->t_marg
;
170 $this->SetXY($x, $y);
171 } // end of the "PMA_PDF_setXyScale" function
173 * Sets the X scaled positions
175 * @param double $ The x position
179 function PMA_PDF_setXScale($x)
181 $x = ($x - $this->x_min
) / $this->scale +
$this->l_marg
;
183 } // end of the "PMA_PDF_setXScale" function
185 * Sets the scaled font size
187 * @param double $ The font size (in points)
189 * @see FPDF::SetFontSize()
191 function PMA_PDF_setFontSizeScale($size)
193 // Set font size in points
194 $size = $size / $this->scale
;
195 $this->SetFontSize($size);
196 } // end of the "PMA_PDF_setFontSizeScale" function
198 * Sets the scaled line width
200 * @param double $ The line width
202 * @see FPDF::SetLineWidth()
204 function PMA_PDF_setLineWidthScale($width)
206 $width = $width / $this->scale
;
207 $this->SetLineWidth($width);
208 } // end of the "PMA_PDF_setLineWidthScale" function
210 * Displays an error message
212 * @param string $ the error mesage
213 * @global array the PMA configuration array
214 * @global integer the current server id
215 * @global string the current language
216 * @global string the charset to convert to
217 * @global string the current database name
218 * @global string the current charset
219 * @global string the current text direction
220 * @global string a localized string
221 * @global string an other localized string
224 function PMA_PDF_die($error_message = '')
227 global $server, $lang, $convcharset, $db;
228 global $charset, $text_dir, $strRunning, $strDatabase;
230 require_once './libraries/header.inc.php';
232 echo '<p><strong>PDF - ' . $GLOBALS['strError'] . '</strong></p>' . "\n";
233 if (!empty($error_message)) {
234 $error_message = htmlspecialchars($error_message);
237 echo ' ' . $error_message . "\n";
240 echo '<a href="db_structure.php?' . PMA_generate_common_url($db)
241 . '">' . $GLOBALS['strBack'] . '</a>';
244 require_once './libraries/footer.inc.php';
245 } // end of the "PMA_PDF_die()" function
247 * Aliases the "Error()" function from the FPDF class to the
248 * "PMA_PDF_die()" one
250 * @param string $ the error mesage
254 function Error($error_message = '')
256 $this->PMA_PDF_die($error_message);
257 } // end of the "Error()" method
261 // We only show this if we find something in the new pdf_pages table
263 // This function must be named "Header" to work with the FPDF library
264 global $cfgRelation, $db, $pdf_page_number, $with_doc;
266 $test_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
267 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
268 . ' AND page_nr = \'' . $pdf_page_number . '\'';
269 $test_rs = PMA_query_as_cu($test_query);
270 $pages = @PMA_DBI_fetch_assoc
($test_rs);
271 $this->SetFont('', 'B', 14);
272 $this->Cell(0, 6, ucfirst($pages['page_descr']), 'B', 1, 'C');
273 $this->SetFont('', '');
279 // This function must be named "Footer" to work with the FPDF library
283 $this->SetFont('', '', 14);
284 $this->Cell(0, 6, $GLOBALS['strPageNumber'] . ' ' . $this->PageNo() . '/{nb}', 'T', 0, 'C');
285 $this->Cell(0, 6, PMA_localisedDate(), 0, 1, 'R');
289 function Bookmark($txt, $level = 0, $y = 0)
292 $this->Outlines
[0][] = $level;
293 $this->Outlines
[1][] = $txt;
294 $this->Outlines
[2][] = $this->page
;
298 $this->Outlines
[3][] = round($this->hPt
- $y * $this->k
, 2);
301 function _putbookmarks()
303 if (count($this->Outlines
) > 0) {
304 // Save object number
306 // Take the number of sub elements for an outline
307 $nb_outlines = sizeof($this->Outlines
[0]);
308 $first_level = array();
311 for ($i = 0; $i < $nb_outlines; $i++
) {
312 $level = $this->Outlines
[0][$i];
319 // Take the previous outline in the same level
320 while ($this->Outlines
[0][$cursor] > $level && $cursor > 0)
322 if ($this->Outlines
[0][$cursor] == $level) {
326 if ($i < $nb_outlines-1) {
328 while (isset($this->Outlines
[0][$cursor]) && $this->Outlines
[0][$cursor] > $level) {
329 // Take the immediate kid in level + 1
330 if ($this->Outlines
[0][$cursor] == $level +
1) {
337 // Take the next outline in the same level
338 while ($this->Outlines
[0][$cursor] > $level && ($cursor +
1 < sizeof($this->Outlines
[0])))
340 if ($this->Outlines
[0][$cursor] == $level) {
345 $parent[$level +
1] = $this->n
;
347 $first_level[] = $this->n
;
350 $this->_out('/Title (' . $this->Outlines
[1][$i] . ')');
351 $this->_out('/Parent ' . $parent[$level] . ' 0 R');
353 $this->_out('/Prev ' . ($memo_n +
$prev +
1) . ' 0 R');
356 $this->_out('/Next ' . ($this->n +
$next - $i) . ' 0 R');
358 $this->_out('/Dest [' . (1 +
(2 * $this->Outlines
[2][$i])) . ' 0 R /XYZ null ' . $this->Outlines
[3][$i] . ' null]');
360 $this->_out('/First ' . ($this->n +
1) . ' 0 R');
361 $this->_out('/Last ' . ($this->n +
$last - $i) . ' 0 R');
362 $this->_out('/Count -' . $kids);
365 $this->_out('endobj');
367 // First page of outlines
369 $this->def_outlines
= $this->n
;
371 $this->_out('/Type');
372 $this->_out('/Outlines');
373 $this->_out('/First ' . $first_level[0] . ' 0 R');
374 $this->_out('/Last ' . $first_level[sizeof($first_level)-1] . ' 0 R');
375 $this->_out('/Count ' . sizeof($first_level));
377 $this->_out('endobj');
381 function _putresources()
383 parent
::_putresources();
384 $this->_putbookmarks();
387 function _putcatalog()
389 parent
::_putcatalog();
390 if (count($this->Outlines
) > 0) {
391 $this->_out('/Outlines ' . $this->def_outlines
. ' 0 R');
392 $this->_out('/PageMode /UseOutlines');
395 function SetWidths($w)
401 function Row($data, $links)
405 $data_cnt = count($data);
406 for ($i = 0;$i < $data_cnt;$i++
)
407 $nb = max($nb, $this->NbLines($this->widths
[$i], $data[$i]));
408 $il = $this->FontSize
;
409 $h = ($il +
1) * $nb;
410 // page break if necessary
411 $this->CheckPageBreak($h);
413 $data_cnt = count($data);
414 for ($i = 0;$i < $data_cnt;$i++
) {
415 $w = $this->widths
[$i];
416 // save current position
420 $this->Rect($x, $y, $w, $h);
421 if (isset($links[$i])) {
422 $this->Link($x, $y, $w, $h, $links[$i]);
425 $this->MultiCell($w, $il +
1, $data[$i], 0, 'L');
427 $this->SetXY($x +
$w, $y);
433 function CheckPageBreak($h)
435 // if height h overflows, manual page break
436 if ($this->GetY() +
$h > $this->PageBreakTrigger
) {
437 $this->AddPage($this->CurOrientation
);
441 function NbLines($w, $txt)
443 // compute number of lines used by a multicell of width w
444 $cw = &$this->CurrentFont
['cw'];
446 $w = $this->w
- $this->rMargin
- $this->x
;
448 $wmax = ($w-2 * $this->cMargin
) * 1000 / $this->FontSize
;
449 $s = str_replace("\r", '', $txt);
451 if ($nb > 0 and $s[$nb-1] == "\n") {
472 $l +
= isset($cw[ord($c)])?
$cw[ord($c)]:0 ;
491 } // end of the "PMA_PDF" class
495 * Draws tables schema
502 * Defines private properties
508 var $fields = array();
509 var $height_cell = 6;
511 var $primary = array();
514 * Sets the width of the table
516 * @param integer $ The font size
517 * @global object The current PDF document
521 function PMA_RT_Table_setWidth($ff)
523 // this looks buggy to me... does it really work if
524 // there are fields that require wider cells than the name of the table?
527 foreach ($this->fields
AS $field) {
528 $this->width
= max($this->width
, $pdf->GetStringWidth($field));
530 $this->width +
= $pdf->GetStringWidth(' ');
531 $pdf->SetFont($ff, 'B');
532 $this->width
= max($this->width
, $pdf->GetStringWidth(' ' . $this->table_name
));
533 $pdf->SetFont($ff, '');
534 } // end of the "PMA_RT_Table_setWidth()" method
536 * Sets the height of the table
540 function PMA_RT_Table_setHeight()
542 $this->height
= (count($this->fields
) +
1) * $this->height_cell
;
543 } // end of the "PMA_RT_Table_setHeight()" method
547 * @param boolean $ Whether to display table position or not
548 * @param integer $ The font size
549 * @param boolean $ Whether to display color
550 * @param integer $ The max. with among tables
551 * @global object The current PDF document
555 function PMA_RT_Table_draw($show_info, $ff, $setcolor = 0)
557 global $pdf, $with_doc;
559 $pdf->PMA_PDF_setXyScale($this->x
, $this->y
);
560 $pdf->SetFont($ff, 'B');
562 $pdf->SetTextColor(200);
563 $pdf->SetFillColor(0, 0, 128);
566 $pdf->SetLink($pdf->PMA_links
['RT'][$this->table_name
]['-'], -1);
568 $pdf->PMA_links
['doc'][$this->table_name
]['-'] = '';
572 $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
]['-']);
574 $pdf->PMA_PDF_cellScale($this->width
, $this->height_cell
, $this->table_name
, 1, 1, 'C', $setcolor, $pdf->PMA_links
['doc'][$this->table_name
]['-']);
576 $pdf->PMA_PDF_setXScale($this->x
);
577 $pdf->SetFont($ff, '');
578 $pdf->SetTextColor(0);
579 $pdf->SetFillColor(255);
581 foreach ($this->fields
AS $field) {
583 // if (in_array($field, $this->primary)) {
585 if (in_array($field, $this->primary
)) {
586 $pdf->SetFillColor(215, 121, 123);
588 if ($field == $this->displayfield
) {
589 $pdf->SetFillColor(142, 159, 224);
593 $pdf->SetLink($pdf->PMA_links
['RT'][$this->table_name
][$field], -1);
595 $pdf->PMA_links
['doc'][$this->table_name
][$field] = '';
598 $pdf->PMA_PDF_cellScale($this->width
, $this->height_cell
, ' ' . $field, 1, 1, 'L', $setcolor, $pdf->PMA_links
['doc'][$this->table_name
][$field]);
599 $pdf->PMA_PDF_setXScale($this->x
);
600 $pdf->SetFillColor(255);
602 /*if ($pdf->PageNo() > 1) {
603 $pdf->PMA_PDF_die($GLOBALS['strScaleFactorSmall']);
605 } // end of the "PMA_RT_Table_draw()" method
607 * The "PMA_RT_Table" constructor
609 * @param string $ The table name
610 * @param integer $ The font size
611 * @param integer $ The max. with among tables
612 * @global object The current PDF document
613 * @global integer The current page number (from the
614 * $cfg['Servers'][$i]['table_coords'] table)
615 * @global array The relations settings
616 * @global string The current db name
618 * @see PMA_PDF, PMA_RT_Table::PMA_RT_Table_setWidth,
619 PMA_RT_Table::PMA_RT_Table_setHeight
621 function __construct($table_name, $ff, &$same_wide_width, $show_keys)
623 global $pdf, $pdf_page_number, $cfgRelation, $db;
625 $this->table_name
= $table_name;
626 $sql = 'DESCRIBE ' . PMA_backquote($table_name);
627 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE
);
628 if (!$result ||
!PMA_DBI_num_rows($result)) {
629 $pdf->PMA_PDF_die(sprintf($GLOBALS['strPdfInvalidTblName'], $table_name));
632 //check to see if it will load all fields or only the foreign keys
634 $indexes = PMA_Index
::getFromTable($this->table_name
, $db);
635 $all_columns = array();
636 foreach ($indexes as $index) {
637 $all_columns = array_merge($all_columns, array_flip(array_keys($index->getColumns())));
639 $this->fields
= array_keys($all_columns);
641 while ($row = PMA_DBI_fetch_row($result)) {
642 $this->fields
[] = $row[0];
646 $this->PMA_RT_Table_setWidth($ff);
647 $this->PMA_RT_Table_setHeight();
648 if ($same_wide_width < $this->width
) {
649 $same_wide_width = $this->width
;
652 $sql = 'SELECT x, y FROM '
653 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
654 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
655 . ' AND table_name = \'' . PMA_sqlAddslashes($table_name) . '\''
656 . ' AND pdf_page_number = ' . $pdf_page_number;
657 $result = PMA_query_as_cu($sql, false, PMA_DBI_QUERY_STORE
);
659 if (!$result ||
!PMA_DBI_num_rows($result)) {
660 $pdf->PMA_PDF_die(sprintf($GLOBALS['strConfigureTableCoord'], $table_name));
662 list($this->x
, $this->y
) = PMA_DBI_fetch_row($result);
663 $this->x
= (double) $this->x
;
664 $this->y
= (double) $this->y
;
666 $this->displayfield
= PMA_getDisplayField($db, $table_name);
668 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($table_name) . ';', null, PMA_DBI_QUERY_STORE
);
669 if (PMA_DBI_num_rows($result) > 0) {
670 while ($row = PMA_DBI_fetch_assoc($result)) {
671 if ($row['Key_name'] == 'PRIMARY') {
672 $this->primary
[] = $row['Column_name'];
676 } // end of the "PMA_RT_Table()" method
677 } // end class "PMA_RT_Table"
679 * Draws relation links
684 class PMA_RT_Relation
{
686 * Defines private properties
691 var $x_dest, $y_dest;
695 * Gets arrows coordinates
697 * @param string $ The current table name
698 * @param string $ The relation column name
699 * @return array Arrows coordinates
702 function PMA_RT_Relation_getXy($table, $column)
704 $pos = array_search($column, $table->fields
);
705 // x_left, x_right, y
706 return array($table->x
, $table->x + +
$table->width
, $table->y +
($pos +
1.5) * $table->height_cell
);
707 } // end of the "PMA_RT_Relation_getXy()" method
709 * Do draws relation links
711 * @param boolean $ Whether to use one color per relation or not
712 * @param integer $ The id of the link to draw
713 * @global object The current PDF document
717 function PMA_RT_Relation_draw($change_color, $i)
734 list ($a, $b, $c) = $case[$d];
735 $e = (1 - ($j - 1) / 6);
736 $pdf->SetDrawColor($a * 255 * $e, $b * 255 * $e, $c * 255 * $e);
738 $pdf->SetDrawColor(0);
739 } // end if... else...
740 $pdf->PMA_PDF_setLineWidthScale(0.2);
741 $pdf->PMA_PDF_lineScale($this->x_src
, $this->y_src
, $this->x_src +
$this->src_dir
* $this->w_tick
, $this->y_src
);
742 $pdf->PMA_PDF_lineScale($this->x_dest +
$this->dest_dir
* $this->w_tick
, $this->y_dest
, $this->x_dest
, $this->y_dest
);
743 $pdf->PMA_PDF_setLineWidthScale(0.1);
744 $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
);
746 $root2 = 2 * sqrt(2);
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);
748 $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);
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->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);
752 $pdf->SetDrawColor(0);
753 } // end of the "PMA_RT_Relation_draw()" method
755 * The "PMA_RT_Relation" constructor
757 * @param string $ The master table name
758 * @param string $ The relation field in the master table
759 * @param string $ The foreign table name
760 * @param string $ The relation field in the foreign table
762 * @see PMA_RT_Relation::PMA_RT_Relation_getXy
764 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
766 $src_pos = $this->PMA_RT_Relation_getXy($master_table, $master_field);
767 $dest_pos = $this->PMA_RT_Relation_getXy($foreign_table, $foreign_field);
768 $src_left = $src_pos[0] - $this->w_tick
;
769 $src_right = $src_pos[1] +
$this->w_tick
;
770 $dest_left = $dest_pos[0] - $this->w_tick
;
771 $dest_right = $dest_pos[1] +
$this->w_tick
;
773 $d1 = abs($src_left - $dest_left);
774 $d2 = abs($src_right - $dest_left);
775 $d3 = abs($src_left - $dest_right);
776 $d4 = abs($src_right - $dest_right);
777 $d = min($d1, $d2, $d3, $d4);
780 $this->x_src
= $src_pos[0];
782 $this->x_dest
= $dest_pos[0];
783 $this->dest_dir
= -1;
784 } elseif ($d == $d2) {
785 $this->x_src
= $src_pos[1];
787 $this->x_dest
= $dest_pos[0];
788 $this->dest_dir
= -1;
789 } elseif ($d == $d3) {
790 $this->x_src
= $src_pos[0];
792 $this->x_dest
= $dest_pos[1];
795 $this->x_src
= $src_pos[1];
797 $this->x_dest
= $dest_pos[1];
800 $this->y_src
= $src_pos[2];
801 $this->y_dest
= $dest_pos[2];
802 } // end of the "PMA_RT_Relation()" method
803 } // end of the "PMA_RT_Relation" class
805 * Draws and send the database schema
812 * Defines private properties
814 var $tables = array();
815 var $relations = array();
816 var $ff = PMA_PDF_FONT
;
830 * Sets X and Y minimum and maximum for a table cell
832 * @param string $ The table name
835 function PMA_RT_setMinMax($table)
837 $this->x_max
= max($this->x_max
, $table->x +
$table->width
);
838 $this->y_max
= max($this->y_max
, $table->y +
$table->height
);
839 $this->x_min
= min($this->x_min
, $table->x
);
840 $this->y_min
= min($this->y_min
, $table->y
);
841 } // end of the "PMA_RT_setMinMax()" method
843 * Defines relation objects
845 * @param string $ The master table name
846 * @param string $ The relation field in the master table
847 * @param string $ The foreign table name
848 * @param string $ The relation field in the foreign table
850 * @see PMA_RT_setMinMax
852 function PMA_RT_addRelation($master_table, $master_field, $foreign_table, $foreign_field)
854 if (!isset($this->tables
[$master_table])) {
855 $this->tables
[$master_table] = new PMA_RT_Table($master_table, $this->ff
, $this->tablewidth
);
856 $this->PMA_RT_setMinMax($this->tables
[$master_table]);
858 if (!isset($this->tables
[$foreign_table])) {
859 $this->tables
[$foreign_table] = new PMA_RT_Table($foreign_table, $this->ff
, $this->tablewidth
);
860 $this->PMA_RT_setMinMax($this->tables
[$foreign_table]);
862 $this->relations
[] = new PMA_RT_Relation($this->tables
[$master_table], $master_field, $this->tables
[$foreign_table], $foreign_field);
863 } // end of the "PMA_RT_addRelation()" method
867 * @global object the current PMA_PDF instance
871 function PMA_RT_strokeGrid()
875 $pdf->SetMargins(0, 0);
876 $pdf->SetDrawColor(200, 200, 200);
877 // Draws horizontal lines
878 for ($l = 0; $l < 21; $l++
) {
879 $pdf->line(0, $l * 10, $pdf->fh
, $l * 10);
882 $pdf->SetXY(0, $l * 10);
883 $label = (string) sprintf('%.0f', ($l * 10 - $this->t_marg
) * $this->scale +
$this->y_min
);
884 $pdf->Cell(5, 5, ' ' . $label);
887 // Draws vertical lines
888 for ($j = 0; $j < 30 ;$j++
) {
889 $pdf->line($j * 10, 0, $j * 10, $pdf->fw
);
890 $pdf->SetXY($j * 10, 0);
891 $label = (string) sprintf('%.0f', ($j * 10 - $this->l_marg
) * $this->scale +
$this->x_min
);
892 $pdf->Cell(5, 7, $label);
894 } // end of the "PMA_RT_strokeGrid()" method
896 * Draws relation arrows
898 * @param boolean $ Whether to use one color per relation or not
900 * @see PMA_RT_Relation::PMA_RT_Relation_draw()
902 function PMA_RT_drawRelations($change_color)
905 foreach ($this->relations
AS $relation) {
906 $relation->PMA_RT_Relation_draw($change_color, $i);
909 } // end of the "PMA_RT_drawRelations()" method
913 * @param boolean $ Whether to display table position or not
915 * @see PMA_RT_Table::PMA_RT_Table_draw()
917 function PMA_RT_drawTables($show_info, $draw_color = 0)
919 foreach ($this->tables
AS $table) {
920 $table->PMA_RT_Table_draw($show_info, $this->ff
, $draw_color);
922 } // end of the "PMA_RT_drawTables()" method
924 * Ouputs the PDF document to a file
926 * @global object The current PDF document
927 * @global string The current database name
928 * @global integer The current page number (from the
929 * $cfg['Servers'][$i]['table_coords'] table)
933 function PMA_RT_showRt()
935 global $pdf, $db, $pdf_page_number, $cfgRelation;
937 $pdf->SetFontSize(14);
938 $pdf->SetLineWidth(0.2);
939 $pdf->SetDisplayMode('fullpage');
940 // Get the name of this pdfpage to use as filename (Mike Beck)
941 $_name_sql = 'SELECT page_descr FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
942 . ' WHERE page_nr = ' . $pdf_page_number;
943 $_name_rs = PMA_query_as_cu($_name_sql);
945 $_name_row = PMA_DBI_fetch_row($_name_rs);
946 $filename = $_name_row[0] . '.pdf';
948 // i don't know if there is a chance for this to happen, but rather be on the safe side:
949 if (empty($filename)) {
950 $filename = $pdf_page_number . '.pdf';
952 // $pdf->Output($db . '_' . $filename, TRUE);
953 $pdf->Output($db . '_' . $filename, 'I'); // destination: Inline
954 } // end of the "PMA_RT_showRt()" method
956 * The "PMA_RT" constructor
958 * @param mixed $ The scaling factor
959 * @param integer $ The page number to draw (from the
960 * $cfg['Servers'][$i]['table_coords'] table)
961 * @param boolean $ Whether to display table position or not
962 * @param boolean $ Was originally whether to use one color per
963 * relation or not, now enables/disables color
964 * everywhere, due to some problems printing with color
965 * @param boolean $ Whether to draw grids or not
966 * @param boolean $ Whether all tables should have the same width or not
967 * @param boolean $ Wheter to show all field or only the keys
968 * @global object The current PDF document
969 * @global string The current db name
970 * @global array The relations settings
974 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)
976 global $pdf, $db, $cfgRelation, $with_doc;
978 $this->same_wide
= $all_tab_same_wide;
979 // Initializes a new document
980 $pdf = new PMA_PDF('L', 'mm', $paper);
981 $pdf->SetTitle(sprintf($GLOBALS['strPdfDbSchema'], $GLOBALS['db'], $which_rel));
984 $pdf->SetAuthor('phpMyAdmin ' . PMA_VERSION
);
985 $pdf->AliasNbPages();
986 $pdf->AddFont('DejaVuSans', '', 'dejavusans.php');
987 $pdf->AddFont('DejaVuSans', 'B', 'dejavusansb.php');
988 $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php');
989 $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserifb.php');
990 $this->ff
= PMA_PDF_FONT
;
991 $pdf->SetFont($this->ff
, '', 14);
992 $pdf->SetAutoPageBreak('auto');
993 // Gets tables on this page
994 $tab_sql = 'SELECT table_name FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
995 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
996 . ' AND pdf_page_number = ' . $which_rel;
997 $tab_rs = PMA_query_as_cu($tab_sql, null, PMA_DBI_QUERY_STORE
);
998 if (!$tab_rs ||
!PMA_DBI_num_rows($tab_rs) > 0) {
999 $pdf->PMA_PDF_die($GLOBALS['strPdfNoTables']);
1000 // die('No tables');
1001 } while ($curr_table = @PMA_DBI_fetch_assoc
($tab_rs)) {
1002 $alltables[] = PMA_sqlAddslashes($curr_table['table_name']);
1003 // $intable = '\'' . implode('\', \'', $alltables) . '\'';
1007 $pdf->SetAutoPageBreak('auto', 15);
1008 $pdf->setCMargin(1);
1009 PMA_RT_DOC($alltables);
1010 $pdf->SetAutoPageBreak('auto');
1011 $pdf->setCMargin(0);
1017 $pdf->SetLink($pdf->PMA_links
['RT']['-'], -1);
1018 $pdf->Bookmark($GLOBALS['strRelationalSchema']);
1019 $pdf->SetAlias('{00}', $pdf->PageNo()) ;
1026 foreach ($alltables AS $table) {
1027 if (!isset($this->tables
[$table])) {
1028 $this->tables
[$table] = new PMA_RT_Table($table, $this->ff
, $this->tablewidth
, $show_keys);
1031 if ($this->same_wide
) {
1032 $this->tables
[$table]->width
= $this->tablewidth
;
1034 $this->PMA_RT_setMinMax($this->tables
[$table]);
1036 // Defines the scale factor
1037 $this->scale
= ceil(
1039 ($this->x_max
- $this->x_min
) / ($pdf->getFh() - $this->r_marg
- $this->l_marg
),
1040 ($this->y_max
- $this->y_min
) / ($pdf->getFw() - $this->t_marg
- $this->b_marg
))
1043 $pdf->PMA_PDF_setScale($this->scale
, $this->x_min
, $this->y_min
, $this->l_marg
, $this->t_marg
);
1044 // Builds and save the PDF document
1045 $pdf->PMA_PDF_setLineWidthScale(0.1);
1048 $pdf->SetFontSize(10);
1049 $this->PMA_RT_strokeGrid();
1051 $pdf->PMA_PDF_setFontSizeScale(14);
1052 // $sql = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
1053 // . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\' '
1054 // . ' AND foreign_db = \'' . PMA_sqlAddslashes($db) . '\' '
1055 // . ' AND master_table IN (' . $intable . ')'
1056 // . ' AND foreign_table IN (' . $intable . ')';
1057 // $result = PMA_query_as_cu($sql);
1060 // previous logic was checking master tables and foreign tables
1061 // but I think that looping on every table of the pdf page as a master
1062 // and finding its foreigns is OK (then we can support innodb)
1063 $seen_a_relation = false;
1064 foreach ($alltables AS $one_table) {
1065 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
1067 $seen_a_relation = true;
1068 foreach ($exist_rel AS $master_field => $rel) {
1069 // put the foreign table on the schema only if selected
1071 // (do not use array_search() because we would have to
1072 // to do a === FALSE and this is not PHP3 compatible)
1073 if (in_array($rel['foreign_table'], $alltables)) {
1074 $this->PMA_RT_addRelation($one_table, $master_field, $rel['foreign_table'], $rel['foreign_field']);
1079 // loic1: also show tables without relations
1080 // $norelations = TRUE;
1081 // if ($result && PMA_DBI_num_rows($result) > 0) {
1082 // $norelations = FALSE;
1083 // while ($row = PMA_DBI_fetch_assoc($result)) {
1084 // $this->PMA_RT_addRelation($row['master_table'], $row['master_field'], $row['foreign_table'], $row['foreign_field']);
1087 // if ($norelations == FALSE) {
1088 if ($seen_a_relation) {
1089 $this->PMA_RT_drawRelations($change_color);
1092 $this->PMA_RT_drawTables($show_info, $change_color);
1094 $this->PMA_RT_showRt();
1095 } // end of the "PMA_RT()" method
1096 } // end of the "PMA_RT" class
1098 function PMA_RT_DOC($alltables)
1100 global $db, $pdf, $orientation, $paper;
1102 $pdf->addpage($GLOBALS['orientation']);
1103 $pdf->Cell(0, 9, $GLOBALS['strTableOfContents'], 1, 0, 'C');
1106 foreach ($alltables AS $table) {
1107 $pdf->PMA_links
['doc'][$table]['-'] = $pdf->AddLink();
1110 $pdf->Cell(0, 6, $GLOBALS['strPageNumber'] . ' {' . sprintf("%02d", $i) . '}', 0, 0, 'R', 0, $pdf->PMA_links
['doc'][$table]['-']);
1112 $pdf->Cell(0, 6, $i . ' ' . $table, 0, 1, 'L', 0, $pdf->PMA_links
['doc'][$table]['-']);
1114 $result = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';');
1115 while ($row = PMA_DBI_fetch_assoc($result)) {
1117 $field_name = $row['Field'];
1118 $pdf->PMA_links
['doc'][$table][$field_name] = $pdf->AddLink();
1119 // $pdf->Cell(0, 6, $field_name,0,1,'L',0, $pdf->PMA_links['doc'][$table][$field_name]);
1121 $lasttable = $table;
1124 $pdf->PMA_links
['RT']['-'] = $pdf->AddLink();
1126 $pdf->Cell(0, 6, $GLOBALS['strPageNumber'] . ' {00}', 0, 0, 'R', 0, $pdf->PMA_links
['doc'][$lasttable]['-']);
1128 $pdf->Cell(0, 6, $i . ' ' . $GLOBALS['strRelationalSchema'], 0, 1, 'L', 0, $pdf->PMA_links
['RT']['-']);
1130 foreach ($alltables AS $table) {
1132 $pdf->addpage($GLOBALS['orientation']);
1133 $pdf->Bookmark($table);
1134 $pdf->SetAlias('{' . sprintf("%02d", $z) . '}', $pdf->PageNo()) ;
1135 $pdf->PMA_links
['RT'][$table]['-'] = $pdf->AddLink();
1136 $pdf->SetLink($pdf->PMA_links
['doc'][$table]['-'], -1);
1137 $pdf->SetFont('', 'B', 18);
1138 $pdf->Cell(0, 8, $z . ' ' . $table, 1, 1, 'C', 0, $pdf->PMA_links
['RT'][$table]['-']);
1139 $pdf->SetFont('', '', 8);
1142 $cfgRelation = PMA_getRelationsParam();
1143 $comments = PMA_getComments($db, $table);
1144 if ($cfgRelation['mimework']) {
1145 $mime_map = PMA_getMIME($db, $table, true);
1149 * Gets table informations
1151 $result = PMA_DBI_query('SHOW TABLE STATUS LIKE \'' . PMA_sqlAddslashes($table, true) . '\';', null, PMA_DBI_QUERY_STORE
);
1152 $showtable = PMA_DBI_fetch_assoc($result);
1153 $num_rows = (isset($showtable['Rows']) ?
$showtable['Rows'] : 0);
1154 $show_comment = (isset($showtable['Comment']) ?
$showtable['Comment'] : '');
1155 $create_time = (isset($showtable['Create_time']) ?
PMA_localisedDate(strtotime($showtable['Create_time'])) : '');
1156 $update_time = (isset($showtable['Update_time']) ?
PMA_localisedDate(strtotime($showtable['Update_time'])) : '');
1157 $check_time = (isset($showtable['Check_time']) ?
PMA_localisedDate(strtotime($showtable['Check_time'])) : '');
1159 PMA_DBI_free_result($result);
1163 * Gets table keys and retains them
1165 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
1169 $indexes_info = array();
1170 $indexes_data = array();
1171 $pk_array = array(); // will be use to emphasis prim. keys in the table
1173 while ($row = PMA_DBI_fetch_assoc($result)) {
1174 // Backups the list of primary keys
1175 if ($row['Key_name'] == 'PRIMARY') {
1176 $primary .= $row['Column_name'] . ', ';
1177 $pk_array[$row['Column_name']] = 1;
1179 // Retains keys informations
1180 if ($row['Key_name'] != $lastIndex) {
1181 $indexes[] = $row['Key_name'];
1182 $lastIndex = $row['Key_name'];
1184 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
1185 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
1186 if (isset($row['Cardinality'])) {
1187 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
1189 // I don't know what does following column mean....
1190 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
1191 $indexes_info[$row['Key_name']]['Comment'] = $row['Comment'];
1193 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
1194 if (isset($row['Sub_part'])) {
1195 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
1199 PMA_DBI_free_result($result);
1203 * Gets fields properties
1205 $result = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE
);
1206 $fields_cnt = PMA_DBI_num_rows($result);
1207 // Check if we can use Relations (Mike Beck)
1208 if (!empty($cfgRelation['relation'])) {
1209 // Find which tables are related with the current one and write it in
1211 $res_rel = PMA_getForeigners($db, $table);
1213 if (count($res_rel) > 0) {
1222 * Displays the comments of the table if MySQL >= 3.23
1226 if (!empty($show_comment)) {
1227 $pdf->Cell(0, 3, $GLOBALS['strTableComments'] . ' : ' . $show_comment, 0, 1);
1231 if (!empty($create_time)) {
1232 $pdf->Cell(0, 3, $GLOBALS['strStatCreateTime'] . ': ' . $create_time, 0, 1);
1236 if (!empty($update_time)) {
1237 $pdf->Cell(0, 3, $GLOBALS['strStatUpdateTime'] . ': ' . $update_time, 0, 1);
1241 if (!empty($check_time)) {
1242 $pdf->Cell(0, 3, $GLOBALS['strStatCheckTime'] . ': ' . $check_time, 0, 1);
1246 if ($break == true) {
1247 $pdf->Cell(0, 3, '', 0, 1);
1251 $pdf->SetFont('', 'B');
1252 if (isset($orientation) && $orientation == 'L') {
1253 $pdf->Cell(25, 8, ucfirst($GLOBALS['strField']), 1, 0, 'C');
1254 $pdf->Cell(20, 8, ucfirst($GLOBALS['strType']), 1, 0, 'C');
1255 $pdf->Cell(20, 8, ucfirst($GLOBALS['strAttr']), 1, 0, 'C');
1256 $pdf->Cell(10, 8, ucfirst($GLOBALS['strNull']), 1, 0, 'C');
1257 $pdf->Cell(20, 8, ucfirst($GLOBALS['strDefault']), 1, 0, 'C');
1258 $pdf->Cell(25, 8, ucfirst($GLOBALS['strExtra']), 1, 0, 'C');
1259 $pdf->Cell(45, 8, ucfirst($GLOBALS['strLinksTo']), 1, 0, 'C');
1261 if ($paper == 'A4') {
1262 $comments_width = 67;
1264 // this is really intended for 'letter'
1266 * @todo find optimal width for all formats
1268 $comments_width = 50;
1270 $pdf->Cell($comments_width, 8, ucfirst($GLOBALS['strComments']), 1, 0, 'C');
1271 $pdf->Cell(45, 8, 'MIME', 1, 1, 'C');
1272 $pdf->SetWidths(array(25, 20, 20, 10, 20, 25, 45, $comments_width, 45));
1274 $pdf->Cell(20, 8, ucfirst($GLOBALS['strField']), 1, 0, 'C');
1275 $pdf->Cell(20, 8, ucfirst($GLOBALS['strType']), 1, 0, 'C');
1276 $pdf->Cell(20, 8, ucfirst($GLOBALS['strAttr']), 1, 0, 'C');
1277 $pdf->Cell(10, 8, ucfirst($GLOBALS['strNull']), 1, 0, 'C');
1278 $pdf->Cell(15, 8, ucfirst($GLOBALS['strDefault']), 1, 0, 'C');
1279 $pdf->Cell(15, 8, ucfirst($GLOBALS['strExtra']), 1, 0, 'C');
1280 $pdf->Cell(30, 8, ucfirst($GLOBALS['strLinksTo']), 1, 0, 'C');
1281 $pdf->Cell(30, 8, ucfirst($GLOBALS['strComments']), 1, 0, 'C');
1282 $pdf->Cell(30, 8, 'MIME', 1, 1, 'C');
1283 $pdf->SetWidths(array(20, 20, 20, 10, 15, 15, 30, 30, 30));
1285 $pdf->SetFont('', '');
1287 while ($row = PMA_DBI_fetch_assoc($result)) {
1288 $type = $row['Type'];
1289 // reformat mysql query output - staybyte - 9. June 2001
1290 // loic1: set or enum types: slashes single quotes inside options
1291 if (preg_match('@^(set|enum)\((.+)\)$@i', $type, $tmp)) {
1292 $tmp[2] = substr(preg_replace("@([^,])''@", "\\1\\'", ',' . $tmp[2]), 1);
1293 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
1300 $type_nowrap = ' nowrap="nowrap"';
1301 $type = preg_replace('@BINARY@i', '', $type);
1302 $type = preg_replace('@ZEROFILL@i', '', $type);
1303 $type = preg_replace('@UNSIGNED@i', '', $type);
1308 $binary = stristr($row['Type'], 'BINARY');
1309 $unsigned = stristr($row['Type'], 'UNSIGNED');
1310 $zerofill = stristr($row['Type'], 'ZEROFILL');
1312 $strAttribute = ' ';
1314 $strAttribute = 'BINARY';
1317 $strAttribute = 'UNSIGNED';
1320 $strAttribute = 'UNSIGNED ZEROFILL';
1322 if (!isset($row['Default'])) {
1323 if ($row['Null'] != '' && $row['Null'] != 'NO') {
1324 $row['Default'] = 'NULL';
1327 $field_name = $row['Field'];
1329 $pdf->PMA_links
['RT'][$table][$field_name] = $pdf->AddLink();
1330 $pdf->Bookmark($field_name, 1, -1);
1331 $pdf->SetLink($pdf->PMA_links
['doc'][$table][$field_name], -1);
1332 $pdf_row = array($field_name,
1335 ($row['Null'] == '' ||
$row['Null'] == 'NO') ?
$GLOBALS['strNo'] : $GLOBALS['strYes'],
1336 ((isset($row['Default'])) ?
$row['Default'] : ''),
1338 ((isset($res_rel[$field_name])) ?
$res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field'] : ''),
1339 ((isset($comments[$field_name])) ?
$comments[$field_name] : ''),
1340 ((isset($mime_map) && isset($mime_map[$field_name])) ?
str_replace('_', '/', $mime_map[$field_name]['mimetype']) : '')
1342 $links[0] = $pdf->PMA_links
['RT'][$table][$field_name];
1343 if (isset($res_rel[$field_name]['foreign_table']) AND
1344 isset($res_rel[$field_name]['foreign_field']) AND
1345 isset($pdf->PMA_links
['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']])
1348 $links[6] = $pdf->PMA_links
['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']];
1352 $pdf->Row($pdf_row, $links);
1354 /*$pdf->Cell(20, 8, $field_name, 1, 0, 'L', 0, $pdf->PMA_links['RT'][$table][$field_name]);
1355 //echo ' ' . $field_name . ' ' . "\n";
1357 $pdf->Cell(20, 8, $type, 1, 0, 'L');
1358 $pdf->Cell(20, 8, $strAttribute, 1, 0, 'L');
1359 $pdf->Cell(15, 8, , 1, 0, 'L');
1360 $pdf->Cell(15, 8, ((isset($row['Default'])) ? $row['Default'] : ''),1,0,'L');
1361 $pdf->Cell(15, 8, $row['Extra'], 1, 0, 'L');
1363 if (isset($res_rel[$field_name])) {
1364 $pdf->Cell(30, 8, $res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field'],1,0,'L');
1367 if ($cfgRelation['commwork']) {
1368 if (isset($comments[$field_name])) {
1369 $pdf->Cell(0, 8, $comments[$field_name], 1, 0, 'L');
1373 $pdf->SetFont('', '', 14);
1374 PMA_DBI_free_result($result);
1376 } // end function PMA_RT_DOC
1381 if (!isset($pdf_page_number)) {
1382 $pdf_page_number = 1;
1385 $show_grid = (isset($show_grid) && $show_grid == 'on') ?
1 : 0;
1386 $show_color = (isset($show_color) && $show_color == 'on') ?
1 : 0;
1387 $show_table_dimension = (isset($show_table_dimension) && $show_table_dimension == 'on') ?
1 : 0;
1388 $all_tab_same_wide = (isset($all_tab_same_wide) && $all_tab_same_wide == 'on') ?
1 : 0;
1389 $with_doc = (isset($with_doc) && $with_doc == 'on') ?
1 : 0;
1390 $orientation = (isset($orientation) && $orientation == 'P') ?
'P' : 'L';
1391 $paper = isset($paper) ?
$paper : 'A4';
1392 $show_keys = (isset($show_keys) && $show_keys == 'on') ?
1 : 0;
1393 PMA_DBI_select_db($db);
1395 $rt = new PMA_RT($pdf_page_number, $show_table_dimension, $show_color, $show_grid, $all_tab_same_wide, $orientation, $paper, $show_keys);