home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / phpMyAdmin / pdf_schema.php < prev    next >
PHP Script  |  2008-06-23  |  53KB  |  1,384 lines

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