display message when search returns zero rows
[phpmyadmin/dkf.git] / libraries / schema / Pdf_Relation_Schema.class.php
blob776370179ec7af559f77bdc8c6c432e898bf7c88
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @package phpMyAdmin
6 */
8 include_once("Export_Relation_Schema.class.php");
10 /**
11 * Font used in PDF.
13 * @todo Make this configuratble (at least Sans/Serif).
15 define('PMA_PDF_FONT', 'DejaVuSans');
16 require_once './libraries/tcpdf/tcpdf.php';
18 /**
19 * Extends the "TCPDF" class and helps
20 * in developing the structure of PDF Schema Export
22 * @name PMA_PDF
23 * @copyright
24 * @license
25 * @access public
26 * @see TCPDF
28 class PMA_PDF extends TCPDF
30 /**
31 * Defines properties
33 var $_xMin;
34 var $_yMin;
35 var $leftMargin = 10;
36 var $topMargin = 10;
37 var $scale;
38 var $PMA_links;
39 var $Outlines = array();
40 var $def_outlines;
41 var $Alias = array();
42 var $widths;
44 public function getFh()
46 return $this->fh;
49 public function getFw()
51 return $this->fw;
54 public function setCMargin($c_margin)
56 $this->cMargin = $c_margin;
59 function SetAlias($name, $value)
61 $this->Alias[$name] = $value ;
64 function _putpages()
66 if (count($this->Alias) > 0) {
67 $nb = $this->page;
68 foreach ($this->Alias as $alias => $value) {
69 for ($n = 1;$n <= $nb;$n++)
70 $this->pages[$n]=str_replace($alias, $value, $this->pages[$n]);
73 parent::_putpages();
76 // added because tcpdf for PHP 5 has a protected $buffer
77 public function getBuffer()
79 return $this->buffer;
82 public function getState()
84 return $this->state;
87 /**
88 * Sets the scaling factor, defines minimum coordinates and margins
90 * @param float scale The scaling factor
91 * @param float _xMin The minimum X coordinate
92 * @param float _yMin The minimum Y coordinate
93 * @param float leftMargin The left margin
94 * @param float topMargin The top margin
95 * @access public
97 function PMA_PDF_setScale($scale = 1, $xMin = 0, $yMin = 0, $leftMargin = -1, $topMargin = -1)
99 $this->scale = $scale;
100 $this->_xMin = $xMin;
101 $this->_yMin = $yMin;
102 if ($this->leftMargin != -1) {
103 $this->leftMargin = $leftMargin;
105 if ($this->topMargin != -1) {
106 $this->topMargin = $topMargin;
111 * Outputs a scaled cell
113 * @param float w The cell width
114 * @param float h The cell height
115 * @param string txt The text to output
116 * @param mixed border Whether to add borders or not
117 * @param integer ln Where to put the cursor once the output is done
118 * @param string align Align mode
119 * @param integer fill Whether to fill the cell with a color or not
120 * @access public
121 * @see TCPDF::Cell()
123 function PMA_PDF_cellScale($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $fill = 0, $link = '')
125 $h = $h / $this->scale;
126 $w = $w / $this->scale;
127 $this->Cell($w, $h, $txt, $border, $ln, $align, $fill, $link);
131 * Draws a scaled line
133 * @param float x1 The horizontal position of the starting point
134 * @param float y1 The vertical position of the starting point
135 * @param float x2 The horizontal position of the ending point
136 * @param float y2 The vertical position of the ending point
137 * @access public
138 * @see TCPDF::Line()
140 function PMA_PDF_lineScale($x1, $y1, $x2, $y2)
142 $x1 = ($x1 - $this->_xMin) / $this->scale + $this->leftMargin;
143 $y1 = ($y1 - $this->_yMin) / $this->scale + $this->topMargin;
144 $x2 = ($x2 - $this->_xMin) / $this->scale + $this->leftMargin;
145 $y2 = ($y2 - $this->_yMin) / $this->scale + $this->topMargin;
146 $this->Line($x1, $y1, $x2, $y2);
150 * Sets x and y scaled positions
152 * @param float x The x position
153 * @param float y The y position
154 * @access public
155 * @see TCPDF::SetXY()
157 function PMA_PDF_setXyScale($x, $y)
159 $x = ($x - $this->_xMin) / $this->scale + $this->leftMargin;
160 $y = ($y - $this->_yMin) / $this->scale + $this->topMargin;
161 $this->SetXY($x, $y);
165 * Sets the X scaled positions
167 * @param float x The x position
168 * @access public
169 * @see TCPDF::SetX()
171 function PMA_PDF_setXScale($x)
173 $x = ($x - $this->_xMin) / $this->scale + $this->leftMargin;
174 $this->SetX($x);
178 * Sets the scaled font size
180 * @param float size The font size (in points)
181 * @access public
182 * @see TCPDF::SetFontSize()
184 function PMA_PDF_setFontSizeScale($size)
186 // Set font size in points
187 $size = $size / $this->scale;
188 $this->SetFontSize($size);
192 * Sets the scaled line width
194 * @param float width The line width
195 * @access public
196 * @see TCPDF::SetLineWidth()
198 function PMA_PDF_setLineWidthScale($width)
200 $width = $width / $this->scale;
201 $this->SetLineWidth($width);
205 * Displays an error message
207 * @param string error_message the error mesage
208 * @access public
209 * @see PMA_Export_Relation_Schema::dieSchema
211 function Error($error_message = '')
213 PMA_Export_Relation_Schema::dieSchema($error_message);
216 function Header()
218 // We only show this if we find something in the new pdf_pages table
220 // This function must be named "Header" to work with the TCPDF library
221 global $cfgRelation, $db, $pdf_page_number, $with_doc;
222 if ($with_doc) {
223 $test_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
224 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
225 . ' AND page_nr = \'' . $pdf_page_number . '\'';
226 $test_rs = PMA_query_as_controluser($test_query);
227 $pages = @PMA_DBI_fetch_assoc($test_rs);
228 $this->SetFont('', 'B', 14);
229 $this->Cell(0, 6, ucfirst($pages['page_descr']), 'B', 1, 'C');
230 $this->SetFont('', '');
231 $this->Ln();
235 function Footer()
237 // This function must be named "Footer" to work with the TCPDF library
238 global $with_doc;
239 if ($with_doc) {
240 $this->SetY(-15);
241 $this->SetFont('', '', 14);
242 $this->Cell(0, 6, __('Page number:') . ' ' . $this->PageNo() . '/{nb}', 'T', 0, 'C');
243 $this->Cell(0, 6, PMA_localisedDate(), 0, 1, 'R');
244 $this->SetY(20);
248 function Bookmark($txt, $level = 0, $y = 0)
250 // Add a bookmark
251 $this->Outlines[0][] = $level;
252 $this->Outlines[1][] = $txt;
253 $this->Outlines[2][] = $this->page;
254 if ($y == -1) {
255 $y = $this->GetY();
257 $this->Outlines[3][] = round($this->hPt - $y * $this->k, 2);
260 function _putbookmarks()
262 if (count($this->Outlines) > 0) {
263 // Save object number
264 $memo_n = $this->n;
265 // Take the number of sub elements for an outline
266 $nb_outlines = sizeof($this->Outlines[0]);
267 $first_level = array();
268 $parent = array();
269 $parent[0] = 1;
270 for ($i = 0; $i < $nb_outlines; $i++) {
271 $level = $this->Outlines[0][$i];
272 $kids = 0;
273 $last = -1;
274 $prev = -1;
275 $next = -1;
276 if ($i > 0) {
277 $cursor = $i-1;
278 // Take the previous outline in the same level
279 while ($this->Outlines[0][$cursor] > $level && $cursor > 0)
280 $cursor--;
281 if ($this->Outlines[0][$cursor] == $level) {
282 $prev = $cursor;
285 if ($i < $nb_outlines-1) {
286 $cursor = $i + 1;
287 while (isset($this->Outlines[0][$cursor]) && $this->Outlines[0][$cursor] > $level) {
288 // Take the immediate kid in level + 1
289 if ($this->Outlines[0][$cursor] == $level + 1) {
290 $kids++;
291 $last = $cursor;
293 $cursor++;
295 $cursor = $i + 1;
296 // Take the next outline in the same level
297 while ($this->Outlines[0][$cursor] > $level && ($cursor + 1 < sizeof($this->Outlines[0])))
298 $cursor++;
299 if ($this->Outlines[0][$cursor] == $level) {
300 $next = $cursor;
303 $this->_newobj();
304 $parent[$level + 1] = $this->n;
305 if ($level == 0) {
306 $first_level[] = $this->n;
308 $this->_out('<<');
309 $this->_out('/Title (' . $this->Outlines[1][$i] . ')');
310 $this->_out('/Parent ' . $parent[$level] . ' 0 R');
311 if ($prev != -1) {
312 $this->_out('/Prev ' . ($memo_n + $prev + 1) . ' 0 R');
314 if ($next != -1) {
315 $this->_out('/Next ' . ($this->n + $next - $i) . ' 0 R');
317 $this->_out('/Dest [' . (1 + (2 * $this->Outlines[2][$i])) . ' 0 R /XYZ null ' . $this->Outlines[3][$i] . ' null]');
318 if ($kids > 0) {
319 $this->_out('/First ' . ($this->n + 1) . ' 0 R');
320 $this->_out('/Last ' . ($this->n + $last - $i) . ' 0 R');
321 $this->_out('/Count -' . $kids);
323 $this->_out('>>');
324 $this->_out('endobj');
326 // First page of outlines
327 $this->_newobj();
328 $this->def_outlines = $this->n;
329 $this->_out('<<');
330 $this->_out('/Type');
331 $this->_out('/Outlines');
332 $this->_out('/First ' . $first_level[0] . ' 0 R');
333 $this->_out('/Last ' . $first_level[sizeof($first_level)-1] . ' 0 R');
334 $this->_out('/Count ' . sizeof($first_level));
335 $this->_out('>>');
336 $this->_out('endobj');
340 function _putresources()
342 parent::_putresources();
343 $this->_putbookmarks();
346 function _putcatalog()
348 parent::_putcatalog();
349 if (count($this->Outlines) > 0) {
350 $this->_out('/Outlines ' . $this->def_outlines . ' 0 R');
351 $this->_out('/PageMode /UseOutlines');
355 function SetWidths($w)
357 // column widths
358 $this->widths = $w;
361 function Row($data, $links)
363 // line height
364 $nb = 0;
365 $data_cnt = count($data);
366 for ($i = 0;$i < $data_cnt;$i++)
367 $nb = max($nb, $this->NbLines($this->widths[$i], $data[$i]));
368 $il = $this->FontSize;
369 $h = ($il + 1) * $nb;
370 // page break if necessary
371 $this->CheckPageBreak($h);
372 // draw the cells
373 $data_cnt = count($data);
374 for ($i = 0;$i < $data_cnt;$i++) {
375 $w = $this->widths[$i];
376 // save current position
377 $x = $this->GetX();
378 $y = $this->GetY();
379 // draw the border
380 $this->Rect($x, $y, $w, $h);
381 if (isset($links[$i])) {
382 $this->Link($x, $y, $w, $h, $links[$i]);
384 // print text
385 $this->MultiCell($w, $il + 1, $data[$i], 0, 'L');
386 // go to right side
387 $this->SetXY($x + $w, $y);
389 // go to line
390 $this->Ln($h);
393 function CheckPageBreak($h)
395 // if height h overflows, manual page break
396 if ($this->GetY() + $h > $this->PageBreakTrigger) {
397 $this->AddPage($this->CurOrientation);
401 function NbLines($w, $txt)
403 // compute number of lines used by a multicell of width w
404 $cw = &$this->CurrentFont['cw'];
405 if ($w == 0) {
406 $w = $this->w - $this->rMargin - $this->x;
408 $wmax = ($w-2 * $this->cMargin) * 1000 / $this->FontSize;
409 $s = str_replace("\r", '', $txt);
410 $nb = strlen($s);
411 if ($nb > 0 and $s[$nb-1] == "\n") {
412 $nb--;
414 $sep = -1;
415 $i = 0;
416 $j = 0;
417 $l = 0;
418 $nl = 1;
419 while ($i < $nb) {
420 $c = $s[$i];
421 if ($c == "\n") {
422 $i++;
423 $sep = -1;
424 $j = $i;
425 $l = 0;
426 $nl++;
427 continue;
429 if ($c == ' ') {
430 $sep = $i;
432 $l += isset($cw[ord($c)])?$cw[ord($c)]:0 ;
433 if ($l > $wmax) {
434 if ($sep == -1) {
435 if ($i == $j) {
436 $i++;
438 } else {
439 $i = $sep + 1;
441 $sep = -1;
442 $j = $i;
443 $l = 0;
444 $nl++;
445 } else {
446 $i++;
449 return $nl;
454 * Table preferences/statistics
456 * This class preserves the table co-ordinates,fields
457 * and helps in drawing/generating the Tables in PDF document.
459 * @name Table_Stats
460 * @copyright
461 * @license
462 * @see PMA_PDF
464 class Table_Stats
467 * Defines properties
469 private $_tableName;
470 private $_showInfo = false;
472 public $nb_fiels;
473 public $width = 0;
474 public $height;
475 public $fields = array();
476 public $heightCell = 6;
477 public $x, $y;
478 public $primary = array();
481 * The "Table_Stats" constructor
483 * @param string table_name The table name
484 * @param integer fontSize The font size
485 * @param integer pageNumber The current page number (from the
486 * $cfg['Servers'][$i]['table_coords'] table)
487 * @param integer sameWideWidth The max. with among tables
488 * @param boolean showKeys Whether to display keys or not
489 * @param boolean showInfo Whether to display table position or not
490 * @global object The current PDF document
491 * @global array The relations settings
492 * @global string The current db name
493 * @see PMA_PDF, Table_Stats::Table_Stats_setWidth,
494 Table_Stats::Table_Stats_setHeight
496 function __construct($tableName, $fontSize, $pageNumber, &$sameWideWidth, $showKeys = false, $showInfo = false)
498 global $pdf, $cfgRelation, $db;
500 $this->_tableName = $tableName;
501 $sql = 'DESCRIBE ' . PMA_backquote($tableName);
502 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE);
503 if (!$result || !PMA_DBI_num_rows($result)) {
504 $pdf->Error(sprintf(__('The %s table doesn\'t exist!'), $tableName));
506 // load fields
507 //check to see if it will load all fields or only the foreign keys
508 if ($showKeys) {
509 $indexes = PMA_Index::getFromTable($this->_tableName, $db);
510 $all_columns = array();
511 foreach ($indexes as $index) {
512 $all_columns = array_merge($all_columns, array_flip(array_keys($index->getColumns())));
514 $this->fields = array_keys($all_columns);
515 } else {
516 while ($row = PMA_DBI_fetch_row($result)) {
517 $this->fields[] = $row[0];
521 $this->_showInfo = $showInfo;
522 $this->_setHeight();
524 * setWidth must me after setHeight, because title
525 * can include table height which changes table width
527 $this->_setWidth($fontSize);
528 if ($sameWideWidth < $this->width) {
529 $sameWideWidth = $this->width;
531 $sql = 'SELECT x, y FROM '
532 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
533 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
534 . ' AND table_name = \'' . PMA_sqlAddslashes($tableName) . '\''
535 . ' AND pdf_page_number = ' . $pageNumber;
536 $result = PMA_query_as_controluser($sql, false, PMA_DBI_QUERY_STORE);
537 if (!$result || !PMA_DBI_num_rows($result)) {
538 $pdf->Error(sprintf(__('Please configure the coordinates for table %s'), $tableName));
540 list($this->x, $this->y) = PMA_DBI_fetch_row($result);
541 $this->x = (double) $this->x;
542 $this->y = (double) $this->y;
544 * displayfield
546 $this->displayfield = PMA_getDisplayField($db, $tableName);
548 * index
550 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tableName) . ';', null, PMA_DBI_QUERY_STORE);
551 if (PMA_DBI_num_rows($result) > 0) {
552 while ($row = PMA_DBI_fetch_assoc($result)) {
553 if ($row['Key_name'] == 'PRIMARY') {
554 $this->primary[] = $row['Column_name'];
561 * Returns title of the current table,
562 * title can have the dimensions of the table
564 * @access private
566 private function _getTitle()
568 return ($this->_showInfo ? sprintf('%.0f', $this->width) . 'x' . sprintf('%.0f', $this->height) : '') . ' ' . $this->_tableName;
572 * Sets the width of the table
574 * @param integer fontSize The font size
575 * @global object The current PDF document
576 * @access private
577 * @see PMA_PDF
579 function _setWidth($fontSize)
581 global $pdf;
583 foreach ($this->fields as $field) {
584 $this->width = max($this->width, $pdf->GetStringWidth($field));
586 $this->width += $pdf->GetStringWidth(' ');
587 $pdf->SetFont($fontSize, 'B');
589 * it is unknown what value must be added, because
590 * table title is affected by the tabe width value
592 while ($this->width < $pdf->GetStringWidth($this->_getTitle())) {
593 $this->width += 5;
595 $pdf->SetFont($fontSize, '');
599 * Sets the height of the table
601 * @access private
603 private function _setHeight()
605 $this->height = (count($this->fields) + 1) * $this->heightCell;
609 * Do draw the table
611 * @param integer fontSize The font size
612 * @param boolean setColor Whether to display color
613 * @global object The current PDF document
614 * @access public
615 * @see PMA_PDF
617 public function tableDraw($fontSize, $withDoc, $setColor = 0)
619 global $pdf, $withDoc;
621 $pdf->PMA_PDF_setXyScale($this->x, $this->y);
622 $pdf->SetFont($fontSize, 'B');
623 if ($setColor) {
624 $pdf->SetTextColor(200);
625 $pdf->SetFillColor(0, 0, 128);
627 if ($withDoc) {
628 $pdf->SetLink($pdf->PMA_links['RT'][$this->_tableName]['-'], -1);
629 } else {
630 $pdf->PMA_links['doc'][$this->_tableName]['-'] = '';
633 $pdf->PMA_PDF_cellScale($this->width, $this->heightCell, $this->_getTitle(), 1, 1, 'C', $setColor, $pdf->PMA_links['doc'][$this->_tableName]['-']);
634 $pdf->PMA_PDF_setXScale($this->x);
635 $pdf->SetFont($fontSize, '');
636 $pdf->SetTextColor(0);
637 $pdf->SetFillColor(255);
639 foreach ($this->fields as $field) {
640 if ($setColor) {
641 if (in_array($field, $this->primary)) {
642 $pdf->SetFillColor(215, 121, 123);
644 if ($field == $this->displayfield) {
645 $pdf->SetFillColor(142, 159, 224);
648 if ($withDoc) {
649 $pdf->SetLink($pdf->PMA_links['RT'][$this->_tableName][$field], -1);
650 } else {
651 $pdf->PMA_links['doc'][$this->_tableName][$field] = '';
654 $pdf->PMA_PDF_cellScale($this->width, $this->heightCell, ' ' . $field, 1, 1, 'L', $setColor, $pdf->PMA_links['doc'][$this->_tableName][$field]);
655 $pdf->PMA_PDF_setXScale($this->x);
656 $pdf->SetFillColor(255);
658 /*if ($pdf->PageNo() > 1) {
659 $pdf->PMA_PDF_die(__('The scale factor is too small to fit the schema on one page'));
660 } */
665 * Relation preferences/statistics
667 * This class fetches the table master and foreign fields positions
668 * and helps in generating the Table references and then connects
669 * master table's master field to foreign table's foreign key
670 * in PDF document.
672 * @name Relation_Stats
673 * @copyright
674 * @license
675 * @see PMA_PDF::SetDrawColor,PMA_PDF::PMA_PDF_setLineWidthScale,PMA_PDF::PMA_PDF_lineScale
677 class Relation_Stats
680 * Defines properties
682 public $xSrc, $ySrc;
683 public $srcDir;
684 public $destDir;
685 public $xDest, $yDest;
686 public $wTick = 5;
689 * The "Relation_Stats" constructor
691 * @param string master_table The master table name
692 * @param string master_field The relation field in the master table
693 * @param string foreign_table The foreign table name
694 * @param string foreigh_field The relation field in the foreign table
695 * @see Relation_Stats::_getXy
697 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
699 $src_pos = $this->_getXy($master_table, $master_field);
700 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
702 * [0] is x-left
703 * [1] is x-right
704 * [2] is y
706 $src_left = $src_pos[0] - $this->wTick;
707 $src_right = $src_pos[1] + $this->wTick;
708 $dest_left = $dest_pos[0] - $this->wTick;
709 $dest_right = $dest_pos[1] + $this->wTick;
711 $d1 = abs($src_left - $dest_left);
712 $d2 = abs($src_right - $dest_left);
713 $d3 = abs($src_left - $dest_right);
714 $d4 = abs($src_right - $dest_right);
715 $d = min($d1, $d2, $d3, $d4);
717 if ($d == $d1) {
718 $this->xSrc = $src_pos[0];
719 $this->srcDir = -1;
720 $this->xDest = $dest_pos[0];
721 $this->destDir = -1;
722 } elseif ($d == $d2) {
723 $this->xSrc = $src_pos[1];
724 $this->srcDir = 1;
725 $this->xDest = $dest_pos[0];
726 $this->destDir = -1;
727 } elseif ($d == $d3) {
728 $this->xSrc = $src_pos[0];
729 $this->srcDir = -1;
730 $this->xDest = $dest_pos[1];
731 $this->destDir = 1;
732 } else {
733 $this->xSrc = $src_pos[1];
734 $this->srcDir = 1;
735 $this->xDest = $dest_pos[1];
736 $this->destDir = 1;
738 $this->ySrc = $src_pos[2];
739 $this->yDest = $dest_pos[2];
743 * Gets arrows coordinates
745 * @param string table The current table name
746 * @param string column The relation column name
747 * @return array Arrows coordinates
748 * @access private
750 private function _getXy($table, $column)
752 $pos = array_search($column, $table->fields);
753 // x_left, x_right, y
754 return array($table->x, $table->x + + $table->width, $table->y + ($pos + 1.5) * $table->heightCell);
758 * draws relation links and arrows
759 * shows foreign key relations
761 * @param boolean changeColor Whether to use one color per relation or not
762 * @param integer i The id of the link to draw
763 * @global object The current PDF document
764 * @access public
765 * @see PMA_PDF
767 public function relationDraw($changeColor, $i)
769 global $pdf;
771 if ($changeColor) {
772 $d = $i % 6;
773 $j = ($i - $d) / 6;
774 $j = $j % 4;
775 $j++;
776 $case = array(
777 array(1, 0, 0),
778 array(0, 1, 0),
779 array(0, 0, 1),
780 array(1, 1, 0),
781 array(1, 0, 1),
782 array(0, 1, 1)
784 list ($a, $b, $c) = $case[$d];
785 $e = (1 - ($j - 1) / 6);
786 $pdf->SetDrawColor($a * 255 * $e, $b * 255 * $e, $c * 255 * $e);
787 } else {
788 $pdf->SetDrawColor(0);
790 $pdf->PMA_PDF_setLineWidthScale(0.2);
791 $pdf->PMA_PDF_lineScale($this->xSrc, $this->ySrc, $this->xSrc + $this->srcDir * $this->wTick, $this->ySrc);
792 $pdf->PMA_PDF_lineScale($this->xDest + $this->destDir * $this->wTick, $this->yDest, $this->xDest, $this->yDest);
793 $pdf->PMA_PDF_setLineWidthScale(0.1);
794 $pdf->PMA_PDF_lineScale($this->xSrc + $this->srcDir * $this->wTick, $this->ySrc, $this->xDest + $this->destDir * $this->wTick, $this->yDest);
796 * Draws arrows ->
798 $root2 = 2 * sqrt(2);
799 $pdf->PMA_PDF_lineScale($this->xSrc + $this->srcDir * $this->wTick * 0.75, $this->ySrc, $this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick, $this->ySrc + $this->wTick / $root2);
800 $pdf->PMA_PDF_lineScale($this->xSrc + $this->srcDir * $this->wTick * 0.75, $this->ySrc, $this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick, $this->ySrc - $this->wTick / $root2);
802 $pdf->PMA_PDF_lineScale($this->xDest + $this->destDir * $this->wTick / 2, $this->yDest, $this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick, $this->yDest + $this->wTick / $root2);
803 $pdf->PMA_PDF_lineScale($this->xDest + $this->destDir * $this->wTick / 2, $this->yDest, $this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick, $this->yDest - $this->wTick / $root2);
804 $pdf->SetDrawColor(0);
809 * Pdf Relation Schema Class
811 * Purpose of this class is to generate the PDF Document. PDF is widely
812 * used format for documenting text,fonts,images and 3d vector graphics.
814 * This class inherits Export_Relation_Schema class has common functionality added
815 * to this class
817 * @name Pdf_Relation_Schema
818 * @copyright
819 * @license
821 class PMA_Pdf_Relation_Schema extends PMA_Export_Relation_Schema
824 * Defines properties
826 private $_tables = array();
827 private $_relations = array();
828 private $_ff = PMA_PDF_FONT;
829 private $_xMax = 0;
830 private $_yMax = 0;
831 private $scale;
832 private $_xMin = 100000;
833 private $_yMin = 100000;
834 private $topMargin = 10;
835 private $bottomMargin = 10;
836 private $leftMargin = 10;
837 private $rightMargin = 10;
838 private $_tablewidth;
841 * The "PMA_Pdf_Relation_Schema" constructor
843 * @global object The current PDF Schema document
844 * @global string The current db name
845 * @global array The relations settings
846 * @access private
847 * @see PMA_PDF
849 function __construct()
851 global $pdf,$db,$cfgRelation;
853 $this->setPageNumber($_POST['pdf_page_number']);
854 $this->setShowGrid(isset($_POST['show_grid']));
855 $this->setShowColor(isset($_POST['show_color']));
856 $this->setShowKeys(isset($_POST['show_keys']));
857 $this->setTableDimension(isset($_POST['show_table_dimension']));
858 $this->setAllTableSameWidth(isset($_POST['all_table_same_wide']));
859 $this->setWithDataDictionary($_POST['with_doc']);
860 $this->setOrientation($_POST['orientation']);
861 $this->setPaper($_POST['paper']);
862 $this->setExportType($_POST['export_type']);
864 // Initializes a new document
865 $pdf = new PMA_PDF('L', 'mm', $this->paper);
866 $pdf->SetTitle(sprintf(__('Schema of the %s database - Page %s'), $GLOBALS['db'], $this->pageNumber));
867 $pdf->setCMargin(0);
868 $pdf->Open();
869 $pdf->SetAuthor('phpMyAdmin ' . PMA_VERSION);
870 $pdf->AliasNbPages();
871 $pdf->AddFont('DejaVuSans', '', 'dejavusans.php');
872 $pdf->AddFont('DejaVuSans', 'B', 'dejavusansb.php');
873 $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php');
874 $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserifb.php');
875 $pdf->SetFont($this->_ff, '', 14);
876 $pdf->SetAutoPageBreak('auto');
877 $alltables = $this->getAllTables($db,$this->pageNumber);
879 if ($this->withDoc) {
880 $pdf->SetAutoPageBreak('auto', 15);
881 $pdf->setCMargin(1);
882 $this->dataDictionaryDoc($alltables);
883 $pdf->SetAutoPageBreak('auto');
884 $pdf->setCMargin(0);
887 $pdf->Addpage();
889 if ($this->withDoc) {
890 $pdf->SetLink($pdf->PMA_links['RT']['-'], -1);
891 $pdf->Bookmark(__('Relational schema'));
892 $pdf->SetAlias('{00}', $pdf->PageNo()) ;
893 $this->topMargin = 28;
894 $this->bottomMargin = 28;
897 /* snip */
898 foreach ($alltables as $table) {
899 if (!isset($this->tables[$table])) {
900 $this->tables[$table] = new Table_Stats($table, $this->_ff, $this->pageNumber, $this->_tablewidth, $this->showKeys, $this->tableDimension);
902 if ($this->sameWide) {
903 $this->tables[$table]->width = $this->_tablewidth;
905 $this->_setMinMax($this->tables[$table]);
908 // Defines the scale factor
909 $this->scale = ceil(
910 max(
911 ($this->_xMax - $this->_xMin) / ($pdf->getFh() - $this->rightMargin - $this->leftMargin),
912 ($this->_yMax - $this->_yMin) / ($pdf->getFw() - $this->topMargin - $this->bottomMargin))
913 * 100) / 100;
915 $pdf->PMA_PDF_setScale($this->scale, $this->_xMin, $this->_yMin, $this->leftMargin, $this->topMargin);
916 // Builds and save the PDF document
917 $pdf->PMA_PDF_setLineWidthScale(0.1);
919 if ($this->showGrid) {
920 $pdf->SetFontSize(10);
921 $this->_strokeGrid();
923 $pdf->PMA_PDF_setFontSizeScale(14);
924 // previous logic was checking master tables and foreign tables
925 // but I think that looping on every table of the pdf page as a master
926 // and finding its foreigns is OK (then we can support innodb)
927 $seen_a_relation = false;
928 foreach ($alltables as $one_table) {
929 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
930 if ($exist_rel) {
931 $seen_a_relation = true;
932 foreach ($exist_rel as $master_field => $rel) {
933 // put the foreign table on the schema only if selected
934 // by the user
935 // (do not use array_search() because we would have to
936 // to do a === FALSE and this is not PHP3 compatible)
937 if (in_array($rel['foreign_table'], $alltables)) {
938 $this->_addRelation($one_table, $master_field, $rel['foreign_table'], $rel['foreign_field'], $this->tableDimension);
940 } // end while
941 } // end if
942 } // end while
944 if ($seen_a_relation) {
945 $this->_drawRelations($this->showColor);
947 $this->_drawTables($this->showColor);
948 $this->_showOutput($this->pageNumber);
949 exit();
953 * Sets X and Y minimum and maximum for a table cell
955 * @param string table The table name of which sets XY co-ordinates
956 * @access private
958 private function _setMinMax($table)
960 $this->_xMax = max($this->_xMax, $table->x + $table->width);
961 $this->_yMax = max($this->_yMax, $table->y + $table->height);
962 $this->_xMin = min($this->_xMin, $table->x);
963 $this->_yMin = min($this->_yMin, $table->y);
967 * Defines relation objects
969 * @param string master_table The master table name
970 * @param string master_field The relation field in the master table
971 * @param string foreign_table The foreign table name
972 * @param string foreign_field The relation field in the foreign table
973 * @param boolean show_info Whether to display table position or not
974 * @access private
975 * @see _setMinMax
977 private function _addRelation($masterTable, $masterField, $foreignTable, $foreignField, $showInfo)
979 if (!isset($this->tables[$masterTable])) {
980 $this->tables[$masterTable] = new Table_Stats($masterTable, $this->_ff, $this->pageNumber, $this->_tablewidth, false, $showInfo);
981 $this->_setMinMax($this->tables[$masterTable]);
983 if (!isset($this->tables[$foreignTable])) {
984 $this->tables[$foreignTable] = new Table_Stats($foreignTable, $this->_ff, $this->pageNumber, $this->_tablewidth, false, $showInfo);
985 $this->_setMinMax($this->tables[$foreignTable]);
987 $this->relations[] = new Relation_Stats($this->tables[$masterTable], $masterField, $this->tables[$foreignTable], $foreignField);
991 * Draws the grid
993 * @global object the current PMA_PDF instance
994 * @access private
995 * @see PMA_PDF
997 private function _strokeGrid()
999 global $pdf;
1001 $pdf->SetMargins(0, 0);
1002 $pdf->SetDrawColor(200, 200, 200);
1003 // Draws horizontal lines
1004 for ($l = 0; $l < 21; $l++) {
1005 $pdf->line(0, $l * 10, $pdf->getFh(), $l * 10);
1006 // Avoid duplicates
1007 if ($l > 0) {
1008 $pdf->SetXY(0, $l * 10);
1009 $label = (string) sprintf('%.0f', ($l * 10 - $this->topMargin) * $this->scale + $this->_yMin);
1010 $pdf->Cell(5, 5, ' ' . $label);
1011 } // end if
1012 } // end for
1013 // Draws vertical lines
1014 for ($j = 0; $j < 30 ;$j++) {
1015 $pdf->line($j * 10, 0, $j * 10, $pdf->getFw());
1016 $pdf->SetXY($j * 10, 0);
1017 $label = (string) sprintf('%.0f', ($j * 10 - $this->leftMargin) * $this->scale + $this->_xMin);
1018 $pdf->Cell(5, 7, $label);
1023 * Draws relation arrows
1025 * @param boolean changeColor Whether to use one color per relation or not
1026 * @access private
1027 * @see Relation_Stats::relationdraw()
1029 private function _drawRelations($changeColor)
1031 $i = 0;
1032 foreach ($this->relations as $relation) {
1033 $relation->relationDraw($changeColor, $i);
1034 $i++;
1039 * Draws tables
1041 * @param boolean changeColor Whether to display table position or not
1042 * @access private
1043 * @see Table_Stats::tableDraw()
1045 private function _drawTables($changeColor = 0)
1047 foreach ($this->tables as $table) {
1048 $table->tableDraw($this->_ff, $this->withDoc, $changeColor);
1053 * Ouputs the PDF document to a file
1054 * or sends the output to browser
1056 * @global object The current PDF document
1057 * @global string The current database name
1058 * @global integer The current page number (from the
1059 * $cfg['Servers'][$i]['table_coords'] table)
1060 * @access private
1061 * @see PMA_PDF
1063 private function _showOutput($pageNumber)
1065 global $pdf, $db, $cfgRelation;
1067 $pdf->SetFontSize(14);
1068 $pdf->SetLineWidth(0.2);
1069 $pdf->SetDisplayMode('fullpage');
1070 // Get the name of this pdfpage to use as filename (Mike Beck)
1071 $_name_sql = 'SELECT page_descr FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
1072 . ' WHERE page_nr = ' . $pageNumber;
1073 $_name_rs = PMA_query_as_controluser($_name_sql);
1074 if ($_name_rs) {
1075 $_name_row = PMA_DBI_fetch_row($_name_rs);
1076 $filename = $_name_row[0] . '.pdf';
1078 if (empty($filename)) {
1079 $filename = $pageNumber . '.pdf';
1081 // instead of $pdf->Output():
1082 $pdfData = $pdf->getPDFData();
1083 header('Content-Type: application/pdf');
1084 header('Content-Length: '.strlen($pdfData).'');
1085 header('Content-disposition: attachment; filename="'.$filename.'"');
1086 echo $pdfData;
1089 public function dataDictionaryDoc($alltables)
1091 global $db, $pdf, $orientation, $paper;
1092 // TOC
1093 $pdf->addpage($GLOBALS['orientation']);
1094 $pdf->Cell(0, 9, __('Table of contents'), 1, 0, 'C');
1095 $pdf->Ln(15);
1096 $i = 1;
1097 foreach ($alltables as $table) {
1098 $pdf->PMA_links['doc'][$table]['-'] = $pdf->AddLink();
1099 $pdf->SetX(10);
1100 // $pdf->Ln(1);
1101 $pdf->Cell(0, 6, __('Page number:') . ' {' . sprintf("%02d", $i + 1) . '}', 0, 0, 'R', 0, $pdf->PMA_links['doc'][$table]['-']);
1102 $pdf->SetX(10);
1103 $pdf->Cell(0, 6, $i . ' ' . $table, 0, 1, 'L', 0, $pdf->PMA_links['doc'][$table]['-']);
1104 // $pdf->Ln(1);
1105 $result = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';');
1106 while ($row = PMA_DBI_fetch_assoc($result)) {
1107 $pdf->SetX(20);
1108 $field_name = $row['Field'];
1109 $pdf->PMA_links['doc'][$table][$field_name] = $pdf->AddLink();
1110 // $pdf->Cell(0, 6, $field_name,0,1,'L',0, $pdf->PMA_links['doc'][$table][$field_name]);
1112 $lasttable = $table;
1113 $i++;
1115 $pdf->PMA_links['RT']['-'] = $pdf->AddLink();
1116 $pdf->SetX(10);
1117 $pdf->Cell(0, 6, __('Page number:') . ' {' . sprintf("%02d", $i + 1) . '}', 0, 0, 'R', 0, $pdf->PMA_links['doc'][$lasttable]['-']);
1118 $pdf->SetX(10);
1119 $pdf->Cell(0, 6, $i + 1 . ' ' . __('Relational schema'), 0, 1, 'L', 0, $pdf->PMA_links['RT']['-']);
1120 $z = 0;
1121 foreach ($alltables as $table) {
1122 $z++;
1123 $pdf->addpage($GLOBALS['orientation']);
1124 $pdf->Bookmark($table);
1125 $pdf->SetAlias('{' . sprintf("%02d", $z) . '}', $pdf->PageNo()) ;
1126 $pdf->PMA_links['RT'][$table]['-'] = $pdf->AddLink();
1127 $pdf->SetLink($pdf->PMA_links['doc'][$table]['-'], -1);
1128 $pdf->SetFont('', 'B', 18);
1129 $pdf->Cell(0, 8, $z . ' ' . $table, 1, 1, 'C', 0, $pdf->PMA_links['RT'][$table]['-']);
1130 $pdf->SetFont('', '', 8);
1131 $pdf->ln();
1133 $cfgRelation = PMA_getRelationsParam();
1134 $comments = PMA_getComments($db, $table);
1135 if ($cfgRelation['mimework']) {
1136 $mime_map = PMA_getMIME($db, $table, true);
1140 * Gets table informations
1142 $showtable = PMA_Table::sGetStatusInfo($db, $table);
1143 $num_rows = (isset($showtable['Rows']) ? $showtable['Rows'] : 0);
1144 $show_comment = (isset($showtable['Comment']) ? $showtable['Comment'] : '');
1145 $create_time = (isset($showtable['Create_time']) ? PMA_localisedDate(strtotime($showtable['Create_time'])) : '');
1146 $update_time = (isset($showtable['Update_time']) ? PMA_localisedDate(strtotime($showtable['Update_time'])) : '');
1147 $check_time = (isset($showtable['Check_time']) ? PMA_localisedDate(strtotime($showtable['Check_time'])) : '');
1150 * Gets table keys and retains them
1152 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
1153 $primary = '';
1154 $indexes = array();
1155 $lastIndex = '';
1156 $indexes_info = array();
1157 $indexes_data = array();
1158 $pk_array = array(); // will be use to emphasis prim. keys in the table
1159 // view
1160 while ($row = PMA_DBI_fetch_assoc($result)) {
1161 // Backups the list of primary keys
1162 if ($row['Key_name'] == 'PRIMARY') {
1163 $primary .= $row['Column_name'] . ', ';
1164 $pk_array[$row['Column_name']] = 1;
1166 // Retains keys informations
1167 if ($row['Key_name'] != $lastIndex) {
1168 $indexes[] = $row['Key_name'];
1169 $lastIndex = $row['Key_name'];
1171 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
1172 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
1173 if (isset($row['Cardinality'])) {
1174 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
1176 // I don't know what does following column mean....
1177 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
1178 $indexes_info[$row['Key_name']]['Comment'] = $row['Comment'];
1180 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
1181 if (isset($row['Sub_part'])) {
1182 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
1184 } // end while
1185 if ($result) {
1186 PMA_DBI_free_result($result);
1190 * Gets fields properties
1192 $result = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE);
1193 $fields_cnt = PMA_DBI_num_rows($result);
1194 // Check if we can use Relations (Mike Beck)
1195 if (!empty($cfgRelation['relation'])) {
1196 // Find which tables are related with the current one and write it in
1197 // an array
1198 $res_rel = PMA_getForeigners($db, $table);
1200 if (count($res_rel) > 0) {
1201 $have_rel = true;
1202 } else {
1203 $have_rel = false;
1205 } else {
1206 $have_rel = false;
1207 } // end if
1210 * Displays the comments of the table if MySQL >= 3.23
1213 $break = false;
1214 if (!empty($show_comment)) {
1215 $pdf->Cell(0, 3, __('Table comments') . ' : ' . $show_comment, 0, 1);
1216 $break = true;
1219 if (!empty($create_time)) {
1220 $pdf->Cell(0, 3, __('Creation') . ': ' . $create_time, 0, 1);
1221 $break = true;
1224 if (!empty($update_time)) {
1225 $pdf->Cell(0, 3, __('Last update') . ': ' . $update_time, 0, 1);
1226 $break = true;
1229 if (!empty($check_time)) {
1230 $pdf->Cell(0, 3, __('Last check') . ': ' . $check_time, 0, 1);
1231 $break = true;
1234 if ($break == true) {
1235 $pdf->Cell(0, 3, '', 0, 1);
1236 $pdf->Ln();
1239 $pdf->SetFont('', 'B');
1240 if (isset($orientation) && $orientation == 'L') {
1241 $pdf->Cell(25, 8, ucfirst(__('Column')), 1, 0, 'C');
1242 $pdf->Cell(20, 8, ucfirst(__('Type')), 1, 0, 'C');
1243 $pdf->Cell(20, 8, ucfirst(__('Attributes')), 1, 0, 'C');
1244 $pdf->Cell(10, 8, ucfirst(__('Null')), 1, 0, 'C');
1245 $pdf->Cell(20, 8, ucfirst(__('Default')), 1, 0, 'C');
1246 $pdf->Cell(25, 8, ucfirst(__('Extra')), 1, 0, 'C');
1247 $pdf->Cell(45, 8, ucfirst(__('Links to')), 1, 0, 'C');
1249 if ($paper == 'A4') {
1250 $comments_width = 67;
1251 } else {
1252 // this is really intended for 'letter'
1254 * @todo find optimal width for all formats
1256 $comments_width = 50;
1258 $pdf->Cell($comments_width, 8, ucfirst(__('Comments')), 1, 0, 'C');
1259 $pdf->Cell(45, 8, 'MIME', 1, 1, 'C');
1260 $pdf->SetWidths(array(25, 20, 20, 10, 20, 25, 45, $comments_width, 45));
1261 } else {
1262 $pdf->Cell(20, 8, ucfirst(__('Column')), 1, 0, 'C');
1263 $pdf->Cell(20, 8, ucfirst(__('Type')), 1, 0, 'C');
1264 $pdf->Cell(20, 8, ucfirst(__('Attributes')), 1, 0, 'C');
1265 $pdf->Cell(10, 8, ucfirst(__('Null')), 1, 0, 'C');
1266 $pdf->Cell(15, 8, ucfirst(__('Default')), 1, 0, 'C');
1267 $pdf->Cell(15, 8, ucfirst(__('Extra')), 1, 0, 'C');
1268 $pdf->Cell(30, 8, ucfirst(__('Links to')), 1, 0, 'C');
1269 $pdf->Cell(30, 8, ucfirst(__('Comments')), 1, 0, 'C');
1270 $pdf->Cell(30, 8, 'MIME', 1, 1, 'C');
1271 $pdf->SetWidths(array(20, 20, 20, 10, 15, 15, 30, 30, 30));
1273 $pdf->SetFont('', '');
1275 while ($row = PMA_DBI_fetch_assoc($result)) {
1276 $type = $row['Type'];
1277 // reformat mysql query output
1278 // set or enum types: slashes single quotes inside options
1279 if (preg_match('@^(set|enum)\((.+)\)$@i', $type, $tmp)) {
1280 $tmp[2] = substr(preg_replace("@([^,])''@", "\\1\\'", ',' . $tmp[2]), 1);
1281 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
1282 $type_nowrap = '';
1284 $binary = 0;
1285 $unsigned = 0;
1286 $zerofill = 0;
1287 } else {
1288 $type_nowrap = ' nowrap="nowrap"';
1289 $type = preg_replace('@BINARY@i', '', $type);
1290 $type = preg_replace('@ZEROFILL@i', '', $type);
1291 $type = preg_replace('@UNSIGNED@i', '', $type);
1292 if (empty($type)) {
1293 $type = '&nbsp;';
1296 $binary = stristr($row['Type'], 'BINARY');
1297 $unsigned = stristr($row['Type'], 'UNSIGNED');
1298 $zerofill = stristr($row['Type'], 'ZEROFILL');
1300 $attribute = ' ';
1301 if ($binary) {
1302 $attribute = 'BINARY';
1304 if ($unsigned) {
1305 $attribute = 'UNSIGNED';
1307 if ($zerofill) {
1308 $attribute = 'UNSIGNED ZEROFILL';
1310 if (!isset($row['Default'])) {
1311 if ($row['Null'] != '' && $row['Null'] != 'NO') {
1312 $row['Default'] = 'NULL';
1315 $field_name = $row['Field'];
1316 // $pdf->Ln();
1317 $pdf->PMA_links['RT'][$table][$field_name] = $pdf->AddLink();
1318 $pdf->Bookmark($field_name, 1, -1);
1319 $pdf->SetLink($pdf->PMA_links['doc'][$table][$field_name], -1);
1320 $pdf_row = array($field_name,
1321 $type,
1322 $attribute,
1323 ($row['Null'] == '' || $row['Null'] == 'NO') ? __('No') : __('Yes'),
1324 ((isset($row['Default'])) ? $row['Default'] : ''),
1325 $row['Extra'],
1326 ((isset($res_rel[$field_name])) ? $res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field'] : ''),
1327 ((isset($comments[$field_name])) ? $comments[$field_name] : ''),
1328 ((isset($mime_map) && isset($mime_map[$field_name])) ? str_replace('_', '/', $mime_map[$field_name]['mimetype']) : '')
1330 $links[0] = $pdf->PMA_links['RT'][$table][$field_name];
1331 if (isset($res_rel[$field_name]['foreign_table']) AND
1332 isset($res_rel[$field_name]['foreign_field']) AND
1333 isset($pdf->PMA_links['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']])
1336 $links[6] = $pdf->PMA_links['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']];
1337 } else {
1338 unset($links[6]);
1340 $pdf->Row($pdf_row, $links);
1341 } // end while
1342 $pdf->SetFont('', '', 14);
1343 PMA_DBI_free_result($result);
1344 } //end each