PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/phpmyadmin/pdf_schema.php

https://bitbucket.org/DenizYldrm/openemr
PHP | 1383 lines | 898 code | 69 blank | 416 comment | 162 complexity | 13d25a7a834b45362904aa6bdaccbcc9 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, MPL-2.0, LGPL-2.1
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Contributed by Maxime Delorme and merged by lem9
  5. *
  6. * @version $Id$
  7. */
  8. /**
  9. * Gets some core scripts
  10. */
  11. require_once './libraries/common.inc.php';
  12. /**
  13. * Settings for relation stuff
  14. */
  15. require_once './libraries/relation.lib.php';
  16. require_once './libraries/transformations.lib.php';
  17. $cfgRelation = PMA_getRelationsParam();
  18. /**
  19. * Now in ./libraries/relation.lib.php we check for all tables
  20. * that we need, but if we don't find them we are quiet about it
  21. * so people can work without.
  22. * This page is absolutely useless if you didn't set up your tables
  23. * correctly, so it is a good place to see which tables we can and
  24. * complain ;-)
  25. */
  26. if (!$cfgRelation['pdfwork']) {
  27. echo '<font color="red">' . $strError . '</font><br />' . "\n";
  28. $url_to_goto = '<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">';
  29. echo sprintf($strRelationNotWorking, $url_to_goto, '</a>') . "\n";
  30. }
  31. /**
  32. * Font used in PDF.
  33. *
  34. * @todo Make this configuratble (at least Sans/Serif).
  35. */
  36. define('PMA_PDF_FONT', 'DejaVuSans');
  37. require_once './libraries/tcpdf/tcpdf.php';
  38. /**
  39. * Extends the "FPDF" class and prepares the work
  40. *
  41. * @access public
  42. * @see FPDF
  43. */
  44. class PMA_PDF extends TCPDF {
  45. /**
  46. * Defines private properties
  47. */
  48. var $x_min;
  49. var $y_min;
  50. var $l_marg = 10;
  51. var $t_marg = 10;
  52. var $scale;
  53. var $title;
  54. var $PMA_links;
  55. var $Outlines = array();
  56. var $def_outlines;
  57. var $Alias ;
  58. var $widths;
  59. /**
  60. * The PMA_PDF constructor
  61. *
  62. * This function just refers to the "FPDF" constructor: with PHP3 a class
  63. * must have a constructor
  64. *
  65. * @param string $ The page orientation (p, portrait, l or landscape)
  66. * @param string $ The unit for sizes (pt, mm, cm or in)
  67. * @param mixed $ The page format (A3, A4, A5, letter, legal or an array
  68. * with page sizes)
  69. * @access public
  70. * @see FPDF::FPDF()
  71. */
  72. function PMA_PDF($orientation = 'L', $unit = 'mm', $format = 'A4')
  73. {
  74. $this->Alias = array() ;
  75. $this->TCPDF($orientation, $unit, $format);
  76. } // end of the "PMA_PDF()" method
  77. function SetAlias($name, $value)
  78. {
  79. $this->Alias[$name] = $value ;
  80. }
  81. function _putpages()
  82. {
  83. if (count($this->Alias) > 0) {
  84. $nb = $this->page;
  85. foreach ($this->Alias AS $alias => $value) {
  86. for ($n = 1;$n <= $nb;$n++)
  87. $this->pages[$n]=str_replace($alias, $value, $this->pages[$n]);
  88. }
  89. }
  90. parent::_putpages();
  91. }
  92. /**
  93. * Sets the scaling factor, defines minimum coordinates and margins
  94. *
  95. * @param double $ The scaling factor
  96. * @param double $ The minimum X coordinate
  97. * @param double $ The minimum Y coordinate
  98. * @param double $ The left margin
  99. * @param double $ The top margin
  100. * @access public
  101. */
  102. function PMA_PDF_setScale($scale = 1, $x_min = 0, $y_min = 0, $l_marg = -1, $t_marg = -1)
  103. {
  104. $this->scale = $scale;
  105. $this->x_min = $x_min;
  106. $this->y_min = $y_min;
  107. if ($this->l_marg != -1) {
  108. $this->l_marg = $l_marg;
  109. }
  110. if ($this->t_marg != -1) {
  111. $this->t_marg = $t_marg;
  112. }
  113. } // end of the "PMA_PDF_setScale" function
  114. /**
  115. * Outputs a scaled cell
  116. *
  117. * @param double $ The cell width
  118. * @param double $ The cell height
  119. * @param string $ The text to output
  120. * @param mixed $ Wether to add borders or not
  121. * @param integer $ Where to put the cursor once the output is done
  122. * @param string $ Align mode
  123. * @param integer $ Whether to fill the cell with a color or not
  124. * @access public
  125. * @see FPDF::Cell()
  126. */
  127. function PMA_PDF_cellScale($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $fill = 0, $link = '')
  128. {
  129. $h = $h / $this->scale;
  130. $w = $w / $this->scale;
  131. $this->Cell($w, $h, $txt, $border, $ln, $align, $fill, $link);
  132. } // end of the "PMA_PDF_cellScale" function
  133. /**
  134. * Draws a scaled line
  135. *
  136. * @param double $ The horizontal position of the starting point
  137. * @param double $ The vertical position of the starting point
  138. * @param double $ The horizontal position of the ending point
  139. * @param double $ The vertical position of the ending point
  140. * @access public
  141. * @see FPDF::Line()
  142. */
  143. function PMA_PDF_lineScale($x1, $y1, $x2, $y2)
  144. {
  145. $x1 = ($x1 - $this->x_min) / $this->scale + $this->l_marg;
  146. $y1 = ($y1 - $this->y_min) / $this->scale + $this->t_marg;
  147. $x2 = ($x2 - $this->x_min) / $this->scale + $this->l_marg;
  148. $y2 = ($y2 - $this->y_min) / $this->scale + $this->t_marg;
  149. $this->Line($x1, $y1, $x2, $y2);
  150. } // end of the "PMA_PDF_lineScale" function
  151. /**
  152. * Sets x and y scaled positions
  153. *
  154. * @param double $ The x position
  155. * @param double $ The y position
  156. * @access public
  157. * @see FPDF::SetXY()
  158. */
  159. function PMA_PDF_setXyScale($x, $y)
  160. {
  161. $x = ($x - $this->x_min) / $this->scale + $this->l_marg;
  162. $y = ($y - $this->y_min) / $this->scale + $this->t_marg;
  163. $this->SetXY($x, $y);
  164. } // end of the "PMA_PDF_setXyScale" function
  165. /**
  166. * Sets the X scaled positions
  167. *
  168. * @param double $ The x position
  169. * @access public
  170. * @see FPDF::SetX()
  171. */
  172. function PMA_PDF_setXScale($x)
  173. {
  174. $x = ($x - $this->x_min) / $this->scale + $this->l_marg;
  175. $this->SetX($x);
  176. } // end of the "PMA_PDF_setXScale" function
  177. /**
  178. * Sets the scaled font size
  179. *
  180. * @param double $ The font size (in points)
  181. * @access public
  182. * @see FPDF::SetFontSize()
  183. */
  184. function PMA_PDF_setFontSizeScale($size)
  185. {
  186. // Set font size in points
  187. $size = $size / $this->scale;
  188. $this->SetFontSize($size);
  189. } // end of the "PMA_PDF_setFontSizeScale" function
  190. /**
  191. * Sets the scaled line width
  192. *
  193. * @param double $ The line width
  194. * @access public
  195. * @see FPDF::SetLineWidth()
  196. */
  197. function PMA_PDF_setLineWidthScale($width)
  198. {
  199. $width = $width / $this->scale;
  200. $this->SetLineWidth($width);
  201. } // end of the "PMA_PDF_setLineWidthScale" function
  202. /**
  203. * Displays an error message
  204. *
  205. * @param string $ the error mesage
  206. * @global array the PMA configuration array
  207. * @global integer the current server id
  208. * @global string the current language
  209. * @global string the charset to convert to
  210. * @global string the current database name
  211. * @global string the current charset
  212. * @global string the current text direction
  213. * @global string a localized string
  214. * @global string an other localized string
  215. * @access public
  216. */
  217. function PMA_PDF_die($error_message = '')
  218. {
  219. global $cfg;
  220. global $server, $lang, $convcharset, $db;
  221. global $charset, $text_dir, $strRunning, $strDatabase;
  222. require_once './libraries/header.inc.php';
  223. echo '<p><b>PDF - ' . $GLOBALS['strError'] . '</b></p>' . "\n";
  224. if (!empty($error_message)) {
  225. $error_message = htmlspecialchars($error_message);
  226. }
  227. echo '<p>' . "\n";
  228. echo ' ' . $error_message . "\n";
  229. echo '</p>' . "\n";
  230. echo '<a href="db_structure.php?' . PMA_generate_common_url($db)
  231. . '">' . $GLOBALS['strBack'] . '</a>';
  232. echo "\n";
  233. require_once './libraries/footer.inc.php';
  234. } // end of the "PMA_PDF_die()" function
  235. /**
  236. * Aliases the "Error()" function from the FPDF class to the
  237. * "PMA_PDF_die()" one
  238. *
  239. * @param string $ the error mesage
  240. * @access public
  241. * @see PMA_PDF_die
  242. */
  243. function Error($error_message = '')
  244. {
  245. $this->PMA_PDF_die($error_message);
  246. } // end of the "Error()" method
  247. function Header()
  248. {
  249. // $datefmt
  250. // We only show this if we find something in the new pdf_pages table
  251. // This function must be named "Header" to work with the FPDF library
  252. global $cfgRelation, $db, $pdf_page_number, $with_doc;
  253. if ($with_doc) {
  254. $test_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
  255. . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
  256. . ' AND page_nr = \'' . $pdf_page_number . '\'';
  257. $test_rs = PMA_query_as_cu($test_query);
  258. $pages = @PMA_DBI_fetch_assoc($test_rs);
  259. $this->SetFont('', 'B', 14);
  260. $this->Cell(0, 6, ucfirst($pages['page_descr']), 'B', 1, 'C');
  261. $this->SetFont('', '');
  262. $this->Ln();
  263. }
  264. }
  265. function Footer()
  266. {
  267. // This function must be named "Footer" to work with the FPDF library
  268. global $with_doc;
  269. if ($with_doc) {
  270. $this->SetY(-15);
  271. $this->SetFont('', '', 14);
  272. $this->Cell(0, 6, $GLOBALS['strPageNumber'] . ' ' . $this->PageNo() . '/{nb}', 'T', 0, 'C');
  273. $this->Cell(0, 6, PMA_localisedDate(), 0, 1, 'R');
  274. $this->SetY(20);
  275. }
  276. }
  277. function Bookmark($txt, $level = 0, $y = 0)
  278. {
  279. // Add a bookmark
  280. $this->Outlines[0][] = $level;
  281. $this->Outlines[1][] = $txt;
  282. $this->Outlines[2][] = $this->page;
  283. if ($y == -1) {
  284. $y = $this->GetY();
  285. }
  286. $this->Outlines[3][] = round($this->hPt - $y * $this->k, 2);
  287. }
  288. function _putbookmarks()
  289. {
  290. if (count($this->Outlines) > 0) {
  291. // Save object number
  292. $memo_n = $this->n;
  293. // Take the number of sub elements for an outline
  294. $nb_outlines = sizeof($this->Outlines[0]);
  295. $first_level = array();
  296. $parent = array();
  297. $parent[0] = 1;
  298. for ($i = 0; $i < $nb_outlines; $i++) {
  299. $level = $this->Outlines[0][$i];
  300. $kids = 0;
  301. $last = -1;
  302. $prev = -1;
  303. $next = -1;
  304. if ($i > 0) {
  305. $cursor = $i-1;
  306. // Take the previous outline in the same level
  307. while ($this->Outlines[0][$cursor] > $level && $cursor > 0)
  308. $cursor--;
  309. if ($this->Outlines[0][$cursor] == $level) {
  310. $prev = $cursor;
  311. }
  312. }
  313. if ($i < $nb_outlines-1) {
  314. $cursor = $i + 1;
  315. while (isset($this->Outlines[0][$cursor]) && $this->Outlines[0][$cursor] > $level) {
  316. // Take the immediate kid in level + 1
  317. if ($this->Outlines[0][$cursor] == $level + 1) {
  318. $kids++;
  319. $last = $cursor;
  320. }
  321. $cursor++;
  322. }
  323. $cursor = $i + 1;
  324. // Take the next outline in the same level
  325. while ($this->Outlines[0][$cursor] > $level && ($cursor + 1 < sizeof($this->Outlines[0])))
  326. $cursor++;
  327. if ($this->Outlines[0][$cursor] == $level) {
  328. $next = $cursor;
  329. }
  330. }
  331. $this->_newobj();
  332. $parent[$level + 1] = $this->n;
  333. if ($level == 0) {
  334. $first_level[] = $this->n;
  335. }
  336. $this->_out('<<');
  337. $this->_out('/Title (' . $this->Outlines[1][$i] . ')');
  338. $this->_out('/Parent ' . $parent[$level] . ' 0 R');
  339. if ($prev != -1) {
  340. $this->_out('/Prev ' . ($memo_n + $prev + 1) . ' 0 R');
  341. }
  342. if ($next != -1) {
  343. $this->_out('/Next ' . ($this->n + $next - $i) . ' 0 R');
  344. }
  345. $this->_out('/Dest [' . (1 + (2 * $this->Outlines[2][$i])) . ' 0 R /XYZ null ' . $this->Outlines[3][$i] . ' null]');
  346. if ($kids > 0) {
  347. $this->_out('/First ' . ($this->n + 1) . ' 0 R');
  348. $this->_out('/Last ' . ($this->n + $last - $i) . ' 0 R');
  349. $this->_out('/Count -' . $kids);
  350. }
  351. $this->_out('>>');
  352. $this->_out('endobj');
  353. }
  354. // First page of outlines
  355. $this->_newobj();
  356. $this->def_outlines = $this->n;
  357. $this->_out('<<');
  358. $this->_out('/Type');
  359. $this->_out('/Outlines');
  360. $this->_out('/First ' . $first_level[0] . ' 0 R');
  361. $this->_out('/Last ' . $first_level[sizeof($first_level)-1] . ' 0 R');
  362. $this->_out('/Count ' . sizeof($first_level));
  363. $this->_out('>>');
  364. $this->_out('endobj');
  365. }
  366. }
  367. function _putresources()
  368. {
  369. parent::_putresources();
  370. $this->_putbookmarks();
  371. }
  372. function _putcatalog()
  373. {
  374. parent::_putcatalog();
  375. if (count($this->Outlines) > 0) {
  376. $this->_out('/Outlines ' . $this->def_outlines . ' 0 R');
  377. $this->_out('/PageMode /UseOutlines');
  378. }
  379. }
  380. function SetWidths($w)
  381. {
  382. // column widths
  383. $this->widths = $w;
  384. }
  385. function Row($data, $links)
  386. {
  387. // line height
  388. $nb = 0;
  389. $data_cnt = count($data);
  390. for ($i = 0;$i < $data_cnt;$i++)
  391. $nb = max($nb, $this->NbLines($this->widths[$i], $data[$i]));
  392. $il = $this->FontSize;
  393. $h = ($il + 1) * $nb;
  394. // page break if necessary
  395. $this->CheckPageBreak($h);
  396. // draw the cells
  397. $data_cnt = count($data);
  398. for ($i = 0;$i < $data_cnt;$i++) {
  399. $w = $this->widths[$i];
  400. // save current position
  401. $x = $this->GetX();
  402. $y = $this->GetY();
  403. // draw the border
  404. $this->Rect($x, $y, $w, $h);
  405. if (isset($links[$i])) {
  406. $this->Link($x, $y, $w, $h, $links[$i]);
  407. }
  408. // print text
  409. $this->MultiCell($w, $il + 1, $data[$i], 0, 'L');
  410. // go to right side
  411. $this->SetXY($x + $w, $y);
  412. }
  413. // go to line
  414. $this->Ln($h);
  415. }
  416. function CheckPageBreak($h)
  417. {
  418. // if height h overflows, manual page break
  419. if ($this->GetY() + $h > $this->PageBreakTrigger) {
  420. $this->AddPage($this->CurOrientation);
  421. }
  422. }
  423. function NbLines($w, $txt)
  424. {
  425. // compute number of lines used by a multicell of width w
  426. $cw = &$this->CurrentFont['cw'];
  427. if ($w == 0) {
  428. $w = $this->w - $this->rMargin - $this->x;
  429. }
  430. $wmax = ($w-2 * $this->cMargin) * 1000 / $this->FontSize;
  431. $s = str_replace("\r", '', $txt);
  432. $nb = strlen($s);
  433. if ($nb > 0 and $s[$nb-1] == "\n") {
  434. $nb--;
  435. }
  436. $sep = -1;
  437. $i = 0;
  438. $j = 0;
  439. $l = 0;
  440. $nl = 1;
  441. while ($i < $nb) {
  442. $c = $s[$i];
  443. if ($c == "\n") {
  444. $i++;
  445. $sep = -1;
  446. $j = $i;
  447. $l = 0;
  448. $nl++;
  449. continue;
  450. }
  451. if ($c == ' ') {
  452. $sep = $i;
  453. }
  454. $l += isset($cw[ord($c)])?$cw[ord($c)]:0 ;
  455. if ($l > $wmax) {
  456. if ($sep == -1) {
  457. if ($i == $j) {
  458. $i++;
  459. }
  460. } else {
  461. $i = $sep + 1;
  462. }
  463. $sep = -1;
  464. $j = $i;
  465. $l = 0;
  466. $nl++;
  467. } else {
  468. $i++;
  469. }
  470. }
  471. return $nl;
  472. }
  473. } // end of the "PMA_PDF" class
  474. /**
  475. * Draws tables schema
  476. *
  477. * @access private
  478. * @see PMA_RT
  479. */
  480. class PMA_RT_Table {
  481. /**
  482. * Defines private properties
  483. */
  484. var $nb_fiels;
  485. var $table_name;
  486. var $width = 0;
  487. var $height;
  488. var $fields = array();
  489. var $height_cell = 6;
  490. var $x, $y;
  491. var $primary = array();
  492. /**
  493. * Sets the width of the table
  494. *
  495. * @param integer $ The font size
  496. * @global object The current PDF document
  497. * @access private
  498. * @see PMA_PDF
  499. */
  500. function PMA_RT_Table_setWidth($ff)
  501. {
  502. // this looks buggy to me... does it really work if
  503. // there are fields that require wider cells than the name of the table?
  504. global $pdf;
  505. foreach ($this->fields AS $field) {
  506. $this->width = max($this->width, $pdf->GetStringWidth($field));
  507. }
  508. $this->width += $pdf->GetStringWidth(' ');
  509. $pdf->SetFont($ff, 'B');
  510. $this->width = max($this->width, $pdf->GetStringWidth(' ' . $this->table_name));
  511. $pdf->SetFont($ff, '');
  512. } // end of the "PMA_RT_Table_setWidth()" method
  513. /**
  514. * Sets the height of the table
  515. *
  516. * @access private
  517. */
  518. function PMA_RT_Table_setHeight()
  519. {
  520. $this->height = (count($this->fields) + 1) * $this->height_cell;
  521. } // end of the "PMA_RT_Table_setHeight()" method
  522. /**
  523. * Do draw the table
  524. *
  525. * @param boolean $ Whether to display table position or not
  526. * @param integer $ The font size
  527. * @param boolean $ Whether to display color
  528. * @param integer $ The max. with among tables
  529. * @global object The current PDF document
  530. * @access private
  531. * @see PMA_PDF
  532. */
  533. function PMA_RT_Table_draw($show_info, $ff, $setcolor = 0)
  534. {
  535. global $pdf, $with_doc;
  536. $pdf->PMA_PDF_setXyScale($this->x, $this->y);
  537. $pdf->SetFont($ff, 'B');
  538. if ($setcolor) {
  539. $pdf->SetTextColor(200);
  540. $pdf->SetFillColor(0, 0, 128);
  541. }
  542. if ($with_doc) {
  543. $pdf->SetLink($pdf->PMA_links['RT'][$this->table_name]['-'], -1);
  544. } else {
  545. $pdf->PMA_links['doc'][$this->table_name]['-'] = '';
  546. }
  547. if ($show_info) {
  548. $pdf->PMA_PDF_cellScale($this->width, $this->height_cell, sprintf('%.0f', $this->width) . 'x' . sprintf('%.0f', $this->height) . ' ' . $this->table_name, 1, 1, 'C', $setcolor, $pdf->PMA_links['doc'][$this->table_name]['-']);
  549. } else {
  550. $pdf->PMA_PDF_cellScale($this->width, $this->height_cell, $this->table_name, 1, 1, 'C', $setcolor, $pdf->PMA_links['doc'][$this->table_name]['-']);
  551. }
  552. $pdf->PMA_PDF_setXScale($this->x);
  553. $pdf->SetFont($ff, '');
  554. $pdf->SetTextColor(0);
  555. $pdf->SetFillColor(255);
  556. foreach ($this->fields AS $field) {
  557. // loic1 : PHP3 fix
  558. // if (in_array($field, $this->primary)) {
  559. if ($setcolor) {
  560. if (in_array($field, $this->primary)) {
  561. $pdf->SetFillColor(215, 121, 123);
  562. }
  563. if ($field == $this->displayfield) {
  564. $pdf->SetFillColor(142, 159, 224);
  565. }
  566. }
  567. if ($with_doc) {
  568. $pdf->SetLink($pdf->PMA_links['RT'][$this->table_name][$field], -1);
  569. } else {
  570. $pdf->PMA_links['doc'][$this->table_name][$field] = '';
  571. }
  572. $pdf->PMA_PDF_cellScale($this->width, $this->height_cell, ' ' . $field, 1, 1, 'L', $setcolor, $pdf->PMA_links['doc'][$this->table_name][$field]);
  573. $pdf->PMA_PDF_setXScale($this->x);
  574. $pdf->SetFillColor(255);
  575. } // end while
  576. /*if ($pdf->PageNo() > 1) {
  577. $pdf->PMA_PDF_die($GLOBALS['strScaleFactorSmall']);
  578. } */
  579. } // end of the "PMA_RT_Table_draw()" method
  580. /**
  581. * The "PMA_RT_Table" constructor
  582. *
  583. * @param string $ The table name
  584. * @param integer $ The font size
  585. * @param integer $ The max. with among tables
  586. * @global object The current PDF document
  587. * @global integer The current page number (from the
  588. * $cfg['Servers'][$i]['table_coords'] table)
  589. * @global array The relations settings
  590. * @global string The current db name
  591. * @access private
  592. * @see PMA_PDF, PMA_RT_Table::PMA_RT_Table_setWidth,
  593. PMA_RT_Table::PMA_RT_Table_setHeight
  594. */
  595. function PMA_RT_Table($table_name, $ff, &$same_wide_width)
  596. {
  597. global $pdf, $pdf_page_number, $cfgRelation, $db;
  598. $this->table_name = $table_name;
  599. $sql = 'DESCRIBE ' . PMA_backquote($table_name);
  600. $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE);
  601. if (!$result || !PMA_DBI_num_rows($result)) {
  602. $pdf->PMA_PDF_die(sprintf($GLOBALS['strPdfInvalidTblName'], $table_name));
  603. }
  604. // load fields
  605. while ($row = PMA_DBI_fetch_row($result)) {
  606. $this->fields[] = $row[0];
  607. }
  608. // height and width
  609. $this->PMA_RT_Table_setWidth($ff);
  610. $this->PMA_RT_Table_setHeight();
  611. if ($same_wide_width < $this->width) {
  612. $same_wide_width = $this->width;
  613. }
  614. // x and y
  615. $sql = 'SELECT x, y FROM '
  616. . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
  617. . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
  618. . ' AND table_name = \'' . PMA_sqlAddslashes($table_name) . '\''
  619. . ' AND pdf_page_number = ' . $pdf_page_number;
  620. $result = PMA_query_as_cu($sql, false, PMA_DBI_QUERY_STORE);
  621. if (!$result || !PMA_DBI_num_rows($result)) {
  622. $pdf->PMA_PDF_die(sprintf($GLOBALS['strConfigureTableCoord'], $table_name));
  623. }
  624. list($this->x, $this->y) = PMA_DBI_fetch_row($result);
  625. $this->x = (double) $this->x;
  626. $this->y = (double) $this->y;
  627. // displayfield
  628. $this->displayfield = PMA_getDisplayField($db, $table_name);
  629. // index
  630. $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($table_name) . ';', null, PMA_DBI_QUERY_STORE);
  631. if (PMA_DBI_num_rows($result) > 0) {
  632. while ($row = PMA_DBI_fetch_assoc($result)) {
  633. if ($row['Key_name'] == 'PRIMARY') {
  634. $this->primary[] = $row['Column_name'];
  635. }
  636. }
  637. } // end if
  638. } // end of the "PMA_RT_Table()" method
  639. } // end class "PMA_RT_Table"
  640. /**
  641. * Draws relation links
  642. *
  643. * @access private
  644. * @see PMA_RT
  645. */
  646. class PMA_RT_Relation {
  647. /**
  648. * Defines private properties
  649. */
  650. var $x_src, $y_src;
  651. var $src_dir ;
  652. var $dest_dir;
  653. var $x_dest, $y_dest;
  654. var $w_tick = 5;
  655. /**
  656. * Gets arrows coordinates
  657. *
  658. * @param string $ The current table name
  659. * @param string $ The relation column name
  660. * @return array Arrows coordinates
  661. * @access private
  662. */
  663. function PMA_RT_Relation_getXy($table, $column)
  664. {
  665. $pos = array_search($column, $table->fields);
  666. // x_left, x_right, y
  667. return array($table->x, $table->x + + $table->width, $table->y + ($pos + 1.5) * $table->height_cell);
  668. } // end of the "PMA_RT_Relation_getXy()" method
  669. /**
  670. * Do draws relation links
  671. *
  672. * @param boolean $ Whether to use one color per relation or not
  673. * @param integer $ The id of the link to draw
  674. * @global object The current PDF document
  675. * @access private
  676. * @see PMA_PDF
  677. */
  678. function PMA_RT_Relation_draw($change_color, $i)
  679. {
  680. global $pdf;
  681. if ($change_color) {
  682. $d = $i % 6;
  683. $j = ($i - $d) / 6;
  684. $j = $j % 4;
  685. $j++;
  686. $case = array(
  687. array(1, 0, 0),
  688. array(0, 1, 0),
  689. array(0, 0, 1),
  690. array(1, 1, 0),
  691. array(1, 0, 1),
  692. array(0, 1, 1)
  693. );
  694. list ($a, $b, $c) = $case[$d];
  695. $e = (1 - ($j - 1) / 6);
  696. $pdf->SetDrawColor($a * 255 * $e, $b * 255 * $e, $c * 255 * $e);
  697. } else {
  698. $pdf->SetDrawColor(0);
  699. } // end if... else...
  700. $pdf->PMA_PDF_setLineWidthScale(0.2);
  701. $pdf->PMA_PDF_lineScale($this->x_src, $this->y_src, $this->x_src + $this->src_dir * $this->w_tick, $this->y_src);
  702. $pdf->PMA_PDF_lineScale($this->x_dest + $this->dest_dir * $this->w_tick, $this->y_dest, $this->x_dest, $this->y_dest);
  703. $pdf->PMA_PDF_setLineWidthScale(0.1);
  704. $pdf->PMA_PDF_lineScale($this->x_src + $this->src_dir * $this->w_tick, $this->y_src, $this->x_dest + $this->dest_dir * $this->w_tick, $this->y_dest);
  705. // arrow
  706. $root2 = 2 * sqrt(2);
  707. $pdf->PMA_PDF_lineScale($this->x_src + $this->src_dir * $this->w_tick * 0.75, $this->y_src, $this->x_src + $this->src_dir * (0.75 - 1 / $root2) * $this->w_tick, $this->y_src + $this->w_tick / $root2);
  708. $pdf->PMA_PDF_lineScale($this->x_src + $this->src_dir * $this->w_tick * 0.75, $this->y_src, $this->x_src + $this->src_dir * (0.75 - 1 / $root2) * $this->w_tick, $this->y_src - $this->w_tick / $root2);
  709. $pdf->PMA_PDF_lineScale($this->x_dest + $this->dest_dir * $this->w_tick / 2, $this->y_dest, $this->x_dest + $this->dest_dir * (0.5 + 1 / $root2) * $this->w_tick, $this->y_dest + $this->w_tick / $root2);
  710. $pdf->PMA_PDF_lineScale($this->x_dest + $this->dest_dir * $this->w_tick / 2, $this->y_dest, $this->x_dest + $this->dest_dir * (0.5 + 1 / $root2) * $this->w_tick, $this->y_dest - $this->w_tick / $root2);
  711. $pdf->SetDrawColor(0);
  712. } // end of the "PMA_RT_Relation_draw()" method
  713. /**
  714. * The "PMA_RT_Relation" constructor
  715. *
  716. * @param string $ The master table name
  717. * @param string $ The relation field in the master table
  718. * @param string $ The foreign table name
  719. * @param string $ The relation field in the foreign table
  720. * @access private
  721. * @see PMA_RT_Relation::PMA_RT_Relation_getXy
  722. */
  723. function PMA_RT_Relation($master_table, $master_field, $foreign_table, $foreign_field)
  724. {
  725. $src_pos = $this->PMA_RT_Relation_getXy($master_table, $master_field);
  726. $dest_pos = $this->PMA_RT_Relation_getXy($foreign_table, $foreign_field);
  727. $src_left = $src_pos[0] - $this->w_tick;
  728. $src_right = $src_pos[1] + $this->w_tick;
  729. $dest_left = $dest_pos[0] - $this->w_tick;
  730. $dest_right = $dest_pos[1] + $this->w_tick;
  731. $d1 = abs($src_left - $dest_left);
  732. $d2 = abs($src_right - $dest_left);
  733. $d3 = abs($src_left - $dest_right);
  734. $d4 = abs($src_right - $dest_right);
  735. $d = min($d1, $d2, $d3, $d4);
  736. if ($d == $d1) {
  737. $this->x_src = $src_pos[0];
  738. $this->src_dir = -1;
  739. $this->x_dest = $dest_pos[0];
  740. $this->dest_dir = -1;
  741. } elseif ($d == $d2) {
  742. $this->x_src = $src_pos[1];
  743. $this->src_dir = 1;
  744. $this->x_dest = $dest_pos[0];
  745. $this->dest_dir = -1;
  746. } elseif ($d == $d3) {
  747. $this->x_src = $src_pos[0];
  748. $this->src_dir = -1;
  749. $this->x_dest = $dest_pos[1];
  750. $this->dest_dir = 1;
  751. } else {
  752. $this->x_src = $src_pos[1];
  753. $this->src_dir = 1;
  754. $this->x_dest = $dest_pos[1];
  755. $this->dest_dir = 1;
  756. }
  757. $this->y_src = $src_pos[2];
  758. $this->y_dest = $dest_pos[2];
  759. } // end of the "PMA_RT_Relation()" method
  760. } // end of the "PMA_RT_Relation" class
  761. /**
  762. * Draws and send the database schema
  763. *
  764. * @access public
  765. * @see PMA_PDF
  766. */
  767. class PMA_RT {
  768. /**
  769. * Defines private properties
  770. */
  771. var $tables = array();
  772. var $relations = array();
  773. var $ff = PMA_PDF_FONT;
  774. var $x_max = 0;
  775. var $y_max = 0;
  776. var $scale;
  777. var $x_min = 100000;
  778. var $y_min = 100000;
  779. var $t_marg = 10;
  780. var $b_marg = 10;
  781. var $l_marg = 10;
  782. var $r_marg = 10;
  783. var $tablewidth;
  784. var $same_wide = 0;
  785. /**
  786. * Sets X and Y minimum and maximum for a table cell
  787. *
  788. * @param string $ The table name
  789. * @access private
  790. */
  791. function PMA_RT_setMinMax($table)
  792. {
  793. $this->x_max = max($this->x_max, $table->x + $table->width);
  794. $this->y_max = max($this->y_max, $table->y + $table->height);
  795. $this->x_min = min($this->x_min, $table->x);
  796. $this->y_min = min($this->y_min, $table->y);
  797. } // end of the "PMA_RT_setMinMax()" method
  798. /**
  799. * Defines relation objects
  800. *
  801. * @param string $ The master table name
  802. * @param string $ The relation field in the master table
  803. * @param string $ The foreign table name
  804. * @param string $ The relation field in the foreign table
  805. * @access private
  806. * @see PMA_RT_setMinMax
  807. */
  808. function PMA_RT_addRelation($master_table, $master_field, $foreign_table, $foreign_field)
  809. {
  810. if (!isset($this->tables[$master_table])) {
  811. $this->tables[$master_table] = new PMA_RT_Table($master_table, $this->ff, $this->tablewidth);
  812. $this->PMA_RT_setMinMax($this->tables[$master_table]);
  813. }
  814. if (!isset($this->tables[$foreign_table])) {
  815. $this->tables[$foreign_table] = new PMA_RT_Table($foreign_table, $this->ff, $this->tablewidth);
  816. $this->PMA_RT_setMinMax($this->tables[$foreign_table]);
  817. }
  818. $this->relations[] = new PMA_RT_Relation($this->tables[$master_table], $master_field, $this->tables[$foreign_table], $foreign_field);
  819. } // end of the "PMA_RT_addRelation()" method
  820. /**
  821. * Draws the grid
  822. *
  823. * @global object the current PMA_PDF instance
  824. * @access private
  825. * @see PMA_PDF
  826. */
  827. function PMA_RT_strokeGrid()
  828. {
  829. global $pdf;
  830. $pdf->SetMargins(0, 0);
  831. $pdf->SetDrawColor(200, 200, 200);
  832. // Draws horizontal lines
  833. for ($l = 0; $l < 21; $l++) {
  834. $pdf->line(0, $l * 10, $pdf->fh, $l * 10);
  835. // Avoid duplicates
  836. if ($l > 0) {
  837. $pdf->SetXY(0, $l * 10);
  838. $label = (string) sprintf('%.0f', ($l * 10 - $this->t_marg) * $this->scale + $this->y_min);
  839. $pdf->Cell(5, 5, ' ' . $label);
  840. } // end if
  841. } // end for
  842. // Draws vertical lines
  843. for ($j = 0; $j < 30 ;$j++) {
  844. $pdf->line($j * 10, 0, $j * 10, $pdf->fw);
  845. $pdf->SetXY($j * 10, 0);
  846. $label = (string) sprintf('%.0f', ($j * 10 - $this->l_marg) * $this->scale + $this->x_min);
  847. $pdf->Cell(5, 7, $label);
  848. } // end for
  849. } // end of the "PMA_RT_strokeGrid()" method
  850. /**
  851. * Draws relation arrows
  852. *
  853. * @param boolean $ Whether to use one color per relation or not
  854. * @access private
  855. * @see PMA_RT_Relation::PMA_RT_Relation_draw()
  856. */
  857. function PMA_RT_drawRelations($change_color)
  858. {
  859. $i = 0;
  860. foreach ($this->relations AS $relation) {
  861. $relation->PMA_RT_Relation_draw($change_color, $i);
  862. $i++;
  863. } // end while
  864. } // end of the "PMA_RT_drawRelations()" method
  865. /**
  866. * Draws tables
  867. *
  868. * @param boolean $ Whether to display table position or not
  869. * @access private
  870. * @see PMA_RT_Table::PMA_RT_Table_draw()
  871. */
  872. function PMA_RT_drawTables($show_info, $draw_color = 0)
  873. {
  874. foreach ($this->tables AS $table) {
  875. $table->PMA_RT_Table_draw($show_info, $this->ff, $draw_color);
  876. }
  877. } // end of the "PMA_RT_drawTables()" method
  878. /**
  879. * Ouputs the PDF document to a file
  880. *
  881. * @global object The current PDF document
  882. * @global string The current database name
  883. * @global integer The current page number (from the
  884. * $cfg['Servers'][$i]['table_coords'] table)
  885. * @access private
  886. * @see PMA_PDF
  887. */
  888. function PMA_RT_showRt()
  889. {
  890. global $pdf, $db, $pdf_page_number, $cfgRelation;
  891. $pdf->SetFontSize(14);
  892. $pdf->SetLineWidth(0.2);
  893. $pdf->SetDisplayMode('fullpage');
  894. // Get the name of this pdfpage to use as filename (Mike Beck)
  895. $_name_sql = 'SELECT page_descr FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
  896. . ' WHERE page_nr = ' . $pdf_page_number;
  897. $_name_rs = PMA_query_as_cu($_name_sql);
  898. if ($_name_rs) {
  899. $_name_row = PMA_DBI_fetch_row($_name_rs);
  900. $filename = $_name_row[0] . '.pdf';
  901. }
  902. // i don't know if there is a chance for this to happen, but rather be on the safe side:
  903. if (empty($filename)) {
  904. $filename = $pdf_page_number . '.pdf';
  905. }
  906. // $pdf->Output($db . '_' . $filename, TRUE);
  907. $pdf->Output($db . '_' . $filename, 'I'); // destination: Inline
  908. } // end of the "PMA_RT_showRt()" method
  909. /**
  910. * The "PMA_RT" constructor
  911. *
  912. * @param mixed $ The scaling factor
  913. * @param integer $ The page number to draw (from the
  914. * $cfg['Servers'][$i]['table_coords'] table)
  915. * @param boolean $ Whether to display table position or not
  916. * @param boolean $ Was originally whether to use one color per
  917. * relation or not, now enables/disables color
  918. * everywhere, due to some problems printing with color
  919. * @param boolean $ Whether to draw grids or not
  920. * @param boolean $ Whether all tables should have the same width or not
  921. * @global object The current PDF document
  922. * @global string The current db name
  923. * @global array The relations settings
  924. * @access private
  925. * @see PMA_PDF
  926. */
  927. function PMA_RT($which_rel, $show_info = 0, $change_color = 0, $show_grid = 0, $all_tab_same_wide = 0, $orientation = 'L', $paper = 'A4')
  928. {
  929. global $pdf, $db, $cfgRelation, $with_doc;
  930. $this->same_wide = $all_tab_same_wide;
  931. // Initializes a new document
  932. $pdf = new PMA_PDF('L', 'mm', $paper);
  933. $pdf->title = sprintf($GLOBALS['strPdfDbSchema'], $GLOBALS['db'], $which_rel);
  934. $pdf->cMargin = 0;
  935. $pdf->Open();
  936. $pdf->SetTitle($pdf->title);
  937. $pdf->SetAuthor('phpMyAdmin ' . PMA_VERSION);
  938. $pdf->AliasNbPages();
  939. $pdf->AddFont('DejaVuSans', '', 'dejavusans.php');
  940. $pdf->AddFont('DejaVuSans', 'B', 'dejavusans-bold.php');
  941. $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php');
  942. $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserif-bold.php');
  943. $this->ff = PMA_PDF_FONT;
  944. $pdf->SetFont($this->ff, '', 14);
  945. $pdf->SetAutoPageBreak('auto');
  946. // Gets tables on this page
  947. $tab_sql = 'SELECT table_name FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
  948. . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
  949. . ' AND pdf_page_number = ' . $which_rel;
  950. $tab_rs = PMA_query_as_cu($tab_sql, null, PMA_DBI_QUERY_STORE);
  951. if (!$tab_rs || !PMA_DBI_num_rows($tab_rs) > 0) {
  952. $pdf->PMA_PDF_die($GLOBALS['strPdfNoTables']);
  953. // die('No tables');
  954. } while ($curr_table = @PMA_DBI_fetch_assoc($tab_rs)) {
  955. $alltables[] = PMA_sqlAddslashes($curr_table['table_name']);
  956. // $intable = '\'' . implode('\', \'', $alltables) . '\'';
  957. }
  958. // make doc //
  959. if ($with_doc) {
  960. $pdf->SetAutoPageBreak('auto', 15);
  961. $pdf->cMargin = 1;
  962. PMA_RT_DOC($alltables);
  963. $pdf->SetAutoPageBreak('auto');
  964. $pdf->cMargin = 0;
  965. }
  966. $pdf->Addpage();
  967. if ($with_doc) {
  968. $pdf->SetLink($pdf->PMA_links['RT']['-'], -1);
  969. $pdf->Bookmark($GLOBALS['strRelationalSchema']);
  970. $pdf->SetAlias('{00}', $pdf->PageNo()) ;
  971. $this->t_marg = 18;
  972. $this->b_marg = 18;
  973. }
  974. /* snip */
  975. foreach ($alltables AS $table) {
  976. if (!isset($this->tables[$table])) {
  977. $this->tables[$table] = new PMA_RT_Table($table, $this->ff, $this->tablewidth);
  978. }
  979. if ($this->same_wide) {
  980. $this->tables[$table]->width = $this->tablewidth;
  981. }
  982. $this->PMA_RT_setMinMax($this->tables[$table]);
  983. }
  984. // Defines the scale factor
  985. $this->scale = ceil(max(($this->x_max - $this->x_min) / ($pdf->fh - $this->r_marg - $this->l_marg), ($this->y_max - $this->y_min) / ($pdf->fw - $this->t_marg - $this->b_marg)) * 100) / 100;
  986. $pdf->PMA_PDF_setScale($this->scale, $this->x_min, $this->y_min, $this->l_marg, $this->t_marg);
  987. // Builds and save the PDF document
  988. $pdf->PMA_PDF_setLineWidthScale(0.1);
  989. if ($show_grid) {
  990. $pdf->SetFontSize(10);
  991. $this->PMA_RT_strokeGrid();
  992. }
  993. $pdf->PMA_PDF_setFontSizeScale(14);
  994. // $sql = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
  995. // . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\' '
  996. // . ' AND foreign_db = \'' . PMA_sqlAddslashes($db) . '\' '
  997. // . ' AND master_table IN (' . $intable . ')'
  998. // . ' AND foreign_table IN (' . $intable . ')';
  999. // $result = PMA_query_as_cu($sql);
  1000. // lem9:
  1001. // previous logic was checking master tables and foreign tables
  1002. // but I think that looping on every table of the pdf page as a master
  1003. // and finding its foreigns is OK (then we can support innodb)
  1004. $seen_a_relation = false;
  1005. foreach ($alltables AS $one_table) {
  1006. $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
  1007. if ($exist_rel) {
  1008. $seen_a_relation = true;
  1009. foreach ($exist_rel AS $master_field => $rel) {
  1010. // put the foreign table on the schema only if selected
  1011. // by the user
  1012. // (do not use array_search() because we would have to
  1013. // to do a === FALSE and this is not PHP3 compatible)
  1014. if (in_array($rel['foreign_table'], $alltables)) {
  1015. $this->PMA_RT_addRelation($one_table, $master_field, $rel['foreign_table'], $rel['foreign_field']);
  1016. }
  1017. } // end while
  1018. } // end if
  1019. } // end while
  1020. // loic1: also show tables without relations
  1021. // $norelations = TRUE;
  1022. // if ($result && PMA_DBI_num_rows($result) > 0) {
  1023. // $norelations = FALSE;
  1024. // while ($row = PMA_DBI_fetch_assoc($result)) {
  1025. // $this->PMA_RT_addRelation($row['master_table'], $row['master_field'], $row['foreign_table'], $row['foreign_field']);
  1026. // }
  1027. // }
  1028. // if ($norelations == FALSE) {
  1029. if ($seen_a_relation) {
  1030. $this->PMA_RT_drawRelations($change_color);
  1031. }
  1032. $this->PMA_RT_drawTables($show_info, $change_color);
  1033. $this->PMA_RT_showRt();
  1034. } // end of the "PMA_RT()" method
  1035. } // end of the "PMA_RT" class
  1036. function PMA_RT_DOC($alltables)
  1037. {
  1038. global $db, $pdf, $orientation, $paper;
  1039. // TOC
  1040. $pdf->addpage($GLOBALS['orientation']);
  1041. $pdf->Cell(0, 9, $GLOBALS['strTableOfContents'], 1, 0, 'C');
  1042. $pdf->Ln(15);
  1043. $i = 1;
  1044. foreach ($alltables AS $table) {
  1045. $pdf->PMA_links['doc'][$table]['-'] = $pdf->AddLink();
  1046. $pdf->SetX(10);
  1047. // $pdf->Ln(1);
  1048. $pdf->Cell(0, 6, $GLOBALS['strPageNumber'] . ' {' . sprintf("%02d", $i) . '}', 0, 0, 'R', 0, $pdf->PMA_links['doc'][$table]['-']);
  1049. $pdf->SetX(10);
  1050. $pdf->Cell(0, 6, $i . ' ' . $table, 0, 1, 'L', 0, $pdf->PMA_links['doc'][$table]['-']);
  1051. // $pdf->Ln(1);
  1052. $result = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';');
  1053. while ($row = PMA_DBI_fetch_assoc($result)) {
  1054. $pdf->SetX(20);
  1055. $field_name = $row['Field'];
  1056. $pdf->PMA_links['doc'][$table][$field_name] = $pdf->AddLink();
  1057. // $pdf->Cell(0, 6, $field_name,0,1,'L',0, $pdf->PMA_links['doc'][$table][$field_name]);
  1058. }
  1059. $lasttable = $table;
  1060. $i++;
  1061. }
  1062. $pdf->PMA_links['RT']['-'] = $pdf->AddLink();
  1063. $pdf->SetX(10);
  1064. $pdf->Cell(0, 6, $GLOBALS['strPageNumber'] . ' {00}', 0, 0, 'R', 0, $pdf->PMA_links['doc'][$lasttable]['-']);
  1065. $pdf->SetX(10);
  1066. $pdf->Cell(0, 6, $i . ' ' . $GLOBALS['strRelationalSchema'], 0, 1, 'L', 0, $pdf->PMA_links['RT']['-']);
  1067. $z = 0;
  1068. foreach ($alltables AS $table) {
  1069. $z++;
  1070. $pdf->addpage($GLOBALS['orientation']);
  1071. $pdf->Bookmark($table);
  1072. $pdf->SetAlias('{' . sprintf("%02d", $z) . '}', $pdf->PageNo()) ;
  1073. $pdf->PMA_links['RT'][$table]['-'] = $pdf->AddLink();
  1074. $pdf->SetLink($pdf->PMA_links['doc'][$table]['-'], -1);
  1075. $pdf->SetFont('', 'B', 18);
  1076. $pdf->Cell(0, 8, $z . ' ' . $table, 1, 1, 'C', 0, $pdf->PMA_links['RT'][$table]['-']);
  1077. $pdf->SetFont('', '', 8);
  1078. $pdf->ln();
  1079. $cfgRelation = PMA_getRelationsParam();
  1080. if ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100) {
  1081. $comments = PMA_getComments($db, $table);
  1082. }
  1083. if ($cfgRelation['mimework']) {
  1084. $mime_map = PMA_getMIME($db, $table, true);
  1085. }
  1086. /**
  1087. * Gets table informations
  1088. */
  1089. $result = PMA_DBI_query('SHOW TABLE STATUS LIKE \'' . PMA_sqlAddslashes($table, true) . '\';', null, PMA_DBI_QUERY_STORE);
  1090. $showtable = PMA_DBI_fetch_assoc($result);
  1091. $num_rows = (isset($showtable['Rows']) ? $showtable['Rows'] : 0);
  1092. $show_comment = (isset($showtable['Comment']) ? $showtable['Comment'] : '');
  1093. $create_time = (isset($showtable['Create_time']) ? PMA_localisedDate(strtotime($showtable['Create_time'])) : '');
  1094. $update_time = (isset($showtable['Update_time']) ? PMA_localisedDate(strtotime($showtable['Update_time'])) : '');
  1095. $check_time = (isset($showtable['Check_time']) ? PMA_localisedDate(strtotime($showtable['Check_time'])) : '');
  1096. PMA_DBI_free_result($result);
  1097. unset($result);
  1098. /**
  1099. * Gets table keys and retains them
  1100. */
  1101. $result = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
  1102. $primary = '';
  1103. $indexes = array();
  1104. $lastIndex = '';
  1105. $indexes_info = array();
  1106. $indexes_data = array();
  1107. $pk_array = array(); // will be use to emphasis prim. keys in the table
  1108. // view
  1109. while ($row = PMA_DBI_fetch_assoc($result)) {
  1110. // Backups the list of primary keys
  1111. if ($row['Key_name'] == 'PRIMARY') {
  1112. $primary .= $row['Column_name'] . ', ';
  1113. $pk_array[$row['Column_name']] = 1;
  1114. }
  1115. // Retains keys informations
  1116. if ($row['Key_name'] != $lastIndex) {
  1117. $indexes[] = $row['Key_name'];
  1118. $lastIndex = $row['Key_name'];
  1119. }
  1120. $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
  1121. $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
  1122. if (isset($row['Cardinality'])) {
  1123. $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
  1124. }
  1125. // I don't know what does following column mean....
  1126. // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
  1127. $indexes_info[$row['Key_name']]['Comment'] = $row['Comment'];
  1128. $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
  1129. if (isset($row['Sub_part'])) {
  1130. $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
  1131. }
  1132. } // end while
  1133. if ($result) {
  1134. PMA_DBI_free_result($result);
  1135. }
  1136. /**
  1137. * Gets fields properties
  1138. */
  1139. $result = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE);
  1140. $fields_cnt = PMA_DBI_num_rows($result);
  1141. // Check if we can use Relations (Mike Beck)
  1142. if (!empty($cfgRelation['relation'])) {
  1143. // Find which tables are related with the current one and write it in
  1144. // an array
  1145. $res_rel = PMA_getForeigners($db, $table);
  1146. if (count($res_rel) > 0) {
  1147. $have_rel = true;
  1148. } else {
  1149. $have_rel = false;
  1150. }
  1151. } else {
  1152. $have_rel = false;
  1153. } // end if
  1154. /**
  1155. * Displays the comments of the table if MySQL >= 3.23
  1156. */
  1157. $break = false;
  1158. if (!empty($show_comment)) {
  1159. $pdf->Cell(0, 3, $GLOBALS['strTableComments'] . ' : ' . $show_comment, 0, 1);
  1160. $break = true;
  1161. }
  1162. if (!empty($create_time)) {
  1163. $pdf->Cell(0, 3, $GLOBALS['strStatCreateTime'] . ': ' . $create_time, 0, 1);
  1164. $break = true;
  1165. }
  1166. if (!empty($update_time)) {
  1167. $pdf->Cell(0, 3, $GLOBALS['strStatUpdateTime'] . ': ' . $update_time, 0, 1);
  1168. $break = true;
  1169. }
  1170. if (!empty($check_time)) {
  1171. $pdf->Cell(0, 3, $GLOBALS['strStatCheckTime'] . ': ' . $check_time, 0, 1);
  1172. $break = true;
  1173. }
  1174. if ($break == true) {
  1175. $pdf->Cell(0, 3, '', 0, 1);
  1176. $pdf->Ln();
  1177. }
  1178. $pdf->SetFont('', 'B');
  1179. if (isset($orientation) && $orientation == 'L') {
  1180. $pdf->Cell(25, 8, ucfirst($GLOBALS['strField']), 1, 0, 'C');
  1181. $pdf->Cell(20, 8, ucfirst($GLOBALS['strType']), 1, 0, 'C');
  1182. $pdf->Cell(20, 8, ucfirst($GLOBALS['strAttr']), 1, 0, 'C');
  1183. $pdf->Cell(10, 8, ucfirst($GLOBALS['strNull']), 1, 0, 'C');
  1184. $pdf->Cell(20, 8, ucfirst($GLOBALS['strDefault']), 1, 0, 'C');
  1185. $pdf->Cell(25, 8, ucfirst($GLOBALS['strExtra']), 1, 0, 'C');
  1186. $pdf->Cell(45, 8, ucfirst($GLOBALS['strLinksTo']), 1, 0, 'C');
  1187. if ($paper == 'A4') {
  1188. $comments_width = 67;
  1189. } else {
  1190. // this is really intended for 'letter'
  1191. /**
  1192. * @todo find optimal width for all formats
  1193. */
  1194. $comments_width = 50;
  1195. }
  1196. $pdf->Cell($comments_width, 8, ucfirst($GLOBALS['strComments']), 1, 0, 'C');
  1197. $pdf->Cell(45, 8, 'MIME', 1, 1, 'C');
  1198. $pdf->SetWidths(array(25, 20, 20, 10, 20, 25, 45, $comments_width, 45));
  1199. } else {
  1200. $pdf->Cell(20, 8, ucfirst($GLOBALS['strField']), 1, 0, 'C');
  1201. $pdf->Cell(20, 8, ucfirst($GLOBALS['strType']), 1, 0, 'C');
  1202. $pdf->Cell(20, 8, ucfirst($GLOBALS['strAttr']), 1, 0, 'C');
  1203. $pdf->Cell(10, 8, ucfirst($GLOBALS['strNull']), 1, 0, 'C');
  1204. $pdf->Cell(15, 8, ucfirst($GLOBALS['strDefault']), 1, 0, 'C');
  1205. $pdf->Cell(15, 8, ucfirst($GLOBALS['strExtra']), 1, 0, 'C');
  1206. $pdf->Cell(30, 8, ucfirst($GLOBALS['strLinksTo']), 1, 0, 'C');
  1207. $pdf->Cell(30, 8, ucfirst($GLOBALS['strComments']), 1, 0, 'C');
  1208. $pdf->Cell(30, 8, 'MIME', 1, 1, 'C');
  1209. $pdf->SetWidths(array(20, 20, 20, 10, 15, 15, 30, 30, 30));
  1210. }
  1211. $pdf->SetFont('', '');
  1212. while ($row = PMA_DBI_fetch_assoc($result)) {
  1213. $type = $row['Type'];
  1214. // reformat mysql query output - staybyte - 9. June 2001
  1215. // loic1: set or enum types: slashes single quotes inside options
  1216. if (preg_match('@^(set|enum)\((.+)\)$@i', $type, $tmp)) {
  1217. $tmp[2] = substr(preg_replace("@([^,])''@", "\\1\\'", ',' . $tmp[2]), 1);
  1218. $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
  1219. $type_nowrap = '';
  1220. $binary = 0;
  1221. $unsigned = 0;
  1222. $zerofill = 0;
  1223. } else {
  1224. $type_nowrap = ' nowrap="nowrap"';
  1225. $type = preg_replace('@BINARY@i', '', $type);
  1226. $type = preg_replace('@ZEROFILL@i', '', $type);
  1227. $type = preg_replace('@UNSIGNED@i', '', $type);
  1228. if (empty($type)) {
  1229. $type = '&nbsp;';
  1230. }
  1231. $binary = stristr($row['Type'], 'BINARY');
  1232. $unsigned = stristr($row['Type'], 'UNSIGNED');
  1233. $zerofill = stristr($row['Type'], 'ZEROFILL');
  1234. }
  1235. $strAttribute = ' ';
  1236. if ($binary) {
  1237. $strAttribute = 'BINARY';
  1238. }
  1239. if ($unsigned) {
  1240. $strAttribute = 'UNSIGNED';
  1241. }
  1242. if ($zerofill) {
  1243. $strAttribute = 'UNSIGNED ZEROFILL';
  1244. }
  1245. if (!isset($row['Default'])) {
  1246. if ($row['Null'] != '' && $row['Null'] != 'NO') {
  1247. $row['Default'] = 'NULL';
  1248. }
  1249. }
  1250. $field_name = $row['Field'];
  1251. // $pdf->Ln();
  1252. $pdf->PMA_links['RT'][$table][$field_name] = $pdf->AddLink();
  1253. $pdf->Bookmark($field_name, 1, -1);
  1254. $pdf->SetLink($pdf->PMA_links['doc'][$table][$field_name], -1);
  1255. $pdf_row = array($field_name,
  1256. $type,
  1257. $strAttribute,
  1258. ($row['Null'] == '' || $row['Null'] == 'NO') ? $GLOBALS['strNo'] : $GLOBALS['strYes'],
  1259. ((isset($row['Default'])) ? $row['Default'] : ''),
  1260. $row['Extra'],
  1261. ((isset($res_rel[$field_name])) ? $res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field'] : ''),
  1262. ((isset($comments[$field_name])) ? $comments[$field_name] : ''),
  1263. ((isset($mime_map) && isset($mime_map[$field_name])) ? str_replace('_', '/', $mime_map[$field_name]['mimetype']) : '')
  1264. );
  1265. $links[0] = $pdf->PMA_links['RT'][$table][$field_name];
  1266. if (isset($res_rel[$field_name]['foreign_table']) AND
  1267. isset($res_rel[$field_name]['foreign_field']) AND
  1268. isset($pdf->PMA_links['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']])
  1269. )
  1270. {
  1271. $links[6] = $pdf->PMA_links['doc'][$res_rel[$field_name]['foreign_table']][$res_rel[$field_name]['foreign_field']];
  1272. } else {
  1273. unset($links[6]);
  1274. }
  1275. $pdf->Row($pdf_row, $links);
  1276. /*$pdf->Cell(20, 8, $field_name, 1, 0, 'L', 0, $pdf->PMA_links['RT'][$table][$field_name]);
  1277. //echo ' ' . $field_name . '&nbsp;' . "\n";
  1278. }
  1279. $pdf->Cell(20, 8, $type, 1, 0, 'L');
  1280. $pdf->Cell(20, 8, $strAttribute, 1, 0, 'L');
  1281. $pdf->Cell(15, 8, , 1, 0, 'L');
  1282. $pdf->Cell(15, 8, ((isset($row['Default'])) ? $row['Default'] : ''),1,0,'L');
  1283. $pdf->Cell(15, 8, $row['Extra'], 1, 0, 'L');
  1284. if ($have_rel) {
  1285. if (isset($res_rel[$field_name])) {
  1286. $pdf->Cell(30, 8, $res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field'],1,0,'L');
  1287. }
  1288. }
  1289. if ($cfgRelation['commwork']) {
  1290. if (isset($comments[$field_name])) {
  1291. $pdf->Cell(0, 8, $comments[$field_name], 1, 0, 'L');
  1292. }
  1293. } */
  1294. } // end while
  1295. $pdf->SetFont('', '', 14);
  1296. PMA_DBI_free_result($result);
  1297. } //end each
  1298. } // end function PMA_RT_DOC
  1299. /**
  1300. * Main logic
  1301. */
  1302. if (!isset($pdf_page_number)) {
  1303. $pdf_page_number = 1;
  1304. }
  1305. $show_grid = (isset($show_grid) && $show_grid == 'on') ? 1 : 0;
  1306. $show_color = (isset($show_color) && $show_color == 'on') ? 1 : 0;
  1307. $show_table_dimension = (isset($show_table_dimension) && $show_table_dimension == 'on') ? 1 : 0;
  1308. $all_tab_same_wide = (isset($all_tab_same_wide) && $all_tab_same_wide == 'on') ? 1 : 0;
  1309. $with_doc = (isset($with_doc) && $with_doc == 'on') ? 1 : 0;
  1310. $orientation = (isset($orientation) && $orientation == 'P') ? 'P' : 'L';
  1311. $paper = isset($paper) ? $paper : 'A4';
  1312. PMA_DBI_select_db($db);
  1313. $rt = new PMA_RT($pdf_page_number, $show_table_dimension, $show_color, $show_grid, $all_tab_same_wide, $orientation, $paper);
  1314. ?>