Hindi translation update
[phpmyadmin-regexreplace.git] / libraries / schema / Pdf_Relation_Schema.class.php
blob607853709b2286a0016349b793f54d6404bb3132
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;
43 private $_ff = PMA_PDF_FONT;
45 public function getH()
47 return $this->h;
50 public function getW()
52 return $this->w;
55 public function setCMargin($c_margin)
57 $this->cMargin = $c_margin;
60 function SetAlias($name, $value)
62 $this->Alias[$name] = $value ;
65 function _putpages()
67 if (count($this->Alias) > 0) {
68 $nb = $this->page;
69 foreach ($this->Alias as $alias => $value) {
70 for ($n = 1;$n <= $nb;$n++)
71 $this->pages[$n]=str_replace($alias, $value, $this->pages[$n]);
74 parent::_putpages();
77 // added because tcpdf for PHP 5 has a protected $buffer
78 public function getBuffer()
80 return $this->buffer;
83 public function getState()
85 return $this->state;
88 /**
89 * Sets the scaling factor, defines minimum coordinates and margins
91 * @param float scale The scaling factor
92 * @param float _xMin The minimum X coordinate
93 * @param float _yMin The minimum Y coordinate
94 * @param float leftMargin The left margin
95 * @param float topMargin The top margin
96 * @access public
98 function PMA_PDF_setScale($scale = 1, $xMin = 0, $yMin = 0, $leftMargin = -1, $topMargin = -1)
100 $this->scale = $scale;
101 $this->_xMin = $xMin;
102 $this->_yMin = $yMin;
103 if ($this->leftMargin != -1) {
104 $this->leftMargin = $leftMargin;
106 if ($this->topMargin != -1) {
107 $this->topMargin = $topMargin;
112 * Outputs a scaled cell
114 * @param float w The cell width
115 * @param float h The cell height
116 * @param string txt The text to output
117 * @param mixed border Whether to add borders or not
118 * @param integer ln Where to put the cursor once the output is done
119 * @param string align Align mode
120 * @param integer fill Whether to fill the cell with a color or not
121 * @access public
122 * @see TCPDF::Cell()
124 function PMA_PDF_cellScale($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $fill = 0, $link = '')
126 $h = $h / $this->scale;
127 $w = $w / $this->scale;
128 $this->Cell($w, $h, $txt, $border, $ln, $align, $fill, $link);
132 * Draws a scaled line
134 * @param float x1 The horizontal position of the starting point
135 * @param float y1 The vertical position of the starting point
136 * @param float x2 The horizontal position of the ending point
137 * @param float y2 The vertical position of the ending point
138 * @access public
139 * @see TCPDF::Line()
141 function PMA_PDF_lineScale($x1, $y1, $x2, $y2)
143 $x1 = ($x1 - $this->_xMin) / $this->scale + $this->leftMargin;
144 $y1 = ($y1 - $this->_yMin) / $this->scale + $this->topMargin;
145 $x2 = ($x2 - $this->_xMin) / $this->scale + $this->leftMargin;
146 $y2 = ($y2 - $this->_yMin) / $this->scale + $this->topMargin;
147 $this->Line($x1, $y1, $x2, $y2);
151 * Sets x and y scaled positions
153 * @param float x The x position
154 * @param float y The y position
155 * @access public
156 * @see TCPDF::SetXY()
158 function PMA_PDF_setXyScale($x, $y)
160 $x = ($x - $this->_xMin) / $this->scale + $this->leftMargin;
161 $y = ($y - $this->_yMin) / $this->scale + $this->topMargin;
162 $this->SetXY($x, $y);
166 * Sets the X scaled positions
168 * @param float x The x position
169 * @access public
170 * @see TCPDF::SetX()
172 function PMA_PDF_setXScale($x)
174 $x = ($x - $this->_xMin) / $this->scale + $this->leftMargin;
175 $this->SetX($x);
179 * Sets the scaled font size
181 * @param float size The font size (in points)
182 * @access public
183 * @see TCPDF::SetFontSize()
185 function PMA_PDF_setFontSizeScale($size)
187 // Set font size in points
188 $size = $size / $this->scale;
189 $this->SetFontSize($size);
193 * Sets the scaled line width
195 * @param float width The line width
196 * @access public
197 * @see TCPDF::SetLineWidth()
199 function PMA_PDF_setLineWidthScale($width)
201 $width = $width / $this->scale;
202 $this->SetLineWidth($width);
206 * Displays an error message
208 * @param string error_message the error mesage
209 * @access public
210 * @see PMA_Export_Relation_Schema::dieSchema
212 function Error($error_message = '')
214 PMA_Export_Relation_Schema::dieSchema($error_message);
217 function Header()
219 // We only show this if we find something in the new pdf_pages table
221 // This function must be named "Header" to work with the TCPDF library
222 global $cfgRelation, $db, $pdf_page_number, $with_doc;
223 if ($with_doc) {
224 $test_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
225 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
226 . ' AND page_nr = \'' . $pdf_page_number . '\'';
227 $test_rs = PMA_query_as_controluser($test_query);
228 $pages = @PMA_DBI_fetch_assoc($test_rs);
229 $this->SetFont($this->_ff, 'B', 14);
230 $this->Cell(0, 6, ucfirst($pages['page_descr']), 'B', 1, 'C');
231 $this->SetFont($this->_ff, '');
232 $this->Ln();
236 function Footer()
238 // This function must be named "Footer" to work with the TCPDF library
239 global $with_doc;
240 if ($with_doc) {
241 $this->SetY(-15);
242 $this->SetFont($this->_ff, '', 14);
243 $this->Cell(0, 6, __('Page number:') . ' ' . $this->getAliasNumPage() . '/' . $this->getAliasNbPages(), 'T', 0, 'C');
244 $this->Cell(0, 6, PMA_localisedDate(), 0, 1, 'R');
245 $this->SetY(20);
249 function Bookmark($txt, $level = 0, $y = 0, $page = '')
251 // Add a bookmark
252 $this->Outlines[0][] = $level;
253 $this->Outlines[1][] = $txt;
254 $this->Outlines[2][] = $this->page;
255 if ($y == -1) {
256 $y = $this->GetY();
258 $this->Outlines[3][] = round($this->hPt - $y * $this->k, 2);
261 function _putbookmarks()
263 if (count($this->Outlines) > 0) {
264 // Save object number
265 $memo_n = $this->n;
266 // Take the number of sub elements for an outline
267 $nb_outlines = sizeof($this->Outlines[0]);
268 $first_level = array();
269 $parent = array();
270 $parent[0] = 1;
271 for ($i = 0; $i < $nb_outlines; $i++) {
272 $level = $this->Outlines[0][$i];
273 $kids = 0;
274 $last = -1;
275 $prev = -1;
276 $next = -1;
277 if ($i > 0) {
278 $cursor = $i-1;
279 // Take the previous outline in the same level
280 while ($this->Outlines[0][$cursor] > $level && $cursor > 0)
281 $cursor--;
282 if ($this->Outlines[0][$cursor] == $level) {
283 $prev = $cursor;
286 if ($i < $nb_outlines-1) {
287 $cursor = $i + 1;
288 while (isset($this->Outlines[0][$cursor]) && $this->Outlines[0][$cursor] > $level) {
289 // Take the immediate kid in level + 1
290 if ($this->Outlines[0][$cursor] == $level + 1) {
291 $kids++;
292 $last = $cursor;
294 $cursor++;
296 $cursor = $i + 1;
297 // Take the next outline in the same level
298 while ($this->Outlines[0][$cursor] > $level && ($cursor + 1 < sizeof($this->Outlines[0])))
299 $cursor++;
300 if ($this->Outlines[0][$cursor] == $level) {
301 $next = $cursor;
304 $this->_newobj();
305 $parent[$level + 1] = $this->n;
306 if ($level == 0) {
307 $first_level[] = $this->n;
309 $this->_out('<<');
310 $this->_out('/Title (' . $this->Outlines[1][$i] . ')');
311 $this->_out('/Parent ' . $parent[$level] . ' 0 R');
312 if ($prev != -1) {
313 $this->_out('/Prev ' . ($memo_n + $prev + 1) . ' 0 R');
315 if ($next != -1) {
316 $this->_out('/Next ' . ($this->n + $next - $i) . ' 0 R');
318 $this->_out('/Dest [' . (1 + (2 * $this->Outlines[2][$i])) . ' 0 R /XYZ null ' . $this->Outlines[3][$i] . ' null]');
319 if ($kids > 0) {
320 $this->_out('/First ' . ($this->n + 1) . ' 0 R');
321 $this->_out('/Last ' . ($this->n + $last - $i) . ' 0 R');
322 $this->_out('/Count -' . $kids);
324 $this->_out('>>');
325 $this->_out('endobj');
327 // First page of outlines
328 $this->_newobj();
329 $this->def_outlines = $this->n;
330 $this->_out('<<');
331 $this->_out('/Type');
332 $this->_out('/Outlines');
333 $this->_out('/First ' . $first_level[0] . ' 0 R');
334 $this->_out('/Last ' . $first_level[sizeof($first_level)-1] . ' 0 R');
335 $this->_out('/Count ' . sizeof($first_level));
336 $this->_out('>>');
337 $this->_out('endobj');
341 function _putresources()
343 parent::_putresources();
344 $this->_putbookmarks();
347 function SetWidths($w)
349 // column widths
350 $this->widths = $w;
353 function Row($data, $links)
355 // line height
356 $nb = 0;
357 $data_cnt = count($data);
358 for ($i = 0;$i < $data_cnt;$i++)
359 $nb = max($nb, $this->NbLines($this->widths[$i], $data[$i]));
360 $il = $this->FontSize;
361 $h = ($il + 1) * $nb;
362 // page break if necessary
363 $this->CheckPageBreak($h);
364 // draw the cells
365 $data_cnt = count($data);
366 for ($i = 0;$i < $data_cnt;$i++) {
367 $w = $this->widths[$i];
368 // save current position
369 $x = $this->GetX();
370 $y = $this->GetY();
371 // draw the border
372 $this->Rect($x, $y, $w, $h);
373 if (isset($links[$i])) {
374 $this->Link($x, $y, $w, $h, $links[$i]);
376 // print text
377 $this->MultiCell($w, $il + 1, $data[$i], 0, 'L');
378 // go to right side
379 $this->SetXY($x + $w, $y);
381 // go to line
382 $this->Ln($h);
385 function NbLines($w, $txt)
387 // compute number of lines used by a multicell of width w
388 $cw = &$this->CurrentFont['cw'];
389 if ($w == 0) {
390 $w = $this->w - $this->rMargin - $this->x;
392 $wmax = ($w-2 * $this->cMargin) * 1000 / $this->FontSize;
393 $s = str_replace("\r", '', $txt);
394 $nb = strlen($s);
395 if ($nb > 0 and $s[$nb-1] == "\n") {
396 $nb--;
398 $sep = -1;
399 $i = 0;
400 $j = 0;
401 $l = 0;
402 $nl = 1;
403 while ($i < $nb) {
404 $c = $s[$i];
405 if ($c == "\n") {
406 $i++;
407 $sep = -1;
408 $j = $i;
409 $l = 0;
410 $nl++;
411 continue;
413 if ($c == ' ') {
414 $sep = $i;
416 $l += isset($cw[ord($c)])?$cw[ord($c)]:0 ;
417 if ($l > $wmax) {
418 if ($sep == -1) {
419 if ($i == $j) {
420 $i++;
422 } else {
423 $i = $sep + 1;
425 $sep = -1;
426 $j = $i;
427 $l = 0;
428 $nl++;
429 } else {
430 $i++;
433 return $nl;
438 * Table preferences/statistics
440 * This class preserves the table co-ordinates,fields
441 * and helps in drawing/generating the Tables in PDF document.
443 * @name Table_Stats
444 * @copyright
445 * @license
446 * @see PMA_PDF
448 class Table_Stats
451 * Defines properties
453 private $_tableName;
454 private $_showInfo = false;
456 public $nb_fiels;
457 public $width = 0;
458 public $height;
459 public $fields = array();
460 public $heightCell = 6;
461 public $x, $y;
462 public $primary = array();
463 private $_ff = PMA_PDF_FONT;
466 * The "Table_Stats" constructor
468 * @param string table_name The table name
469 * @param integer fontSize The font size
470 * @param integer pageNumber The current page number (from the
471 * $cfg['Servers'][$i]['table_coords'] table)
472 * @param integer sameWideWidth The max. with among tables
473 * @param boolean showKeys Whether to display keys or not
474 * @param boolean showInfo Whether to display table position or not
475 * @global object The current PDF document
476 * @global array The relations settings
477 * @global string The current db name
478 * @see PMA_PDF, Table_Stats::Table_Stats_setWidth,
479 Table_Stats::Table_Stats_setHeight
481 function __construct($tableName, $fontSize, $pageNumber, &$sameWideWidth, $showKeys = false, $showInfo = false)
483 global $pdf, $cfgRelation, $db;
485 $this->_tableName = $tableName;
486 $sql = 'DESCRIBE ' . PMA_backquote($tableName);
487 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE);
488 if (!$result || !PMA_DBI_num_rows($result)) {
489 $pdf->Error(sprintf(__('The %s table doesn\'t exist!'), $tableName));
491 // load fields
492 //check to see if it will load all fields or only the foreign keys
493 if ($showKeys) {
494 $indexes = PMA_Index::getFromTable($this->_tableName, $db);
495 $all_columns = array();
496 foreach ($indexes as $index) {
497 $all_columns = array_merge($all_columns, array_flip(array_keys($index->getColumns())));
499 $this->fields = array_keys($all_columns);
500 } else {
501 while ($row = PMA_DBI_fetch_row($result)) {
502 $this->fields[] = $row[0];
506 $this->_showInfo = $showInfo;
507 $this->_setHeight();
509 * setWidth must me after setHeight, because title
510 * can include table height which changes table width
512 $this->_setWidth($fontSize);
513 if ($sameWideWidth < $this->width) {
514 $sameWideWidth = $this->width;
516 $sql = 'SELECT x, y FROM '
517 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
518 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
519 . ' AND table_name = \'' . PMA_sqlAddslashes($tableName) . '\''
520 . ' AND pdf_page_number = ' . $pageNumber;
521 $result = PMA_query_as_controluser($sql, false, PMA_DBI_QUERY_STORE);
522 if (!$result || !PMA_DBI_num_rows($result)) {
523 $pdf->Error(sprintf(__('Please configure the coordinates for table %s'), $tableName));
525 list($this->x, $this->y) = PMA_DBI_fetch_row($result);
526 $this->x = (double) $this->x;
527 $this->y = (double) $this->y;
529 * displayfield
531 $this->displayfield = PMA_getDisplayField($db, $tableName);
533 * index
535 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tableName) . ';', null, PMA_DBI_QUERY_STORE);
536 if (PMA_DBI_num_rows($result) > 0) {
537 while ($row = PMA_DBI_fetch_assoc($result)) {
538 if ($row['Key_name'] == 'PRIMARY') {
539 $this->primary[] = $row['Column_name'];
546 * Returns title of the current table,
547 * title can have the dimensions of the table
549 * @access private
551 private function _getTitle()
553 return ($this->_showInfo ? sprintf('%.0f', $this->width) . 'x' . sprintf('%.0f', $this->height) : '') . ' ' . $this->_tableName;
557 * Sets the width of the table
559 * @param integer fontSize The font size
560 * @global object The current PDF document
561 * @access private
562 * @see PMA_PDF
564 function _setWidth($fontSize)
566 global $pdf;
568 foreach ($this->fields as $field) {
569 $this->width = max($this->width, $pdf->GetStringWidth($field));
571 $this->width += $pdf->GetStringWidth(' ');
572 $pdf->SetFont($this->_ff, 'B', $fontSize);
574 * it is unknown what value must be added, because
575 * table title is affected by the tabe width value
577 while ($this->width < $pdf->GetStringWidth($this->_getTitle())) {
578 $this->width += 5;
580 $pdf->SetFont($this->_ff, '', $fontSize);
584 * Sets the height of the table
586 * @access private
588 private function _setHeight()
590 $this->height = (count($this->fields) + 1) * $this->heightCell;
594 * Do draw the table
596 * @param integer fontSize The font size
597 * @param boolean setColor Whether to display color
598 * @global object The current PDF document
599 * @access public
600 * @see PMA_PDF
602 public function tableDraw($fontSize, $withDoc, $setColor = 0)
604 global $pdf, $withDoc;
606 $pdf->PMA_PDF_setXyScale($this->x, $this->y);
607 $pdf->SetFont($this->_ff, 'B', $fontSize);
608 if ($setColor) {
609 $pdf->SetTextColor(200);
610 $pdf->SetFillColor(0, 0, 128);
612 if ($withDoc) {
613 $pdf->SetLink($pdf->PMA_links['RT'][$this->_tableName]['-'], -1);
614 } else {
615 $pdf->PMA_links['doc'][$this->_tableName]['-'] = '';
618 $pdf->PMA_PDF_cellScale($this->width, $this->heightCell, $this->_getTitle(), 1, 1, 'C', $setColor, $pdf->PMA_links['doc'][$this->_tableName]['-']);
619 $pdf->PMA_PDF_setXScale($this->x);
620 $pdf->SetFont($this->_ff, '', $fontSize);
621 $pdf->SetTextColor(0);
622 $pdf->SetFillColor(255);
624 foreach ($this->fields as $field) {
625 if ($setColor) {
626 if (in_array($field, $this->primary)) {
627 $pdf->SetFillColor(215, 121, 123);
629 if ($field == $this->displayfield) {
630 $pdf->SetFillColor(142, 159, 224);
633 if ($withDoc) {
634 $pdf->SetLink($pdf->PMA_links['RT'][$this->_tableName][$field], -1);
635 } else {
636 $pdf->PMA_links['doc'][$this->_tableName][$field] = '';
639 $pdf->PMA_PDF_cellScale($this->width, $this->heightCell, ' ' . $field, 1, 1, 'L', $setColor, $pdf->PMA_links['doc'][$this->_tableName][$field]);
640 $pdf->PMA_PDF_setXScale($this->x);
641 $pdf->SetFillColor(255);
643 /*if ($pdf->PageNo() > 1) {
644 $pdf->PMA_PDF_die(__('The scale factor is too small to fit the schema on one page'));
645 } */
650 * Relation preferences/statistics
652 * This class fetches the table master and foreign fields positions
653 * and helps in generating the Table references and then connects
654 * master table's master field to foreign table's foreign key
655 * in PDF document.
657 * @name Relation_Stats
658 * @copyright
659 * @license
660 * @see PMA_PDF::SetDrawColor,PMA_PDF::PMA_PDF_setLineWidthScale,PMA_PDF::PMA_PDF_lineScale
662 class Relation_Stats
665 * Defines properties
667 public $xSrc, $ySrc;
668 public $srcDir;
669 public $destDir;
670 public $xDest, $yDest;
671 public $wTick = 5;
674 * The "Relation_Stats" constructor
676 * @param string master_table The master table name
677 * @param string master_field The relation field in the master table
678 * @param string foreign_table The foreign table name
679 * @param string foreigh_field The relation field in the foreign table
680 * @see Relation_Stats::_getXy
682 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
684 $src_pos = $this->_getXy($master_table, $master_field);
685 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
687 * [0] is x-left
688 * [1] is x-right
689 * [2] is y
691 $src_left = $src_pos[0] - $this->wTick;
692 $src_right = $src_pos[1] + $this->wTick;
693 $dest_left = $dest_pos[0] - $this->wTick;
694 $dest_right = $dest_pos[1] + $this->wTick;
696 $d1 = abs($src_left - $dest_left);
697 $d2 = abs($src_right - $dest_left);
698 $d3 = abs($src_left - $dest_right);
699 $d4 = abs($src_right - $dest_right);
700 $d = min($d1, $d2, $d3, $d4);
702 if ($d == $d1) {
703 $this->xSrc = $src_pos[0];
704 $this->srcDir = -1;
705 $this->xDest = $dest_pos[0];
706 $this->destDir = -1;
707 } elseif ($d == $d2) {
708 $this->xSrc = $src_pos[1];
709 $this->srcDir = 1;
710 $this->xDest = $dest_pos[0];
711 $this->destDir = -1;
712 } elseif ($d == $d3) {
713 $this->xSrc = $src_pos[0];
714 $this->srcDir = -1;
715 $this->xDest = $dest_pos[1];
716 $this->destDir = 1;
717 } else {
718 $this->xSrc = $src_pos[1];
719 $this->srcDir = 1;
720 $this->xDest = $dest_pos[1];
721 $this->destDir = 1;
723 $this->ySrc = $src_pos[2];
724 $this->yDest = $dest_pos[2];
728 * Gets arrows coordinates
730 * @param string table The current table name
731 * @param string column The relation column name
732 * @return array Arrows coordinates
733 * @access private
735 private function _getXy($table, $column)
737 $pos = array_search($column, $table->fields);
738 // x_left, x_right, y
739 return array($table->x, $table->x + + $table->width, $table->y + ($pos + 1.5) * $table->heightCell);
743 * draws relation links and arrows
744 * shows foreign key relations
746 * @param boolean changeColor Whether to use one color per relation or not
747 * @param integer i The id of the link to draw
748 * @global object The current PDF document
749 * @access public
750 * @see PMA_PDF
752 public function relationDraw($changeColor, $i)
754 global $pdf;
756 if ($changeColor) {
757 $d = $i % 6;
758 $j = ($i - $d) / 6;
759 $j = $j % 4;
760 $j++;
761 $case = array(
762 array(1, 0, 0),
763 array(0, 1, 0),
764 array(0, 0, 1),
765 array(1, 1, 0),
766 array(1, 0, 1),
767 array(0, 1, 1)
769 list ($a, $b, $c) = $case[$d];
770 $e = (1 - ($j - 1) / 6);
771 $pdf->SetDrawColor($a * 255 * $e, $b * 255 * $e, $c * 255 * $e);
772 } else {
773 $pdf->SetDrawColor(0);
775 $pdf->PMA_PDF_setLineWidthScale(0.2);
776 $pdf->PMA_PDF_lineScale($this->xSrc, $this->ySrc, $this->xSrc + $this->srcDir * $this->wTick, $this->ySrc);
777 $pdf->PMA_PDF_lineScale($this->xDest + $this->destDir * $this->wTick, $this->yDest, $this->xDest, $this->yDest);
778 $pdf->PMA_PDF_setLineWidthScale(0.1);
779 $pdf->PMA_PDF_lineScale($this->xSrc + $this->srcDir * $this->wTick, $this->ySrc, $this->xDest + $this->destDir * $this->wTick, $this->yDest);
781 * Draws arrows ->
783 $root2 = 2 * sqrt(2);
784 $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->xSrc + $this->srcDir * $this->wTick * 0.75, $this->ySrc, $this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick, $this->ySrc - $this->wTick / $root2);
787 $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);
788 $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);
789 $pdf->SetDrawColor(0);
794 * Pdf Relation Schema Class
796 * Purpose of this class is to generate the PDF Document. PDF is widely
797 * used format for documenting text,fonts,images and 3d vector graphics.
799 * This class inherits Export_Relation_Schema class has common functionality added
800 * to this class
802 * @name Pdf_Relation_Schema
803 * @copyright
804 * @license
806 class PMA_Pdf_Relation_Schema extends PMA_Export_Relation_Schema
809 * Defines properties
811 private $_tables = array();
812 private $_relations = array();
813 private $_ff = PMA_PDF_FONT;
814 private $_xMax = 0;
815 private $_yMax = 0;
816 private $scale;
817 private $_xMin = 100000;
818 private $_yMin = 100000;
819 private $topMargin = 10;
820 private $bottomMargin = 10;
821 private $leftMargin = 10;
822 private $rightMargin = 10;
823 private $_tablewidth;
826 * The "PMA_Pdf_Relation_Schema" constructor
828 * @global object The current PDF Schema document
829 * @global string The current db name
830 * @global array The relations settings
831 * @access private
832 * @see PMA_PDF
834 function __construct()
836 global $pdf,$db,$cfgRelation;
838 $this->setPageNumber($_POST['pdf_page_number']);
839 $this->setShowGrid(isset($_POST['show_grid']));
840 $this->setShowColor(isset($_POST['show_color']));
841 $this->setShowKeys(isset($_POST['show_keys']));
842 $this->setTableDimension(isset($_POST['show_table_dimension']));
843 $this->setAllTableSameWidth(isset($_POST['all_table_same_wide']));
844 $this->setWithDataDictionary($_POST['with_doc']);
845 $this->setOrientation($_POST['orientation']);
846 $this->setPaper($_POST['paper']);
847 $this->setExportType($_POST['export_type']);
849 // Initializes a new document
850 $pdf = new PMA_PDF($this->orientation, 'mm', $this->paper);
851 $pdf->SetTitle(sprintf(__('Schema of the %s database - Page %s'), $GLOBALS['db'], $this->pageNumber));
852 $pdf->setCMargin(0);
853 $pdf->Open();
854 $pdf->SetAuthor('phpMyAdmin ' . PMA_VERSION);
855 $pdf->AliasNbPages();
856 $pdf->AddFont('DejaVuSans', '', 'dejavusans.php');
857 $pdf->AddFont('DejaVuSans', 'B', 'dejavusansb.php');
858 $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php');
859 $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserifb.php');
860 $pdf->SetFont($this->_ff, '', 14);
861 $pdf->setFooterFont(array($this->_ff, '', 14));
862 $pdf->SetAutoPageBreak('auto');
863 $alltables = $this->getAllTables($db,$this->pageNumber);
865 if ($this->withDoc) {
866 $pdf->SetAutoPageBreak('auto', 15);
867 $pdf->setCMargin(1);
868 $this->dataDictionaryDoc($alltables);
869 $pdf->SetAutoPageBreak('auto');
870 $pdf->setCMargin(0);
873 $pdf->Addpage();
875 if ($this->withDoc) {
876 $pdf->SetLink($pdf->PMA_links['RT']['-'], -1);
877 $pdf->Bookmark(__('Relational schema'));
878 $pdf->SetAlias('{00}', $pdf->PageNo()) ;
879 $this->topMargin = 28;
880 $this->bottomMargin = 28;
883 /* snip */
884 foreach ($alltables as $table) {
885 if (!isset($this->tables[$table])) {
886 $this->tables[$table] = new Table_Stats($table, $this->_ff, $this->pageNumber, $this->_tablewidth, $this->showKeys, $this->tableDimension);
888 if ($this->sameWide) {
889 $this->tables[$table]->width = $this->_tablewidth;
891 $this->_setMinMax($this->tables[$table]);
894 // Defines the scale factor
895 $this->scale = ceil(
896 max(
897 ($this->_xMax - $this->_xMin) / ($pdf->getW() - $this->rightMargin - $this->leftMargin),
898 ($this->_yMax - $this->_yMin) / ($pdf->getH() - $this->topMargin - $this->bottomMargin))
899 * 100) / 100;
901 $pdf->PMA_PDF_setScale($this->scale, $this->_xMin, $this->_yMin, $this->leftMargin, $this->topMargin);
902 // Builds and save the PDF document
903 $pdf->PMA_PDF_setLineWidthScale(0.1);
905 if ($this->showGrid) {
906 $pdf->SetFontSize(10);
907 $this->_strokeGrid();
909 $pdf->PMA_PDF_setFontSizeScale(14);
910 // previous logic was checking master tables and foreign tables
911 // but I think that looping on every table of the pdf page as a master
912 // and finding its foreigns is OK (then we can support innodb)
913 $seen_a_relation = false;
914 foreach ($alltables as $one_table) {
915 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
916 if ($exist_rel) {
917 $seen_a_relation = true;
918 foreach ($exist_rel as $master_field => $rel) {
919 // put the foreign table on the schema only if selected
920 // by the user
921 // (do not use array_search() because we would have to
922 // to do a === FALSE and this is not PHP3 compatible)
923 if (in_array($rel['foreign_table'], $alltables)) {
924 $this->_addRelation($one_table, $master_field, $rel['foreign_table'], $rel['foreign_field'], $this->tableDimension);
926 } // end while
927 } // end if
928 } // end while
930 if ($seen_a_relation) {
931 $this->_drawRelations($this->showColor);
933 $this->_drawTables($this->showColor);
934 $this->_showOutput($this->pageNumber);
935 exit();
939 * Sets X and Y minimum and maximum for a table cell
941 * @param string table The table name of which sets XY co-ordinates
942 * @access private
944 private function _setMinMax($table)
946 $this->_xMax = max($this->_xMax, $table->x + $table->width);
947 $this->_yMax = max($this->_yMax, $table->y + $table->height);
948 $this->_xMin = min($this->_xMin, $table->x);
949 $this->_yMin = min($this->_yMin, $table->y);
953 * Defines relation objects
955 * @param string master_table The master table name
956 * @param string master_field The relation field in the master table
957 * @param string foreign_table The foreign table name
958 * @param string foreign_field The relation field in the foreign table
959 * @param boolean show_info Whether to display table position or not
960 * @access private
961 * @see _setMinMax
963 private function _addRelation($masterTable, $masterField, $foreignTable, $foreignField, $showInfo)
965 if (!isset($this->tables[$masterTable])) {
966 $this->tables[$masterTable] = new Table_Stats($masterTable, $this->_ff, $this->pageNumber, $this->_tablewidth, false, $showInfo);
967 $this->_setMinMax($this->tables[$masterTable]);
969 if (!isset($this->tables[$foreignTable])) {
970 $this->tables[$foreignTable] = new Table_Stats($foreignTable, $this->_ff, $this->pageNumber, $this->_tablewidth, false, $showInfo);
971 $this->_setMinMax($this->tables[$foreignTable]);
973 $this->relations[] = new Relation_Stats($this->tables[$masterTable], $masterField, $this->tables[$foreignTable], $foreignField);
977 * Draws the grid
979 * @global object the current PMA_PDF instance
980 * @access private
981 * @see PMA_PDF
983 private function _strokeGrid()
985 global $pdf;
987 $gridSize = 10;
988 $labelHeight = 4;
989 $labelWidth = 5;
990 if ($this->withDoc) {
991 $topSpace = 6;
992 $bottomSpace = 15;
993 } else {
994 $topSpace = 0;
995 $bottomSpace = 0;
998 $pdf->SetMargins(0, 0);
999 $pdf->SetDrawColor(200, 200, 200);
1000 // Draws horizontal lines
1001 for ($l = 0; $l <= intval(($pdf->getH() - $topSpace - $bottomSpace) / $gridSize); $l++) {
1002 $pdf->line(0, $l * $gridSize + $topSpace, $pdf->getW(), $l * $gridSize + $topSpace);
1003 // Avoid duplicates
1004 if ($l > 0 && $l <= intval(($pdf->getH() - $topSpace - $bottomSpace - $labelHeight) / $gridSize)) {
1005 $pdf->SetXY(0, $l * $gridSize + $topSpace);
1006 $label = (string) sprintf('%.0f', ($l * $gridSize + $topSpace - $this->topMargin) * $this->scale + $this->_yMin);
1007 $pdf->Cell($labelWidth, $labelHeight, ' ' . $label);
1008 } // end if
1009 } // end for
1010 // Draws vertical lines
1011 for ($j = 0; $j <= intval($pdf->getW() / $gridSize); $j++) {
1012 $pdf->line($j * $gridSize, $topSpace, $j * $gridSize, $pdf->getH() - $bottomSpace);
1013 $pdf->SetXY($j * $gridSize, $topSpace);
1014 $label = (string) sprintf('%.0f', ($j * $gridSize - $this->leftMargin) * $this->scale + $this->_xMin);
1015 $pdf->Cell($labelWidth, $labelHeight, $label);
1020 * Draws relation arrows
1022 * @param boolean changeColor Whether to use one color per relation or not
1023 * @access private
1024 * @see Relation_Stats::relationdraw()
1026 private function _drawRelations($changeColor)
1028 $i = 0;
1029 foreach ($this->relations as $relation) {
1030 $relation->relationDraw($changeColor, $i);
1031 $i++;
1036 * Draws tables
1038 * @param boolean changeColor Whether to display table position or not
1039 * @access private
1040 * @see Table_Stats::tableDraw()
1042 private function _drawTables($changeColor = 0)
1044 foreach ($this->tables as $table) {
1045 $table->tableDraw($this->_ff, $this->withDoc, $changeColor);
1050 * Ouputs the PDF document to a file
1051 * or sends the output to browser
1053 * @global object The current PDF document
1054 * @global string The current database name
1055 * @global integer The current page number (from the
1056 * $cfg['Servers'][$i]['table_coords'] table)
1057 * @access private
1058 * @see PMA_PDF
1060 private function _showOutput($pageNumber)
1062 global $pdf, $db, $cfgRelation;
1064 $pdf->SetFontSize(14);
1065 $pdf->SetLineWidth(0.2);
1066 $pdf->SetDisplayMode('fullpage');
1067 // Get the name of this pdfpage to use as filename (Mike Beck)
1068 $_name_sql = 'SELECT page_descr FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
1069 . ' WHERE page_nr = ' . $pageNumber;
1070 $_name_rs = PMA_query_as_controluser($_name_sql);
1071 if ($_name_rs) {
1072 $_name_row = PMA_DBI_fetch_row($_name_rs);
1073 $filename = $_name_row[0] . '.pdf';
1075 if (empty($filename)) {
1076 $filename = $pageNumber . '.pdf';
1078 // instead of $pdf->Output():
1079 $pdfData = $pdf->getPDFData();
1080 header('Content-Type: application/pdf');
1081 header('Content-Length: '.strlen($pdfData).'');
1082 header('Content-disposition: attachment; filename="'.$filename.'"');
1083 echo $pdfData;
1086 public function dataDictionaryDoc($alltables)
1088 global $db, $pdf, $orientation, $paper;
1089 // TOC
1090 $pdf->addpage($GLOBALS['orientation']);
1091 $pdf->Cell(0, 9, __('Table of contents'), 1, 0, 'C');
1092 $pdf->Ln(15);
1093 $i = 1;
1094 foreach ($alltables as $table) {
1095 $pdf->PMA_links['doc'][$table]['-'] = $pdf->AddLink();
1096 $pdf->SetX(10);
1097 // $pdf->Ln(1);
1098 $pdf->Cell(0, 6, __('Page number:') . ' {' . sprintf("%02d", $i + 1) . '}', 0, 0, 'R', 0, $pdf->PMA_links['doc'][$table]['-']);
1099 $pdf->SetX(10);
1100 $pdf->Cell(0, 6, $i . ' ' . $table, 0, 1, 'L', 0, $pdf->PMA_links['doc'][$table]['-']);
1101 // $pdf->Ln(1);
1102 $result = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';');
1103 while ($row = PMA_DBI_fetch_assoc($result)) {
1104 $pdf->SetX(20);
1105 $field_name = $row['Field'];
1106 $pdf->PMA_links['doc'][$table][$field_name] = $pdf->AddLink();
1107 // $pdf->Cell(0, 6, $field_name,0,1,'L',0, $pdf->PMA_links['doc'][$table][$field_name]);
1109 $lasttable = $table;
1110 $i++;
1112 $pdf->PMA_links['RT']['-'] = $pdf->AddLink();
1113 $pdf->SetX(10);
1114 $pdf->Cell(0, 6, __('Page number:') . ' {' . sprintf("%02d", $i + 1) . '}', 0, 0, 'R', 0, $pdf->PMA_links['RT']['-']);
1115 $pdf->SetX(10);
1116 $pdf->Cell(0, 6, $i . ' ' . __('Relational schema'), 0, 1, 'L', 0, $pdf->PMA_links['RT']['-']);
1117 $z = 0;
1118 foreach ($alltables as $table) {
1119 $z++;
1120 $pdf->SetAutoPageBreak(true, 15);
1121 $pdf->addpage($GLOBALS['orientation']);
1122 $pdf->Bookmark($table);
1123 $pdf->SetAlias('{' . sprintf("%02d", $z) . '}', $pdf->PageNo()) ;
1124 $pdf->PMA_links['RT'][$table]['-'] = $pdf->AddLink();
1125 $pdf->SetLink($pdf->PMA_links['doc'][$table]['-'], -1);
1126 $pdf->SetFont($this->_ff, 'B', 18);
1127 $pdf->Cell(0, 8, $z . ' ' . $table, 1, 1, 'C', 0, $pdf->PMA_links['RT'][$table]['-']);
1128 $pdf->SetFont($this->_ff, '', 8);
1129 $pdf->ln();
1131 $cfgRelation = PMA_getRelationsParam();
1132 $comments = PMA_getComments($db, $table);
1133 if ($cfgRelation['mimework']) {
1134 $mime_map = PMA_getMIME($db, $table, true);
1138 * Gets table informations
1140 $showtable = PMA_Table::sGetStatusInfo($db, $table);
1141 $num_rows = (isset($showtable['Rows']) ? $showtable['Rows'] : 0);
1142 $show_comment = (isset($showtable['Comment']) ? $showtable['Comment'] : '');
1143 $create_time = (isset($showtable['Create_time']) ? PMA_localisedDate(strtotime($showtable['Create_time'])) : '');
1144 $update_time = (isset($showtable['Update_time']) ? PMA_localisedDate(strtotime($showtable['Update_time'])) : '');
1145 $check_time = (isset($showtable['Check_time']) ? PMA_localisedDate(strtotime($showtable['Check_time'])) : '');
1148 * Gets table keys and retains them
1150 $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
1151 $primary = '';
1152 $indexes = array();
1153 $lastIndex = '';
1154 $indexes_info = array();
1155 $indexes_data = array();
1156 $pk_array = array(); // will be use to emphasis prim. keys in the table
1157 // view
1158 while ($row = PMA_DBI_fetch_assoc($result)) {
1159 // Backups the list of primary keys
1160 if ($row['Key_name'] == 'PRIMARY') {
1161 $primary .= $row['Column_name'] . ', ';
1162 $pk_array[$row['Column_name']] = 1;
1164 // Retains keys informations
1165 if ($row['Key_name'] != $lastIndex) {
1166 $indexes[] = $row['Key_name'];
1167 $lastIndex = $row['Key_name'];
1169 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
1170 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
1171 if (isset($row['Cardinality'])) {
1172 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
1174 // I don't know what does following column mean....
1175 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
1176 $indexes_info[$row['Key_name']]['Comment'] = $row['Comment'];
1178 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
1179 if (isset($row['Sub_part'])) {
1180 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
1182 } // end while
1183 if ($result) {
1184 PMA_DBI_free_result($result);
1188 * Gets fields properties
1190 $result = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE);
1191 $fields_cnt = PMA_DBI_num_rows($result);
1192 // Check if we can use Relations (Mike Beck)
1193 if (!empty($cfgRelation['relation'])) {
1194 // Find which tables are related with the current one and write it in
1195 // an array
1196 $res_rel = PMA_getForeigners($db, $table);
1198 if (count($res_rel) > 0) {
1199 $have_rel = true;
1200 } else {
1201 $have_rel = false;
1203 } else {
1204 $have_rel = false;
1205 } // end if
1208 * Displays the comments of the table if MySQL >= 3.23
1211 $break = false;
1212 if (!empty($show_comment)) {
1213 $pdf->Cell(0, 3, __('Table comments') . ' : ' . $show_comment, 0, 1);
1214 $break = true;
1217 if (!empty($create_time)) {
1218 $pdf->Cell(0, 3, __('Creation') . ': ' . $create_time, 0, 1);
1219 $break = true;
1222 if (!empty($update_time)) {
1223 $pdf->Cell(0, 3, __('Last update') . ': ' . $update_time, 0, 1);
1224 $break = true;
1227 if (!empty($check_time)) {
1228 $pdf->Cell(0, 3, __('Last check') . ': ' . $check_time, 0, 1);
1229 $break = true;
1232 if ($break == true) {
1233 $pdf->Cell(0, 3, '', 0, 1);
1234 $pdf->Ln();
1237 $pdf->SetFont($this->_ff, 'B');
1238 if (isset($orientation) && $orientation == 'L') {
1239 $pdf->Cell(25, 8, ucfirst(__('Column')), 1, 0, 'C');
1240 $pdf->Cell(20, 8, ucfirst(__('Type')), 1, 0, 'C');
1241 $pdf->Cell(20, 8, ucfirst(__('Attributes')), 1, 0, 'C');
1242 $pdf->Cell(10, 8, ucfirst(__('Null')), 1, 0, 'C');
1243 $pdf->Cell(20, 8, ucfirst(__('Default')), 1, 0, 'C');
1244 $pdf->Cell(25, 8, ucfirst(__('Extra')), 1, 0, 'C');
1245 $pdf->Cell(45, 8, ucfirst(__('Links to')), 1, 0, 'C');
1247 if ($paper == 'A4') {
1248 $comments_width = 67;
1249 } else {
1250 // this is really intended for 'letter'
1252 * @todo find optimal width for all formats
1254 $comments_width = 50;
1256 $pdf->Cell($comments_width, 8, ucfirst(__('Comments')), 1, 0, 'C');
1257 $pdf->Cell(45, 8, 'MIME', 1, 1, 'C');
1258 $pdf->SetWidths(array(25, 20, 20, 10, 20, 25, 45, $comments_width, 45));
1259 } else {
1260 $pdf->Cell(20, 8, ucfirst(__('Column')), 1, 0, 'C');
1261 $pdf->Cell(20, 8, ucfirst(__('Type')), 1, 0, 'C');
1262 $pdf->Cell(20, 8, ucfirst(__('Attributes')), 1, 0, 'C');
1263 $pdf->Cell(10, 8, ucfirst(__('Null')), 1, 0, 'C');
1264 $pdf->Cell(15, 8, ucfirst(__('Default')), 1, 0, 'C');
1265 $pdf->Cell(15, 8, ucfirst(__('Extra')), 1, 0, 'C');
1266 $pdf->Cell(30, 8, ucfirst(__('Links to')), 1, 0, 'C');
1267 $pdf->Cell(30, 8, ucfirst(__('Comments')), 1, 0, 'C');
1268 $pdf->Cell(30, 8, 'MIME', 1, 1, 'C');
1269 $pdf->SetWidths(array(20, 20, 20, 10, 15, 15, 30, 30, 30));
1271 $pdf->SetFont($this->_ff, '');
1273 while ($row = PMA_DBI_fetch_assoc($result)) {
1274 $type = $row['Type'];
1275 // reformat mysql query output
1276 // set or enum types: slashes single quotes inside options
1277 if (preg_match('@^(set|enum)\((.+)\)$@i', $type, $tmp)) {
1278 $tmp[2] = substr(preg_replace("@([^,])''@", "\\1\\'", ',' . $tmp[2]), 1);
1279 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
1280 $type_nowrap = '';
1282 $binary = 0;
1283 $unsigned = 0;
1284 $zerofill = 0;
1285 } else {
1286 $type_nowrap = ' nowrap="nowrap"';
1287 $type = preg_replace('@BINARY@i', '', $type);
1288 $type = preg_replace('@ZEROFILL@i', '', $type);
1289 $type = preg_replace('@UNSIGNED@i', '', $type);
1290 if (empty($type)) {
1291 $type = '&nbsp;';
1294 $binary = stristr($row['Type'], 'BINARY');
1295 $unsigned = stristr($row['Type'], 'UNSIGNED');
1296 $zerofill = stristr($row['Type'], 'ZEROFILL');
1298 $attribute = ' ';
1299 if ($binary) {
1300 $attribute = 'BINARY';
1302 if ($unsigned) {
1303 $attribute = 'UNSIGNED';
1305 if ($zerofill) {
1306 $attribute = 'UNSIGNED ZEROFILL';
1308 if (!isset($row['Default'])) {
1309 if ($row['Null'] != '' && $row['Null'] != 'NO') {
1310 $row['Default'] = 'NULL';
1313 $field_name = $row['Field'];
1314 // $pdf->Ln();
1315 $pdf->PMA_links['RT'][$table][$field_name] = $pdf->AddLink();
1316 $pdf->Bookmark($field_name, 1, -1);
1317 $pdf->SetLink($pdf->PMA_links['doc'][$table][$field_name], -1);
1318 $pdf_row = array($field_name,
1319 $type,
1320 $attribute,
1321 ($row['Null'] == '' || $row['Null'] == 'NO') ? __('No') : __('Yes'),
1322 ((isset($row['Default'])) ? $row['Default'] : ''),
1323 $row['Extra'],
1324 ((isset($res_rel[$field_name])) ? $res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field'] : ''),
1325 ((isset($comments[$field_name])) ? $comments[$field_name] : ''),
1326 ((isset($mime_map) && isset($mime_map[$field_name])) ? str_replace('_', '/', $mime_map[$field_name]['mimetype']) : '')
1328 $links[0] = $pdf->PMA_links['RT'][$table][$field_name];
1329 if (isset($res_rel[$field_name]['foreign_table']) AND
1330 isset($res_rel[$field_name]['foreign_field']) AND
1331 isset($pdf->PMA_links['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']])
1334 $links[6] = $pdf->PMA_links['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']];
1335 } else {
1336 unset($links[6]);
1338 $pdf->Row($pdf_row, $links);
1339 } // end while
1340 $pdf->SetFont($this->_ff, '', 14);
1341 PMA_DBI_free_result($result);
1342 } //end each