forked from phpmyadmin/phpmyadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf_schema.php
1406 lines (1330 loc) · 51.9 KB
/
pdf_schema.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
*
* @version $Id$
* @package phpMyAdmin
*/
/**
* Gets some core scripts
*/
require_once './libraries/common.inc.php';
/**
* Settings for relation stuff
*/
require_once './libraries/relation.lib.php';
require_once './libraries/transformations.lib.php';
require_once './libraries/Index.class.php';
$cfgRelation = PMA_getRelationsParam();
/**
* Now in ./libraries/relation.lib.php we check for all tables
* that we need, but if we don't find them we are quiet about it
* so people can work without.
* This page is absolutely useless if you didn't set up your tables
* correctly, so it is a good place to see which tables we can and
* complain ;-)
*/
if (!$cfgRelation['pdfwork']) {
echo '<font color="red">' . __('Error') . '</font><br />' . "\n";
$url_to_goto = '<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">';
echo sprintf(__('The additional features for working with linked tables have been deactivated. To find out why click %shere%s.'), $url_to_goto, '</a>') . "\n";
}
/**
* Font used in PDF.
*
* @todo Make this configuratble (at least Sans/Serif).
*/
define('PMA_PDF_FONT', 'DejaVuSans');
require_once './libraries/tcpdf/tcpdf.php';
/**
* Extends the "FPDF" class and prepares the work
*
* @access public
* @see FPDF
* @package phpMyAdmin
*/
class PMA_PDF extends TCPDF {
/**
* Defines private properties
*/
var $x_min;
var $y_min;
var $l_marg = 10;
var $t_marg = 10;
var $scale;
var $PMA_links;
var $Outlines = array();
var $def_outlines;
var $Alias = array();
var $widths;
public function getFh()
{
return $this->fh;
}
public function getFw()
{
return $this->fw;
}
public function setCMargin($c_margin)
{
$this->cMargin = $c_margin;
}
function SetAlias($name, $value)
{
$this->Alias[$name] = $value ;
}
function _putpages()
{
if (count($this->Alias) > 0) {
$nb = $this->page;
foreach ($this->Alias AS $alias => $value) {
for ($n = 1;$n <= $nb;$n++)
$this->pages[$n]=str_replace($alias, $value, $this->pages[$n]);
}
}
parent::_putpages();
}
/**
* Sets the scaling factor, defines minimum coordinates and margins
*
* @param float scale The scaling factor
* @param float x_min The minimum X coordinate
* @param float y_min The minimum Y coordinate
* @param float l_marg The left margin
* @param float t_marg The top margin
* @access public
*/
function PMA_PDF_setScale($scale = 1, $x_min = 0, $y_min = 0, $l_marg = -1, $t_marg = -1)
{
$this->scale = $scale;
$this->x_min = $x_min;
$this->y_min = $y_min;
if ($this->l_marg != -1) {
$this->l_marg = $l_marg;
}
if ($this->t_marg != -1) {
$this->t_marg = $t_marg;
}
} // end of the "PMA_PDF_setScale" function
/**
* Outputs a scaled cell
*
* @param float w The cell width
* @param float h The cell height
* @param string txt The text to output
* @param mixed border Whether to add borders or not
* @param integer ln Where to put the cursor once the output is done
* @param string align Align mode
* @param integer fill Whether to fill the cell with a color or not
* @access public
* @see FPDF::Cell()
*/
function PMA_PDF_cellScale($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $fill = 0, $link = '')
{
$h = $h / $this->scale;
$w = $w / $this->scale;
$this->Cell($w, $h, $txt, $border, $ln, $align, $fill, $link);
} // end of the "PMA_PDF_cellScale" function
/**
* Draws a scaled line
*
* @param float x1 The horizontal position of the starting point
* @param float y1 The vertical position of the starting point
* @param float x2 The horizontal position of the ending point
* @param float y2 The vertical position of the ending point
* @access public
* @see FPDF::Line()
*/
function PMA_PDF_lineScale($x1, $y1, $x2, $y2)
{
$x1 = ($x1 - $this->x_min) / $this->scale + $this->l_marg;
$y1 = ($y1 - $this->y_min) / $this->scale + $this->t_marg;
$x2 = ($x2 - $this->x_min) / $this->scale + $this->l_marg;
$y2 = ($y2 - $this->y_min) / $this->scale + $this->t_marg;
$this->Line($x1, $y1, $x2, $y2);
} // end of the "PMA_PDF_lineScale" function
/**
* Sets x and y scaled positions
*
* @param float x The x position
* @param float y The y position
* @access public
* @see FPDF::SetXY()
*/
function PMA_PDF_setXyScale($x, $y)
{
$x = ($x - $this->x_min) / $this->scale + $this->l_marg;
$y = ($y - $this->y_min) / $this->scale + $this->t_marg;
$this->SetXY($x, $y);
} // end of the "PMA_PDF_setXyScale" function
/**
* Sets the X scaled positions
*
* @param float x The x position
* @access public
* @see FPDF::SetX()
*/
function PMA_PDF_setXScale($x)
{
$x = ($x - $this->x_min) / $this->scale + $this->l_marg;
$this->SetX($x);
} // end of the "PMA_PDF_setXScale" function
/**
* Sets the scaled font size
*
* @param float size The font size (in points)
* @access public
* @see FPDF::SetFontSize()
*/
function PMA_PDF_setFontSizeScale($size)
{
// Set font size in points
$size = $size / $this->scale;
$this->SetFontSize($size);
} // end of the "PMA_PDF_setFontSizeScale" function
/**
* Sets the scaled line width
*
* @param float width The line width
* @access public
* @see FPDF::SetLineWidth()
*/
function PMA_PDF_setLineWidthScale($width)
{
$width = $width / $this->scale;
$this->SetLineWidth($width);
} // end of the "PMA_PDF_setLineWidthScale" function
/**
* Displays an error message
*
* @param string error_message the error mesage
* @global array the PMA configuration array
* @global integer the current server id
* @global string the current language
* @global string the charset to convert to
* @global string the current database name
* @global string the current charset
* @global string the current text direction
* @global string a localized string
* @global string an other localized string
* @access public
*/
function PMA_PDF_die($error_message = '')
{
global $cfg;
global $server, $lang, $db;
global $charset, $text_dir;
require_once './libraries/header.inc.php';
echo '<p><strong>PDF - ' . __('Error') . '</strong></p>' . "\n";
if (!empty($error_message)) {
$error_message = htmlspecialchars($error_message);
}
echo '<p>' . "\n";
echo ' ' . $error_message . "\n";
echo '</p>' . "\n";
echo '<a href="db_structure.php?' . PMA_generate_common_url($db)
. '">' . __('Back') . '</a>';
echo "\n";
require_once './libraries/footer.inc.php';
} // end of the "PMA_PDF_die()" function
/**
* Aliases the "Error()" function from the FPDF class to the
* "PMA_PDF_die()" one
*
* @param string error_message the error mesage
* @access public
* @see PMA_PDF_die
*/
function Error($error_message = '')
{
$this->PMA_PDF_die($error_message);
} // end of the "Error()" method
function Header()
{
// We only show this if we find something in the new pdf_pages table
// This function must be named "Header" to work with the FPDF library
global $cfgRelation, $db, $pdf_page_number, $with_doc;
if ($with_doc) {
$test_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
. ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
. ' AND page_nr = \'' . $pdf_page_number . '\'';
$test_rs = PMA_query_as_controluser($test_query);
$pages = @PMA_DBI_fetch_assoc($test_rs);
$this->SetFont('', 'B', 14);
$this->Cell(0, 6, ucfirst($pages['page_descr']), 'B', 1, 'C');
$this->SetFont('', '');
$this->Ln();
}
}
function Footer()
{
// This function must be named "Footer" to work with the FPDF library
global $with_doc;
if ($with_doc) {
$this->SetY(-15);
$this->SetFont('', '', 14);
$this->Cell(0, 6, __('Page number:') . ' ' . $this->PageNo() . '/{nb}', 'T', 0, 'C');
$this->Cell(0, 6, PMA_localisedDate(), 0, 1, 'R');
$this->SetY(20);
}
}
function Bookmark($txt, $level = 0, $y = 0)
{
// Add a bookmark
$this->Outlines[0][] = $level;
$this->Outlines[1][] = $txt;
$this->Outlines[2][] = $this->page;
if ($y == -1) {
$y = $this->GetY();
}
$this->Outlines[3][] = round($this->hPt - $y * $this->k, 2);
}
function _putbookmarks()
{
if (count($this->Outlines) > 0) {
// Save object number
$memo_n = $this->n;
// Take the number of sub elements for an outline
$nb_outlines = sizeof($this->Outlines[0]);
$first_level = array();
$parent = array();
$parent[0] = 1;
for ($i = 0; $i < $nb_outlines; $i++) {
$level = $this->Outlines[0][$i];
$kids = 0;
$last = -1;
$prev = -1;
$next = -1;
if ($i > 0) {
$cursor = $i-1;
// Take the previous outline in the same level
while ($this->Outlines[0][$cursor] > $level && $cursor > 0)
$cursor--;
if ($this->Outlines[0][$cursor] == $level) {
$prev = $cursor;
}
}
if ($i < $nb_outlines-1) {
$cursor = $i + 1;
while (isset($this->Outlines[0][$cursor]) && $this->Outlines[0][$cursor] > $level) {
// Take the immediate kid in level + 1
if ($this->Outlines[0][$cursor] == $level + 1) {
$kids++;
$last = $cursor;
}
$cursor++;
}
$cursor = $i + 1;
// Take the next outline in the same level
while ($this->Outlines[0][$cursor] > $level && ($cursor + 1 < sizeof($this->Outlines[0])))
$cursor++;
if ($this->Outlines[0][$cursor] == $level) {
$next = $cursor;
}
}
$this->_newobj();
$parent[$level + 1] = $this->n;
if ($level == 0) {
$first_level[] = $this->n;
}
$this->_out('<<');
$this->_out('/Title (' . $this->Outlines[1][$i] . ')');
$this->_out('/Parent ' . $parent[$level] . ' 0 R');
if ($prev != -1) {
$this->_out('/Prev ' . ($memo_n + $prev + 1) . ' 0 R');
}
if ($next != -1) {
$this->_out('/Next ' . ($this->n + $next - $i) . ' 0 R');
}
$this->_out('/Dest [' . (1 + (2 * $this->Outlines[2][$i])) . ' 0 R /XYZ null ' . $this->Outlines[3][$i] . ' null]');
if ($kids > 0) {
$this->_out('/First ' . ($this->n + 1) . ' 0 R');
$this->_out('/Last ' . ($this->n + $last - $i) . ' 0 R');
$this->_out('/Count -' . $kids);
}
$this->_out('>>');
$this->_out('endobj');
}
// First page of outlines
$this->_newobj();
$this->def_outlines = $this->n;
$this->_out('<<');
$this->_out('/Type');
$this->_out('/Outlines');
$this->_out('/First ' . $first_level[0] . ' 0 R');
$this->_out('/Last ' . $first_level[sizeof($first_level)-1] . ' 0 R');
$this->_out('/Count ' . sizeof($first_level));
$this->_out('>>');
$this->_out('endobj');
}
}
function _putresources()
{
parent::_putresources();
$this->_putbookmarks();
}
function _putcatalog()
{
parent::_putcatalog();
if (count($this->Outlines) > 0) {
$this->_out('/Outlines ' . $this->def_outlines . ' 0 R');
$this->_out('/PageMode /UseOutlines');
}
}
function SetWidths($w)
{
// column widths
$this->widths = $w;
}
function Row($data, $links)
{
// line height
$nb = 0;
$data_cnt = count($data);
for ($i = 0;$i < $data_cnt;$i++)
$nb = max($nb, $this->NbLines($this->widths[$i], $data[$i]));
$il = $this->FontSize;
$h = ($il + 1) * $nb;
// page break if necessary
$this->CheckPageBreak($h);
// draw the cells
$data_cnt = count($data);
for ($i = 0;$i < $data_cnt;$i++) {
$w = $this->widths[$i];
// save current position
$x = $this->GetX();
$y = $this->GetY();
// draw the border
$this->Rect($x, $y, $w, $h);
if (isset($links[$i])) {
$this->Link($x, $y, $w, $h, $links[$i]);
}
// print text
$this->MultiCell($w, $il + 1, $data[$i], 0, 'L');
// go to right side
$this->SetXY($x + $w, $y);
}
// go to line
$this->Ln($h);
}
function CheckPageBreak($h)
{
// if height h overflows, manual page break
if ($this->GetY() + $h > $this->PageBreakTrigger) {
$this->AddPage($this->CurOrientation);
}
}
function NbLines($w, $txt)
{
// compute number of lines used by a multicell of width w
$cw = &$this->CurrentFont['cw'];
if ($w == 0) {
$w = $this->w - $this->rMargin - $this->x;
}
$wmax = ($w-2 * $this->cMargin) * 1000 / $this->FontSize;
$s = str_replace("\r", '', $txt);
$nb = strlen($s);
if ($nb > 0 and $s[$nb-1] == "\n") {
$nb--;
}
$sep = -1;
$i = 0;
$j = 0;
$l = 0;
$nl = 1;
while ($i < $nb) {
$c = $s[$i];
if ($c == "\n") {
$i++;
$sep = -1;
$j = $i;
$l = 0;
$nl++;
continue;
}
if ($c == ' ') {
$sep = $i;
}
$l += isset($cw[ord($c)])?$cw[ord($c)]:0 ;
if ($l > $wmax) {
if ($sep == -1) {
if ($i == $j) {
$i++;
}
} else {
$i = $sep + 1;
}
$sep = -1;
$j = $i;
$l = 0;
$nl++;
} else {
$i++;
}
}
return $nl;
}
} // end of the "PMA_PDF" class
/**
* Draws tables schema
*
* @access private
* @see PMA_RT
* @package phpMyAdmin
*/
class PMA_RT_Table {
/**
* Defines private properties
*/
var $nb_fiels;
var $table_name;
var $width = 0;
var $height;
var $fields = array();
var $height_cell = 6;
var $x, $y;
var $primary = array();
var $show_info = false;
/**
* Returns title of the current table,
* title can have the dimensions of the table
*
* @access private
*/
function getTitle()
{
return ($this->show_info ? sprintf('%.0f', $this->width) . 'x' . sprintf('%.0f', $this->height) : '') . ' ' . $this->table_name;
} // end of the "getTitle" function
/**
* Sets the width of the table
*
* @param integer ff The font size
* @global object The current PDF document
* @access private
* @see PMA_PDF
*/
function PMA_RT_Table_setWidth($ff)
{
global $pdf;
foreach ($this->fields AS $field) {
$this->width = max($this->width, $pdf->GetStringWidth($field));
}
$this->width += $pdf->GetStringWidth(' ');
$pdf->SetFont($ff, 'B');
// it is unknown what value must be added, because
// table title is affected by the tabe width value
while ($this->width < $pdf->GetStringWidth($this->getTitle())) {
$this->width += 5;
}
$pdf->SetFont($ff, '');
} // end of the "PMA_RT_Table_setWidth()" method
/**
* Sets the height of the table
*
* @access private
*/
function PMA_RT_Table_setHeight()
{
$this->height = (count($this->fields) + 1) * $this->height_cell;
} // end of the "PMA_RT_Table_setHeight()" method
/**
* Do draw the table
*
* @param integer ff The font size
* @param boolean setcolortWhether to display color
* @global object The current PDF document
* @access private
* @see PMA_PDF
*/
function PMA_RT_Table_draw($ff, $setcolor = 0)
{
global $pdf, $with_doc;
$pdf->PMA_PDF_setXyScale($this->x, $this->y);
$pdf->SetFont($ff, 'B');
if ($setcolor) {
$pdf->SetTextColor(200);
$pdf->SetFillColor(0, 0, 128);
}
if ($with_doc) {
$pdf->SetLink($pdf->PMA_links['RT'][$this->table_name]['-'], -1);
} else {
$pdf->PMA_links['doc'][$this->table_name]['-'] = '';
}
$pdf->PMA_PDF_cellScale($this->width, $this->height_cell, $this->getTitle(), 1, 1, 'C', $setcolor, $pdf->PMA_links['doc'][$this->table_name]['-']);
$pdf->PMA_PDF_setXScale($this->x);
$pdf->SetFont($ff, '');
$pdf->SetTextColor(0);
$pdf->SetFillColor(255);
foreach ($this->fields AS $field) {
if ($setcolor) {
if (in_array($field, $this->primary)) {
$pdf->SetFillColor(215, 121, 123);
}
if ($field == $this->displayfield) {
$pdf->SetFillColor(142, 159, 224);
}
}
if ($with_doc) {
$pdf->SetLink($pdf->PMA_links['RT'][$this->table_name][$field], -1);
} else {
$pdf->PMA_links['doc'][$this->table_name][$field] = '';
}
$pdf->PMA_PDF_cellScale($this->width, $this->height_cell, ' ' . $field, 1, 1, 'L', $setcolor, $pdf->PMA_links['doc'][$this->table_name][$field]);
$pdf->PMA_PDF_setXScale($this->x);
$pdf->SetFillColor(255);
} // end while
/*if ($pdf->PageNo() > 1) {
$pdf->PMA_PDF_die(__('The scale factor is too small to fit the schema on one page'));
} */
} // end of the "PMA_RT_Table_draw()" method
/**
* The "PMA_RT_Table" constructor
*
* @param string table_name The table name
* @param integer ff The font size
* @param integer same_width The max. with among tables
* @param boolean show_keys Whether to display keys or not
* @param boolean show_info Whether to display table position or not
* @global object The current PDF document
* @global integer The current page number (from the
* $cfg['Servers'][$i]['table_coords'] table)
* @global array The relations settings
* @global string The current db name
* @access private
* @see PMA_PDF, PMA_RT_Table::PMA_RT_Table_setWidth,
PMA_RT_Table::PMA_RT_Table_setHeight
*/
function __construct($table_name, $ff, &$same_wide_width, $show_keys = false, $show_info = false)
{
global $pdf, $pdf_page_number, $cfgRelation, $db;
$this->table_name = $table_name;
$sql = 'DESCRIBE ' . PMA_backquote($table_name);
$result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE);
if (!$result || !PMA_DBI_num_rows($result)) {
$pdf->PMA_PDF_die(sprintf(__('The %s table doesn\'t exist!'), $table_name));
}
// load fields
//check to see if it will load all fields or only the foreign keys
if ($show_keys) {
$indexes = PMA_Index::getFromTable($this->table_name, $db);
$all_columns = array();
foreach ($indexes as $index) {
$all_columns = array_merge($all_columns, array_flip(array_keys($index->getColumns())));
}
$this->fields = array_keys($all_columns);
} else {
while ($row = PMA_DBI_fetch_row($result)) {
$this->fields[] = $row[0];
}
}
$this->show_info = $show_info;
// height and width
$this->PMA_RT_Table_setHeight();
// setWidth must me after setHeight, because title
// can include table height which changes table width
$this->PMA_RT_Table_setWidth($ff);
if ($same_wide_width < $this->width) {
$same_wide_width = $this->width;
}
// x and y
$sql = 'SELECT x, y FROM '
. PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
. ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
. ' AND table_name = \'' . PMA_sqlAddslashes($table_name) . '\''
. ' AND pdf_page_number = ' . $pdf_page_number;
$result = PMA_query_as_controluser($sql, false, PMA_DBI_QUERY_STORE);
if (!$result || !PMA_DBI_num_rows($result)) {
$pdf->PMA_PDF_die(sprintf(__('Please configure the coordinates for table %s'), $table_name));
}
list($this->x, $this->y) = PMA_DBI_fetch_row($result);
$this->x = (double) $this->x;
$this->y = (double) $this->y;
// displayfield
$this->displayfield = PMA_getDisplayField($db, $table_name);
// index
$result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($table_name) . ';', null, PMA_DBI_QUERY_STORE);
if (PMA_DBI_num_rows($result) > 0) {
while ($row = PMA_DBI_fetch_assoc($result)) {
if ($row['Key_name'] == 'PRIMARY') {
$this->primary[] = $row['Column_name'];
}
}
} // end if
} // end of the "PMA_RT_Table()" method
} // end class "PMA_RT_Table"
/**
* Draws relation links
*
* @access private
* @see PMA_RT
* @package phpMyAdmin
*/
class PMA_RT_Relation {
/**
* Defines private properties
*/
var $x_src, $y_src;
var $src_dir ;
var $dest_dir;
var $x_dest, $y_dest;
var $w_tick = 5;
/**
* Gets arrows coordinates
*
* @param string table The current table name
* @param string column The relation column name
* @return array Arrows coordinates
* @access private
*/
function PMA_RT_Relation_getXy($table, $column)
{
$pos = array_search($column, $table->fields);
// x_left, x_right, y
return array($table->x, $table->x + + $table->width, $table->y + ($pos + 1.5) * $table->height_cell);
} // end of the "PMA_RT_Relation_getXy()" method
/**
* Do draws relation links
*
* @param boolean change_color Whether to use one color per relation or not
* @param integer i The id of the link to draw
* @global object The current PDF document
* @access private
* @see PMA_PDF
*/
function PMA_RT_Relation_draw($change_color, $i)
{
global $pdf;
if ($change_color) {
$d = $i % 6;
$j = ($i - $d) / 6;
$j = $j % 4;
$j++;
$case = array(
array(1, 0, 0),
array(0, 1, 0),
array(0, 0, 1),
array(1, 1, 0),
array(1, 0, 1),
array(0, 1, 1)
);
list ($a, $b, $c) = $case[$d];
$e = (1 - ($j - 1) / 6);
$pdf->SetDrawColor($a * 255 * $e, $b * 255 * $e, $c * 255 * $e);
} else {
$pdf->SetDrawColor(0);
} // end if... else...
$pdf->PMA_PDF_setLineWidthScale(0.2);
$pdf->PMA_PDF_lineScale($this->x_src, $this->y_src, $this->x_src + $this->src_dir * $this->w_tick, $this->y_src);
$pdf->PMA_PDF_lineScale($this->x_dest + $this->dest_dir * $this->w_tick, $this->y_dest, $this->x_dest, $this->y_dest);
$pdf->PMA_PDF_setLineWidthScale(0.1);
$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);
// arrow
$root2 = 2 * sqrt(2);
$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);
$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);
$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);
$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);
$pdf->SetDrawColor(0);
} // end of the "PMA_RT_Relation_draw()" method
/**
* The "PMA_RT_Relation" constructor
*
* @param string master_table The master table name
* @param string master_field The relation field in the master table
* @param string foreign_table The foreign table name
* @param string foreigh_field The relation field in the foreign table
* @access private
* @see PMA_RT_Relation::PMA_RT_Relation_getXy
*/
function __construct($master_table, $master_field, $foreign_table, $foreign_field)
{
$src_pos = $this->PMA_RT_Relation_getXy($master_table, $master_field);
$dest_pos = $this->PMA_RT_Relation_getXy($foreign_table, $foreign_field);
$src_left = $src_pos[0] - $this->w_tick;
$src_right = $src_pos[1] + $this->w_tick;
$dest_left = $dest_pos[0] - $this->w_tick;
$dest_right = $dest_pos[1] + $this->w_tick;
$d1 = abs($src_left - $dest_left);
$d2 = abs($src_right - $dest_left);
$d3 = abs($src_left - $dest_right);
$d4 = abs($src_right - $dest_right);
$d = min($d1, $d2, $d3, $d4);
if ($d == $d1) {
$this->x_src = $src_pos[0];
$this->src_dir = -1;
$this->x_dest = $dest_pos[0];
$this->dest_dir = -1;
} elseif ($d == $d2) {
$this->x_src = $src_pos[1];
$this->src_dir = 1;
$this->x_dest = $dest_pos[0];
$this->dest_dir = -1;
} elseif ($d == $d3) {
$this->x_src = $src_pos[0];
$this->src_dir = -1;
$this->x_dest = $dest_pos[1];
$this->dest_dir = 1;
} else {
$this->x_src = $src_pos[1];
$this->src_dir = 1;
$this->x_dest = $dest_pos[1];
$this->dest_dir = 1;
}
$this->y_src = $src_pos[2];
$this->y_dest = $dest_pos[2];
} // end of the "PMA_RT_Relation()" method
} // end of the "PMA_RT_Relation" class
/**
* Draws and send the database schema
*
* @access public
* @see PMA_PDF
* @package phpMyAdmin
*/
class PMA_RT {
/**
* Defines private properties
*/
var $tables = array();
var $relations = array();
var $ff = PMA_PDF_FONT;
var $x_max = 0;
var $y_max = 0;
var $scale;
var $x_min = 100000;
var $y_min = 100000;
var $t_marg = 10;
var $b_marg = 10;
var $l_marg = 10;
var $r_marg = 10;
var $tablewidth;
var $same_wide = 0;
/**
* Sets X and Y minimum and maximum for a table cell
*
* @param string table The table name
* @access private
*/
function PMA_RT_setMinMax($table)
{
$this->x_max = max($this->x_max, $table->x + $table->width);
$this->y_max = max($this->y_max, $table->y + $table->height);
$this->x_min = min($this->x_min, $table->x);
$this->y_min = min($this->y_min, $table->y);
} // end of the "PMA_RT_setMinMax()" method
/**
* Defines relation objects
*
* @param string master_table The master table name
* @param string master_field The relation field in the master table
* @param string foreign_table The foreign table name
* @param string foreign_field The relation field in the foreign table
* @param boolean show_info Whether to display table position or not
* @access private
* @see PMA_RT_setMinMax
*/
function PMA_RT_addRelation($master_table, $master_field, $foreign_table, $foreign_field, $show_info)
{
if (!isset($this->tables[$master_table])) {
$this->tables[$master_table] = new PMA_RT_Table($master_table, $this->ff, $this->tablewidth, false, $show_info);
$this->PMA_RT_setMinMax($this->tables[$master_table]);
}
if (!isset($this->tables[$foreign_table])) {
$this->tables[$foreign_table] = new PMA_RT_Table($foreign_table, $this->ff, $this->tablewidth, false, $show_info);
$this->PMA_RT_setMinMax($this->tables[$foreign_table]);
}
$this->relations[] = new PMA_RT_Relation($this->tables[$master_table], $master_field, $this->tables[$foreign_table], $foreign_field);
} // end of the "PMA_RT_addRelation()" method
/**
* Draws the grid
*
* @global object the current PMA_PDF instance
* @access private
* @see PMA_PDF
*/
function PMA_RT_strokeGrid()
{
global $pdf;
$pdf->SetMargins(0, 0);
$pdf->SetDrawColor(200, 200, 200);
// Draws horizontal lines
for ($l = 0; $l < 21; $l++) {
$pdf->line(0, $l * 10, $pdf->getFh(), $l * 10);
// Avoid duplicates
if ($l > 0) {
$pdf->SetXY(0, $l * 10);
$label = (string) sprintf('%.0f', ($l * 10 - $this->t_marg) * $this->scale + $this->y_min);
$pdf->Cell(5, 5, ' ' . $label);
} // end if
} // end for
// Draws vertical lines
for ($j = 0; $j < 30 ;$j++) {
$pdf->line($j * 10, 0, $j * 10, $pdf->getFw());
$pdf->SetXY($j * 10, 0);
$label = (string) sprintf('%.0f', ($j * 10 - $this->l_marg) * $this->scale + $this->x_min);
$pdf->Cell(5, 7, $label);
} // end for
} // end of the "PMA_RT_strokeGrid()" method
/**
* Draws relation arrows
*
* @param boolean change_color Whether to use one color per relation or not
* @access private
* @see PMA_RT_Relation::PMA_RT_Relation_draw()
*/
function PMA_RT_drawRelations($change_color)
{
$i = 0;
foreach ($this->relations AS $relation) {
$relation->PMA_RT_Relation_draw($change_color, $i);
$i++;
} // end while
} // end of the "PMA_RT_drawRelations()" method
/**
* Draws tables
*
* @param boolean draw_color Whether to display table position or not
* @access private
* @see PMA_RT_Table::PMA_RT_Table_draw()
*/
function PMA_RT_drawTables($draw_color = 0)
{
foreach ($this->tables AS $table) {
$table->PMA_RT_Table_draw($this->ff, $draw_color);
}
} // end of the "PMA_RT_drawTables()" method
/**
* Ouputs the PDF document to a file
*
* @global object The current PDF document
* @global string The current database name
* @global integer The current page number (from the
* $cfg['Servers'][$i]['table_coords'] table)
* @access private
* @see PMA_PDF
*/
function PMA_RT_showRt()
{
global $pdf, $db, $pdf_page_number, $cfgRelation;
$pdf->SetFontSize(14);
$pdf->SetLineWidth(0.2);
$pdf->SetDisplayMode('fullpage');
// Get the name of this pdfpage to use as filename (Mike Beck)
$_name_sql = 'SELECT page_descr FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
. ' WHERE page_nr = ' . $pdf_page_number;
$_name_rs = PMA_query_as_controluser($_name_sql);
if ($_name_rs) {
$_name_row = PMA_DBI_fetch_row($_name_rs);
$filename = $_name_row[0] . '.pdf';
}
// i don't know if there is a chance for this to happen, but rather be on the safe side:
if (empty($filename)) {
$filename = $pdf_page_number . '.pdf';
}
// $pdf->Output($db . '_' . $filename, TRUE);
$pdf->Output($db . '_' . $filename, 'I'); // destination: Inline
} // end of the "PMA_RT_showRt()" method
/**
* The "PMA_RT" constructor
*
* @param integer which_rel The page number to draw (from the
* $cfg['Servers'][$i]['table_coords'] table)
* @param boolean show_info Whether to display table position or not
* @param boolean change_color Was originally whether to use one color per
* relation or not, now enables/disables color
* everywhere, due to some problems printing with color
* @param boolean show_grid Whether to draw grids or not
* @param boolean all_tab_same_wide Whether all tables should have the same width or not
* @param boolean show_keys Wheter to show all field or only the keys
* @global object The current PDF document
* @global string The current db name
* @global array The relations settings
* @access private
* @see PMA_PDF
*/
function __construct($which_rel, $show_info = 0, $change_color = 0, $show_grid = 0, $all_tab_same_wide = 0, $orientation = 'L', $paper = 'A4', $show_keys = 0)
{
global $pdf, $db, $cfgRelation, $with_doc;
$this->same_wide = $all_tab_same_wide;
// Initializes a new document
$pdf = new PMA_PDF('L', 'mm', $paper);
$pdf->SetTitle(sprintf(__('Schema of the %s database - Page %s'), $GLOBALS['db'], $which_rel));
$pdf->setCMargin(0);
$pdf->Open();
$pdf->SetAuthor('phpMyAdmin ' . PMA_VERSION);
$pdf->AliasNbPages();
$pdf->AddFont('DejaVuSans', '', 'dejavusans.php');