3 // vim: expandtab sw=4 ts=4 sts=4:
7 * Contributed by Maxime Delorme and merged by lem9
12 * Gets some core scripts
14 require('./libraries/grab_globals.lib.php3');
15 require('./libraries/common.lib.php3');
19 * Settings for relation stuff
21 require('./libraries/relation.lib.php3');
22 $cfgRelation = PMA_getRelationsParam();
26 * Now in ./libraries/relation.lib.php3 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.php3?' . $url_query . '">';
36 echo sprintf($strRelationNotWorking, $url_to_goto, '</a>') . "\n";
41 * Gets the "fpdf" libraries and defines the pdf font path
43 require('./libraries/fpdf/fpdf.php3');
44 // loic1: PHP3 compatibility
45 // define('FPDF_FONTPATH', './libraries/fpdf/font/');
46 $FPDF_font_path = './libraries/fpdf/font/';
50 * Emulates the "array_search" function with PHP < 4.0.5
52 if (PMA_PHP_INT_VERSION
< 40005) {
53 function array_search($needle, $haystack) {
57 while (list($key, $value) = each($haystack)) {
58 if ($value == $needle) {
64 } // end of the "array_search" function
70 * Extends the "FPDF" class and prepares the work
76 class PMA_PDF
extends FPDF
79 * Defines private properties
88 var $Outlines=array();
94 * The PMA_PDF constructor
96 * This function just refers to the "FPDF" constructor: with PHP3 a class
97 * must have a constructor
99 * @param string The page orientation (p, portrait, l or landscape)
100 * @param string The unit for sizes (pt, mm, cm or in)
101 * @param mixed The page format (A3, A4, A5, letter, legal or an array
108 function PMA_PDF($orientation = 'L', $unit = 'mm', $format = 'A4')
110 $this->Alias
= array() ;
111 $this->FPDF($orientation, $unit, $format);
112 } // end of the "PMA_PDF()" method
113 function SetAlias($name, $value)
115 $this->Alias
[$name] = $value ;
119 if(count($this->Alias
) > 0)
122 foreach($this->Alias
as $alias => $value)
123 for($n=1;$n<=$nb;$n++
)
124 $this->pages
[$n]=str_replace($alias,$value,$this->pages
[$n]);
130 * Sets the scaling factor, defines minimum coordinates and margins
132 * @param double The scaling factor
133 * @param double The minimum X coordinate
134 * @param double The minimum Y coordinate
135 * @param double The left margin
136 * @param double The top margin
140 function PMA_PDF_setScale($scale = 1, $x_min = 0, $y_min = 0, $l_marg = -1, $t_marg = -1)
142 $this->scale
= $scale;
143 $this->x_min
= $x_min;
144 $this->y_min
= $y_min;
145 if ($this->l_marg
!= -1) {
146 $this->l_marg
= $l_marg;
148 if ($this->t_marg
!= -1) {
149 $this->t_marg
= $t_marg;
151 } // end of the "PMA_PDF_setScale" function
155 * Outputs a scaled cell
157 * @param double The cell width
158 * @param double The cell height
159 * @param string The text to output
160 * @param mixed Wether to add borders or not
161 * @param integer Where to put the cursor once the output is done
162 * @param string Align mode
163 * @param integer Whether to fill the cell with a color or not
169 function PMA_PDF_cellScale($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $fill = 0,$link ='')
171 $h = $h / $this->scale
;
172 $w = $w / $this->scale
;
173 $this->Cell($w, $h, $txt, $border, $ln, $align, $fill,$link);
174 } // end of the "PMA_PDF_cellScale" function
178 * Draws a scaled line
180 * @param double The horizontal position of the starting point
181 * @param double The vertical position of the starting point
182 * @param double The horizontal position of the ending point
183 * @param double The vertical position of the ending point
189 function PMA_PDF_lineScale($x1, $y1, $x2, $y2)
191 $x1 = ($x1 - $this->x_min
) / $this->scale +
$this->l_marg
;
192 $y1 = ($y1 - $this->y_min
) / $this->scale +
$this->t_marg
;
193 $x2 = ($x2 - $this->x_min
) / $this->scale +
$this->l_marg
;
194 $y2 = ($y2 - $this->y_min
) / $this->scale +
$this->t_marg
;
195 $this->Line($x1, $y1, $x2, $y2);
196 } // end of the "PMA_PDF_lineScale" function
200 * Sets x and y scaled positions
202 * @param double The x position
203 * @param double The y position
209 function PMA_PDF_setXyScale($x, $y)
211 $x = ($x - $this->x_min
) / $this->scale +
$this->l_marg
;
212 $y = ($y - $this->y_min
) / $this->scale +
$this->t_marg
;
213 $this->SetXY($x, $y);
214 } // end of the "PMA_PDF_setXyScale" function
218 * Sets the X scaled positions
220 * @param double The x position
226 function PMA_PDF_setXScale($x)
228 $x = ($x - $this->x_min
) / $this->scale +
$this->l_marg
;
230 } // end of the "PMA_PDF_setXScale" function
234 * Sets the scaled font size
236 * @param double The font size (in points)
240 * @see FPDF::SetFontSize()
242 function PMA_PDF_setFontSizeScale($size)
244 // Set font size in points
245 $size = $size / $this->scale
;
246 $this->SetFontSize($size);
247 } // end of the "PMA_PDF_setFontSizeScale" function
251 * Sets the scaled line width
253 * @param double The line width
257 * @see FPDF::SetLineWidth()
259 function PMA_PDF_setLineWidthScale($width)
261 $width = $width / $this->scale
;
262 $this->SetLineWidth($width);
263 } // end of the "PMA_PDF_setLineWidthScale" function
267 * Displays an error message
269 * @param string the error mesage
271 * @global array the PMA configuration array
272 * @global integer the current server id
273 * @global string the current language
274 * @global string the charset to convert to
275 * @global string the current database name
276 * @global string the current charset
277 * @global string the current text direction
278 * @global string a localized string
279 * @global string an other localized string
283 function PMA_PDF_die($error_message = '')
286 global $server, $lang, $convcharset, $db;
287 global $charset, $text_dir, $strRunning, $strDatabase;
289 include('./header.inc.php3');
291 echo '<p><b>PDF - '. $GLOBALS['strError'] . '</b></p>' . "\n";
292 if (!empty($error_message)) {
293 $error_message = htmlspecialchars($error_message);
296 echo ' ' . $error_message . "\n";
299 echo '<a href="db_details_structure.php3'
301 . '&convcharset=' . $convcharset
302 . '&server=' . $server
303 . '&db=' . urlencode($db)
304 . '">' . $GLOBALS['strBack'] . '</a>';
307 include('./footer.inc.php3');
309 } // end of the "PMA_PDF_die()" function
313 * Aliases the "Error()" function from the FPDF class to the
314 * "PMA_PDF_die()" one
316 * @param string the error mesage
322 function Error($error_message = '')
324 $this->PMA_PDF_die($error_message);
325 } // end of the "Error()" method
329 // We only show this if we find something in the new pdf_pages table
331 // This function must be named "Header" to work with the FPDF library
333 global $cfgRelation,$db,$pdf_page_number,$with_doc;
335 $test_query = 'SELECT * FROM ' . PMA_backquote($cfgRelation['pdf_pages'])
336 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
337 . ' AND page_nr = \'' . $pdf_page_number . '\'';
338 $test_rs = PMA_query_as_cu($test_query);
339 $pages = @PMA_mysql_fetch_array
($test_rs);
340 $this->SetFont('', 'B');
341 $this->Cell(0,6, ucfirst($pages['page_descr']),'B',1,'C');
342 $this->SetFont('', '');
347 // This function must be named "Footer" to work with the FPDF library
351 $this->Cell(0,6, $GLOBALS['strPageNumber'] .' '.$this->PageNo() .'/{nb}','T',0,'C');
352 $this->Cell(0,6, PMA_localisedDate(),0,1,'R');
356 function Bookmark($txt,$level=0,$y=0)
359 $this->Outlines
[0][]=$level;
360 $this->Outlines
[1][]=$txt;
361 $this->Outlines
[2][]=$this->page
;
364 $this->Outlines
[3][]=round($this->hPt
-$y*$this->k
,2);
367 function _putbookmarks()
369 if(count($this->Outlines
)>0)
373 //Take the number of sub elements for an outline
374 $nb_outlines=sizeof($this->Outlines
[0]);
375 $first_level=array();
378 for( $i=0; $i<$nb_outlines; $i++
)
380 $level=$this->Outlines
[0][$i];
388 //Take the previous outline in the same level
389 while( $this->Outlines
[0][$cursor] > $level && $cursor > 0)
391 if( $this->Outlines
[0][$cursor] == $level)
394 if( $i<$nb_outlines-1)
397 while( isset($this->Outlines
[0][$cursor]) && $this->Outlines
[0][$cursor] > $level)
399 //Take the immediate kid in level + 1
400 if( $this->Outlines
[0][$cursor] == $level+
1)
408 //Take the next outline in the same level
409 while( $this->Outlines
[0][$cursor] > $level && ($cursor+
1 < sizeof($this->Outlines
[0])))
411 if( $this->Outlines
[0][$cursor] == $level)
415 $parent[$level+
1]=$this->n
;
417 $first_level[]=$this->n
;
419 $this->_out('/Title ('.$this->Outlines
[1][$i].')');
420 $this->_out('/Parent '.$parent[$level].' 0 R');
422 $this->_out('/Prev '.($memo_n+
$prev+
1).' 0 R');
424 $this->_out('/Next '.($this->n+
$next-$i).' 0 R');
425 $this->_out('/Dest ['.(1+
(2*$this->Outlines
[2][$i])).' 0 R /XYZ null '.$this->Outlines
[3][$i].' null]');
428 $this->_out('/First '.($this->n+
1).' 0 R');
429 $this->_out('/Last '.($this->n+
$last-$i).' 0 R');
430 $this->_out('/Count -'.$kids);
433 $this->_out('endobj');
435 //First page of outlines
437 $this->def_outlines
= $this->n
;
439 $this->_out('/Type');
440 $this->_out('/Outlines');
441 $this->_out('/First '.$first_level[0].' 0 R');
442 $this->_out('/Last '.$first_level[sizeof($first_level)-1].' 0 R');
443 $this->_out('/Count '.sizeof($first_level));
445 $this->_out('endobj');
449 function _putresources()
451 parent
::_putresources();
452 $this->_putbookmarks();
455 function _putcatalog()
457 parent
::_putcatalog();
458 if(count($this->Outlines
)>0)
460 $this->_out('/Outlines '.$this->def_outlines
.' 0 R');
461 $this->_out('/PageMode /UseOutlines');
464 function SetWidths($w)
470 function Row($data,$links)
474 for($i=0;$i<count($data);$i++
)
475 $nb=max($nb,$this->NbLines($this->widths
[$i],$data[$i]));
478 // page break if necessary
479 $this->CheckPageBreak($h);
481 for($i=0;$i<count($data);$i++
)
483 $w=$this->widths
[$i];
484 // save current position
488 $this->Rect($x,$y,$w,$h);
489 if (isset($links[$i]))
490 $this->Link($x,$y,$w,$h,$links[$i]);
492 $this->MultiCell($w,$il+
1,$data[$i],0,'L');
494 $this->SetXY($x+
$w,$y);
500 function CheckPageBreak($h)
502 // if height h overflows, manual page break
503 if($this->GetY()+
$h>$this->PageBreakTrigger
)
504 $this->AddPage($this->CurOrientation
);
507 function NbLines($w,$txt)
509 // compute number of lines used by a multicell of width w
510 $cw=&$this->CurrentFont
['cw'];
512 $w=$this->w
-$this->rMargin
-$this->x
;
513 $wmax=($w-2*$this->cMargin
)*1000/$this->FontSize
;
514 $s=str_replace("\r",'',$txt);
516 if($nb>0 and $s[$nb-1]=="\n")
558 } // end of the "PMA_PDF" class
562 * Draws tables schema
571 * Defines private properties
577 var $fields = array();
578 var $height_cell = 6;
580 var $primary = array();
584 * Sets the width of the table
586 * @param integer The font size
588 * @global object The current PDF document
594 function PMA_RT_Table_setWidth($ff)
596 // this looks buggy to me... does it really work if
597 // there are fields that require wider cells than the name of the table?
600 reset($this->fields
);
601 while (list(, $field) = each($this->fields
)) {
602 $this->width
= max($this->width
, $pdf->GetStringWidth($field));
604 $this->width +
= $pdf->GetStringWidth(' ');
605 $pdf->SetFont($ff, 'B');
606 $this->width
= max($this->width
, $pdf->GetStringWidth(' ' . $this->table_name
));
607 $pdf->SetFont($ff, '');
608 } // end of the "PMA_RT_Table_setWidth()" method
612 * Sets the height of the table
616 function PMA_RT_Table_setHeight()
618 $this->height
= (count($this->fields
) +
1) * $this->height_cell
;
619 } // end of the "PMA_RT_Table_setHeight()" method
625 * @param boolean Whether to display table position or not
626 * @param integer The font size
627 * @param boolean Whether all tables should have the same width or not
628 * @param integer The max. with among tables
630 * @global object The current PDF document
636 function PMA_RT_Table_draw($show_info, $ff, $same_wide = 0, $same_wide_width = 0)
638 global $pdf, $with_doc;
640 if ($same_wide == 1 && $same_wide_width > 0) {
641 $this->width
= $same_wide_width;
644 $pdf->PMA_PDF_setXyScale($this->x
, $this->y
);
645 $pdf->SetFont($ff, 'B');
646 $pdf->SetTextColor(200);
647 $pdf->SetFillColor(0, 0, 128);
648 if ($with_doc) $pdf->SetLink($pdf->PMA_links
['RT'][$this->table_name
]['-'],-1);
649 else $pdf->PMA_links
['doc'][$this->table_name
]['-'] = '';
651 $pdf->PMA_PDF_cellScale($this->width
, $this->height_cell
, sprintf('%.0f', $this->width
) . 'x' . sprintf('%.0f', $this->height
) . ' ' . $this->table_name
, 1, 1, 'C', 1,$pdf->PMA_links
['doc'][$this->table_name
]['-']);
653 $pdf->PMA_PDF_cellScale($this->width
, $this->height_cell
, $this->table_name
, 1, 1, 'C', 1,$pdf->PMA_links
['doc'][$this->table_name
]['-']);
655 $pdf->PMA_PDF_setXScale($this->x
);
656 $pdf->SetFont($ff, '');
657 $pdf->SetTextColor(0);
658 $pdf->SetFillColor(255);
660 reset($this->fields
);
661 while (list(, $field) = each($this->fields
)) {
663 // if (in_array($field, $this->primary)) {
664 if (PMA_isInto($field, $this->primary
) != -1) {
665 $pdf->SetFillColor(215, 121, 123);
667 if ($field == $this->displayfield
) {
668 $pdf->SetFillColor(142, 159, 224);
670 if ($with_doc) $pdf->SetLink($pdf->PMA_links
['RT'][$this->table_name
][$field],-1);
671 else $pdf->PMA_links
['doc'][$this->table_name
][$field] = '';
674 $pdf->PMA_PDF_cellScale($this->width
, $this->height_cell
, ' ' . $field, 1, 1, 'L', 1,$pdf->PMA_links
['doc'][$this->table_name
][$field]);
675 $pdf->PMA_PDF_setXScale($this->x
);
676 $pdf->SetFillColor(255);
679 /*if ($pdf->PageNo() > 1) {
680 $pdf->PMA_PDF_die($GLOBALS['strScaleFactorSmall']);
682 } // end of the "PMA_RT_Table_draw()" method
686 * The "PMA_RT_Table" constructor
688 * @param string The table name
689 * @param integer The font size
690 * @param integer The max. with among tables
692 * @global object The current PDF document
693 * @global integer The current page number (from the
694 * $cfg['Servers'][$i]['table_coords'] table)
695 * @global array The relations settings
696 * @global string The current db name
700 * @see PMA_PDF, PMA_RT_Table::PMA_RT_Table_setWidth,
701 * PMA_RT_Table::PMA_RT_Table_setHeight
703 function PMA_RT_Table($table_name, $ff, &$same_wide_width)
705 global $pdf, $pdf_page_number, $cfgRelation, $db;
707 $this->table_name
= $table_name;
708 $sql = 'DESCRIBE ' . PMA_backquote($table_name);
709 $result = PMA_mysql_query($sql);
710 if (!$result ||
!mysql_num_rows($result)) {
711 $pdf->PMA_PDF_die(sprintf($GLOBALS['strPdfInvalidTblName'], $table_name));
714 while ($row = PMA_mysql_fetch_array($result)) {
715 $this->fields
[] = $row[0];
719 $this->PMA_RT_Table_setWidth($ff);
720 $this->PMA_RT_Table_setHeight();
721 if ($same_wide_width < $this->width
) {
722 $same_wide_width = $this->width
;
726 $sql = 'SELECT x, y FROM '
727 . PMA_backquote($cfgRelation['table_coords'])
728 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
729 . ' AND table_name = \'' . PMA_sqlAddslashes($table_name) . '\''
730 . ' AND pdf_page_number = ' . $pdf_page_number;
731 $result = PMA_query_as_cu($sql);
733 if (!$result ||
!mysql_num_rows($result)) {
734 $pdf->PMA_PDF_die(sprintf($GLOBALS['strConfigureTableCoord'], $table_name));
736 list($this->x
, $this->y
) = PMA_mysql_fetch_array($result);
737 $this->x
= (double) $this->x
;
738 $this->y
= (double) $this->y
;
741 $this->displayfield
= PMA_getDisplayField($db, $table_name);
744 $sql = 'SHOW index FROM ' . PMA_backquote($table_name);
745 $result = PMA_mysql_query($sql);
746 if ($result && mysql_num_rows($result) > 0) {
747 while ($row = PMA_mysql_fetch_array($result)) {
748 if ($row['Key_name'] == 'PRIMARY') {
749 $this->primary
[] = $row['Column_name'];
753 } // end of the "PMA_RT_Table()" method
754 } // end class "PMA_RT_Table"
759 * Draws relation links
765 class PMA_RT_Relation
768 * Defines private properties
773 var $x_dest, $y_dest;
778 * Gets arrows coordinates
780 * @param string The current table name
781 * @param string The relation column name
783 * @return array Arrows coordinates
787 function PMA_RT_Relation_getXy($table, $column)
789 $pos = array_search($column, $table->fields
);
790 // x_left, x_right, y
791 return array($table->x
, $table->x +
$table->width
, $table->y +
($pos +
1.5) * $table->height_cell
);
792 } // end of the "PMA_RT_Relation_getXy()" method
796 * Do draws relation links
798 * @param boolean Whether to use one color per relation or not
799 * @param integer The id of the link to draw
801 * @global object The current PDF document
807 function PMA_RT_Relation_draw($change_color, $i)
824 list ($a, $b, $c) = $case[$d];
825 $e = (1 - ($j - 1) / 6);
826 $pdf->SetDrawColor($a * 255 * $e, $b * 255 * $e, $c * 255 * $e); }
828 $pdf->SetDrawColor(0);
829 } // end if... else...
831 $pdf->PMA_PDF_setLineWidthScale(0.2);
832 $pdf->PMA_PDF_lineScale($this->x_src
, $this->y_src
, $this->x_src +
$this->src_dir
* $this->w_tick
, $this->y_src
);
833 $pdf->PMA_PDF_lineScale($this->x_dest +
$this->dest_dir
* $this->w_tick
, $this->y_dest
, $this->x_dest
, $this->y_dest
);
834 $pdf->PMA_PDF_setLineWidthScale(0.1);
835 $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
);
838 $root2 = 2 * sqrt(2);
839 $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);
840 $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);
842 $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);
843 $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);
844 $pdf->SetDrawColor(0);
845 } // end of the "PMA_RT_Relation_draw()" method
849 * The "PMA_RT_Relation" constructor
851 * @param string The master table name
852 * @param string The relation field in the master table
853 * @param string The foreign table name
854 * @param string The relation field in the foreign table
859 * @see PMA_RT_Relation::PMA_RT_Relation_getXy
861 function PMA_RT_Relation($master_table, $master_field, $foreign_table, $foreign_field)
863 $src_pos = $this->PMA_RT_Relation_getXy($master_table , $master_field);
864 $dest_pos = $this->PMA_RT_Relation_getXy($foreign_table, $foreign_field);
865 $src_left = $src_pos[0] - $this->w_tick
;
866 $src_right = $src_pos[1] +
$this->w_tick
;
867 $dest_left = $dest_pos[0] - $this->w_tick
;
868 $dest_right = $dest_pos[1] +
$this->w_tick
;
870 $d1 = abs($src_left - $dest_left);
871 $d2 = abs($src_right - $dest_left);
872 $d3 = abs($src_left - $dest_right);
873 $d4 = abs($src_right - $dest_right);
874 $d = min($d1, $d2, $d3, $d4);
877 $this->x_src
= $src_pos[0];
879 $this->x_dest
= $dest_pos[0];
880 $this->dest_dir
= -1;
881 } else if ($d == $d2) {
882 $this->x_src
= $src_pos[1];
884 $this->x_dest
= $dest_pos[0];
885 $this->dest_dir
= -1;
886 } else if ($d == $d3) {
887 $this->x_src
= $src_pos[0];
889 $this->x_dest
= $dest_pos[1];
892 $this->x_src
= $src_pos[1];
894 $this->x_dest
= $dest_pos[1];
897 $this->y_src
= $src_pos[2];
898 $this->y_dest
= $dest_pos[2];
899 } // end of the "PMA_RT_Relation()" method
900 } // end of the "PMA_RT_Relation" class
905 * Draws and send the database schema
914 * Defines private properties
916 var $tables = array();
917 var $relations = array();
932 * Sets X and Y minimum and maximum for a table cell
934 * @param string The table name
938 function PMA_RT_setMinMax($table)
940 $this->x_max
= max($this->x_max
, $table->x +
$table->width
);
941 $this->y_max
= max($this->y_max
, $table->y +
$table->height
);
942 $this->x_min
= min($this->x_min
, $table->x
);
943 $this->y_min
= min($this->y_min
, $table->y
);
944 } // end of the "PMA_RT_setMinMax()" method
948 * Defines relation objects
950 * @param string The master table name
951 * @param string The relation field in the master table
952 * @param string The foreign table name
953 * @param string The relation field in the foreign table
957 * @see PMA_RT_setMinMax()
959 function PMA_RT_addRelation($master_table , $master_field, $foreign_table, $foreign_field)
961 if (!isset($this->tables
[$master_table])) {
962 $this->tables
[$master_table] = new PMA_RT_Table($master_table, $this->ff
, $this->tablewidth
);
963 $this->PMA_RT_setMinMax($this->tables
[$master_table]);
965 if (!isset($this->tables
[$foreign_table])) {
966 $this->tables
[$foreign_table] = new PMA_RT_Table($foreign_table, $this->ff
, $this->tablewidth
);
967 $this->PMA_RT_setMinMax($this->tables
[$foreign_table]);
969 $this->relations
[] = new PMA_RT_Relation($this->tables
[$master_table], $master_field, $this->tables
[$foreign_table], $foreign_field);
970 } // end of the "PMA_RT_addRelation()" method
976 * @global object the current PMA_PDF instance
982 function PMA_RT_strokeGrid()
986 $pdf->SetMargins(0, 0);
987 $pdf->SetDrawColor(200, 200, 200);
989 // Draws horizontal lines
990 for ($l = 0; $l < 21; $l++
) {
991 $pdf->line(0, $l * 10, 297, $l * 10);
994 $pdf->SetXY(0, $l * 10);
995 $label = (string) sprintf('%.0f', ($l * 10 - $this->t_marg
) * $this->scale +
$this->y_min
);
996 $pdf->Cell(5, 5, ' ' . $label);
1000 // Draws vertical lines
1001 for ($j = 0; $j < 30 ;$j++
) {
1002 $pdf->line($j * 10, 0, $j * 10, 210);
1003 $pdf->SetXY($j * 10, 0);
1004 $label = (string) sprintf('%.0f', ($j * 10 - $this->l_marg
) * $this->scale +
$this->x_min
);
1005 $pdf->Cell(5, 7, $label);
1007 } // end of the "PMA_RT_strokeGrid()" method
1011 * Draws relation arrows
1013 * @param boolean Whether to use one color per relation or not
1017 * @see PMA_RT_Relation::PMA_RT_Relation_draw()
1019 function PMA_RT_drawRelations($change_color)
1022 reset($this->relations
);
1023 while (list(, $relation) = each($this->relations
)) {
1024 $relation->PMA_RT_Relation_draw($change_color, $i);
1027 } // end of the "PMA_RT_drawRelations()" method
1033 * @param boolean Whether to display table position or not
1037 * @see PMA_RT_Table::PMA_RT_Table_draw()
1039 function PMA_RT_drawTables($show_info)
1041 reset($this->tables
);
1042 while (list(, $table) = each($this->tables
)) {
1043 $table->PMA_RT_Table_draw($show_info, $this->ff
, $this->same_wide
, $this->tablewidth
);
1045 } // end of the "PMA_RT_drawTables()" method
1049 * Ouputs the PDF document to a file
1051 * @global object The current PDF document
1052 * @global string The current database name
1053 * @global integer The current page number (from the
1054 * $cfg['Servers'][$i]['table_coords'] table)
1060 function PMA_RT_showRt()
1062 global $pdf, $db, $pdf_page_number;
1063 $pdf->SetFontSize(14);
1064 $pdf->SetLineWidth(0.2);
1065 $pdf->SetDisplayMode('fullpage');
1066 $pdf->Output($db . '_' . $pdf_page_number . '.pdf', TRUE);
1067 //$pdf->Output('', TRUE);
1068 } // end of the "PMA_RT_showRt()" method
1072 * The "PMA_RT" constructor
1074 * @param mixed The scaling factor
1075 * @param integer The page number to draw (from the
1076 * $cfg['Servers'][$i]['table_coords'] table)
1077 * @param boolean Whether to display table position or not
1078 * @param boolean Whether to use one color per relation or not
1079 * @param boolean Whether to draw grids or not
1080 * @param boolean Whether all tables should have the same width or not
1082 * @global object The current PDF document
1083 * @global string The current db name
1084 * @global array The relations settings
1090 function PMA_RT($scale, $which_rel, $show_info = 0, $change_color = 0 , $show_grid = 0, $all_tab_same_wide = 0, $orientation = 'L')
1092 global $pdf, $db, $cfgRelation, $with_doc;
1094 // Font face depends on the current language
1095 $this->ff
= str_replace('"', '', substr($GLOBALS['right_font_family'], 0, strpos($GLOBALS['right_font_family'], ',')));
1096 $this->same_wide
= $all_tab_same_wide;
1098 // Initializes a new document
1099 $pdf = new PMA_PDF('L');
1100 $pdf->title
= sprintf($GLOBALS['strPdfDbSchema'], $GLOBALS['db'], $which_rel);
1103 $pdf->SetTitle($pdf->title
);
1104 $pdf->SetAuthor('phpMyAdmin ' . PMA_VERSION
);
1105 $pdf->AliasNbPages();
1107 $pdf->SetFont($this->ff
, '', 14);
1108 $pdf->SetAutoPageBreak('auto');
1110 // Gets tables on this page
1111 $tab_sql = 'SELECT table_name FROM ' . PMA_backquote($cfgRelation['table_coords'])
1112 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
1113 . ' AND pdf_page_number = ' . $which_rel;
1114 $tab_rs = PMA_query_as_cu($tab_sql);
1115 if (!$tab_rs ||
!mysql_num_rows($tab_rs) > 0) {
1116 $pdf->PMA_PDF_die($GLOBALS['strPdfNoTables']);
1117 // die('No tables');
1119 while ($curr_table = @PMA_mysql_fetch_array
($tab_rs)) {
1120 $alltables[] = PMA_sqlAddslashes($curr_table['table_name']);
1121 $intable = '\'' . implode('\', \'', $alltables) . '\'';
1125 $pdf->SetAutoPageBreak('auto',15);
1127 PMA_RT_DOC($alltables);
1128 $pdf->SetAutoPageBreak('auto');
1136 $pdf->SetLink($pdf->PMA_links
['RT']['-'],-1);
1137 $pdf->Bookmark($GLOBALS['strRelationalSchema']);
1138 $pdf->SetAlias('{00}', $pdf->PageNo()) ;
1144 $sql = 'SELECT * FROM ' . PMA_backquote($cfgRelation['relation'])
1145 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\' '
1146 . ' AND foreign_db = \'' . PMA_sqlAddslashes($db) . '\' '
1147 . ' AND master_table IN (' . $intable . ')'
1148 . ' AND foreign_table IN (' . $intable . ')';
1149 $result = PMA_query_as_cu($sql);
1151 // loic1: also show tables without relations
1152 $norelations = TRUE;
1153 if ($result && mysql_num_rows($result) > 0) {
1154 $norelations = FALSE;
1155 while ($row = PMA_mysql_fetch_array($result)) {
1156 $this->PMA_RT_addRelation($row['master_table'] , $row['master_field'], $row['foreign_table'], $row['foreign_field']);
1160 while (list(, $table) = each ($alltables)) {
1161 if (!isset($this->tables
[$table])) {
1162 $this->tables
[$table] = new PMA_RT_Table($table, $this->ff
, $this->tablewidth
);
1163 $this->PMA_RT_setMinMax($this->tables
[$table]);
1167 // Defines the scale factor
1168 if ($scale == 'auto') {
1169 $this->scale
= ceil(max(($this->x_max
- $this->x_min
) / (297 - $this->r_marg
- $this->l_marg
), ($this->y_max
- $this->y_min
) / (210 - $this->t_marg
- $this->b_marg
)) * 100) / 100;
1170 $pdf->PMA_PDF_setScale($this->scale
, $this->x_min
, $this->y_min
, $this->l_marg
, $this->t_marg
);
1172 $this->scale
= $scale;
1173 $pdf->PMA_PDF_setScale($scale);
1174 } // end if... else...
1176 // Builds and save the PDF document
1177 $pdf->PMA_PDF_setLineWidthScale(0.1);
1180 $pdf->SetFontSize(10);
1181 $this->PMA_RT_strokeGrid();
1183 $pdf->PMA_PDF_setFontSizeScale(14);
1184 if ($norelations == FALSE) {
1185 $this->PMA_RT_drawRelations($change_color);
1187 $this->PMA_RT_drawTables($show_info);
1189 $this->PMA_RT_showRt();
1190 } // end of the "PMA_RT()" method
1191 } // end of the "PMA_RT" class
1193 function PMA_RT_DOC($alltables ){
1194 global $db, $pdf, $orientation;
1197 $pdf->Cell(0,9, $GLOBALS['strTableOfContents'],1,0,'C');
1200 foreach($alltables as $table ){
1201 $pdf->PMA_links
['doc'][$table]['-'] = $pdf->AddLink();
1204 $pdf->Cell(0,6,$GLOBALS['strPageNumber'] . ' {'.sprintf("%02d", $i).'}',0,0,'R',0,$pdf->PMA_links
['doc'][$table]['-']);
1206 $pdf->Cell(0,6,$i.' '. $table,0,1,'L',0,$pdf->PMA_links
['doc'][$table]['-']);
1209 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($table);
1210 $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url);
1211 while ($row = PMA_mysql_fetch_array($result)) {
1213 $field_name = $row['Field'];
1214 $pdf->PMA_links
['doc'][$table][$field_name] =$pdf->AddLink();
1215 //$pdf->Cell(0,6,$field_name,0,1,'L',0,$pdf->PMA_links['doc'][$table][$field_name]);
1219 $pdf->PMA_links
['RT']['-'] =$pdf->AddLink();
1221 $pdf->Cell(0,6,$GLOBALS['strPageNumber'] . ' {00}',0,0,'R',0,$pdf->PMA_links
['doc'][$table]['-']);
1223 $pdf->Cell(0,6,$i.' '. $GLOBALS['strRelationalSchema'],0,1,'L',0,$pdf->PMA_links
['RT']['-']);
1225 foreach($alltables as $table ){
1227 $pdf->addpage($GLOBALS['orientation']);
1228 $pdf->Bookmark($table);
1229 $pdf->SetAlias('{'.sprintf("%02d", $z).'}', $pdf->PageNo()) ;
1230 $pdf->PMA_links
['RT'][$table]['-'] =$pdf->AddLink();
1231 $pdf->SetLink($pdf->PMA_links
['doc'][$table]['-'],-1);
1232 $pdf->SetFont('', 'B',18);
1233 $pdf->Cell(0,8, $z .' '.$table,1,1,'C',0,$pdf->PMA_links
['RT'][$table]['-']);
1234 $pdf->SetFont('', '',8);
1237 $cfgRelation = PMA_getRelationsParam();
1238 if ($cfgRelation['commwork']) {
1239 $comments = PMA_getComments($db, $table);
1244 * Gets table informations
1246 // The 'show table' statement works correct since 3.23.03
1247 if (PMA_MYSQL_INT_VERSION
>= 32303) {
1248 $local_query = "SHOW TABLE STATUS LIKE '" . PMA_sqlAddslashes($table, TRUE) . "'";
1249 $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url);
1250 $showtable = PMA_mysql_fetch_array($result);
1251 $num_rows = (isset($showtable['Rows']) ?
$showtable['Rows'] : 0);
1252 $show_comment = (isset($showtable['Comment']) ?
$showtable['Comment'] : '');
1254 $local_query = 'SELECT COUNT(*) AS count FROM ' . PMA_backquote($table);
1255 $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url);
1256 $showtable = array();
1257 $num_rows = PMA_mysql_result($result, 0, 'count');
1259 } // end display comments
1261 mysql_free_result($result);
1266 * Gets table keys and retains them
1268 $local_query = 'SHOW KEYS FROM ' . PMA_backquote($table);
1269 $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url);
1273 $indexes_info = array();
1274 $indexes_data = array();
1275 $pk_array = array(); // will be use to emphasis prim. keys in the table
1277 while ($row = PMA_mysql_fetch_array($result)) {
1278 // Backups the list of primary keys
1279 if ($row['Key_name'] == 'PRIMARY') {
1280 $primary .= $row['Column_name'] . ', ';
1281 $pk_array[$row['Column_name']] = 1;
1283 // Retains keys informations
1284 if ($row['Key_name'] != $lastIndex ){
1285 $indexes[] = $row['Key_name'];
1286 $lastIndex = $row['Key_name'];
1288 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
1289 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
1290 if (isset($row['Cardinality'])) {
1291 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
1293 // I don't know what does following column mean....
1294 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
1295 $indexes_info[$row['Key_name']]['Comment'] = $row['Comment'];
1297 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
1298 if (isset($row['Sub_part'])) {
1299 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
1304 mysql_free_result($result);
1309 * Gets fields properties
1311 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($table);
1312 $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url);
1313 $fields_cnt = mysql_num_rows($result);
1316 // Check if we can use Relations (Mike Beck)
1317 if (!empty($cfgRelation['relation'])) {
1318 // Find which tables are related with the current one and write it in
1320 $res_rel = PMA_getForeigners($db, $table);
1322 if (count($res_rel) > 0) {
1334 * Displays the comments of the table if MySQL >= 3.23
1337 if (!empty($show_comment)) {
1338 $pdf->Cell(0,8,$GLOBALS['strTableComments'] . ' : ' . $show_comment,0,1);
1343 $pdf->SetFont('', 'B');
1344 if (isset($orientation) && $orientation == 'L') {
1345 $pdf->Cell(25,8,ucfirst($GLOBALS['strField']),1,0,'C');
1346 $pdf->Cell(20,8,ucfirst($GLOBALS['strType']),1,0,'C');
1347 $pdf->Cell(20,8,ucfirst($GLOBALS['strAttr']),1,0,'C');
1348 $pdf->Cell(10,8,ucfirst($GLOBALS['strNull']),1,0,'C');
1349 $pdf->Cell(20,8,ucfirst($GLOBALS['strDefault']),1,0,'C');
1350 $pdf->Cell(25,8,ucfirst($GLOBALS['strExtra']),1,0,'C');
1351 $pdf->Cell(45,8,ucfirst($GLOBALS['strLinksTo']),1,0,'C');
1352 $pdf->Cell(100,8,ucfirst($GLOBALS['strComments']),1,1,'C');
1353 $pdf->SetWidths(array(25,20,20,10,20,25,45,100));
1355 $pdf->Cell(20,8,ucfirst($GLOBALS['strField']),1,0,'C');
1356 $pdf->Cell(20,8,ucfirst($GLOBALS['strType']),1,0,'C');
1357 $pdf->Cell(20,8,ucfirst($GLOBALS['strAttr']),1,0,'C');
1358 $pdf->Cell(10,8,ucfirst($GLOBALS['strNull']),1,0,'C');
1359 $pdf->Cell(15,8,ucfirst($GLOBALS['strDefault']),1,0,'C');
1360 $pdf->Cell(15,8,ucfirst($GLOBALS['strExtra']),1,0,'C');
1361 $pdf->Cell(30,8,ucfirst($GLOBALS['strLinksTo']),1,0,'C');
1362 $pdf->Cell(60,8,ucfirst($GLOBALS['strComments']),1,1,'C');
1363 $pdf->SetWidths(array(20,20,20,10,15,15,30,60));
1365 $pdf->SetFont('', '');
1367 while ($row = PMA_mysql_fetch_array($result)) {
1368 $bgcolor = ($i %
2) ?
$GLOBALS['cfg']['BgcolorOne'] : $GLOBALS['cfg']['BgcolorTwo'];
1371 $type = $row['Type'];
1372 // reformat mysql query output - staybyte - 9. June 2001
1373 // loic1: set or enum types: slashes single quotes inside options
1374 if (eregi('^(set|enum)\((.+)\)$', $type, $tmp)) {
1375 $tmp[2] = substr(ereg_replace("([^,])''", "\\1\\'", ',' . $tmp[2]), 1);
1376 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
1379 $type_nowrap = ' nowrap="nowrap"';
1381 $type = eregi_replace('BINARY', '', $type);
1382 $type = eregi_replace('ZEROFILL', '', $type);
1383 $type = eregi_replace('UNSIGNED', '', $type);
1388 $binary = eregi('BINARY', $row['Type'], $test);
1389 $unsigned = eregi('UNSIGNED', $row['Type'], $test);
1390 $zerofill = eregi('ZEROFILL', $row['Type'], $test);
1391 $strAttribute = ' ';
1393 $strAttribute = 'BINARY';
1396 $strAttribute = 'UNSIGNED';
1399 $strAttribute = 'UNSIGNED ZEROFILL';
1401 if (!isset($row['Default'])) {
1402 if ($row['Null'] != '') {
1403 $row['Default'] = 'NULL';
1406 $field_name = $row['Field'];
1408 $pdf->PMA_links
['RT'][$table][$field_name] =$pdf->AddLink();
1409 $pdf->Bookmark($field_name,1,-1);
1410 $pdf->SetLink($pdf->PMA_links
['doc'][$table][$field_name],-1);
1411 $pdf_row = array($field_name ,
1414 ($row['Null'] == '') ?
$GLOBALS['strNo'] : $GLOBALS['strYes'],
1415 ((isset($row['Default'])) ?
$row['Default'] : ''),
1417 ((isset($res_rel[$field_name])) ?
$res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field'] : ''),
1418 ((isset($comments[$field_name])) ?
$comments[$field_name] : '' )
1420 $links[0] = $pdf->PMA_links
['RT'][$table][$field_name];
1421 if (isset($res_rel[$field_name]['foreign_table']) AND
1422 isset($res_rel[$field_name]['foreign_field']) AND
1423 isset($pdf->PMA_links
['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']])
1424 ) $links[6] = $pdf->PMA_links
['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']];
1425 else unset($links[6]);
1426 $pdf->Row($pdf_row, $links);
1428 /*$pdf->Cell(20,8,$field_name,1,0,'L',0,$pdf->PMA_links['RT'][$table][$field_name]);
1429 //echo ' ' . $field_name . ' ' . "\n";
1431 $pdf->Cell(20,8,$type,1,0,'L');
1432 $pdf->Cell(20,8,$strAttribute,1,0,'L');
1433 $pdf->Cell(15,8,,1,0,'L');
1434 $pdf->Cell(15,8,((isset($row['Default'])) ? $row['Default'] : ''),1,0,'L');
1435 $pdf->Cell(15,8,$row['Extra'],1,0,'L');
1437 if (isset($res_rel[$field_name])) {
1438 $pdf->Cell(30,8,$res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field'],1,0,'L');
1441 if ($cfgRelation['commwork']) {
1442 if (isset($comments[$field_name])) {
1443 $pdf->Cell(0,8,$comments[$field_name],1,0,'L');
1447 $pdf->SetFont('', '',14);
1448 mysql_free_result($result);
1452 } // end function PMA_RT_DOC
1458 if (!isset($pdf_page_number)) {
1459 $pdf_page_number = 1;
1461 $show_grid = (isset($show_grid) && $show_grid == 'on') ?
1 : 0;
1462 $show_color = (isset($show_color) && $show_color == 'on') ?
1 : 0;
1463 $show_table_dimension = (isset($show_table_dimension) && $show_table_dimension == 'on') ?
1 : 0;
1464 $all_tab_same_wide = (isset($all_tab_same_wide) && $all_tab_same_wide == 'on') ?
1 : 0;
1465 $with_doc = (isset($with_doc) && $with_doc == 'on') ?
1 : 0;
1466 $orientation = (isset($orientation) && $orientation == 'P') ?
'P' : 'L';
1467 PMA_mysql_select_db($db);
1469 $rt = new PMA_RT('auto', $pdf_page_number, $show_table_dimension, $show_color, $show_grid, $all_tab_same_wide, $orientation);