Conditional Ajax for DROP DATABASE
[phpmyadmin-themes.git] / libraries / schema / Pdf_Relation_Schema.class.php
blob46644dc613e2a018d2dddc35787adfe31a437853
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 getH()
46 return $this->h;
49 public function getW()
51 return $this->w;
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, $page = '')
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 SetWidths($w)
348 // column widths
349 $this->widths = $w;
352 function Row($data, $links)
354 // line height
355 $nb = 0;
356 $data_cnt = count($data);
357 for ($i = 0;$i < $data_cnt;$i++)
358 $nb = max($nb, $this->NbLines($this->widths[$i], $data[$i]));
359 $il = $this->FontSize;
360 $h = ($il + 1) * $nb;
361 // page break if necessary
362 $this->CheckPageBreak($h);
363 // draw the cells
364 $data_cnt = count($data);
365 for ($i = 0;$i < $data_cnt;$i++) {
366 $w = $this->widths[$i];
367 // save current position
368 $x = $this->GetX();
369 $y = $this->GetY();
370 // draw the border
371 $this->Rect($x, $y, $w, $h);
372 if (isset($links[$i])) {
373 $this->Link($x, $y, $w, $h, $links[$i]);
375 // print text
376 $this->MultiCell($w, $il + 1, $data[$i], 0, 'L');
377 // go to right side
378 $this->SetXY($x + $w, $y);
380 // go to line
381 $this->Ln($h);
384 function NbLines($w, $txt)
386 // compute number of lines used by a multicell of width w
387 $cw = &$this->CurrentFont['cw'];
388 if ($w == 0) {
389 $w = $this->w - $this->rMargin - $this->x;
391 $wmax = ($w-2 * $this->cMargin) * 1000 / $this->FontSize;
392 $s = str_replace("\r", '', $txt);
393 $nb = strlen($s);
394 if ($nb > 0 and $s[$nb-1] == "\n") {
395 $nb--;
397 $sep = -1;
398 $i = 0;
399 $j = 0;
400 $l = 0;
401 $nl = 1;
402 while ($i < $nb) {
403 $c = $s[$i];
404 if ($c == "\n") {
405 $i++;
406 $sep = -1;
407 $j = $i;
408 $l = 0;
409 $nl++;
410 continue;
412 if ($c == ' ') {
413 $sep = $i;
415 $l += isset($cw[ord($c)])?$cw[ord($c)]:0 ;
416 if ($l > $wmax) {
417 if ($sep == -1) {
418 if ($i == $j) {
419 $i++;
421 } else {
422 $i = $sep + 1;
424 $sep = -1;
425 $j = $i;
426 $l = 0;
427 $nl++;
428 } else {
429 $i++;
432 return $nl;
437 * Table preferences/statistics
439 * This class preserves the table co-ordinates,fields
440 * and helps in drawing/generating the Tables in PDF document.
442 * @name Table_Stats
443 * @copyright
444 * @license
445 * @see PMA_PDF
447 class Table_Stats
450 * Defines properties
452 private $_tableName;
453 private $_showInfo = false;
455 public $nb_fiels;
456 public $width = 0;
457 public $height;
458 public $fields = array();
459 public $heightCell = 6;
460 public $x, $y;
461 public $primary = array();
464 * The "Table_Stats" constructor
466 * @param string table_name The table name
467 * @param integer fontSize The font size
468 * @param integer pageNumber The current page number (from the
469 * $cfg['Servers'][$i]['table_coords'] table)
470 * @param integer sameWideWidth The max. with among tables
471 * @param boolean showKeys Whether to display keys or not
472 * @param boolean showInfo Whether to display table position or not
473 * @global object The current PDF document
474 * @global array The relations settings
475 * @global string The current db name
476 * @see PMA_PDF, Table_Stats::Table_Stats_setWidth,
477 Table_Stats::Table_Stats_setHeight
479 function __construct($tableName, $fontSize, $pageNumber, &$sameWideWidth, $showKeys = false, $showInfo = false)
481 global $pdf, $cfgRelation, $db;
483 $this->_tableName = $tableName;
484 $sql = 'DESCRIBE ' . PMA_backquote($tableName);
485 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE);
486 if (!$result || !PMA_DBI_num_rows($result)) {
487 $pdf->Error(sprintf(__('The %s table doesn\'t exist!'), $tableName));
489 // load fields
490 //check to see if it will load all fields or only the foreign keys
491 if ($showKeys) {
492 $indexes = PMA_Index::getFromTable($this->_tableName, $db);
493 $all_columns = array();
494 foreach ($indexes as $index) {
495 $all_columns = array_merge($all_columns, array_flip(array_keys($index->getColumns())));
497 $this->fields = array_keys($all_columns);
498 } else {
499 while ($row = PMA_DBI_fetch_row($result)) {
500 $this->fields[] = $row[0];
504 $this->_showInfo = $showInfo;
505 $this->_setHeight();
507 * setWidth must me after setHeight, because title
508 * can include table height which changes table width
510 $this->_setWidth($fontSize);
511 if ($sameWideWidth < $this->width) {
512 $sameWideWidth = $this->width;
514 $sql = 'SELECT x, y FROM '
515 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
516 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
517 . ' AND table_name = \'' . PMA_sqlAddslashes($tableName) . '\''
518 . ' AND pdf_page_number = ' . $pageNumber;
519 $result = PMA_query_as_controluser($sql, false, PMA_DBI_QUERY_STORE);
520 if (!$result || !PMA_DBI_num_rows($result)) {
521 $pdf->Error(sprintf(__('Please configure the coordinates for table %s'), $tableName));
523 list($this->x, $this->y) = PMA_DBI_fetch_row($result);
524 $this->x = (double) $this->x;
525 $this->y = (double) $this->y;
527 * displayfield
529 $this->displayfield = PMA_getDisplayField($db, $tableName);
531 * index
533 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tableName) . ';', null, PMA_DBI_QUERY_STORE);
534 if (PMA_DBI_num_rows($result) > 0) {
535 while ($row = PMA_DBI_fetch_assoc($result)) {
536 if ($row['Key_name'] == 'PRIMARY') {
537 $this->primary[] = $row['Column_name'];
544 * Returns title of the current table,
545 * title can have the dimensions of the table
547 * @access private
549 private function _getTitle()
551 return ($this->_showInfo ? sprintf('%.0f', $this->width) . 'x' . sprintf('%.0f', $this->height) : '') . ' ' . $this->_tableName;
555 * Sets the width of the table
557 * @param integer fontSize The font size
558 * @global object The current PDF document
559 * @access private
560 * @see PMA_PDF
562 function _setWidth($fontSize)
564 global $pdf;
566 foreach ($this->fields as $field) {
567 $this->width = max($this->width, $pdf->GetStringWidth($field));
569 $this->width += $pdf->GetStringWidth(' ');
570 $pdf->SetFont($fontSize, 'B');
572 * it is unknown what value must be added, because
573 * table title is affected by the tabe width value
575 while ($this->width < $pdf->GetStringWidth($this->_getTitle())) {
576 $this->width += 5;
578 $pdf->SetFont($fontSize, '');
582 * Sets the height of the table
584 * @access private
586 private function _setHeight()
588 $this->height = (count($this->fields) + 1) * $this->heightCell;
592 * Do draw the table
594 * @param integer fontSize The font size
595 * @param boolean setColor Whether to display color
596 * @global object The current PDF document
597 * @access public
598 * @see PMA_PDF
600 public function tableDraw($fontSize, $withDoc, $setColor = 0)
602 global $pdf, $withDoc;
604 $pdf->PMA_PDF_setXyScale($this->x, $this->y);
605 $pdf->SetFont($fontSize, 'B');
606 if ($setColor) {
607 $pdf->SetTextColor(200);
608 $pdf->SetFillColor(0, 0, 128);
610 if ($withDoc) {
611 $pdf->SetLink($pdf->PMA_links['RT'][$this->_tableName]['-'], -1);
612 } else {
613 $pdf->PMA_links['doc'][$this->_tableName]['-'] = '';
616 $pdf->PMA_PDF_cellScale($this->width, $this->heightCell, $this->_getTitle(), 1, 1, 'C', $setColor, $pdf->PMA_links['doc'][$this->_tableName]['-']);
617 $pdf->PMA_PDF_setXScale($this->x);
618 $pdf->SetFont($fontSize, '');
619 $pdf->SetTextColor(0);
620 $pdf->SetFillColor(255);
622 foreach ($this->fields as $field) {
623 if ($setColor) {
624 if (in_array($field, $this->primary)) {
625 $pdf->SetFillColor(215, 121, 123);
627 if ($field == $this->displayfield) {
628 $pdf->SetFillColor(142, 159, 224);
631 if ($withDoc) {
632 $pdf->SetLink($pdf->PMA_links['RT'][$this->_tableName][$field], -1);
633 } else {
634 $pdf->PMA_links['doc'][$this->_tableName][$field] = '';
637 $pdf->PMA_PDF_cellScale($this->width, $this->heightCell, ' ' . $field, 1, 1, 'L', $setColor, $pdf->PMA_links['doc'][$this->_tableName][$field]);
638 $pdf->PMA_PDF_setXScale($this->x);
639 $pdf->SetFillColor(255);
641 /*if ($pdf->PageNo() > 1) {
642 $pdf->PMA_PDF_die(__('The scale factor is too small to fit the schema on one page'));
643 } */
648 * Relation preferences/statistics
650 * This class fetches the table master and foreign fields positions
651 * and helps in generating the Table references and then connects
652 * master table's master field to foreign table's foreign key
653 * in PDF document.
655 * @name Relation_Stats
656 * @copyright
657 * @license
658 * @see PMA_PDF::SetDrawColor,PMA_PDF::PMA_PDF_setLineWidthScale,PMA_PDF::PMA_PDF_lineScale
660 class Relation_Stats
663 * Defines properties
665 public $xSrc, $ySrc;
666 public $srcDir;
667 public $destDir;
668 public $xDest, $yDest;
669 public $wTick = 5;
672 * The "Relation_Stats" constructor
674 * @param string master_table The master table name
675 * @param string master_field The relation field in the master table
676 * @param string foreign_table The foreign table name
677 * @param string foreigh_field The relation field in the foreign table
678 * @see Relation_Stats::_getXy
680 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
682 $src_pos = $this->_getXy($master_table, $master_field);
683 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
685 * [0] is x-left
686 * [1] is x-right
687 * [2] is y
689 $src_left = $src_pos[0] - $this->wTick;
690 $src_right = $src_pos[1] + $this->wTick;
691 $dest_left = $dest_pos[0] - $this->wTick;
692 $dest_right = $dest_pos[1] + $this->wTick;
694 $d1 = abs($src_left - $dest_left);
695 $d2 = abs($src_right - $dest_left);
696 $d3 = abs($src_left - $dest_right);
697 $d4 = abs($src_right - $dest_right);
698 $d = min($d1, $d2, $d3, $d4);
700 if ($d == $d1) {
701 $this->xSrc = $src_pos[0];
702 $this->srcDir = -1;
703 $this->xDest = $dest_pos[0];
704 $this->destDir = -1;
705 } elseif ($d == $d2) {
706 $this->xSrc = $src_pos[1];
707 $this->srcDir = 1;
708 $this->xDest = $dest_pos[0];
709 $this->destDir = -1;
710 } elseif ($d == $d3) {
711 $this->xSrc = $src_pos[0];
712 $this->srcDir = -1;
713 $this->xDest = $dest_pos[1];
714 $this->destDir = 1;
715 } else {
716 $this->xSrc = $src_pos[1];
717 $this->srcDir = 1;
718 $this->xDest = $dest_pos[1];
719 $this->destDir = 1;
721 $this->ySrc = $src_pos[2];
722 $this->yDest = $dest_pos[2];
726 * Gets arrows coordinates
728 * @param string table The current table name
729 * @param string column The relation column name
730 * @return array Arrows coordinates
731 * @access private
733 private function _getXy($table, $column)
735 $pos = array_search($column, $table->fields);
736 // x_left, x_right, y
737 return array($table->x, $table->x + + $table->width, $table->y + ($pos + 1.5) * $table->heightCell);
741 * draws relation links and arrows
742 * shows foreign key relations
744 * @param boolean changeColor Whether to use one color per relation or not
745 * @param integer i The id of the link to draw
746 * @global object The current PDF document
747 * @access public
748 * @see PMA_PDF
750 public function relationDraw($changeColor, $i)
752 global $pdf;
754 if ($changeColor) {
755 $d = $i % 6;
756 $j = ($i - $d) / 6;
757 $j = $j % 4;
758 $j++;
759 $case = array(
760 array(1, 0, 0),
761 array(0, 1, 0),
762 array(0, 0, 1),
763 array(1, 1, 0),
764 array(1, 0, 1),
765 array(0, 1, 1)
767 list ($a, $b, $c) = $case[$d];
768 $e = (1 - ($j - 1) / 6);
769 $pdf->SetDrawColor($a * 255 * $e, $b * 255 * $e, $c * 255 * $e);
770 } else {
771 $pdf->SetDrawColor(0);
773 $pdf->PMA_PDF_setLineWidthScale(0.2);
774 $pdf->PMA_PDF_lineScale($this->xSrc, $this->ySrc, $this->xSrc + $this->srcDir * $this->wTick, $this->ySrc);
775 $pdf->PMA_PDF_lineScale($this->xDest + $this->destDir * $this->wTick, $this->yDest, $this->xDest, $this->yDest);
776 $pdf->PMA_PDF_setLineWidthScale(0.1);
777 $pdf->PMA_PDF_lineScale($this->xSrc + $this->srcDir * $this->wTick, $this->ySrc, $this->xDest + $this->destDir * $this->wTick, $this->yDest);
779 * Draws arrows ->
781 $root2 = 2 * sqrt(2);
782 $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);
783 $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);
785 $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);
786 $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);
787 $pdf->SetDrawColor(0);
792 * Pdf Relation Schema Class
794 * Purpose of this class is to generate the PDF Document. PDF is widely
795 * used format for documenting text,fonts,images and 3d vector graphics.
797 * This class inherits Export_Relation_Schema class has common functionality added
798 * to this class
800 * @name Pdf_Relation_Schema
801 * @copyright
802 * @license
804 class PMA_Pdf_Relation_Schema extends PMA_Export_Relation_Schema
807 * Defines properties
809 private $_tables = array();
810 private $_relations = array();
811 private $_ff = PMA_PDF_FONT;
812 private $_xMax = 0;
813 private $_yMax = 0;
814 private $scale;
815 private $_xMin = 100000;
816 private $_yMin = 100000;
817 private $topMargin = 10;
818 private $bottomMargin = 10;
819 private $leftMargin = 10;
820 private $rightMargin = 10;
821 private $_tablewidth;
824 * The "PMA_Pdf_Relation_Schema" constructor
826 * @global object The current PDF Schema document
827 * @global string The current db name
828 * @global array The relations settings
829 * @access private
830 * @see PMA_PDF
832 function __construct()
834 global $pdf,$db,$cfgRelation;
836 $this->setPageNumber($_POST['pdf_page_number']);
837 $this->setShowGrid(isset($_POST['show_grid']));
838 $this->setShowColor(isset($_POST['show_color']));
839 $this->setShowKeys(isset($_POST['show_keys']));
840 $this->setTableDimension(isset($_POST['show_table_dimension']));
841 $this->setAllTableSameWidth(isset($_POST['all_table_same_wide']));
842 $this->setWithDataDictionary($_POST['with_doc']);
843 $this->setOrientation($_POST['orientation']);
844 $this->setPaper($_POST['paper']);
845 $this->setExportType($_POST['export_type']);
847 // Initializes a new document
848 $pdf = new PMA_PDF($this->orientation, 'mm', $this->paper);
849 $pdf->SetTitle(sprintf(__('Schema of the %s database - Page %s'), $GLOBALS['db'], $this->pageNumber));
850 $pdf->setCMargin(0);
851 $pdf->Open();
852 $pdf->SetAuthor('phpMyAdmin ' . PMA_VERSION);
853 $pdf->AliasNbPages();
854 $pdf->AddFont('DejaVuSans', '', 'dejavusans.php');
855 $pdf->AddFont('DejaVuSans', 'B', 'dejavusansb.php');
856 $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php');
857 $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserifb.php');
858 $pdf->SetFont($this->_ff, '', 14);
859 $pdf->SetAutoPageBreak('auto');
860 $alltables = $this->getAllTables($db,$this->pageNumber);
862 if ($this->withDoc) {
863 $pdf->SetAutoPageBreak('auto', 15);
864 $pdf->setCMargin(1);
865 $this->dataDictionaryDoc($alltables);
866 $pdf->SetAutoPageBreak('auto');
867 $pdf->setCMargin(0);
870 $pdf->Addpage();
872 if ($this->withDoc) {
873 $pdf->SetLink($pdf->PMA_links['RT']['-'], -1);
874 $pdf->Bookmark(__('Relational schema'));
875 $pdf->SetAlias('{00}', $pdf->PageNo()) ;
876 $this->topMargin = 28;
877 $this->bottomMargin = 28;
880 /* snip */
881 foreach ($alltables as $table) {
882 if (!isset($this->tables[$table])) {
883 $this->tables[$table] = new Table_Stats($table, $this->_ff, $this->pageNumber, $this->_tablewidth, $this->showKeys, $this->tableDimension);
885 if ($this->sameWide) {
886 $this->tables[$table]->width = $this->_tablewidth;
888 $this->_setMinMax($this->tables[$table]);
891 // Defines the scale factor
892 $this->scale = ceil(
893 max(
894 ($this->_xMax - $this->_xMin) / ($pdf->getW() - $this->rightMargin - $this->leftMargin),
895 ($this->_yMax - $this->_yMin) / ($pdf->getH() - $this->topMargin - $this->bottomMargin))
896 * 100) / 100;
898 $pdf->PMA_PDF_setScale($this->scale, $this->_xMin, $this->_yMin, $this->leftMargin, $this->topMargin);
899 // Builds and save the PDF document
900 $pdf->PMA_PDF_setLineWidthScale(0.1);
902 if ($this->showGrid) {
903 $pdf->SetFontSize(10);
904 $this->_strokeGrid();
906 $pdf->PMA_PDF_setFontSizeScale(14);
907 // previous logic was checking master tables and foreign tables
908 // but I think that looping on every table of the pdf page as a master
909 // and finding its foreigns is OK (then we can support innodb)
910 $seen_a_relation = false;
911 foreach ($alltables as $one_table) {
912 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
913 if ($exist_rel) {
914 $seen_a_relation = true;
915 foreach ($exist_rel as $master_field => $rel) {
916 // put the foreign table on the schema only if selected
917 // by the user
918 // (do not use array_search() because we would have to
919 // to do a === FALSE and this is not PHP3 compatible)
920 if (in_array($rel['foreign_table'], $alltables)) {
921 $this->_addRelation($one_table, $master_field, $rel['foreign_table'], $rel['foreign_field'], $this->tableDimension);
923 } // end while
924 } // end if
925 } // end while
927 if ($seen_a_relation) {
928 $this->_drawRelations($this->showColor);
930 $this->_drawTables($this->showColor);
931 $this->_showOutput($this->pageNumber);
932 exit();
936 * Sets X and Y minimum and maximum for a table cell
938 * @param string table The table name of which sets XY co-ordinates
939 * @access private
941 private function _setMinMax($table)
943 $this->_xMax = max($this->_xMax, $table->x + $table->width);
944 $this->_yMax = max($this->_yMax, $table->y + $table->height);
945 $this->_xMin = min($this->_xMin, $table->x);
946 $this->_yMin = min($this->_yMin, $table->y);
950 * Defines relation objects
952 * @param string master_table The master table name
953 * @param string master_field The relation field in the master table
954 * @param string foreign_table The foreign table name
955 * @param string foreign_field The relation field in the foreign table
956 * @param boolean show_info Whether to display table position or not
957 * @access private
958 * @see _setMinMax
960 private function _addRelation($masterTable, $masterField, $foreignTable, $foreignField, $showInfo)
962 if (!isset($this->tables[$masterTable])) {
963 $this->tables[$masterTable] = new Table_Stats($masterTable, $this->_ff, $this->pageNumber, $this->_tablewidth, false, $showInfo);
964 $this->_setMinMax($this->tables[$masterTable]);
966 if (!isset($this->tables[$foreignTable])) {
967 $this->tables[$foreignTable] = new Table_Stats($foreignTable, $this->_ff, $this->pageNumber, $this->_tablewidth, false, $showInfo);
968 $this->_setMinMax($this->tables[$foreignTable]);
970 $this->relations[] = new Relation_Stats($this->tables[$masterTable], $masterField, $this->tables[$foreignTable], $foreignField);
974 * Draws the grid
976 * @global object the current PMA_PDF instance
977 * @access private
978 * @see PMA_PDF
980 private function _strokeGrid()
982 global $pdf;
984 $gridSize = 10;
985 $labelHeight = 4;
986 $labelWidth = 5;
987 if ($this->withDoc) {
988 $topSpace = 6;
989 $bottomSpace = 15;
990 } else {
991 $topSpace = 0;
992 $bottomSpace = 0;
995 $pdf->SetMargins(0, 0);
996 $pdf->SetDrawColor(200, 200, 200);
997 // Draws horizontal lines
998 for ($l = 0; $l <= intval(($pdf->getH() - $topSpace - $bottomSpace) / $gridSize); $l++) {
999 $pdf->line(0, $l * $gridSize + $topSpace, $pdf->getW(), $l * $gridSize + $topSpace);
1000 // Avoid duplicates
1001 if ($l > 0 && $l <= intval(($pdf->getH() - $topSpace - $bottomSpace - $labelHeight) / $gridSize)) {
1002 $pdf->SetXY(0, $l * $gridSize + $topSpace);
1003 $label = (string) sprintf('%.0f', ($l * $gridSize + $topSpace - $this->topMargin) * $this->scale + $this->_yMin);
1004 $pdf->Cell($labelWidth, $labelHeight, ' ' . $label);
1005 } // end if
1006 } // end for
1007 // Draws vertical lines
1008 for ($j = 0; $j <= intval($pdf->getW() / $gridSize); $j++) {
1009 $pdf->line($j * $gridSize, $topSpace, $j * $gridSize, $pdf->getH() - $bottomSpace);
1010 $pdf->SetXY($j * $gridSize, $topSpace);
1011 $label = (string) sprintf('%.0f', ($j * $gridSize - $this->leftMargin) * $this->scale + $this->_xMin);
1012 $pdf->Cell($labelWidth, $labelHeight, $label);
1017 * Draws relation arrows
1019 * @param boolean changeColor Whether to use one color per relation or not
1020 * @access private
1021 * @see Relation_Stats::relationdraw()
1023 private function _drawRelations($changeColor)
1025 $i = 0;
1026 foreach ($this->relations as $relation) {
1027 $relation->relationDraw($changeColor, $i);
1028 $i++;
1033 * Draws tables
1035 * @param boolean changeColor Whether to display table position or not
1036 * @access private
1037 * @see Table_Stats::tableDraw()
1039 private function _drawTables($changeColor = 0)
1041 foreach ($this->tables as $table) {
1042 $table->tableDraw($this->_ff, $this->withDoc, $changeColor);
1047 * Ouputs the PDF document to a file
1048 * or sends the output to browser
1050 * @global object The current PDF document
1051 * @global string The current database name
1052 * @global integer The current page number (from the
1053 * $cfg['Servers'][$i]['table_coords'] table)
1054 * @access private
1055 * @see PMA_PDF
1057 private function _showOutput($pageNumber)
1059 global $pdf, $db, $cfgRelation;
1061 $pdf->SetFontSize(14);
1062 $pdf->SetLineWidth(0.2);
1063 $pdf->SetDisplayMode('fullpage');
1064 // Get the name of this pdfpage to use as filename (Mike Beck)
1065 $_name_sql = 'SELECT page_descr FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
1066 . ' WHERE page_nr = ' . $pageNumber;
1067 $_name_rs = PMA_query_as_controluser($_name_sql);
1068 if ($_name_rs) {
1069 $_name_row = PMA_DBI_fetch_row($_name_rs);
1070 $filename = $_name_row[0] . '.pdf';
1072 if (empty($filename)) {
1073 $filename = $pageNumber . '.pdf';
1075 // instead of $pdf->Output():
1076 $pdfData = $pdf->getPDFData();
1077 header('Content-Type: application/pdf');
1078 header('Content-Length: '.strlen($pdfData).'');
1079 header('Content-disposition: attachment; filename="'.$filename.'"');
1080 echo $pdfData;
1083 public function dataDictionaryDoc($alltables)
1085 global $db, $pdf, $orientation, $paper;
1086 // TOC
1087 $pdf->addpage($GLOBALS['orientation']);
1088 $pdf->Cell(0, 9, __('Table of contents'), 1, 0, 'C');
1089 $pdf->Ln(15);
1090 $i = 1;
1091 foreach ($alltables as $table) {
1092 $pdf->PMA_links['doc'][$table]['-'] = $pdf->AddLink();
1093 $pdf->SetX(10);
1094 // $pdf->Ln(1);
1095 $pdf->Cell(0, 6, __('Page number:') . ' {' . sprintf("%02d", $i + 1) . '}', 0, 0, 'R', 0, $pdf->PMA_links['doc'][$table]['-']);
1096 $pdf->SetX(10);
1097 $pdf->Cell(0, 6, $i . ' ' . $table, 0, 1, 'L', 0, $pdf->PMA_links['doc'][$table]['-']);
1098 // $pdf->Ln(1);
1099 $result = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';');
1100 while ($row = PMA_DBI_fetch_assoc($result)) {
1101 $pdf->SetX(20);
1102 $field_name = $row['Field'];
1103 $pdf->PMA_links['doc'][$table][$field_name] = $pdf->AddLink();
1104 // $pdf->Cell(0, 6, $field_name,0,1,'L',0, $pdf->PMA_links['doc'][$table][$field_name]);
1106 $lasttable = $table;
1107 $i++;
1109 $pdf->PMA_links['RT']['-'] = $pdf->AddLink();
1110 $pdf->SetX(10);
1111 $pdf->Cell(0, 6, __('Page number:') . ' {' . sprintf("%02d", $i + 1) . '}', 0, 0, 'R', 0, $pdf->PMA_links['RT']['-']);
1112 $pdf->SetX(10);
1113 $pdf->Cell(0, 6, $i . ' ' . __('Relational schema'), 0, 1, 'L', 0, $pdf->PMA_links['RT']['-']);
1114 $z = 0;
1115 foreach ($alltables as $table) {
1116 $z++;
1117 $pdf->SetAutoPageBreak(true);
1118 $pdf->addpage($GLOBALS['orientation']);
1119 $pdf->Bookmark($table);
1120 $pdf->SetAlias('{' . sprintf("%02d", $z) . '}', $pdf->PageNo()) ;
1121 $pdf->PMA_links['RT'][$table]['-'] = $pdf->AddLink();
1122 $pdf->SetLink($pdf->PMA_links['doc'][$table]['-'], -1);
1123 $pdf->SetFont('', 'B', 18);
1124 $pdf->Cell(0, 8, $z . ' ' . $table, 1, 1, 'C', 0, $pdf->PMA_links['RT'][$table]['-']);
1125 $pdf->SetFont('', '', 8);
1126 $pdf->ln();
1128 $cfgRelation = PMA_getRelationsParam();
1129 $comments = PMA_getComments($db, $table);
1130 if ($cfgRelation['mimework']) {
1131 $mime_map = PMA_getMIME($db, $table, true);
1135 * Gets table informations
1137 $showtable = PMA_Table::sGetStatusInfo($db, $table);
1138 $num_rows = (isset($showtable['Rows']) ? $showtable['Rows'] : 0);
1139 $show_comment = (isset($showtable['Comment']) ? $showtable['Comment'] : '');
1140 $create_time = (isset($showtable['Create_time']) ? PMA_localisedDate(strtotime($showtable['Create_time'])) : '');
1141 $update_time = (isset($showtable['Update_time']) ? PMA_localisedDate(strtotime($showtable['Update_time'])) : '');
1142 $check_time = (isset($showtable['Check_time']) ? PMA_localisedDate(strtotime($showtable['Check_time'])) : '');
1145 * Gets table keys and retains them
1147 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
1148 $primary = '';
1149 $indexes = array();
1150 $lastIndex = '';
1151 $indexes_info = array();
1152 $indexes_data = array();
1153 $pk_array = array(); // will be use to emphasis prim. keys in the table
1154 // view
1155 while ($row = PMA_DBI_fetch_assoc($result)) {
1156 // Backups the list of primary keys
1157 if ($row['Key_name'] == 'PRIMARY') {
1158 $primary .= $row['Column_name'] . ', ';
1159 $pk_array[$row['Column_name']] = 1;
1161 // Retains keys informations
1162 if ($row['Key_name'] != $lastIndex) {
1163 $indexes[] = $row['Key_name'];
1164 $lastIndex = $row['Key_name'];
1166 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
1167 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
1168 if (isset($row['Cardinality'])) {
1169 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
1171 // I don't know what does following column mean....
1172 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
1173 $indexes_info[$row['Key_name']]['Comment'] = $row['Comment'];
1175 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
1176 if (isset($row['Sub_part'])) {
1177 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
1179 } // end while
1180 if ($result) {
1181 PMA_DBI_free_result($result);
1185 * Gets fields properties
1187 $result = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE);
1188 $fields_cnt = PMA_DBI_num_rows($result);
1189 // Check if we can use Relations (Mike Beck)
1190 if (!empty($cfgRelation['relation'])) {
1191 // Find which tables are related with the current one and write it in
1192 // an array
1193 $res_rel = PMA_getForeigners($db, $table);
1195 if (count($res_rel) > 0) {
1196 $have_rel = true;
1197 } else {
1198 $have_rel = false;
1200 } else {
1201 $have_rel = false;
1202 } // end if
1205 * Displays the comments of the table if MySQL >= 3.23
1208 $break = false;
1209 if (!empty($show_comment)) {
1210 $pdf->Cell(0, 3, __('Table comments') . ' : ' . $show_comment, 0, 1);
1211 $break = true;
1214 if (!empty($create_time)) {
1215 $pdf->Cell(0, 3, __('Creation') . ': ' . $create_time, 0, 1);
1216 $break = true;
1219 if (!empty($update_time)) {
1220 $pdf->Cell(0, 3, __('Last update') . ': ' . $update_time, 0, 1);
1221 $break = true;
1224 if (!empty($check_time)) {
1225 $pdf->Cell(0, 3, __('Last check') . ': ' . $check_time, 0, 1);
1226 $break = true;
1229 if ($break == true) {
1230 $pdf->Cell(0, 3, '', 0, 1);
1231 $pdf->Ln();
1234 $pdf->SetFont('', 'B');
1235 if (isset($orientation) && $orientation == 'L') {
1236 $pdf->Cell(25, 8, ucfirst(__('Column')), 1, 0, 'C');
1237 $pdf->Cell(20, 8, ucfirst(__('Type')), 1, 0, 'C');
1238 $pdf->Cell(20, 8, ucfirst(__('Attributes')), 1, 0, 'C');
1239 $pdf->Cell(10, 8, ucfirst(__('Null')), 1, 0, 'C');
1240 $pdf->Cell(20, 8, ucfirst(__('Default')), 1, 0, 'C');
1241 $pdf->Cell(25, 8, ucfirst(__('Extra')), 1, 0, 'C');
1242 $pdf->Cell(45, 8, ucfirst(__('Links to')), 1, 0, 'C');
1244 if ($paper == 'A4') {
1245 $comments_width = 67;
1246 } else {
1247 // this is really intended for 'letter'
1249 * @todo find optimal width for all formats
1251 $comments_width = 50;
1253 $pdf->Cell($comments_width, 8, ucfirst(__('Comments')), 1, 0, 'C');
1254 $pdf->Cell(45, 8, 'MIME', 1, 1, 'C');
1255 $pdf->SetWidths(array(25, 20, 20, 10, 20, 25, 45, $comments_width, 45));
1256 } else {
1257 $pdf->Cell(20, 8, ucfirst(__('Column')), 1, 0, 'C');
1258 $pdf->Cell(20, 8, ucfirst(__('Type')), 1, 0, 'C');
1259 $pdf->Cell(20, 8, ucfirst(__('Attributes')), 1, 0, 'C');
1260 $pdf->Cell(10, 8, ucfirst(__('Null')), 1, 0, 'C');
1261 $pdf->Cell(15, 8, ucfirst(__('Default')), 1, 0, 'C');
1262 $pdf->Cell(15, 8, ucfirst(__('Extra')), 1, 0, 'C');
1263 $pdf->Cell(30, 8, ucfirst(__('Links to')), 1, 0, 'C');
1264 $pdf->Cell(30, 8, ucfirst(__('Comments')), 1, 0, 'C');
1265 $pdf->Cell(30, 8, 'MIME', 1, 1, 'C');
1266 $pdf->SetWidths(array(20, 20, 20, 10, 15, 15, 30, 30, 30));
1268 $pdf->SetFont('', '');
1270 while ($row = PMA_DBI_fetch_assoc($result)) {
1271 $type = $row['Type'];
1272 // reformat mysql query output
1273 // set or enum types: slashes single quotes inside options
1274 if (preg_match('@^(set|enum)\((.+)\)$@i', $type, $tmp)) {
1275 $tmp[2] = substr(preg_replace("@([^,])''@", "\\1\\'", ',' . $tmp[2]), 1);
1276 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
1277 $type_nowrap = '';
1279 $binary = 0;
1280 $unsigned = 0;
1281 $zerofill = 0;
1282 } else {
1283 $type_nowrap = ' nowrap="nowrap"';
1284 $type = preg_replace('@BINARY@i', '', $type);
1285 $type = preg_replace('@ZEROFILL@i', '', $type);
1286 $type = preg_replace('@UNSIGNED@i', '', $type);
1287 if (empty($type)) {
1288 $type = '&nbsp;';
1291 $binary = stristr($row['Type'], 'BINARY');
1292 $unsigned = stristr($row['Type'], 'UNSIGNED');
1293 $zerofill = stristr($row['Type'], 'ZEROFILL');
1295 $attribute = ' ';
1296 if ($binary) {
1297 $attribute = 'BINARY';
1299 if ($unsigned) {
1300 $attribute = 'UNSIGNED';
1302 if ($zerofill) {
1303 $attribute = 'UNSIGNED ZEROFILL';
1305 if (!isset($row['Default'])) {
1306 if ($row['Null'] != '' && $row['Null'] != 'NO') {
1307 $row['Default'] = 'NULL';
1310 $field_name = $row['Field'];
1311 // $pdf->Ln();
1312 $pdf->PMA_links['RT'][$table][$field_name] = $pdf->AddLink();
1313 $pdf->Bookmark($field_name, 1, -1);
1314 $pdf->SetLink($pdf->PMA_links['doc'][$table][$field_name], -1);
1315 $pdf_row = array($field_name,
1316 $type,
1317 $attribute,
1318 ($row['Null'] == '' || $row['Null'] == 'NO') ? __('No') : __('Yes'),
1319 ((isset($row['Default'])) ? $row['Default'] : ''),
1320 $row['Extra'],
1321 ((isset($res_rel[$field_name])) ? $res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field'] : ''),
1322 ((isset($comments[$field_name])) ? $comments[$field_name] : ''),
1323 ((isset($mime_map) && isset($mime_map[$field_name])) ? str_replace('_', '/', $mime_map[$field_name]['mimetype']) : '')
1325 $links[0] = $pdf->PMA_links['RT'][$table][$field_name];
1326 if (isset($res_rel[$field_name]['foreign_table']) AND
1327 isset($res_rel[$field_name]['foreign_field']) AND
1328 isset($pdf->PMA_links['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']])
1331 $links[6] = $pdf->PMA_links['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']];
1332 } else {
1333 unset($links[6]);
1335 $pdf->Row($pdf_row, $links);
1336 } // end while
1337 $pdf->SetFont('', '', 14);
1338 PMA_DBI_free_result($result);
1339 } //end each