Translation update done using Pootle.
[phpmyadmin/ammaryasirr.git] / libraries / schema / Pdf_Relation_Schema.class.php
blob6fd19769413266f2eca30da938ba6edc04a458c3
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 * @access public
23 * @see TCPDF
25 class PMA_PDF extends TCPDF
27 /**
28 * Defines properties
30 var $_xMin;
31 var $_yMin;
32 var $leftMargin = 10;
33 var $topMargin = 10;
34 var $scale;
35 var $PMA_links;
36 var $Outlines = array();
37 var $def_outlines;
38 var $Alias = array();
39 var $widths;
40 private $_ff = PMA_PDF_FONT;
42 public function getH()
44 return $this->h;
47 public function getW()
49 return $this->w;
52 public function setCMargin($c_margin)
54 $this->cMargin = $c_margin;
57 function SetAlias($name, $value)
59 $this->Alias[$name] = $value ;
62 function _putpages()
64 if (count($this->Alias) > 0) {
65 $nb = $this->page;
66 foreach ($this->Alias as $alias => $value) {
67 for ($n = 1;$n <= $nb;$n++)
68 $this->pages[$n]=str_replace($alias, $value, $this->pages[$n]);
71 parent::_putpages();
74 // added because tcpdf for PHP 5 has a protected $buffer
75 public function getBuffer()
77 return $this->buffer;
80 public function getState()
82 return $this->state;
85 /**
86 * Sets the scaling factor, defines minimum coordinates and margins
88 * @param float scale The scaling factor
89 * @param float _xMin The minimum X coordinate
90 * @param float _yMin The minimum Y coordinate
91 * @param float leftMargin The left margin
92 * @param float topMargin The top margin
93 * @access public
95 function PMA_PDF_setScale($scale = 1, $xMin = 0, $yMin = 0, $leftMargin = -1, $topMargin = -1)
97 $this->scale = $scale;
98 $this->_xMin = $xMin;
99 $this->_yMin = $yMin;
100 if ($this->leftMargin != -1) {
101 $this->leftMargin = $leftMargin;
103 if ($this->topMargin != -1) {
104 $this->topMargin = $topMargin;
109 * Outputs a scaled cell
111 * @param float w The cell width
112 * @param float h The cell height
113 * @param string txt The text to output
114 * @param mixed border Whether to add borders or not
115 * @param integer ln Where to put the cursor once the output is done
116 * @param string align Align mode
117 * @param integer fill Whether to fill the cell with a color or not
118 * @access public
119 * @see TCPDF::Cell()
121 function PMA_PDF_cellScale($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $fill = 0, $link = '')
123 $h = $h / $this->scale;
124 $w = $w / $this->scale;
125 $this->Cell($w, $h, $txt, $border, $ln, $align, $fill, $link);
129 * Draws a scaled line
131 * @param float x1 The horizontal position of the starting point
132 * @param float y1 The vertical position of the starting point
133 * @param float x2 The horizontal position of the ending point
134 * @param float y2 The vertical position of the ending point
135 * @access public
136 * @see TCPDF::Line()
138 function PMA_PDF_lineScale($x1, $y1, $x2, $y2)
140 $x1 = ($x1 - $this->_xMin) / $this->scale + $this->leftMargin;
141 $y1 = ($y1 - $this->_yMin) / $this->scale + $this->topMargin;
142 $x2 = ($x2 - $this->_xMin) / $this->scale + $this->leftMargin;
143 $y2 = ($y2 - $this->_yMin) / $this->scale + $this->topMargin;
144 $this->Line($x1, $y1, $x2, $y2);
148 * Sets x and y scaled positions
150 * @param float x The x position
151 * @param float y The y position
152 * @access public
153 * @see TCPDF::SetXY()
155 function PMA_PDF_setXyScale($x, $y)
157 $x = ($x - $this->_xMin) / $this->scale + $this->leftMargin;
158 $y = ($y - $this->_yMin) / $this->scale + $this->topMargin;
159 $this->SetXY($x, $y);
163 * Sets the X scaled positions
165 * @param float x The x position
166 * @access public
167 * @see TCPDF::SetX()
169 function PMA_PDF_setXScale($x)
171 $x = ($x - $this->_xMin) / $this->scale + $this->leftMargin;
172 $this->SetX($x);
176 * Sets the scaled font size
178 * @param float size The font size (in points)
179 * @access public
180 * @see TCPDF::SetFontSize()
182 function PMA_PDF_setFontSizeScale($size)
184 // Set font size in points
185 $size = $size / $this->scale;
186 $this->SetFontSize($size);
190 * Sets the scaled line width
192 * @param float width The line width
193 * @access public
194 * @see TCPDF::SetLineWidth()
196 function PMA_PDF_setLineWidthScale($width)
198 $width = $width / $this->scale;
199 $this->SetLineWidth($width);
203 * Displays an error message
205 * @param string error_message the error mesage
206 * @access public
207 * @see PMA_Export_Relation_Schema::dieSchema
209 function Error($error_message = '')
211 PMA_Export_Relation_Schema::dieSchema($error_message);
214 function Header()
216 // We only show this if we find something in the new pdf_pages table
218 // This function must be named "Header" to work with the TCPDF library
219 global $cfgRelation, $db, $pdf_page_number, $with_doc;
220 if ($with_doc) {
221 $test_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
222 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
223 . ' AND page_nr = \'' . $pdf_page_number . '\'';
224 $test_rs = PMA_query_as_controluser($test_query);
225 $pages = @PMA_DBI_fetch_assoc($test_rs);
226 $this->SetFont($this->_ff, 'B', 14);
227 $this->Cell(0, 6, ucfirst($pages['page_descr']), 'B', 1, 'C');
228 $this->SetFont($this->_ff, '');
229 $this->Ln();
233 function Footer()
235 // This function must be named "Footer" to work with the TCPDF library
236 global $with_doc;
237 if ($with_doc) {
238 $this->SetY(-15);
239 $this->SetFont($this->_ff, '', 14);
240 $this->Cell(0, 6, __('Page number:') . ' ' . $this->getAliasNumPage() . '/' . $this->getAliasNbPages(), 'T', 0, 'C');
241 $this->Cell(0, 6, PMA_localisedDate(), 0, 1, 'R');
242 $this->SetY(20);
246 function Bookmark($txt, $level = 0, $y = 0, $page = '')
248 // Add a bookmark
249 $this->Outlines[0][] = $level;
250 $this->Outlines[1][] = $txt;
251 $this->Outlines[2][] = $this->page;
252 if ($y == -1) {
253 $y = $this->GetY();
255 $this->Outlines[3][] = round($this->hPt - $y * $this->k, 2);
258 function _putbookmarks()
260 if (count($this->Outlines) > 0) {
261 // Save object number
262 $memo_n = $this->n;
263 // Take the number of sub elements for an outline
264 $nb_outlines = sizeof($this->Outlines[0]);
265 $first_level = array();
266 $parent = array();
267 $parent[0] = 1;
268 for ($i = 0; $i < $nb_outlines; $i++) {
269 $level = $this->Outlines[0][$i];
270 $kids = 0;
271 $last = -1;
272 $prev = -1;
273 $next = -1;
274 if ($i > 0) {
275 $cursor = $i-1;
276 // Take the previous outline in the same level
277 while ($this->Outlines[0][$cursor] > $level && $cursor > 0)
278 $cursor--;
279 if ($this->Outlines[0][$cursor] == $level) {
280 $prev = $cursor;
283 if ($i < $nb_outlines-1) {
284 $cursor = $i + 1;
285 while (isset($this->Outlines[0][$cursor]) && $this->Outlines[0][$cursor] > $level) {
286 // Take the immediate kid in level + 1
287 if ($this->Outlines[0][$cursor] == $level + 1) {
288 $kids++;
289 $last = $cursor;
291 $cursor++;
293 $cursor = $i + 1;
294 // Take the next outline in the same level
295 while ($this->Outlines[0][$cursor] > $level && ($cursor + 1 < sizeof($this->Outlines[0])))
296 $cursor++;
297 if ($this->Outlines[0][$cursor] == $level) {
298 $next = $cursor;
301 $this->_newobj();
302 $parent[$level + 1] = $this->n;
303 if ($level == 0) {
304 $first_level[] = $this->n;
306 $this->_out('<<');
307 $this->_out('/Title (' . $this->Outlines[1][$i] . ')');
308 $this->_out('/Parent ' . $parent[$level] . ' 0 R');
309 if ($prev != -1) {
310 $this->_out('/Prev ' . ($memo_n + $prev + 1) . ' 0 R');
312 if ($next != -1) {
313 $this->_out('/Next ' . ($this->n + $next - $i) . ' 0 R');
315 $this->_out('/Dest [' . (1 + (2 * $this->Outlines[2][$i])) . ' 0 R /XYZ null ' . $this->Outlines[3][$i] . ' null]');
316 if ($kids > 0) {
317 $this->_out('/First ' . ($this->n + 1) . ' 0 R');
318 $this->_out('/Last ' . ($this->n + $last - $i) . ' 0 R');
319 $this->_out('/Count -' . $kids);
321 $this->_out('>>');
322 $this->_out('endobj');
324 // First page of outlines
325 $this->_newobj();
326 $this->def_outlines = $this->n;
327 $this->_out('<<');
328 $this->_out('/Type');
329 $this->_out('/Outlines');
330 $this->_out('/First ' . $first_level[0] . ' 0 R');
331 $this->_out('/Last ' . $first_level[sizeof($first_level)-1] . ' 0 R');
332 $this->_out('/Count ' . sizeof($first_level));
333 $this->_out('>>');
334 $this->_out('endobj');
338 function _putresources()
340 parent::_putresources();
341 $this->_putbookmarks();
344 function SetWidths($w)
346 // column widths
347 $this->widths = $w;
350 function Row($data, $links)
352 // line height
353 $nb = 0;
354 $data_cnt = count($data);
355 for ($i = 0;$i < $data_cnt;$i++)
356 $nb = max($nb, $this->NbLines($this->widths[$i], $data[$i]));
357 $il = $this->FontSize;
358 $h = ($il + 1) * $nb;
359 // page break if necessary
360 $this->CheckPageBreak($h);
361 // draw the cells
362 $data_cnt = count($data);
363 for ($i = 0;$i < $data_cnt;$i++) {
364 $w = $this->widths[$i];
365 // save current position
366 $x = $this->GetX();
367 $y = $this->GetY();
368 // draw the border
369 $this->Rect($x, $y, $w, $h);
370 if (isset($links[$i])) {
371 $this->Link($x, $y, $w, $h, $links[$i]);
373 // print text
374 $this->MultiCell($w, $il + 1, $data[$i], 0, 'L');
375 // go to right side
376 $this->SetXY($x + $w, $y);
378 // go to line
379 $this->Ln($h);
382 function NbLines($w, $txt)
384 // compute number of lines used by a multicell of width w
385 $cw = &$this->CurrentFont['cw'];
386 if ($w == 0) {
387 $w = $this->w - $this->rMargin - $this->x;
389 $wmax = ($w-2 * $this->cMargin) * 1000 / $this->FontSize;
390 $s = str_replace("\r", '', $txt);
391 $nb = strlen($s);
392 if ($nb > 0 and $s[$nb-1] == "\n") {
393 $nb--;
395 $sep = -1;
396 $i = 0;
397 $j = 0;
398 $l = 0;
399 $nl = 1;
400 while ($i < $nb) {
401 $c = $s[$i];
402 if ($c == "\n") {
403 $i++;
404 $sep = -1;
405 $j = $i;
406 $l = 0;
407 $nl++;
408 continue;
410 if ($c == ' ') {
411 $sep = $i;
413 $l += isset($cw[ord($c)])?$cw[ord($c)]:0 ;
414 if ($l > $wmax) {
415 if ($sep == -1) {
416 if ($i == $j) {
417 $i++;
419 } else {
420 $i = $sep + 1;
422 $sep = -1;
423 $j = $i;
424 $l = 0;
425 $nl++;
426 } else {
427 $i++;
430 return $nl;
435 * Table preferences/statistics
437 * This class preserves the table co-ordinates,fields
438 * and helps in drawing/generating the Tables in PDF document.
440 * @name Table_Stats
441 * @see PMA_PDF
443 class Table_Stats
446 * Defines properties
448 private $_tableName;
449 private $_showInfo = false;
451 public $nb_fiels;
452 public $width = 0;
453 public $height;
454 public $fields = array();
455 public $heightCell = 6;
456 public $x, $y;
457 public $primary = array();
458 private $_ff = PMA_PDF_FONT;
461 * The "Table_Stats" constructor
463 * @param string table_name The table name
464 * @param integer fontSize The font size
465 * @param integer pageNumber The current page number (from the
466 * $cfg['Servers'][$i]['table_coords'] table)
467 * @param integer sameWideWidth The max. with among tables
468 * @param boolean showKeys Whether to display keys or not
469 * @param boolean showInfo Whether to display table position or not
470 * @global object The current PDF document
471 * @global array The relations settings
472 * @global string The current db name
473 * @see PMA_PDF, Table_Stats::Table_Stats_setWidth,
474 Table_Stats::Table_Stats_setHeight
476 function __construct($tableName, $fontSize, $pageNumber, &$sameWideWidth, $showKeys = false, $showInfo = false)
478 global $pdf, $cfgRelation, $db;
480 $this->_tableName = $tableName;
481 $sql = 'DESCRIBE ' . PMA_backquote($tableName);
482 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE);
483 if (!$result || !PMA_DBI_num_rows($result)) {
484 $pdf->Error(sprintf(__('The %s table doesn\'t exist!'), $tableName));
486 // load fields
487 //check to see if it will load all fields or only the foreign keys
488 if ($showKeys) {
489 $indexes = PMA_Index::getFromTable($this->_tableName, $db);
490 $all_columns = array();
491 foreach ($indexes as $index) {
492 $all_columns = array_merge($all_columns, array_flip(array_keys($index->getColumns())));
494 $this->fields = array_keys($all_columns);
495 } else {
496 while ($row = PMA_DBI_fetch_row($result)) {
497 $this->fields[] = $row[0];
501 $this->_showInfo = $showInfo;
502 $this->_setHeight();
504 * setWidth must me after setHeight, because title
505 * can include table height which changes table width
507 $this->_setWidth($fontSize);
508 if ($sameWideWidth < $this->width) {
509 $sameWideWidth = $this->width;
511 $sql = 'SELECT x, y FROM '
512 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
513 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
514 . ' AND table_name = \'' . PMA_sqlAddSlashes($tableName) . '\''
515 . ' AND pdf_page_number = ' . $pageNumber;
516 $result = PMA_query_as_controluser($sql, false, PMA_DBI_QUERY_STORE);
517 if (!$result || !PMA_DBI_num_rows($result)) {
518 $pdf->Error(sprintf(__('Please configure the coordinates for table %s'), $tableName));
520 list($this->x, $this->y) = PMA_DBI_fetch_row($result);
521 $this->x = (double) $this->x;
522 $this->y = (double) $this->y;
524 * displayfield
526 $this->displayfield = PMA_getDisplayField($db, $tableName);
528 * index
530 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tableName) . ';', null, PMA_DBI_QUERY_STORE);
531 if (PMA_DBI_num_rows($result) > 0) {
532 while ($row = PMA_DBI_fetch_assoc($result)) {
533 if ($row['Key_name'] == 'PRIMARY') {
534 $this->primary[] = $row['Column_name'];
541 * Returns title of the current table,
542 * title can have the dimensions of the table
544 * @access private
546 private function _getTitle()
548 return ($this->_showInfo ? sprintf('%.0f', $this->width) . 'x' . sprintf('%.0f', $this->height) : '') . ' ' . $this->_tableName;
552 * Sets the width of the table
554 * @param integer fontSize The font size
555 * @global object The current PDF document
556 * @access private
557 * @see PMA_PDF
559 function _setWidth($fontSize)
561 global $pdf;
563 foreach ($this->fields as $field) {
564 $this->width = max($this->width, $pdf->GetStringWidth($field));
566 $this->width += $pdf->GetStringWidth(' ');
567 $pdf->SetFont($this->_ff, 'B', $fontSize);
569 * it is unknown what value must be added, because
570 * table title is affected by the tabe width value
572 while ($this->width < $pdf->GetStringWidth($this->_getTitle())) {
573 $this->width += 5;
575 $pdf->SetFont($this->_ff, '', $fontSize);
579 * Sets the height of the table
581 * @access private
583 private function _setHeight()
585 $this->height = (count($this->fields) + 1) * $this->heightCell;
589 * Do draw the table
591 * @param integer fontSize The font size
592 * @param boolean setColor Whether to display color
593 * @global object The current PDF document
594 * @access public
595 * @see PMA_PDF
597 public function tableDraw($fontSize, $withDoc, $setColor = 0)
599 global $pdf, $withDoc;
601 $pdf->PMA_PDF_setXyScale($this->x, $this->y);
602 $pdf->SetFont($this->_ff, 'B', $fontSize);
603 if ($setColor) {
604 $pdf->SetTextColor(200);
605 $pdf->SetFillColor(0, 0, 128);
607 if ($withDoc) {
608 $pdf->SetLink($pdf->PMA_links['RT'][$this->_tableName]['-'], -1);
609 } else {
610 $pdf->PMA_links['doc'][$this->_tableName]['-'] = '';
613 $pdf->PMA_PDF_cellScale($this->width, $this->heightCell, $this->_getTitle(), 1, 1, 'C', $setColor, $pdf->PMA_links['doc'][$this->_tableName]['-']);
614 $pdf->PMA_PDF_setXScale($this->x);
615 $pdf->SetFont($this->_ff, '', $fontSize);
616 $pdf->SetTextColor(0);
617 $pdf->SetFillColor(255);
619 foreach ($this->fields as $field) {
620 if ($setColor) {
621 if (in_array($field, $this->primary)) {
622 $pdf->SetFillColor(215, 121, 123);
624 if ($field == $this->displayfield) {
625 $pdf->SetFillColor(142, 159, 224);
628 if ($withDoc) {
629 $pdf->SetLink($pdf->PMA_links['RT'][$this->_tableName][$field], -1);
630 } else {
631 $pdf->PMA_links['doc'][$this->_tableName][$field] = '';
634 $pdf->PMA_PDF_cellScale($this->width, $this->heightCell, ' ' . $field, 1, 1, 'L', $setColor, $pdf->PMA_links['doc'][$this->_tableName][$field]);
635 $pdf->PMA_PDF_setXScale($this->x);
636 $pdf->SetFillColor(255);
638 /*if ($pdf->PageNo() > 1) {
639 $pdf->PMA_PDF_die(__('The scale factor is too small to fit the schema on one page'));
640 } */
645 * Relation preferences/statistics
647 * This class fetches the table master and foreign fields positions
648 * and helps in generating the Table references and then connects
649 * master table's master field to foreign table's foreign key
650 * in PDF document.
652 * @name Relation_Stats
653 * @see PMA_PDF::SetDrawColor,PMA_PDF::PMA_PDF_setLineWidthScale,PMA_PDF::PMA_PDF_lineScale
655 class Relation_Stats
658 * Defines properties
660 public $xSrc, $ySrc;
661 public $srcDir;
662 public $destDir;
663 public $xDest, $yDest;
664 public $wTick = 5;
667 * The "Relation_Stats" constructor
669 * @param string master_table The master table name
670 * @param string master_field The relation field in the master table
671 * @param string foreign_table The foreign table name
672 * @param string foreigh_field The relation field in the foreign table
673 * @see Relation_Stats::_getXy
675 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
677 $src_pos = $this->_getXy($master_table, $master_field);
678 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
680 * [0] is x-left
681 * [1] is x-right
682 * [2] is y
684 $src_left = $src_pos[0] - $this->wTick;
685 $src_right = $src_pos[1] + $this->wTick;
686 $dest_left = $dest_pos[0] - $this->wTick;
687 $dest_right = $dest_pos[1] + $this->wTick;
689 $d1 = abs($src_left - $dest_left);
690 $d2 = abs($src_right - $dest_left);
691 $d3 = abs($src_left - $dest_right);
692 $d4 = abs($src_right - $dest_right);
693 $d = min($d1, $d2, $d3, $d4);
695 if ($d == $d1) {
696 $this->xSrc = $src_pos[0];
697 $this->srcDir = -1;
698 $this->xDest = $dest_pos[0];
699 $this->destDir = -1;
700 } elseif ($d == $d2) {
701 $this->xSrc = $src_pos[1];
702 $this->srcDir = 1;
703 $this->xDest = $dest_pos[0];
704 $this->destDir = -1;
705 } elseif ($d == $d3) {
706 $this->xSrc = $src_pos[0];
707 $this->srcDir = -1;
708 $this->xDest = $dest_pos[1];
709 $this->destDir = 1;
710 } else {
711 $this->xSrc = $src_pos[1];
712 $this->srcDir = 1;
713 $this->xDest = $dest_pos[1];
714 $this->destDir = 1;
716 $this->ySrc = $src_pos[2];
717 $this->yDest = $dest_pos[2];
721 * Gets arrows coordinates
723 * @param string table The current table name
724 * @param string column The relation column name
725 * @return array Arrows coordinates
726 * @access private
728 private function _getXy($table, $column)
730 $pos = array_search($column, $table->fields);
731 // x_left, x_right, y
732 return array($table->x, $table->x + + $table->width, $table->y + ($pos + 1.5) * $table->heightCell);
736 * draws relation links and arrows
737 * shows foreign key relations
739 * @param boolean changeColor Whether to use one color per relation or not
740 * @param integer i The id of the link to draw
741 * @global object The current PDF document
742 * @access public
743 * @see PMA_PDF
745 public function relationDraw($changeColor, $i)
747 global $pdf;
749 if ($changeColor) {
750 $d = $i % 6;
751 $j = ($i - $d) / 6;
752 $j = $j % 4;
753 $j++;
754 $case = array(
755 array(1, 0, 0),
756 array(0, 1, 0),
757 array(0, 0, 1),
758 array(1, 1, 0),
759 array(1, 0, 1),
760 array(0, 1, 1)
762 list ($a, $b, $c) = $case[$d];
763 $e = (1 - ($j - 1) / 6);
764 $pdf->SetDrawColor($a * 255 * $e, $b * 255 * $e, $c * 255 * $e);
765 } else {
766 $pdf->SetDrawColor(0);
768 $pdf->PMA_PDF_setLineWidthScale(0.2);
769 $pdf->PMA_PDF_lineScale($this->xSrc, $this->ySrc, $this->xSrc + $this->srcDir * $this->wTick, $this->ySrc);
770 $pdf->PMA_PDF_lineScale($this->xDest + $this->destDir * $this->wTick, $this->yDest, $this->xDest, $this->yDest);
771 $pdf->PMA_PDF_setLineWidthScale(0.1);
772 $pdf->PMA_PDF_lineScale($this->xSrc + $this->srcDir * $this->wTick, $this->ySrc, $this->xDest + $this->destDir * $this->wTick, $this->yDest);
774 * Draws arrows ->
776 $root2 = 2 * sqrt(2);
777 $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);
778 $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);
780 $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);
781 $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);
782 $pdf->SetDrawColor(0);
787 * Pdf Relation Schema Class
789 * Purpose of this class is to generate the PDF Document. PDF is widely
790 * used format for documenting text,fonts,images and 3d vector graphics.
792 * This class inherits Export_Relation_Schema class has common functionality added
793 * to this class
795 * @name Pdf_Relation_Schema
797 class PMA_Pdf_Relation_Schema extends PMA_Export_Relation_Schema
800 * Defines properties
802 private $_tables = array();
803 private $_relations = array();
804 private $_ff = PMA_PDF_FONT;
805 private $_xMax = 0;
806 private $_yMax = 0;
807 private $scale;
808 private $_xMin = 100000;
809 private $_yMin = 100000;
810 private $topMargin = 10;
811 private $bottomMargin = 10;
812 private $leftMargin = 10;
813 private $rightMargin = 10;
814 private $_tablewidth;
817 * The "PMA_Pdf_Relation_Schema" constructor
819 * @global object The current PDF Schema document
820 * @global string The current db name
821 * @global array The relations settings
822 * @access private
823 * @see PMA_PDF
825 function __construct()
827 global $pdf,$db,$cfgRelation;
829 $this->setPageNumber($_POST['pdf_page_number']);
830 $this->setShowGrid(isset($_POST['show_grid']));
831 $this->setShowColor(isset($_POST['show_color']));
832 $this->setShowKeys(isset($_POST['show_keys']));
833 $this->setTableDimension(isset($_POST['show_table_dimension']));
834 $this->setAllTableSameWidth(isset($_POST['all_table_same_wide']));
835 $this->setWithDataDictionary($_POST['with_doc']);
836 $this->setOrientation($_POST['orientation']);
837 $this->setPaper($_POST['paper']);
838 $this->setExportType($_POST['export_type']);
840 // Initializes a new document
841 $pdf = new PMA_PDF($this->orientation, 'mm', $this->paper);
842 $pdf->SetTitle(sprintf(__('Schema of the %s database - Page %s'), $GLOBALS['db'], $this->pageNumber));
843 $pdf->setCMargin(0);
844 $pdf->Open();
845 $pdf->SetAuthor('phpMyAdmin ' . PMA_VERSION);
846 $pdf->AliasNbPages();
847 $pdf->AddFont('DejaVuSans', '', 'dejavusans.php');
848 $pdf->AddFont('DejaVuSans', 'B', 'dejavusansb.php');
849 $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php');
850 $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserifb.php');
851 $pdf->SetFont($this->_ff, '', 14);
852 $pdf->setFooterFont(array($this->_ff, '', 14));
853 $pdf->SetAutoPageBreak('auto');
854 $alltables = $this->getAllTables($db,$this->pageNumber);
856 if ($this->withDoc) {
857 $pdf->SetAutoPageBreak('auto', 15);
858 $pdf->setCMargin(1);
859 $this->dataDictionaryDoc($alltables);
860 $pdf->SetAutoPageBreak('auto');
861 $pdf->setCMargin(0);
864 $pdf->Addpage();
866 if ($this->withDoc) {
867 $pdf->SetLink($pdf->PMA_links['RT']['-'], -1);
868 $pdf->Bookmark(__('Relational schema'));
869 $pdf->SetAlias('{00}', $pdf->PageNo()) ;
870 $this->topMargin = 28;
871 $this->bottomMargin = 28;
874 /* snip */
875 foreach ($alltables as $table) {
876 if (! isset($this->tables[$table])) {
877 $this->tables[$table] = new Table_Stats($table, $this->_ff, $this->pageNumber, $this->_tablewidth, $this->showKeys, $this->tableDimension);
879 if ($this->sameWide) {
880 $this->tables[$table]->width = $this->_tablewidth;
882 $this->_setMinMax($this->tables[$table]);
885 // Defines the scale factor
886 $this->scale = ceil(
887 max(
888 ($this->_xMax - $this->_xMin) / ($pdf->getW() - $this->rightMargin - $this->leftMargin),
889 ($this->_yMax - $this->_yMin) / ($pdf->getH() - $this->topMargin - $this->bottomMargin))
890 * 100) / 100;
892 $pdf->PMA_PDF_setScale($this->scale, $this->_xMin, $this->_yMin, $this->leftMargin, $this->topMargin);
893 // Builds and save the PDF document
894 $pdf->PMA_PDF_setLineWidthScale(0.1);
896 if ($this->showGrid) {
897 $pdf->SetFontSize(10);
898 $this->_strokeGrid();
900 $pdf->PMA_PDF_setFontSizeScale(14);
901 // previous logic was checking master tables and foreign tables
902 // but I think that looping on every table of the pdf page as a master
903 // and finding its foreigns is OK (then we can support innodb)
904 $seen_a_relation = false;
905 foreach ($alltables as $one_table) {
906 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
907 if ($exist_rel) {
908 $seen_a_relation = true;
909 foreach ($exist_rel as $master_field => $rel) {
910 // put the foreign table on the schema only if selected
911 // by the user
912 // (do not use array_search() because we would have to
913 // to do a === false and this is not PHP3 compatible)
914 if (in_array($rel['foreign_table'], $alltables)) {
915 $this->_addRelation($one_table, $master_field, $rel['foreign_table'], $rel['foreign_field'], $this->tableDimension);
917 } // end while
918 } // end if
919 } // end while
921 if ($seen_a_relation) {
922 $this->_drawRelations($this->showColor);
924 $this->_drawTables($this->showColor);
925 $this->_showOutput($this->pageNumber);
926 exit();
930 * Sets X and Y minimum and maximum for a table cell
932 * @param string table The table name of which sets XY co-ordinates
933 * @access private
935 private function _setMinMax($table)
937 $this->_xMax = max($this->_xMax, $table->x + $table->width);
938 $this->_yMax = max($this->_yMax, $table->y + $table->height);
939 $this->_xMin = min($this->_xMin, $table->x);
940 $this->_yMin = min($this->_yMin, $table->y);
944 * Defines relation objects
946 * @param string master_table The master table name
947 * @param string master_field The relation field in the master table
948 * @param string foreign_table The foreign table name
949 * @param string foreign_field The relation field in the foreign table
950 * @param boolean show_info Whether to display table position or not
951 * @access private
952 * @see _setMinMax
954 private function _addRelation($masterTable, $masterField, $foreignTable, $foreignField, $showInfo)
956 if (! isset($this->tables[$masterTable])) {
957 $this->tables[$masterTable] = new Table_Stats($masterTable, $this->_ff, $this->pageNumber, $this->_tablewidth, false, $showInfo);
958 $this->_setMinMax($this->tables[$masterTable]);
960 if (! isset($this->tables[$foreignTable])) {
961 $this->tables[$foreignTable] = new Table_Stats($foreignTable, $this->_ff, $this->pageNumber, $this->_tablewidth, false, $showInfo);
962 $this->_setMinMax($this->tables[$foreignTable]);
964 $this->relations[] = new Relation_Stats($this->tables[$masterTable], $masterField, $this->tables[$foreignTable], $foreignField);
968 * Draws the grid
970 * @global object the current PMA_PDF instance
971 * @access private
972 * @see PMA_PDF
974 private function _strokeGrid()
976 global $pdf;
978 $gridSize = 10;
979 $labelHeight = 4;
980 $labelWidth = 5;
981 if ($this->withDoc) {
982 $topSpace = 6;
983 $bottomSpace = 15;
984 } else {
985 $topSpace = 0;
986 $bottomSpace = 0;
989 $pdf->SetMargins(0, 0);
990 $pdf->SetDrawColor(200, 200, 200);
991 // Draws horizontal lines
992 for ($l = 0; $l <= intval(($pdf->getH() - $topSpace - $bottomSpace) / $gridSize); $l++) {
993 $pdf->line(0, $l * $gridSize + $topSpace, $pdf->getW(), $l * $gridSize + $topSpace);
994 // Avoid duplicates
995 if ($l > 0 && $l <= intval(($pdf->getH() - $topSpace - $bottomSpace - $labelHeight) / $gridSize)) {
996 $pdf->SetXY(0, $l * $gridSize + $topSpace);
997 $label = (string) sprintf('%.0f', ($l * $gridSize + $topSpace - $this->topMargin) * $this->scale + $this->_yMin);
998 $pdf->Cell($labelWidth, $labelHeight, ' ' . $label);
999 } // end if
1000 } // end for
1001 // Draws vertical lines
1002 for ($j = 0; $j <= intval($pdf->getW() / $gridSize); $j++) {
1003 $pdf->line($j * $gridSize, $topSpace, $j * $gridSize, $pdf->getH() - $bottomSpace);
1004 $pdf->SetXY($j * $gridSize, $topSpace);
1005 $label = (string) sprintf('%.0f', ($j * $gridSize - $this->leftMargin) * $this->scale + $this->_xMin);
1006 $pdf->Cell($labelWidth, $labelHeight, $label);
1011 * Draws relation arrows
1013 * @param boolean changeColor Whether to use one color per relation or not
1014 * @access private
1015 * @see Relation_Stats::relationdraw()
1017 private function _drawRelations($changeColor)
1019 $i = 0;
1020 foreach ($this->relations as $relation) {
1021 $relation->relationDraw($changeColor, $i);
1022 $i++;
1027 * Draws tables
1029 * @param boolean changeColor Whether to display table position or not
1030 * @access private
1031 * @see Table_Stats::tableDraw()
1033 private function _drawTables($changeColor = 0)
1035 foreach ($this->tables as $table) {
1036 $table->tableDraw($this->_ff, $this->withDoc, $changeColor);
1041 * Ouputs the PDF document to a file
1042 * or sends the output to browser
1044 * @global object The current PDF document
1045 * @global string The current database name
1046 * @global integer The current page number (from the
1047 * $cfg['Servers'][$i]['table_coords'] table)
1048 * @access private
1049 * @see PMA_PDF
1051 private function _showOutput($pageNumber)
1053 global $pdf, $db, $cfgRelation;
1055 $pdf->SetFontSize(14);
1056 $pdf->SetLineWidth(0.2);
1057 $pdf->SetDisplayMode('fullpage');
1058 // Get the name of this pdfpage to use as filename
1059 $_name_sql = 'SELECT page_descr FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
1060 . ' WHERE page_nr = ' . $pageNumber;
1061 $_name_rs = PMA_query_as_controluser($_name_sql);
1062 if ($_name_rs) {
1063 $_name_row = PMA_DBI_fetch_row($_name_rs);
1064 $filename = $_name_row[0] . '.pdf';
1066 if (empty($filename)) {
1067 $filename = $pageNumber . '.pdf';
1069 // instead of $pdf->Output():
1070 $pdfData = $pdf->getPDFData();
1071 PMA_download_header($filename, 'application/pdf', strlen($pdfData));
1072 echo $pdfData;
1075 public function dataDictionaryDoc($alltables)
1077 global $db, $pdf, $orientation, $paper;
1078 // TOC
1079 $pdf->addpage($GLOBALS['orientation']);
1080 $pdf->Cell(0, 9, __('Table of contents'), 1, 0, 'C');
1081 $pdf->Ln(15);
1082 $i = 1;
1083 foreach ($alltables as $table) {
1084 $pdf->PMA_links['doc'][$table]['-'] = $pdf->AddLink();
1085 $pdf->SetX(10);
1086 // $pdf->Ln(1);
1087 $pdf->Cell(0, 6, __('Page number:') . ' {' . sprintf("%02d", $i + 1) . '}', 0, 0, 'R', 0, $pdf->PMA_links['doc'][$table]['-']);
1088 $pdf->SetX(10);
1089 $pdf->Cell(0, 6, $i . ' ' . $table, 0, 1, 'L', 0, $pdf->PMA_links['doc'][$table]['-']);
1090 // $pdf->Ln(1);
1091 $result = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';');
1092 while ($row = PMA_DBI_fetch_assoc($result)) {
1093 $pdf->SetX(20);
1094 $field_name = $row['Field'];
1095 $pdf->PMA_links['doc'][$table][$field_name] = $pdf->AddLink();
1096 // $pdf->Cell(0, 6, $field_name,0,1,'L',0, $pdf->PMA_links['doc'][$table][$field_name]);
1098 $lasttable = $table;
1099 $i++;
1101 $pdf->PMA_links['RT']['-'] = $pdf->AddLink();
1102 $pdf->SetX(10);
1103 $pdf->Cell(0, 6, __('Page number:') . ' {' . sprintf("%02d", $i + 1) . '}', 0, 0, 'R', 0, $pdf->PMA_links['RT']['-']);
1104 $pdf->SetX(10);
1105 $pdf->Cell(0, 6, $i . ' ' . __('Relational schema'), 0, 1, 'L', 0, $pdf->PMA_links['RT']['-']);
1106 $z = 0;
1107 foreach ($alltables as $table) {
1108 $z++;
1109 $pdf->SetAutoPageBreak(true, 15);
1110 $pdf->addpage($GLOBALS['orientation']);
1111 $pdf->Bookmark($table);
1112 $pdf->SetAlias('{' . sprintf("%02d", $z) . '}', $pdf->PageNo()) ;
1113 $pdf->PMA_links['RT'][$table]['-'] = $pdf->AddLink();
1114 $pdf->SetLink($pdf->PMA_links['doc'][$table]['-'], -1);
1115 $pdf->SetFont($this->_ff, 'B', 18);
1116 $pdf->Cell(0, 8, $z . ' ' . $table, 1, 1, 'C', 0, $pdf->PMA_links['RT'][$table]['-']);
1117 $pdf->SetFont($this->_ff, '', 8);
1118 $pdf->ln();
1120 $cfgRelation = PMA_getRelationsParam();
1121 $comments = PMA_getComments($db, $table);
1122 if ($cfgRelation['mimework']) {
1123 $mime_map = PMA_getMIME($db, $table, true);
1127 * Gets table informations
1129 $showtable = PMA_Table::sGetStatusInfo($db, $table);
1130 $num_rows = (isset($showtable['Rows']) ? $showtable['Rows'] : 0);
1131 $show_comment = (isset($showtable['Comment']) ? $showtable['Comment'] : '');
1132 $create_time = (isset($showtable['Create_time']) ? PMA_localisedDate(strtotime($showtable['Create_time'])) : '');
1133 $update_time = (isset($showtable['Update_time']) ? PMA_localisedDate(strtotime($showtable['Update_time'])) : '');
1134 $check_time = (isset($showtable['Check_time']) ? PMA_localisedDate(strtotime($showtable['Check_time'])) : '');
1137 * Gets table keys and retains them
1139 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
1140 $primary = '';
1141 $indexes = array();
1142 $lastIndex = '';
1143 $indexes_info = array();
1144 $indexes_data = array();
1145 $pk_array = array(); // will be use to emphasis prim. keys in the table
1146 // view
1147 while ($row = PMA_DBI_fetch_assoc($result)) {
1148 // Backups the list of primary keys
1149 if ($row['Key_name'] == 'PRIMARY') {
1150 $primary .= $row['Column_name'] . ', ';
1151 $pk_array[$row['Column_name']] = 1;
1153 // Retains keys informations
1154 if ($row['Key_name'] != $lastIndex) {
1155 $indexes[] = $row['Key_name'];
1156 $lastIndex = $row['Key_name'];
1158 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
1159 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
1160 if (isset($row['Cardinality'])) {
1161 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
1163 // I don't know what does following column mean....
1164 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
1165 $indexes_info[$row['Key_name']]['Comment'] = $row['Comment'];
1167 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
1168 if (isset($row['Sub_part'])) {
1169 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
1171 } // end while
1172 if ($result) {
1173 PMA_DBI_free_result($result);
1177 * Gets fields properties
1179 $columns = PMA_DBI_get_columns($db, $table);
1180 // Check if we can use Relations
1181 if (!empty($cfgRelation['relation'])) {
1182 // Find which tables are related with the current one and write it in
1183 // an array
1184 $res_rel = PMA_getForeigners($db, $table);
1186 if (count($res_rel) > 0) {
1187 $have_rel = true;
1188 } else {
1189 $have_rel = false;
1191 } else {
1192 $have_rel = false;
1193 } // end if
1196 * Displays the comments of the table if MySQL >= 3.23
1199 $break = false;
1200 if (!empty($show_comment)) {
1201 $pdf->Cell(0, 3, __('Table comments') . ' : ' . $show_comment, 0, 1);
1202 $break = true;
1205 if (!empty($create_time)) {
1206 $pdf->Cell(0, 3, __('Creation') . ': ' . $create_time, 0, 1);
1207 $break = true;
1210 if (!empty($update_time)) {
1211 $pdf->Cell(0, 3, __('Last update') . ': ' . $update_time, 0, 1);
1212 $break = true;
1215 if (!empty($check_time)) {
1216 $pdf->Cell(0, 3, __('Last check') . ': ' . $check_time, 0, 1);
1217 $break = true;
1220 if ($break == true) {
1221 $pdf->Cell(0, 3, '', 0, 1);
1222 $pdf->Ln();
1225 $pdf->SetFont($this->_ff, 'B');
1226 if (isset($orientation) && $orientation == 'L') {
1227 $pdf->Cell(25, 8, ucfirst(__('Column')), 1, 0, 'C');
1228 $pdf->Cell(20, 8, ucfirst(__('Type')), 1, 0, 'C');
1229 $pdf->Cell(20, 8, ucfirst(__('Attributes')), 1, 0, 'C');
1230 $pdf->Cell(10, 8, ucfirst(__('Null')), 1, 0, 'C');
1231 $pdf->Cell(20, 8, ucfirst(__('Default')), 1, 0, 'C');
1232 $pdf->Cell(25, 8, ucfirst(__('Extra')), 1, 0, 'C');
1233 $pdf->Cell(45, 8, ucfirst(__('Links to')), 1, 0, 'C');
1235 if ($paper == 'A4') {
1236 $comments_width = 67;
1237 } else {
1238 // this is really intended for 'letter'
1240 * @todo find optimal width for all formats
1242 $comments_width = 50;
1244 $pdf->Cell($comments_width, 8, ucfirst(__('Comments')), 1, 0, 'C');
1245 $pdf->Cell(45, 8, 'MIME', 1, 1, 'C');
1246 $pdf->SetWidths(array(25, 20, 20, 10, 20, 25, 45, $comments_width, 45));
1247 } else {
1248 $pdf->Cell(20, 8, ucfirst(__('Column')), 1, 0, 'C');
1249 $pdf->Cell(20, 8, ucfirst(__('Type')), 1, 0, 'C');
1250 $pdf->Cell(20, 8, ucfirst(__('Attributes')), 1, 0, 'C');
1251 $pdf->Cell(10, 8, ucfirst(__('Null')), 1, 0, 'C');
1252 $pdf->Cell(15, 8, ucfirst(__('Default')), 1, 0, 'C');
1253 $pdf->Cell(15, 8, ucfirst(__('Extra')), 1, 0, 'C');
1254 $pdf->Cell(30, 8, ucfirst(__('Links to')), 1, 0, 'C');
1255 $pdf->Cell(30, 8, ucfirst(__('Comments')), 1, 0, 'C');
1256 $pdf->Cell(30, 8, 'MIME', 1, 1, 'C');
1257 $pdf->SetWidths(array(20, 20, 20, 10, 15, 15, 30, 30, 30));
1259 $pdf->SetFont($this->_ff, '');
1261 foreach ($columns as $row) {
1262 $extracted_fieldspec = PMA_extractFieldSpec($row['Type']);
1263 $type = $extracted_fieldspec['print_type'];
1264 $attribute = $extracted_fieldspec['attribute'];
1265 if (! isset($row['Default'])) {
1266 if ($row['Null'] != '' && $row['Null'] != 'NO') {
1267 $row['Default'] = 'NULL';
1270 $field_name = $row['Field'];
1271 // $pdf->Ln();
1272 $pdf->PMA_links['RT'][$table][$field_name] = $pdf->AddLink();
1273 $pdf->Bookmark($field_name, 1, -1);
1274 $pdf->SetLink($pdf->PMA_links['doc'][$table][$field_name], -1);
1275 $pdf_row = array($field_name,
1276 $type,
1277 $attribute,
1278 ($row['Null'] == '' || $row['Null'] == 'NO') ? __('No') : __('Yes'),
1279 ((isset($row['Default'])) ? $row['Default'] : ''),
1280 $row['Extra'],
1281 ((isset($res_rel[$field_name])) ? $res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field'] : ''),
1282 ((isset($comments[$field_name])) ? $comments[$field_name] : ''),
1283 ((isset($mime_map) && isset($mime_map[$field_name])) ? str_replace('_', '/', $mime_map[$field_name]['mimetype']) : '')
1285 $links[0] = $pdf->PMA_links['RT'][$table][$field_name];
1286 if (isset($res_rel[$field_name]['foreign_table']) AND
1287 isset($res_rel[$field_name]['foreign_field']) AND
1288 isset($pdf->PMA_links['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']])
1291 $links[6] = $pdf->PMA_links['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']];
1292 } else {
1293 unset($links[6]);
1295 $pdf->Row($pdf_row, $links);
1296 } // end foreach
1297 $pdf->SetFont($this->_ff, '', 14);
1298 } //end each