2 /* vim: set expandtab sw=4 ts=4 sts=4: */
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
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_controluser($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
499 * @package phpMyAdmin
503 * Defines private properties
509 var $fields = array();
510 var $height_cell = 6;
512 var $primary = array();
513 var $show_info = false;
516 * Returns title of the current table,
517 * title can have the dimensions of the table
523 return ($this->show_info ?
sprintf('%.0f', $this->width
) . 'x' . sprintf('%.0f', $this->height
) : '') . ' ' . $this->table_name
;
524 } // end of the "getTitle" function
526 * Sets the width of the table
528 * @param integer $ The font size
529 * @global object The current PDF document
533 function PMA_RT_Table_setWidth($ff)
537 foreach ($this->fields
AS $field) {
538 $this->width
= max($this->width
, $pdf->GetStringWidth($field));
540 $this->width +
= $pdf->GetStringWidth(' ');
541 $pdf->SetFont($ff, 'B');
542 // it is unknown what value must be added, because
543 // table title is affected by the tabe width value
544 while ($this->width
< $pdf->GetStringWidth($this->getTitle())) {
547 $pdf->SetFont($ff, '');
548 } // end of the "PMA_RT_Table_setWidth()" method
550 * Sets the height of the table
554 function PMA_RT_Table_setHeight()
556 $this->height
= (count($this->fields
) +
1) * $this->height_cell
;
557 } // end of the "PMA_RT_Table_setHeight()" method
561 * @param integer $ The font size
562 * @param boolean $ Whether to display color
563 * @param integer $ The max. with among tables
564 * @global object The current PDF document
568 function PMA_RT_Table_draw($ff, $setcolor = 0)
570 global $pdf, $with_doc;
572 $pdf->PMA_PDF_setXyScale($this->x
, $this->y
);
573 $pdf->SetFont($ff, 'B');
575 $pdf->SetTextColor(200);
576 $pdf->SetFillColor(0, 0, 128);
579 $pdf->SetLink($pdf->PMA_links
['RT'][$this->table_name
]['-'], -1);
581 $pdf->PMA_links
['doc'][$this->table_name
]['-'] = '';
584 $pdf->PMA_PDF_cellScale($this->width
, $this->height_cell
, $this->getTitle(), 1, 1, 'C', $setcolor, $pdf->PMA_links
['doc'][$this->table_name
]['-']);
585 $pdf->PMA_PDF_setXScale($this->x
);
586 $pdf->SetFont($ff, '');
587 $pdf->SetTextColor(0);
588 $pdf->SetFillColor(255);
590 foreach ($this->fields
AS $field) {
592 if (in_array($field, $this->primary
)) {
593 $pdf->SetFillColor(215, 121, 123);
595 if ($field == $this->displayfield
) {
596 $pdf->SetFillColor(142, 159, 224);
600 $pdf->SetLink($pdf->PMA_links
['RT'][$this->table_name
][$field], -1);
602 $pdf->PMA_links
['doc'][$this->table_name
][$field] = '';
605 $pdf->PMA_PDF_cellScale($this->width
, $this->height_cell
, ' ' . $field, 1, 1, 'L', $setcolor, $pdf->PMA_links
['doc'][$this->table_name
][$field]);
606 $pdf->PMA_PDF_setXScale($this->x
);
607 $pdf->SetFillColor(255);
609 /*if ($pdf->PageNo() > 1) {
610 $pdf->PMA_PDF_die($GLOBALS['strScaleFactorSmall']);
612 } // end of the "PMA_RT_Table_draw()" method
614 * The "PMA_RT_Table" constructor
616 * @param string $ The table name
617 * @param integer $ The font size
618 * @param integer $ The max. with among tables
619 * @param boolean $ Whether to display keys or not
620 * @param boolean $ Whether to display table position or not
621 * @global object The current PDF document
622 * @global integer The current page number (from the
623 * $cfg['Servers'][$i]['table_coords'] table)
624 * @global array The relations settings
625 * @global string The current db name
627 * @see PMA_PDF, PMA_RT_Table::PMA_RT_Table_setWidth,
628 PMA_RT_Table::PMA_RT_Table_setHeight
630 function __construct($table_name, $ff, &$same_wide_width, $show_keys = false, $show_info = false)
632 global $pdf, $pdf_page_number, $cfgRelation, $db;
634 $this->table_name
= $table_name;
635 $sql = 'DESCRIBE ' . PMA_backquote($table_name);
636 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE
);
637 if (!$result ||
!PMA_DBI_num_rows($result)) {
638 $pdf->PMA_PDF_die(sprintf($GLOBALS['strPdfInvalidTblName'], $table_name));
641 //check to see if it will load all fields or only the foreign keys
643 $indexes = PMA_Index
::getFromTable($this->table_name
, $db);
644 $all_columns = array();
645 foreach ($indexes as $index) {
646 $all_columns = array_merge($all_columns, array_flip(array_keys($index->getColumns())));
648 $this->fields
= array_keys($all_columns);
650 while ($row = PMA_DBI_fetch_row($result)) {
651 $this->fields
[] = $row[0];
655 $this->show_info
= $show_info;
658 $this->PMA_RT_Table_setHeight();
659 // setWidth must me after setHeight, because title
660 // can include table height which changes table width
661 $this->PMA_RT_Table_setWidth($ff);
662 if ($same_wide_width < $this->width
) {
663 $same_wide_width = $this->width
;
666 $sql = 'SELECT x, y FROM '
667 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
668 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
669 . ' AND table_name = \'' . PMA_sqlAddslashes($table_name) . '\''
670 . ' AND pdf_page_number = ' . $pdf_page_number;
671 $result = PMA_query_as_controluser($sql, false, PMA_DBI_QUERY_STORE
);
673 if (!$result ||
!PMA_DBI_num_rows($result)) {
674 $pdf->PMA_PDF_die(sprintf($GLOBALS['strConfigureTableCoord'], $table_name));
676 list($this->x
, $this->y
) = PMA_DBI_fetch_row($result);
677 $this->x
= (double) $this->x
;
678 $this->y
= (double) $this->y
;
680 $this->displayfield
= PMA_getDisplayField($db, $table_name);
682 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($table_name) . ';', null, PMA_DBI_QUERY_STORE
);
683 if (PMA_DBI_num_rows($result) > 0) {
684 while ($row = PMA_DBI_fetch_assoc($result)) {
685 if ($row['Key_name'] == 'PRIMARY') {
686 $this->primary
[] = $row['Column_name'];
690 } // end of the "PMA_RT_Table()" method
691 } // end class "PMA_RT_Table"
693 * Draws relation links
697 * @package phpMyAdmin
699 class PMA_RT_Relation
{
701 * Defines private properties
706 var $x_dest, $y_dest;
710 * Gets arrows coordinates
712 * @param string $ The current table name
713 * @param string $ The relation column name
714 * @return array Arrows coordinates
717 function PMA_RT_Relation_getXy($table, $column)
719 $pos = array_search($column, $table->fields
);
720 // x_left, x_right, y
721 return array($table->x
, $table->x + +
$table->width
, $table->y +
($pos +
1.5) * $table->height_cell
);
722 } // end of the "PMA_RT_Relation_getXy()" method
724 * Do draws relation links
726 * @param boolean $ Whether to use one color per relation or not
727 * @param integer $ The id of the link to draw
728 * @global object The current PDF document
732 function PMA_RT_Relation_draw($change_color, $i)
749 list ($a, $b, $c) = $case[$d];
750 $e = (1 - ($j - 1) / 6);
751 $pdf->SetDrawColor($a * 255 * $e, $b * 255 * $e, $c * 255 * $e);
753 $pdf->SetDrawColor(0);
754 } // end if... else...
755 $pdf->PMA_PDF_setLineWidthScale(0.2);
756 $pdf->PMA_PDF_lineScale($this->x_src
, $this->y_src
, $this->x_src +
$this->src_dir
* $this->w_tick
, $this->y_src
);
757 $pdf->PMA_PDF_lineScale($this->x_dest +
$this->dest_dir
* $this->w_tick
, $this->y_dest
, $this->x_dest
, $this->y_dest
);
758 $pdf->PMA_PDF_setLineWidthScale(0.1);
759 $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
);
761 $root2 = 2 * sqrt(2);
762 $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);
763 $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);
765 $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);
766 $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);
767 $pdf->SetDrawColor(0);
768 } // end of the "PMA_RT_Relation_draw()" method
770 * The "PMA_RT_Relation" constructor
772 * @param string $ The master table name
773 * @param string $ The relation field in the master table
774 * @param string $ The foreign table name
775 * @param string $ The relation field in the foreign table
777 * @see PMA_RT_Relation::PMA_RT_Relation_getXy
779 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
781 $src_pos = $this->PMA_RT_Relation_getXy($master_table, $master_field);
782 $dest_pos = $this->PMA_RT_Relation_getXy($foreign_table, $foreign_field);
783 $src_left = $src_pos[0] - $this->w_tick
;
784 $src_right = $src_pos[1] +
$this->w_tick
;
785 $dest_left = $dest_pos[0] - $this->w_tick
;
786 $dest_right = $dest_pos[1] +
$this->w_tick
;
788 $d1 = abs($src_left - $dest_left);
789 $d2 = abs($src_right - $dest_left);
790 $d3 = abs($src_left - $dest_right);
791 $d4 = abs($src_right - $dest_right);
792 $d = min($d1, $d2, $d3, $d4);
795 $this->x_src
= $src_pos[0];
797 $this->x_dest
= $dest_pos[0];
798 $this->dest_dir
= -1;
799 } elseif ($d == $d2) {
800 $this->x_src
= $src_pos[1];
802 $this->x_dest
= $dest_pos[0];
803 $this->dest_dir
= -1;
804 } elseif ($d == $d3) {
805 $this->x_src
= $src_pos[0];
807 $this->x_dest
= $dest_pos[1];
810 $this->x_src
= $src_pos[1];
812 $this->x_dest
= $dest_pos[1];
815 $this->y_src
= $src_pos[2];
816 $this->y_dest
= $dest_pos[2];
817 } // end of the "PMA_RT_Relation()" method
818 } // end of the "PMA_RT_Relation" class
820 * Draws and send the database schema
824 * @package phpMyAdmin
828 * Defines private properties
830 var $tables = array();
831 var $relations = array();
832 var $ff = PMA_PDF_FONT
;
846 * Sets X and Y minimum and maximum for a table cell
848 * @param string $ The table name
851 function PMA_RT_setMinMax($table)
853 $this->x_max
= max($this->x_max
, $table->x +
$table->width
);
854 $this->y_max
= max($this->y_max
, $table->y +
$table->height
);
855 $this->x_min
= min($this->x_min
, $table->x
);
856 $this->y_min
= min($this->y_min
, $table->y
);
857 } // end of the "PMA_RT_setMinMax()" method
859 * Defines relation objects
861 * @param string $ The master table name
862 * @param string $ The relation field in the master table
863 * @param string $ The foreign table name
864 * @param string $ The relation field in the foreign table
865 * @param boolean $ Whether to display table position or not
867 * @see PMA_RT_setMinMax
869 function PMA_RT_addRelation($master_table, $master_field, $foreign_table, $foreign_field, $show_info)
871 if (!isset($this->tables
[$master_table])) {
872 $this->tables
[$master_table] = new PMA_RT_Table($master_table, $this->ff
, $this->tablewidth
, false, $show_info);
873 $this->PMA_RT_setMinMax($this->tables
[$master_table]);
875 if (!isset($this->tables
[$foreign_table])) {
876 $this->tables
[$foreign_table] = new PMA_RT_Table($foreign_table, $this->ff
, $this->tablewidth
, false, $show_info);
877 $this->PMA_RT_setMinMax($this->tables
[$foreign_table]);
879 $this->relations
[] = new PMA_RT_Relation($this->tables
[$master_table], $master_field, $this->tables
[$foreign_table], $foreign_field);
880 } // end of the "PMA_RT_addRelation()" method
884 * @global object the current PMA_PDF instance
888 function PMA_RT_strokeGrid()
892 $pdf->SetMargins(0, 0);
893 $pdf->SetDrawColor(200, 200, 200);
894 // Draws horizontal lines
895 for ($l = 0; $l < 21; $l++
) {
896 $pdf->line(0, $l * 10, $pdf->getFh(), $l * 10);
899 $pdf->SetXY(0, $l * 10);
900 $label = (string) sprintf('%.0f', ($l * 10 - $this->t_marg
) * $this->scale +
$this->y_min
);
901 $pdf->Cell(5, 5, ' ' . $label);
904 // Draws vertical lines
905 for ($j = 0; $j < 30 ;$j++
) {
906 $pdf->line($j * 10, 0, $j * 10, $pdf->getFw());
907 $pdf->SetXY($j * 10, 0);
908 $label = (string) sprintf('%.0f', ($j * 10 - $this->l_marg
) * $this->scale +
$this->x_min
);
909 $pdf->Cell(5, 7, $label);
911 } // end of the "PMA_RT_strokeGrid()" method
913 * Draws relation arrows
915 * @param boolean $ Whether to use one color per relation or not
917 * @see PMA_RT_Relation::PMA_RT_Relation_draw()
919 function PMA_RT_drawRelations($change_color)
922 foreach ($this->relations
AS $relation) {
923 $relation->PMA_RT_Relation_draw($change_color, $i);
926 } // end of the "PMA_RT_drawRelations()" method
930 * @param boolean $ Whether to display table position or not
932 * @see PMA_RT_Table::PMA_RT_Table_draw()
934 function PMA_RT_drawTables($draw_color = 0)
936 foreach ($this->tables
AS $table) {
937 $table->PMA_RT_Table_draw($this->ff
, $draw_color);
939 } // end of the "PMA_RT_drawTables()" method
941 * Ouputs the PDF document to a file
943 * @global object The current PDF document
944 * @global string The current database name
945 * @global integer The current page number (from the
946 * $cfg['Servers'][$i]['table_coords'] table)
950 function PMA_RT_showRt()
952 global $pdf, $db, $pdf_page_number, $cfgRelation;
954 $pdf->SetFontSize(14);
955 $pdf->SetLineWidth(0.2);
956 $pdf->SetDisplayMode('fullpage');
957 // Get the name of this pdfpage to use as filename (Mike Beck)
958 $_name_sql = 'SELECT page_descr FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
959 . ' WHERE page_nr = ' . $pdf_page_number;
960 $_name_rs = PMA_query_as_controluser($_name_sql);
962 $_name_row = PMA_DBI_fetch_row($_name_rs);
963 $filename = $_name_row[0] . '.pdf';
965 // i don't know if there is a chance for this to happen, but rather be on the safe side:
966 if (empty($filename)) {
967 $filename = $pdf_page_number . '.pdf';
969 // $pdf->Output($db . '_' . $filename, TRUE);
970 $pdf->Output($db . '_' . $filename, 'I'); // destination: Inline
971 } // end of the "PMA_RT_showRt()" method
973 * The "PMA_RT" constructor
975 * @param mixed $ The scaling factor
976 * @param integer $ The page number to draw (from the
977 * $cfg['Servers'][$i]['table_coords'] table)
978 * @param boolean $ Whether to display table position or not
979 * @param boolean $ Was originally whether to use one color per
980 * relation or not, now enables/disables color
981 * everywhere, due to some problems printing with color
982 * @param boolean $ Whether to draw grids or not
983 * @param boolean $ Whether all tables should have the same width or not
984 * @param boolean $ Wheter to show all field or only the keys
985 * @global object The current PDF document
986 * @global string The current db name
987 * @global array The relations settings
991 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)
993 global $pdf, $db, $cfgRelation, $with_doc;
995 $this->same_wide
= $all_tab_same_wide;
996 // Initializes a new document
997 $pdf = new PMA_PDF('L', 'mm', $paper);
998 $pdf->SetTitle(sprintf($GLOBALS['strPdfDbSchema'], $GLOBALS['db'], $which_rel));
1001 $pdf->SetAuthor('phpMyAdmin ' . PMA_VERSION
);
1002 $pdf->AliasNbPages();
1003 $pdf->AddFont('DejaVuSans', '', 'dejavusans.php');
1004 $pdf->AddFont('DejaVuSans', 'B', 'dejavusansb.php');
1005 $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php');
1006 $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserifb.php');
1007 $this->ff
= PMA_PDF_FONT
;
1008 $pdf->SetFont($this->ff
, '', 14);
1009 $pdf->SetAutoPageBreak('auto');
1010 // Gets tables on this page
1011 $tab_sql = 'SELECT table_name FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
1012 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
1013 . ' AND pdf_page_number = ' . $which_rel;
1014 $tab_rs = PMA_query_as_controluser($tab_sql, null, PMA_DBI_QUERY_STORE
);
1015 if (!$tab_rs ||
!PMA_DBI_num_rows($tab_rs) > 0) {
1016 $pdf->PMA_PDF_die($GLOBALS['strPdfNoTables']);
1017 // die('No tables');
1018 } while ($curr_table = @PMA_DBI_fetch_assoc
($tab_rs)) {
1019 $alltables[] = PMA_sqlAddslashes($curr_table['table_name']);
1020 // $intable = '\'' . implode('\', \'', $alltables) . '\'';
1024 $pdf->SetAutoPageBreak('auto', 15);
1025 $pdf->setCMargin(1);
1026 PMA_RT_DOC($alltables);
1027 $pdf->SetAutoPageBreak('auto');
1028 $pdf->setCMargin(0);
1034 $pdf->SetLink($pdf->PMA_links
['RT']['-'], -1);
1035 $pdf->Bookmark($GLOBALS['strRelationalSchema']);
1036 $pdf->SetAlias('{00}', $pdf->PageNo()) ;
1043 foreach ($alltables AS $table) {
1044 if (!isset($this->tables
[$table])) {
1045 $this->tables
[$table] = new PMA_RT_Table($table, $this->ff
, $this->tablewidth
, $show_keys, $show_info);
1048 if ($this->same_wide
) {
1049 $this->tables
[$table]->width
= $this->tablewidth
;
1051 $this->PMA_RT_setMinMax($this->tables
[$table]);
1053 // Defines the scale factor
1054 $this->scale
= ceil(
1056 ($this->x_max
- $this->x_min
) / ($pdf->getFh() - $this->r_marg
- $this->l_marg
),
1057 ($this->y_max
- $this->y_min
) / ($pdf->getFw() - $this->t_marg
- $this->b_marg
))
1060 $pdf->PMA_PDF_setScale($this->scale
, $this->x_min
, $this->y_min
, $this->l_marg
, $this->t_marg
);
1061 // Builds and save the PDF document
1062 $pdf->PMA_PDF_setLineWidthScale(0.1);
1065 $pdf->SetFontSize(10);
1066 $this->PMA_RT_strokeGrid();
1068 $pdf->PMA_PDF_setFontSizeScale(14);
1069 // $sql = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
1070 // . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\' '
1071 // . ' AND foreign_db = \'' . PMA_sqlAddslashes($db) . '\' '
1072 // . ' AND master_table IN (' . $intable . ')'
1073 // . ' AND foreign_table IN (' . $intable . ')';
1074 // $result = PMA_query_as_controluser($sql);
1076 // previous logic was checking master tables and foreign tables
1077 // but I think that looping on every table of the pdf page as a master
1078 // and finding its foreigns is OK (then we can support innodb)
1079 $seen_a_relation = false;
1080 foreach ($alltables AS $one_table) {
1081 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
1083 $seen_a_relation = true;
1084 foreach ($exist_rel AS $master_field => $rel) {
1085 // put the foreign table on the schema only if selected
1087 // (do not use array_search() because we would have to
1088 // to do a === FALSE and this is not PHP3 compatible)
1089 if (in_array($rel['foreign_table'], $alltables)) {
1090 $this->PMA_RT_addRelation($one_table, $master_field, $rel['foreign_table'], $rel['foreign_field'], $show_info);
1095 // also show tables without relations
1096 // $norelations = TRUE;
1097 // if ($result && PMA_DBI_num_rows($result) > 0) {
1098 // $norelations = FALSE;
1099 // while ($row = PMA_DBI_fetch_assoc($result)) {
1100 // $this->PMA_RT_addRelation($row['master_table'], $row['master_field'], $row['foreign_table'], $row['foreign_field']);
1103 // if ($norelations == FALSE) {
1104 if ($seen_a_relation) {
1105 $this->PMA_RT_drawRelations($change_color);
1108 $this->PMA_RT_drawTables($change_color);
1110 $this->PMA_RT_showRt();
1111 } // end of the "PMA_RT()" method
1112 } // end of the "PMA_RT" class
1114 function PMA_RT_DOC($alltables)
1116 global $db, $pdf, $orientation, $paper;
1118 $pdf->addpage($GLOBALS['orientation']);
1119 $pdf->Cell(0, 9, $GLOBALS['strTableOfContents'], 1, 0, 'C');
1122 foreach ($alltables AS $table) {
1123 $pdf->PMA_links
['doc'][$table]['-'] = $pdf->AddLink();
1126 $pdf->Cell(0, 6, $GLOBALS['strPageNumber'] . ' {' . sprintf("%02d", $i) . '}', 0, 0, 'R', 0, $pdf->PMA_links
['doc'][$table]['-']);
1128 $pdf->Cell(0, 6, $i . ' ' . $table, 0, 1, 'L', 0, $pdf->PMA_links
['doc'][$table]['-']);
1130 $result = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';');
1131 while ($row = PMA_DBI_fetch_assoc($result)) {
1133 $field_name = $row['Field'];
1134 $pdf->PMA_links
['doc'][$table][$field_name] = $pdf->AddLink();
1135 // $pdf->Cell(0, 6, $field_name,0,1,'L',0, $pdf->PMA_links['doc'][$table][$field_name]);
1137 $lasttable = $table;
1140 $pdf->PMA_links
['RT']['-'] = $pdf->AddLink();
1142 $pdf->Cell(0, 6, $GLOBALS['strPageNumber'] . ' {00}', 0, 0, 'R', 0, $pdf->PMA_links
['doc'][$lasttable]['-']);
1144 $pdf->Cell(0, 6, $i . ' ' . $GLOBALS['strRelationalSchema'], 0, 1, 'L', 0, $pdf->PMA_links
['RT']['-']);
1146 foreach ($alltables AS $table) {
1148 $pdf->addpage($GLOBALS['orientation']);
1149 $pdf->Bookmark($table);
1150 $pdf->SetAlias('{' . sprintf("%02d", $z) . '}', $pdf->PageNo()) ;
1151 $pdf->PMA_links
['RT'][$table]['-'] = $pdf->AddLink();
1152 $pdf->SetLink($pdf->PMA_links
['doc'][$table]['-'], -1);
1153 $pdf->SetFont('', 'B', 18);
1154 $pdf->Cell(0, 8, $z . ' ' . $table, 1, 1, 'C', 0, $pdf->PMA_links
['RT'][$table]['-']);
1155 $pdf->SetFont('', '', 8);
1158 $cfgRelation = PMA_getRelationsParam();
1159 $comments = PMA_getComments($db, $table);
1160 if ($cfgRelation['mimework']) {
1161 $mime_map = PMA_getMIME($db, $table, true);
1165 * Gets table informations
1167 $showtable = PMA_Table
::sGetStatusInfo($db, $table);
1168 $num_rows = (isset($showtable['Rows']) ?
$showtable['Rows'] : 0);
1169 $show_comment = (isset($showtable['Comment']) ?
$showtable['Comment'] : '');
1170 $create_time = (isset($showtable['Create_time']) ?
PMA_localisedDate(strtotime($showtable['Create_time'])) : '');
1171 $update_time = (isset($showtable['Update_time']) ?
PMA_localisedDate(strtotime($showtable['Update_time'])) : '');
1172 $check_time = (isset($showtable['Check_time']) ?
PMA_localisedDate(strtotime($showtable['Check_time'])) : '');
1175 * Gets table keys and retains them
1177 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
1181 $indexes_info = array();
1182 $indexes_data = array();
1183 $pk_array = array(); // will be use to emphasis prim. keys in the table
1185 while ($row = PMA_DBI_fetch_assoc($result)) {
1186 // Backups the list of primary keys
1187 if ($row['Key_name'] == 'PRIMARY') {
1188 $primary .= $row['Column_name'] . ', ';
1189 $pk_array[$row['Column_name']] = 1;
1191 // Retains keys informations
1192 if ($row['Key_name'] != $lastIndex) {
1193 $indexes[] = $row['Key_name'];
1194 $lastIndex = $row['Key_name'];
1196 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
1197 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
1198 if (isset($row['Cardinality'])) {
1199 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
1201 // I don't know what does following column mean....
1202 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
1203 $indexes_info[$row['Key_name']]['Comment'] = $row['Comment'];
1205 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
1206 if (isset($row['Sub_part'])) {
1207 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
1211 PMA_DBI_free_result($result);
1215 * Gets fields properties
1217 $result = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE
);
1218 $fields_cnt = PMA_DBI_num_rows($result);
1219 // Check if we can use Relations (Mike Beck)
1220 if (!empty($cfgRelation['relation'])) {
1221 // Find which tables are related with the current one and write it in
1223 $res_rel = PMA_getForeigners($db, $table);
1225 if (count($res_rel) > 0) {
1234 * Displays the comments of the table if MySQL >= 3.23
1238 if (!empty($show_comment)) {
1239 $pdf->Cell(0, 3, $GLOBALS['strTableComments'] . ' : ' . $show_comment, 0, 1);
1243 if (!empty($create_time)) {
1244 $pdf->Cell(0, 3, $GLOBALS['strStatCreateTime'] . ': ' . $create_time, 0, 1);
1248 if (!empty($update_time)) {
1249 $pdf->Cell(0, 3, $GLOBALS['strStatUpdateTime'] . ': ' . $update_time, 0, 1);
1253 if (!empty($check_time)) {
1254 $pdf->Cell(0, 3, $GLOBALS['strStatCheckTime'] . ': ' . $check_time, 0, 1);
1258 if ($break == true) {
1259 $pdf->Cell(0, 3, '', 0, 1);
1263 $pdf->SetFont('', 'B');
1264 if (isset($orientation) && $orientation == 'L') {
1265 $pdf->Cell(25, 8, ucfirst($GLOBALS['strField']), 1, 0, 'C');
1266 $pdf->Cell(20, 8, ucfirst($GLOBALS['strType']), 1, 0, 'C');
1267 $pdf->Cell(20, 8, ucfirst($GLOBALS['strAttr']), 1, 0, 'C');
1268 $pdf->Cell(10, 8, ucfirst($GLOBALS['strNull']), 1, 0, 'C');
1269 $pdf->Cell(20, 8, ucfirst($GLOBALS['strDefault']), 1, 0, 'C');
1270 $pdf->Cell(25, 8, ucfirst($GLOBALS['strExtra']), 1, 0, 'C');
1271 $pdf->Cell(45, 8, ucfirst($GLOBALS['strLinksTo']), 1, 0, 'C');
1273 if ($paper == 'A4') {
1274 $comments_width = 67;
1276 // this is really intended for 'letter'
1278 * @todo find optimal width for all formats
1280 $comments_width = 50;
1282 $pdf->Cell($comments_width, 8, ucfirst($GLOBALS['strComments']), 1, 0, 'C');
1283 $pdf->Cell(45, 8, 'MIME', 1, 1, 'C');
1284 $pdf->SetWidths(array(25, 20, 20, 10, 20, 25, 45, $comments_width, 45));
1286 $pdf->Cell(20, 8, ucfirst($GLOBALS['strField']), 1, 0, 'C');
1287 $pdf->Cell(20, 8, ucfirst($GLOBALS['strType']), 1, 0, 'C');
1288 $pdf->Cell(20, 8, ucfirst($GLOBALS['strAttr']), 1, 0, 'C');
1289 $pdf->Cell(10, 8, ucfirst($GLOBALS['strNull']), 1, 0, 'C');
1290 $pdf->Cell(15, 8, ucfirst($GLOBALS['strDefault']), 1, 0, 'C');
1291 $pdf->Cell(15, 8, ucfirst($GLOBALS['strExtra']), 1, 0, 'C');
1292 $pdf->Cell(30, 8, ucfirst($GLOBALS['strLinksTo']), 1, 0, 'C');
1293 $pdf->Cell(30, 8, ucfirst($GLOBALS['strComments']), 1, 0, 'C');
1294 $pdf->Cell(30, 8, 'MIME', 1, 1, 'C');
1295 $pdf->SetWidths(array(20, 20, 20, 10, 15, 15, 30, 30, 30));
1297 $pdf->SetFont('', '');
1299 while ($row = PMA_DBI_fetch_assoc($result)) {
1300 $type = $row['Type'];
1301 // reformat mysql query output
1302 // set or enum types: slashes single quotes inside options
1303 if (preg_match('@^(set|enum)\((.+)\)$@i', $type, $tmp)) {
1304 $tmp[2] = substr(preg_replace("@([^,])''@", "\\1\\'", ',' . $tmp[2]), 1);
1305 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
1312 $type_nowrap = ' nowrap="nowrap"';
1313 $type = preg_replace('@BINARY@i', '', $type);
1314 $type = preg_replace('@ZEROFILL@i', '', $type);
1315 $type = preg_replace('@UNSIGNED@i', '', $type);
1320 $binary = stristr($row['Type'], 'BINARY');
1321 $unsigned = stristr($row['Type'], 'UNSIGNED');
1322 $zerofill = stristr($row['Type'], 'ZEROFILL');
1324 $strAttribute = ' ';
1326 $strAttribute = 'BINARY';
1329 $strAttribute = 'UNSIGNED';
1332 $strAttribute = 'UNSIGNED ZEROFILL';
1334 if (!isset($row['Default'])) {
1335 if ($row['Null'] != '' && $row['Null'] != 'NO') {
1336 $row['Default'] = 'NULL';
1339 $field_name = $row['Field'];
1341 $pdf->PMA_links
['RT'][$table][$field_name] = $pdf->AddLink();
1342 $pdf->Bookmark($field_name, 1, -1);
1343 $pdf->SetLink($pdf->PMA_links
['doc'][$table][$field_name], -1);
1344 $pdf_row = array($field_name,
1347 ($row['Null'] == '' ||
$row['Null'] == 'NO') ?
$GLOBALS['strNo'] : $GLOBALS['strYes'],
1348 ((isset($row['Default'])) ?
$row['Default'] : ''),
1350 ((isset($res_rel[$field_name])) ?
$res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field'] : ''),
1351 ((isset($comments[$field_name])) ?
$comments[$field_name] : ''),
1352 ((isset($mime_map) && isset($mime_map[$field_name])) ?
str_replace('_', '/', $mime_map[$field_name]['mimetype']) : '')
1354 $links[0] = $pdf->PMA_links
['RT'][$table][$field_name];
1355 if (isset($res_rel[$field_name]['foreign_table']) AND
1356 isset($res_rel[$field_name]['foreign_field']) AND
1357 isset($pdf->PMA_links
['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']])
1360 $links[6] = $pdf->PMA_links
['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']];
1364 $pdf->Row($pdf_row, $links);
1366 /*$pdf->Cell(20, 8, $field_name, 1, 0, 'L', 0, $pdf->PMA_links['RT'][$table][$field_name]);
1367 //echo ' ' . $field_name . ' ' . "\n";
1369 $pdf->Cell(20, 8, $type, 1, 0, 'L');
1370 $pdf->Cell(20, 8, $strAttribute, 1, 0, 'L');
1371 $pdf->Cell(15, 8, , 1, 0, 'L');
1372 $pdf->Cell(15, 8, ((isset($row['Default'])) ? $row['Default'] : ''),1,0,'L');
1373 $pdf->Cell(15, 8, $row['Extra'], 1, 0, 'L');
1375 if (isset($res_rel[$field_name])) {
1376 $pdf->Cell(30, 8, $res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field'],1,0,'L');
1379 if ($cfgRelation['commwork']) {
1380 if (isset($comments[$field_name])) {
1381 $pdf->Cell(0, 8, $comments[$field_name], 1, 0, 'L');
1385 $pdf->SetFont('', '', 14);
1386 PMA_DBI_free_result($result);
1388 } // end function PMA_RT_DOC
1393 if (!isset($pdf_page_number)) {
1394 $pdf_page_number = 1;
1397 $show_grid = (isset($show_grid) && $show_grid == 'on') ?
1 : 0;
1398 $show_color = (isset($show_color) && $show_color == 'on') ?
1 : 0;
1399 $show_table_dimension = (isset($show_table_dimension) && $show_table_dimension == 'on') ?
1 : 0;
1400 $all_tab_same_wide = (isset($all_tab_same_wide) && $all_tab_same_wide == 'on') ?
1 : 0;
1401 $with_doc = (isset($with_doc) && $with_doc == 'on') ?
1 : 0;
1402 $orientation = (isset($orientation) && $orientation == 'P') ?
'P' : 'L';
1403 $paper = isset($paper) ?
$paper : 'A4';
1404 $show_keys = (isset($show_keys) && $show_keys == 'on') ?
1 : 0;
1405 PMA_DBI_select_db($db);
1407 $rt = new PMA_RT($pdf_page_number, $show_table_dimension, $show_color, $show_grid, $all_tab_same_wide, $orientation, $paper, $show_keys);