home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / pdf_schema.php < prev    next >
Encoding:
PHP Script  |  2003-11-26  |  50.6 KB  |  1,525 lines

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