3 // vim: expandtab sw=4 ts=4 sts=4:
6 * Contributed by Maxime Delorme and merged by lem9
11 * Gets some core scripts
13 require_once('./libraries/common.lib.php');
17 * Settings for relation stuff
19 require_once('./libraries/relation.lib.php');
20 require_once('./libraries/transformations.lib.php');
22 $cfgRelation = PMA_getRelationsParam();
26 * Now in ./libraries/relation.lib.php we check for all tables
27 * that we need, but if we don't find them we are quiet about it
28 * so people can work without.
29 * This page is absolutely useless if you didn't set up your tables
30 * correctly, so it is a good place to see which tables we can and
33 if (!$cfgRelation['pdfwork']) {
34 echo '<font color="red">' . $strError . '</font><br />' . "\n";
35 $url_to_goto = '<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">';
36 echo sprintf($strRelationNotWorking, $url_to_goto, '</a>') . "\n";
41 * Gets the "fpdf" libraries and defines the pdf font path, use unicode version for unicode.
43 define('FPDF_FONTPATH','./libraries/fpdf/font/');
44 if ($charset == 'utf-8') {
45 define('PMA_PDF_FONT', 'FreeSans');
46 require_once('./libraries/fpdf/ufpdf.php');
47 class PMA_FPDF
extends UFPDF
{
50 define('PMA_PDF_FONT', 'Arial');
51 require_once('./libraries/fpdf/fpdf.php');
52 class PMA_FPDF
extends FPDF
{
58 * Extends the "FPDF" class and prepares the work
64 class PMA_PDF
extends PMA_FPDF
67 * Defines private properties
76 var $Outlines=array();
82 * The PMA_PDF constructor
84 * This function just refers to the "FPDF" constructor: with PHP3 a class
85 * must have a constructor
87 * @param string The page orientation (p, portrait, l or landscape)
88 * @param string The unit for sizes (pt, mm, cm or in)
89 * @param mixed The page format (A3, A4, A5, letter, legal or an array
96 function PMA_PDF($orientation = 'L', $unit = 'mm', $format = 'A4')
98 $this->Alias
= array() ;
99 $this->FPDF($orientation, $unit, $format);
100 } // end of the "PMA_PDF()" method
101 function SetAlias($name, $value)
103 $this->Alias
[$name] = $value ;
107 if (count($this->Alias
) > 0)
110 foreach ($this->Alias
AS $alias => $value) {
111 for ($n=1;$n<=$nb;$n++
)
112 $this->pages
[$n]=$this->_strreplace($alias,$value,$this->pages
[$n]);
119 * Sets the scaling factor, defines minimum coordinates and margins
121 * @param double The scaling factor
122 * @param double The minimum X coordinate
123 * @param double The minimum Y coordinate
124 * @param double The left margin
125 * @param double The top margin
129 function PMA_PDF_setScale($scale = 1, $x_min = 0, $y_min = 0, $l_marg = -1, $t_marg = -1)
131 $this->scale
= $scale;
132 $this->x_min
= $x_min;
133 $this->y_min
= $y_min;
134 if ($this->l_marg
!= -1) {
135 $this->l_marg
= $l_marg;
137 if ($this->t_marg
!= -1) {
138 $this->t_marg
= $t_marg;
140 } // end of the "PMA_PDF_setScale" function
144 * Outputs a scaled cell
146 * @param double The cell width
147 * @param double The cell height
148 * @param string The text to output
149 * @param mixed Wether to add borders or not
150 * @param integer Where to put the cursor once the output is done
151 * @param string Align mode
152 * @param integer Whether to fill the cell with a color or not
158 function PMA_PDF_cellScale($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $fill = 0,$link ='')
160 $h = $h / $this->scale
;
161 $w = $w / $this->scale
;
162 $this->Cell($w, $h, $txt, $border, $ln, $align, $fill,$link);
163 } // end of the "PMA_PDF_cellScale" function
167 * Draws a scaled line
169 * @param double The horizontal position of the starting point
170 * @param double The vertical position of the starting point
171 * @param double The horizontal position of the ending point
172 * @param double The vertical position of the ending point
178 function PMA_PDF_lineScale($x1, $y1, $x2, $y2)
180 $x1 = ($x1 - $this->x_min
) / $this->scale +
$this->l_marg
;
181 $y1 = ($y1 - $this->y_min
) / $this->scale +
$this->t_marg
;
182 $x2 = ($x2 - $this->x_min
) / $this->scale +
$this->l_marg
;
183 $y2 = ($y2 - $this->y_min
) / $this->scale +
$this->t_marg
;
184 $this->Line($x1, $y1, $x2, $y2);
185 } // end of the "PMA_PDF_lineScale" function
189 * Sets x and y scaled positions
191 * @param double The x position
192 * @param double The y position
198 function PMA_PDF_setXyScale($x, $y)
200 $x = ($x - $this->x_min
) / $this->scale +
$this->l_marg
;
201 $y = ($y - $this->y_min
) / $this->scale +
$this->t_marg
;
202 $this->SetXY($x, $y);
203 } // end of the "PMA_PDF_setXyScale" function
207 * Sets the X scaled positions
209 * @param double The x position
215 function PMA_PDF_setXScale($x)
217 $x = ($x - $this->x_min
) / $this->scale +
$this->l_marg
;
219 } // end of the "PMA_PDF_setXScale" function
223 * Sets the scaled font size
225 * @param double The font size (in points)
229 * @see FPDF::SetFontSize()
231 function PMA_PDF_setFontSizeScale($size)
233 // Set font size in points
234 $size = $size / $this->scale
;
235 $this->SetFontSize($size);
236 } // end of the "PMA_PDF_setFontSizeScale" function
240 * Sets the scaled line width
242 * @param double The line width
246 * @see FPDF::SetLineWidth()
248 function PMA_PDF_setLineWidthScale($width)
250 $width = $width / $this->scale
;
251 $this->SetLineWidth($width);
252 } // end of the "PMA_PDF_setLineWidthScale" function
256 * Displays an error message
258 * @param string the error mesage
260 * @global array the PMA configuration array
261 * @global integer the current server id
262 * @global string the current language
263 * @global string the charset to convert to
264 * @global string the current database name
265 * @global string the current charset
266 * @global string the current text direction
267 * @global string a localized string
268 * @global string an other localized string
272 function PMA_PDF_die($error_message = '')
275 global $server, $lang, $convcharset, $db;
276 global $charset, $text_dir, $strRunning, $strDatabase;
278 require_once('./header.inc.php');
280 echo '<p><b>PDF - '. $GLOBALS['strError'] . '</b></p>' . "\n";
281 if (!empty($error_message)) {
282 $error_message = htmlspecialchars($error_message);
285 echo ' ' . $error_message . "\n";
288 echo '<a href="db_details_structure.php?' . PMA_generate_common_url($db)
289 . '">' . $GLOBALS['strBack'] . '</a>';
292 require_once('./footer.inc.php');
293 } // end of the "PMA_PDF_die()" function
297 * Aliases the "Error()" function from the FPDF class to the
298 * "PMA_PDF_die()" one
300 * @param string the error mesage
306 function Error($error_message = '')
308 $this->PMA_PDF_die($error_message);
309 } // end of the "Error()" method
313 // We only show this if we find something in the new pdf_pages table
315 // This function must be named "Header" to work with the FPDF library
317 global $cfgRelation,$db,$pdf_page_number,$with_doc;
319 $test_query = 'SELECT * FROM ' . PMA_backquote($cfgRelation['pdf_pages'])
320 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
321 . ' AND page_nr = \'' . $pdf_page_number . '\'';
322 $test_rs = PMA_query_as_cu($test_query);
323 $pages = @PMA_DBI_fetch_assoc
($test_rs);
324 $this->SetFont('', 'B', 14);
325 $this->Cell(0,6, ucfirst($pages['page_descr']),'B',1,'C');
326 $this->SetFont('', '');
331 // This function must be named "Footer" to work with the FPDF library
335 $this->SetFont('', '',14);
336 $this->Cell(0,6, $GLOBALS['strPageNumber'] .' '.$this->PageNo() .'/{nb}','T',0,'C');
337 $this->Cell(0,6, PMA_localisedDate(),0,1,'R');
341 function Bookmark($txt,$level=0,$y=0)
344 $this->Outlines
[0][]=$level;
345 $this->Outlines
[1][]=$txt;
346 $this->Outlines
[2][]=$this->page
;
349 $this->Outlines
[3][]=round($this->hPt
-$y*$this->k
,2);
352 function _putbookmarks()
354 if (count($this->Outlines
)>0)
358 //Take the number of sub elements for an outline
359 $nb_outlines=sizeof($this->Outlines
[0]);
360 $first_level=array();
363 for ($i=0; $i<$nb_outlines; $i++
)
365 $level=$this->Outlines
[0][$i];
373 //Take the previous outline in the same level
374 while ($this->Outlines
[0][$cursor] > $level && $cursor > 0)
376 if ($this->Outlines
[0][$cursor] == $level)
379 if ($i<$nb_outlines-1)
382 while (isset($this->Outlines
[0][$cursor]) && $this->Outlines
[0][$cursor] > $level)
384 //Take the immediate kid in level + 1
385 if ($this->Outlines
[0][$cursor] == $level+
1)
393 //Take the next outline in the same level
394 while ($this->Outlines
[0][$cursor] > $level && ($cursor+
1 < sizeof($this->Outlines
[0])))
396 if ($this->Outlines
[0][$cursor] == $level)
400 $parent[$level+
1]=$this->n
;
402 $first_level[]=$this->n
;
404 $this->_out('/Title ('.$this->Outlines
[1][$i].')');
405 $this->_out('/Parent '.$parent[$level].' 0 R');
407 $this->_out('/Prev '.($memo_n+
$prev+
1).' 0 R');
409 $this->_out('/Next '.($this->n+
$next-$i).' 0 R');
410 $this->_out('/Dest ['.(1+
(2*$this->Outlines
[2][$i])).' 0 R /XYZ null '.$this->Outlines
[3][$i].' null]');
413 $this->_out('/First '.($this->n+
1).' 0 R');
414 $this->_out('/Last '.($this->n+
$last-$i).' 0 R');
415 $this->_out('/Count -'.$kids);
418 $this->_out('endobj');
420 //First page of outlines
422 $this->def_outlines
= $this->n
;
424 $this->_out('/Type');
425 $this->_out('/Outlines');
426 $this->_out('/First '.$first_level[0].' 0 R');
427 $this->_out('/Last '.$first_level[sizeof($first_level)-1].' 0 R');
428 $this->_out('/Count '.sizeof($first_level));
430 $this->_out('endobj');
434 function _putresources()
436 parent
::_putresources();
437 $this->_putbookmarks();
440 function _putcatalog()
442 parent
::_putcatalog();
443 if (count($this->Outlines
)>0)
445 $this->_out('/Outlines '.$this->def_outlines
.' 0 R');
446 $this->_out('/PageMode /UseOutlines');
449 function SetWidths($w)
455 function Row($data,$links)
459 $data_cnt = count($data);
460 for ($i=0;$i<$data_cnt;$i++
)
461 $nb=max($nb,$this->NbLines($this->widths
[$i],$data[$i]));
462 $il = $this->FontSize
;
464 // page break if necessary
465 $this->CheckPageBreak($h);
467 $data_cnt = count($data);
468 for ($i=0;$i<$data_cnt;$i++
)
470 $w=$this->widths
[$i];
471 // save current position
475 $this->Rect($x,$y,$w,$h);
476 if (isset($links[$i]))
477 $this->Link($x,$y,$w,$h,$links[$i]);
479 $this->MultiCell($w,$il+
1,$data[$i],0,'L');
481 $this->SetXY($x+
$w,$y);
487 function CheckPageBreak($h)
489 // if height h overflows, manual page break
490 if ($this->GetY()+
$h>$this->PageBreakTrigger
)
491 $this->AddPage($this->CurOrientation
);
494 function NbLines($w,$txt)
496 // compute number of lines used by a multicell of width w
497 $cw=&$this->CurrentFont
['cw'];
499 $w=$this->w
-$this->rMargin
-$this->x
;
500 $wmax=($w-2*$this->cMargin
)*1000/$this->FontSize
;
501 $s=str_replace("\r",'',$txt);
503 if ($nb>0 and $s[$nb-1]=="\n")
524 $l+
=isset($cw[ord($c)])?
$cw[ord($c)]:0 ;
545 } // end of the "PMA_PDF" class
549 * Draws tables schema
558 * Defines private properties
564 var $fields = array();
565 var $height_cell = 6;
567 var $primary = array();
571 * Sets the width of the table
573 * @param integer The font size
575 * @global object The current PDF document
581 function PMA_RT_Table_setWidth($ff)
583 // this looks buggy to me... does it really work if
584 // there are fields that require wider cells than the name of the table?
587 foreach ($this->fields
AS $field) {
588 $this->width
= max($this->width
, $pdf->GetStringWidth($field));
590 $this->width +
= $pdf->GetStringWidth(' ');
591 $pdf->SetFont($ff, 'B');
592 $this->width
= max($this->width
, $pdf->GetStringWidth(' ' . $this->table_name
));
593 $pdf->SetFont($ff, '');
594 } // end of the "PMA_RT_Table_setWidth()" method
598 * Sets the height of the table
602 function PMA_RT_Table_setHeight()
604 $this->height
= (count($this->fields
) +
1) * $this->height_cell
;
605 } // end of the "PMA_RT_Table_setHeight()" method
611 * @param boolean Whether to display table position or not
612 * @param integer The font size
613 * @param boolean Whether to display color
614 * @param integer The max. with among tables
616 * @global object The current PDF document
622 function PMA_RT_Table_draw($show_info, $ff, $setcolor=0)
624 global $pdf, $with_doc;
626 $pdf->PMA_PDF_setXyScale($this->x
, $this->y
);
627 $pdf->SetFont($ff, 'B');
629 $pdf->SetTextColor(200);
630 $pdf->SetFillColor(0, 0, 128);
632 if ($with_doc) $pdf->SetLink($pdf->PMA_links
['RT'][$this->table_name
]['-'],-1);
633 else $pdf->PMA_links
['doc'][$this->table_name
]['-'] = '';
635 $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
]['-']);
637 $pdf->PMA_PDF_cellScale($this->width
, $this->height_cell
, $this->table_name
, 1, 1, 'C', $setcolor, $pdf->PMA_links
['doc'][$this->table_name
]['-']);
639 $pdf->PMA_PDF_setXScale($this->x
);
640 $pdf->SetFont($ff, '');
641 $pdf->SetTextColor(0);
642 $pdf->SetFillColor(255);
644 foreach ($this->fields
AS $field) {
646 // if (in_array($field, $this->primary)) {
648 if (PMA_isInto($field, $this->primary
) != -1) {
649 $pdf->SetFillColor(215, 121, 123);
651 if ($field == $this->displayfield
) {
652 $pdf->SetFillColor(142, 159, 224);
655 if ($with_doc) $pdf->SetLink($pdf->PMA_links
['RT'][$this->table_name
][$field],-1);
656 else $pdf->PMA_links
['doc'][$this->table_name
][$field] = '';
659 $pdf->PMA_PDF_cellScale($this->width
, $this->height_cell
, ' ' . $field, 1, 1, 'L', $setcolor,$pdf->PMA_links
['doc'][$this->table_name
][$field]);
660 $pdf->PMA_PDF_setXScale($this->x
);
661 $pdf->SetFillColor(255);
664 /*if ($pdf->PageNo() > 1) {
665 $pdf->PMA_PDF_die($GLOBALS['strScaleFactorSmall']);
667 } // end of the "PMA_RT_Table_draw()" method
671 * The "PMA_RT_Table" constructor
673 * @param string The table name
674 * @param integer The font size
675 * @param integer The max. with among tables
677 * @global object The current PDF document
678 * @global integer The current page number (from the
679 * $cfg['Servers'][$i]['table_coords'] table)
680 * @global array The relations settings
681 * @global string The current db name
685 * @see PMA_PDF, PMA_RT_Table::PMA_RT_Table_setWidth,
686 * PMA_RT_Table::PMA_RT_Table_setHeight
688 function PMA_RT_Table($table_name, $ff, &$same_wide_width)
690 global $pdf, $pdf_page_number, $cfgRelation, $db;
692 $this->table_name
= $table_name;
693 $sql = 'DESCRIBE ' . PMA_backquote($table_name);
694 $result = PMA_DBI_try_query($sql, NULL, PMA_DBI_QUERY_STORE
);
695 if (!$result ||
!PMA_DBI_num_rows($result)) {
696 $pdf->PMA_PDF_die(sprintf($GLOBALS['strPdfInvalidTblName'], $table_name));
699 while ($row = PMA_DBI_fetch_row($result)) {
700 $this->fields
[] = $row[0];
704 $this->PMA_RT_Table_setWidth($ff);
705 $this->PMA_RT_Table_setHeight();
706 if ($same_wide_width < $this->width
) {
707 $same_wide_width = $this->width
;
711 $sql = 'SELECT x, y FROM '
712 . PMA_backquote($cfgRelation['table_coords'])
713 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
714 . ' AND table_name = \'' . PMA_sqlAddslashes($table_name) . '\''
715 . ' AND pdf_page_number = ' . $pdf_page_number;
716 $result = PMA_query_as_cu($sql, FALSE, PMA_DBI_QUERY_STORE
);
718 if (!$result ||
!PMA_DBI_num_rows($result)) {
719 $pdf->PMA_PDF_die(sprintf($GLOBALS['strConfigureTableCoord'], $table_name));
721 list($this->x
, $this->y
) = PMA_DBI_fetch_row($result);
722 $this->x
= (double) $this->x
;
723 $this->y
= (double) $this->y
;
725 $this->displayfield
= PMA_getDisplayField($db, $table_name);
728 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($table_name) . ';', NULL, PMA_DBI_QUERY_STORE
);
729 if (PMA_DBI_num_rows($result) > 0) {
730 while ($row = PMA_DBI_fetch_assoc($result)) {
731 if ($row['Key_name'] == 'PRIMARY') {
732 $this->primary
[] = $row['Column_name'];
736 } // end of the "PMA_RT_Table()" method
737 } // end class "PMA_RT_Table"
742 * Draws relation links
748 class PMA_RT_Relation
751 * Defines private properties
756 var $x_dest, $y_dest;
761 * Gets arrows coordinates
763 * @param string The current table name
764 * @param string The relation column name
766 * @return array Arrows coordinates
770 function PMA_RT_Relation_getXy($table, $column)
772 $pos = array_search($column, $table->fields
);
773 // x_left, x_right, y
774 return array($table->x
, $table->x + +
$table->width
, $table->y +
($pos +
1.5) * $table->height_cell
);
775 } // end of the "PMA_RT_Relation_getXy()" method
779 * Do draws relation links
781 * @param boolean Whether to use one color per relation or not
782 * @param integer The id of the link to draw
784 * @global object The current PDF document
790 function PMA_RT_Relation_draw($change_color, $i)
807 list ($a, $b, $c) = $case[$d];
808 $e = (1 - ($j - 1) / 6);
809 $pdf->SetDrawColor($a * 255 * $e, $b * 255 * $e, $c * 255 * $e); }
811 $pdf->SetDrawColor(0);
812 } // end if... else...
814 $pdf->PMA_PDF_setLineWidthScale(0.2);
815 $pdf->PMA_PDF_lineScale($this->x_src
, $this->y_src
, $this->x_src +
$this->src_dir
* $this->w_tick
, $this->y_src
);
816 $pdf->PMA_PDF_lineScale($this->x_dest +
$this->dest_dir
* $this->w_tick
, $this->y_dest
, $this->x_dest
, $this->y_dest
);
817 $pdf->PMA_PDF_setLineWidthScale(0.1);
818 $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
);
821 $root2 = 2 * sqrt(2);
822 $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);
823 $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);
825 $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);
826 $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);
827 $pdf->SetDrawColor(0);
828 } // end of the "PMA_RT_Relation_draw()" method
832 * The "PMA_RT_Relation" constructor
834 * @param string The master table name
835 * @param string The relation field in the master table
836 * @param string The foreign table name
837 * @param string The relation field in the foreign table
842 * @see PMA_RT_Relation::PMA_RT_Relation_getXy
844 function PMA_RT_Relation($master_table, $master_field, $foreign_table, $foreign_field)
846 $src_pos = $this->PMA_RT_Relation_getXy($master_table , $master_field);
847 $dest_pos = $this->PMA_RT_Relation_getXy($foreign_table, $foreign_field);
848 $src_left = $src_pos[0] - $this->w_tick
;
849 $src_right = $src_pos[1] +
$this->w_tick
;
850 $dest_left = $dest_pos[0] - $this->w_tick
;
851 $dest_right = $dest_pos[1] +
$this->w_tick
;
853 $d1 = abs($src_left - $dest_left);
854 $d2 = abs($src_right - $dest_left);
855 $d3 = abs($src_left - $dest_right);
856 $d4 = abs($src_right - $dest_right);
857 $d = min($d1, $d2, $d3, $d4);
860 $this->x_src
= $src_pos[0];
862 $this->x_dest
= $dest_pos[0];
863 $this->dest_dir
= -1;
864 } else if ($d == $d2) {
865 $this->x_src
= $src_pos[1];
867 $this->x_dest
= $dest_pos[0];
868 $this->dest_dir
= -1;
869 } else if ($d == $d3) {
870 $this->x_src
= $src_pos[0];
872 $this->x_dest
= $dest_pos[1];
875 $this->x_src
= $src_pos[1];
877 $this->x_dest
= $dest_pos[1];
880 $this->y_src
= $src_pos[2];
881 $this->y_dest
= $dest_pos[2];
882 } // end of the "PMA_RT_Relation()" method
883 } // end of the "PMA_RT_Relation" class
888 * Draws and send the database schema
897 * Defines private properties
899 var $tables = array();
900 var $relations = array();
901 var $ff = PMA_PDF_FONT
;
915 * Sets X and Y minimum and maximum for a table cell
917 * @param string The table name
921 function PMA_RT_setMinMax($table)
923 $this->x_max
= max($this->x_max
, $table->x +
$table->width
);
924 $this->y_max
= max($this->y_max
, $table->y +
$table->height
);
925 $this->x_min
= min($this->x_min
, $table->x
);
926 $this->y_min
= min($this->y_min
, $table->y
);
927 } // end of the "PMA_RT_setMinMax()" method
931 * Defines relation objects
933 * @param string The master table name
934 * @param string The relation field in the master table
935 * @param string The foreign table name
936 * @param string The relation field in the foreign table
940 * @see PMA_RT_setMinMax()
942 function PMA_RT_addRelation($master_table , $master_field, $foreign_table, $foreign_field)
944 if (!isset($this->tables
[$master_table])) {
945 $this->tables
[$master_table] = new PMA_RT_Table($master_table, $this->ff
, $this->tablewidth
);
946 $this->PMA_RT_setMinMax($this->tables
[$master_table]);
948 if (!isset($this->tables
[$foreign_table])) {
949 $this->tables
[$foreign_table] = new PMA_RT_Table($foreign_table, $this->ff
, $this->tablewidth
);
950 $this->PMA_RT_setMinMax($this->tables
[$foreign_table]);
952 $this->relations
[] = new PMA_RT_Relation($this->tables
[$master_table], $master_field, $this->tables
[$foreign_table], $foreign_field);
953 } // end of the "PMA_RT_addRelation()" method
959 * @global object the current PMA_PDF instance
965 function PMA_RT_strokeGrid()
969 $pdf->SetMargins(0, 0);
970 $pdf->SetDrawColor(200, 200, 200);
972 // Draws horizontal lines
973 for ($l = 0; $l < 21; $l++
) {
974 $pdf->line(0, $l * 10, $pdf->fh
, $l * 10);
977 $pdf->SetXY(0, $l * 10);
978 $label = (string) sprintf('%.0f', ($l * 10 - $this->t_marg
) * $this->scale +
$this->y_min
);
979 $pdf->Cell(5, 5, ' ' . $label);
983 // Draws vertical lines
984 for ($j = 0; $j < 30 ;$j++
) {
985 $pdf->line($j * 10, 0, $j * 10, $pdf->fw
);
986 $pdf->SetXY($j * 10, 0);
987 $label = (string) sprintf('%.0f', ($j * 10 - $this->l_marg
) * $this->scale +
$this->x_min
);
988 $pdf->Cell(5, 7, $label);
990 } // end of the "PMA_RT_strokeGrid()" method
994 * Draws relation arrows
996 * @param boolean Whether to use one color per relation or not
1000 * @see PMA_RT_Relation::PMA_RT_Relation_draw()
1002 function PMA_RT_drawRelations($change_color)
1005 foreach ($this->relations
AS $relation) {
1006 $relation->PMA_RT_Relation_draw($change_color, $i);
1009 } // end of the "PMA_RT_drawRelations()" method
1015 * @param boolean Whether to display table position or not
1019 * @see PMA_RT_Table::PMA_RT_Table_draw()
1021 function PMA_RT_drawTables($show_info,$draw_color=0)
1023 foreach ($this->tables
AS $table) {
1024 $table->PMA_RT_Table_draw($show_info, $this->ff
,$draw_color);
1026 } // end of the "PMA_RT_drawTables()" method
1030 * Ouputs the PDF document to a file
1032 * @global object The current PDF document
1033 * @global string The current database name
1034 * @global integer The current page number (from the
1035 * $cfg['Servers'][$i]['table_coords'] table)
1041 function PMA_RT_showRt()
1043 global $pdf, $db, $pdf_page_number, $cfgRelation;
1045 $pdf->SetFontSize(14);
1046 $pdf->SetLineWidth(0.2);
1047 $pdf->SetDisplayMode('fullpage');
1048 // Get the name of this pdfpage to use as filename (Mike Beck)
1049 $_name_sql = 'SELECT page_descr FROM ' . PMA_backquote($cfgRelation['pdf_pages'])
1050 . ' WHERE page_nr = ' . $pdf_page_number;
1051 $_name_rs = PMA_query_as_cu($_name_sql);
1053 $_name_row = PMA_DBI_fetch_row($_name_rs);
1054 $filename = $_name_row[0] . '.pdf';
1056 // i don't know if there is a chance for this to happen, but rather be on the safe side:
1057 if (empty($filename)) {
1058 $filename = $pdf_page_number . '.pdf';
1060 //$pdf->Output($db . '_' . $filename, TRUE);
1061 $pdf->Output($db . '_' . $filename, 'I'); // destination: Inline
1062 } // end of the "PMA_RT_showRt()" method
1066 * The "PMA_RT" constructor
1068 * @param mixed The scaling factor
1069 * @param integer The page number to draw (from the
1070 * $cfg['Servers'][$i]['table_coords'] table)
1071 * @param boolean Whether to display table position or not
1072 * @param boolean Was originally whether to use one color per
1073 * relation or not, now enables/disables color
1074 * everywhere, due to some problems printing with color
1075 * @param boolean Whether to draw grids or not
1076 * @param boolean Whether all tables should have the same width or not
1078 * @global object The current PDF document
1079 * @global string The current db name
1080 * @global array The relations settings
1086 function PMA_RT( $which_rel, $show_info = 0, $change_color = 0 , $show_grid = 0, $all_tab_same_wide = 0, $orientation = 'L', $paper = 'A4')
1088 global $pdf, $db, $cfgRelation, $with_doc;
1090 // Font face depends on the current language
1091 $this->ff
= str_replace('"', '', substr($GLOBALS['right_font_family'], 0, strpos($GLOBALS['right_font_family'], ',')));
1093 $this->same_wide
= $all_tab_same_wide;
1095 // Initializes a new document
1096 $pdf = new PMA_PDF('L', 'mm', $paper);
1097 $pdf->title
= sprintf($GLOBALS['strPdfDbSchema'], $GLOBALS['db'], $which_rel);
1100 $pdf->SetTitle($pdf->title
);
1101 $pdf->SetAuthor('phpMyAdmin ' . PMA_VERSION
);
1102 $pdf->AliasNbPages();
1104 if ($GLOBALS['charset'] == 'utf-8') {
1105 // Force FreeSans for utf-8
1106 $this->ff
= 'FreeSans';
1107 $pdf->AddFont('FreeSans','','FreeSans.php');
1108 $pdf->AddFont('FreeSans','B','FreeSansBold.php');
1109 $pdf->SetFont('FreeSans', '', 14);
1111 // fonts added to phpMyAdmin and considered non-standard by fpdf
1112 // (Note: those tahoma fonts are iso-8859-2 based)
1113 if ($this->ff
== 'tahoma') {
1114 $pdf->AddFont('tahoma','','tahoma.php');
1115 $pdf->AddFont('tahoma','B','tahomab.php');
1118 $pdf->SetFont($this->ff
, '', 14);
1119 $pdf->SetAutoPageBreak('auto');
1121 // Gets tables on this page
1122 $tab_sql = 'SELECT table_name FROM ' . PMA_backquote($cfgRelation['table_coords'])
1123 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
1124 . ' AND pdf_page_number = ' . $which_rel;
1125 $tab_rs = PMA_query_as_cu($tab_sql, NULL, PMA_DBI_QUERY_STORE
);
1126 if (!$tab_rs ||
!PMA_DBI_num_rows($tab_rs) > 0) {
1127 $pdf->PMA_PDF_die($GLOBALS['strPdfNoTables']);
1128 // die('No tables');
1130 while ($curr_table = @PMA_DBI_fetch_assoc
($tab_rs)) {
1131 $alltables[] = PMA_sqlAddslashes($curr_table['table_name']);
1132 //$intable = '\'' . implode('\', \'', $alltables) . '\'';
1137 $pdf->SetAutoPageBreak('auto',15);
1139 PMA_RT_DOC($alltables);
1140 $pdf->SetAutoPageBreak('auto');
1148 $pdf->SetLink($pdf->PMA_links
['RT']['-'],-1);
1149 $pdf->Bookmark($GLOBALS['strRelationalSchema']);
1150 $pdf->SetAlias('{00}', $pdf->PageNo()) ;
1157 foreach ($alltables AS $table) {
1158 if (!isset($this->tables
[$table])) {
1159 $this->tables
[$table] = new PMA_RT_Table($table, $this->ff
, $this->tablewidth
);
1162 if ($this->same_wide
){
1163 $this->tables
[$table]->width
= $this->tablewidth
;
1165 $this->PMA_RT_setMinMax($this->tables
[$table]);
1167 // Defines the scale factor
1168 $this->scale
= ceil(max(($this->x_max
- $this->x_min
) / ($pdf->fh
- $this->r_marg
- $this->l_marg
), ($this->y_max
- $this->y_min
) / ($pdf->fw
- $this->t_marg
- $this->b_marg
)) * 100) / 100;
1169 $pdf->PMA_PDF_setScale($this->scale
, $this->x_min
, $this->y_min
, $this->l_marg
, $this->t_marg
);
1172 // Builds and save the PDF document
1173 $pdf->PMA_PDF_setLineWidthScale(0.1);
1176 $pdf->SetFontSize(10);
1177 $this->PMA_RT_strokeGrid();
1179 $pdf->PMA_PDF_setFontSizeScale(14);
1182 // $sql = 'SELECT * FROM ' . PMA_backquote($cfgRelation['relation'])
1183 // . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\' '
1184 // . ' AND foreign_db = \'' . PMA_sqlAddslashes($db) . '\' '
1185 // . ' AND master_table IN (' . $intable . ')'
1186 // . ' AND foreign_table IN (' . $intable . ')';
1187 // $result = PMA_query_as_cu($sql);
1190 // previous logic was checking master tables and foreign tables
1191 // but I think that looping on every table of the pdf page as a master
1192 // and finding its foreigns is OK (then we can support innodb)
1194 $seen_a_relation = FALSE;
1195 foreach ($alltables AS $one_table) {
1197 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
1199 $seen_a_relation = TRUE;
1200 foreach ($exist_rel AS $master_field => $rel) {
1201 // put the foreign table on the schema only if selected
1203 // (do not use array_search() because we would have to
1204 // to do a === FALSE and this is not PHP3 compatible)
1206 if (PMA_isInto($rel['foreign_table'], $alltables)> -1) {
1207 $this->PMA_RT_addRelation($one_table , $master_field, $rel['foreign_table'], $rel['foreign_field']);
1214 // loic1: also show tables without relations
1215 // $norelations = TRUE;
1216 // if ($result && PMA_DBI_num_rows($result) > 0) {
1217 // $norelations = FALSE;
1218 // while ($row = PMA_DBI_fetch_assoc($result)) {
1219 // $this->PMA_RT_addRelation($row['master_table'] , $row['master_field'], $row['foreign_table'], $row['foreign_field']);
1224 // if ($norelations == FALSE) {
1225 if ($seen_a_relation) {
1226 $this->PMA_RT_drawRelations($change_color);
1229 $this->PMA_RT_drawTables($show_info,$change_color);
1231 $this->PMA_RT_showRt();
1232 } // end of the "PMA_RT()" method
1233 } // end of the "PMA_RT" class
1235 function PMA_RT_DOC($alltables ){
1236 global $db, $pdf, $orientation, $paper;
1238 $pdf->addpage($GLOBALS['orientation']);
1239 $pdf->Cell(0,9, $GLOBALS['strTableOfContents'],1,0,'C');
1242 foreach ($alltables AS $table) {
1243 $pdf->PMA_links
['doc'][$table]['-'] = $pdf->AddLink();
1246 $pdf->Cell(0,6,$GLOBALS['strPageNumber'] . ' {'.sprintf("%02d", $i).'}',0,0,'R',0,$pdf->PMA_links
['doc'][$table]['-']);
1248 $pdf->Cell(0,6,$i.' '. $table,0,1,'L',0,$pdf->PMA_links
['doc'][$table]['-']);
1251 $result = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';');
1252 while ($row = PMA_DBI_fetch_assoc($result)) {
1254 $field_name = $row['Field'];
1255 $pdf->PMA_links
['doc'][$table][$field_name] =$pdf->AddLink();
1256 //$pdf->Cell(0,6,$field_name,0,1,'L',0,$pdf->PMA_links['doc'][$table][$field_name]);
1258 $lasttable = $table;
1261 $pdf->PMA_links
['RT']['-'] =$pdf->AddLink();
1263 $pdf->Cell(0,6,$GLOBALS['strPageNumber'] . ' {00}',0,0,'R',0,$pdf->PMA_links
['doc'][$lasttable]['-']);
1265 $pdf->Cell(0,6,$i.' '. $GLOBALS['strRelationalSchema'],0,1,'L',0,$pdf->PMA_links
['RT']['-']);
1267 foreach ($alltables AS $table) {
1269 $pdf->addpage($GLOBALS['orientation']);
1270 $pdf->Bookmark($table);
1271 $pdf->SetAlias('{'.sprintf("%02d", $z).'}', $pdf->PageNo()) ;
1272 $pdf->PMA_links
['RT'][$table]['-'] =$pdf->AddLink();
1273 $pdf->SetLink($pdf->PMA_links
['doc'][$table]['-'],-1);
1274 $pdf->SetFont('', 'B',18);
1275 $pdf->Cell(0,8, $z .' '.$table,1,1,'C',0,$pdf->PMA_links
['RT'][$table]['-']);
1276 $pdf->SetFont('', '',8);
1279 $cfgRelation = PMA_getRelationsParam();
1280 if ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION
>= 40100) {
1281 $comments = PMA_getComments($db, $table);
1283 if ($cfgRelation['mimework']) {
1284 $mime_map = PMA_getMIME($db, $table, true);
1289 * Gets table informations
1291 $result = PMA_DBI_query('SHOW TABLE STATUS LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\';', NULL, PMA_DBI_QUERY_STORE
);
1292 $showtable = PMA_DBI_fetch_assoc($result);
1293 $num_rows = (isset($showtable['Rows']) ?
$showtable['Rows'] : 0);
1294 $show_comment = (isset($showtable['Comment']) ?
$showtable['Comment'] : '');
1295 $create_time = (isset($showtable['Create_time']) ?
PMA_localisedDate(strtotime($showtable['Create_time'])) : '');
1296 $update_time = (isset($showtable['Update_time']) ?
PMA_localisedDate(strtotime($showtable['Update_time'])) : '');
1297 $check_time = (isset($showtable['Check_time']) ?
PMA_localisedDate(strtotime($showtable['Check_time'])) : '');
1299 PMA_DBI_free_result($result);
1304 * Gets table keys and retains them
1306 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
1310 $indexes_info = array();
1311 $indexes_data = array();
1312 $pk_array = array(); // will be use to emphasis prim. keys in the table
1314 while ($row = PMA_DBI_fetch_assoc($result)) {
1315 // Backups the list of primary keys
1316 if ($row['Key_name'] == 'PRIMARY') {
1317 $primary .= $row['Column_name'] . ', ';
1318 $pk_array[$row['Column_name']] = 1;
1320 // Retains keys informations
1321 if ($row['Key_name'] != $lastIndex ){
1322 $indexes[] = $row['Key_name'];
1323 $lastIndex = $row['Key_name'];
1325 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
1326 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
1327 if (isset($row['Cardinality'])) {
1328 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
1330 // I don't know what does following column mean....
1331 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
1332 $indexes_info[$row['Key_name']]['Comment'] = $row['Comment'];
1334 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
1335 if (isset($row['Sub_part'])) {
1336 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
1341 PMA_DBI_free_result($result);
1346 * Gets fields properties
1348 $result = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';', NULL, PMA_DBI_QUERY_STORE
);
1349 $fields_cnt = PMA_DBI_num_rows($result);
1352 // Check if we can use Relations (Mike Beck)
1353 if (!empty($cfgRelation['relation'])) {
1354 // Find which tables are related with the current one and write it in
1356 $res_rel = PMA_getForeigners($db, $table);
1358 if (count($res_rel) > 0) {
1370 * Displays the comments of the table if MySQL >= 3.23
1374 if (!empty($show_comment)) {
1375 $pdf->Cell(0,3,$GLOBALS['strTableComments'] . ' : ' . $show_comment,0,1);
1379 if (!empty($create_time)) {
1380 $pdf->Cell(0,3,$GLOBALS['strStatCreateTime'] . ': ' . $create_time,0,1);
1384 if (!empty($update_time)) {
1385 $pdf->Cell(0,3,$GLOBALS['strStatUpdateTime'] . ': ' . $update_time,0,1);
1389 if (!empty($check_time)) {
1390 $pdf->Cell(0,3,$GLOBALS['strStatCheckTime'] . ': ' . $check_time,0,1);
1394 if ($break == true) {
1395 $pdf->Cell(0,3,'',0,1);
1400 $pdf->SetFont('', 'B');
1401 if (isset($orientation) && $orientation == 'L') {
1402 $pdf->Cell(25,8,ucfirst($GLOBALS['strField']),1,0,'C');
1403 $pdf->Cell(20,8,ucfirst($GLOBALS['strType']),1,0,'C');
1404 $pdf->Cell(20,8,ucfirst($GLOBALS['strAttr']),1,0,'C');
1405 $pdf->Cell(10,8,ucfirst($GLOBALS['strNull']),1,0,'C');
1406 $pdf->Cell(20,8,ucfirst($GLOBALS['strDefault']),1,0,'C');
1407 $pdf->Cell(25,8,ucfirst($GLOBALS['strExtra']),1,0,'C');
1408 $pdf->Cell(45,8,ucfirst($GLOBALS['strLinksTo']),1,0,'C');
1410 if ($paper == 'A4') {
1411 $comments_width = 67;
1413 // this is really intended for 'letter'
1414 // TODO: find optimal width for all formats
1415 $comments_width = 50;
1417 $pdf->Cell($comments_width,8,ucfirst($GLOBALS['strComments']),1,0,'C');
1418 $pdf->Cell(45,8,'MIME',1,1,'C');
1419 $pdf->SetWidths(array(25,20,20,10,20,25,45,$comments_width,45));
1421 $pdf->Cell(20,8,ucfirst($GLOBALS['strField']),1,0,'C');
1422 $pdf->Cell(20,8,ucfirst($GLOBALS['strType']),1,0,'C');
1423 $pdf->Cell(20,8,ucfirst($GLOBALS['strAttr']),1,0,'C');
1424 $pdf->Cell(10,8,ucfirst($GLOBALS['strNull']),1,0,'C');
1425 $pdf->Cell(15,8,ucfirst($GLOBALS['strDefault']),1,0,'C');
1426 $pdf->Cell(15,8,ucfirst($GLOBALS['strExtra']),1,0,'C');
1427 $pdf->Cell(30,8,ucfirst($GLOBALS['strLinksTo']),1,0,'C');
1428 $pdf->Cell(30,8,ucfirst($GLOBALS['strComments']),1,0,'C');
1429 $pdf->Cell(30,8,'MIME',1,1,'C');
1430 $pdf->SetWidths(array(20,20,20,10,15,15,30,30,30));
1432 $pdf->SetFont('', '');
1434 while ($row = PMA_DBI_fetch_assoc($result)) {
1435 $bgcolor = ($i %
2) ?
$GLOBALS['cfg']['BgcolorOne'] : $GLOBALS['cfg']['BgcolorTwo'];
1438 $type = $row['Type'];
1439 // reformat mysql query output - staybyte - 9. June 2001
1440 // loic1: set or enum types: slashes single quotes inside options
1441 if (preg_match('@^(set|enum)\((.+)\)$@i', $type, $tmp = array())) {
1442 $tmp[2] = substr(preg_replace("@([^,])''@", "\\1\\'", ',' . $tmp[2]), 1);
1443 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
1450 $type_nowrap = ' nowrap="nowrap"';
1451 $type = preg_replace('@BINARY@i', '', $type);
1452 $type = preg_replace('@ZEROFILL@i', '', $type);
1453 $type = preg_replace('@UNSIGNED@i', '', $type);
1458 $binary = stristr($row['Type'], 'BINARY');
1459 $unsigned = stristr($row['Type'], 'UNSIGNED');
1460 $zerofill = stristr($row['Type'], 'ZEROFILL');
1462 $strAttribute = ' ';
1464 $strAttribute = 'BINARY';
1467 $strAttribute = 'UNSIGNED';
1470 $strAttribute = 'UNSIGNED ZEROFILL';
1472 if (!isset($row['Default'])) {
1473 if ($row['Null'] != '') {
1474 $row['Default'] = 'NULL';
1477 $field_name = $row['Field'];
1479 $pdf->PMA_links
['RT'][$table][$field_name] =$pdf->AddLink();
1480 $pdf->Bookmark($field_name,1,-1);
1481 $pdf->SetLink($pdf->PMA_links
['doc'][$table][$field_name],-1);
1482 $pdf_row = array($field_name ,
1485 ($row['Null'] == '') ?
$GLOBALS['strNo'] : $GLOBALS['strYes'],
1486 ((isset($row['Default'])) ?
$row['Default'] : ''),
1488 ((isset($res_rel[$field_name])) ?
$res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field'] : ''),
1489 ((isset($comments[$field_name])) ?
$comments[$field_name] : '' ),
1490 ((isset($mime_map) && isset($mime_map[$field_name])) ?
str_replace('_', '/', $mime_map[$field_name]['mimetype']) : '' )
1492 $links[0] = $pdf->PMA_links
['RT'][$table][$field_name];
1493 if (isset($res_rel[$field_name]['foreign_table']) AND
1494 isset($res_rel[$field_name]['foreign_field']) AND
1495 isset($pdf->PMA_links
['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']])
1496 ) $links[6] = $pdf->PMA_links
['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']];
1497 else unset($links[6]);
1498 $pdf->Row($pdf_row, $links);
1500 /*$pdf->Cell(20,8,$field_name,1,0,'L',0,$pdf->PMA_links['RT'][$table][$field_name]);
1501 //echo ' ' . $field_name . ' ' . "\n";
1503 $pdf->Cell(20,8,$type,1,0,'L');
1504 $pdf->Cell(20,8,$strAttribute,1,0,'L');
1505 $pdf->Cell(15,8,,1,0,'L');
1506 $pdf->Cell(15,8,((isset($row['Default'])) ? $row['Default'] : ''),1,0,'L');
1507 $pdf->Cell(15,8,$row['Extra'],1,0,'L');
1509 if (isset($res_rel[$field_name])) {
1510 $pdf->Cell(30,8,$res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field'],1,0,'L');
1513 if ($cfgRelation['commwork']) {
1514 if (isset($comments[$field_name])) {
1515 $pdf->Cell(0,8,$comments[$field_name],1,0,'L');
1519 $pdf->SetFont('', '',14);
1520 PMA_DBI_free_result($result);
1524 } // end function PMA_RT_DOC
1530 if (!isset($pdf_page_number)) {
1531 $pdf_page_number = 1;
1533 $show_grid = (isset($show_grid) && $show_grid == 'on') ?
1 : 0;
1534 $show_color = (isset($show_color) && $show_color == 'on') ?
1 : 0;
1535 $show_table_dimension = (isset($show_table_dimension) && $show_table_dimension == 'on') ?
1 : 0;
1536 $all_tab_same_wide = (isset($all_tab_same_wide) && $all_tab_same_wide == 'on') ?
1 : 0;
1537 $with_doc = (isset($with_doc) && $with_doc == 'on') ?
1 : 0;
1538 $orientation = (isset($orientation) && $orientation == 'P') ?
'P' : 'L';
1539 $paper = isset($paper) ?
$paper : 'A4';
1540 PMA_DBI_select_db($db);
1542 $rt = new PMA_RT($pdf_page_number, $show_table_dimension, $show_color, $show_grid, $all_tab_same_wide, $orientation, $paper);