2 /*=======================================================================
3 // File: IMGDBSCHEMA.INC
4 // Description: Classes to help drawing a DB schema semi-automtically
5 // See dbschema_ddda.php for example of use.
7 // Author: Johan Persson (johanp@aditus.nu)
8 // Ver: $Id: imgdbschema.inc,v 1.2 2002/08/29 10:11:22 aditus Exp $
10 // License: This code is released under QPL
11 // Copyright (C) 2001,2002 Johan Persson
12 //========================================================================
16 //===================================================
18 // Description: Utility class for drawing an graphical
19 // illustration of a DB table
20 //===================================================
23 var $iheader_color='white',$iheader_fillcolor='navy';
24 var $ibody_color='black', $ibody_fillcolor='lightyellow';
25 var $iheader_txt,$ibody_txt;
27 var $iheader_font=FF_FONT1,$iheader_style=FS_BOLD,$iheader_fontsize=14;
28 var $ibody_font=FF_FONT1,$ibody_style=FS_NORMAL,$ibody_fontsize=12;
30 function ImgDBTable($w=0,$h=0) {
35 function SetPos($x,$y) {
40 function HeaderColor($aFontColor,$aFillColor) {
41 $this->iheader_color=$aFontColor;
42 $this->iheader_fillcolor=$aFillColor;
45 function BodyColor($aFontColor,$aFillColor) {
46 $this->ibody_color=$aFontColor;
47 $this->ibody_fillcolor=$aFillColor;
50 function Set($aHeader,$aBody,$aWidth=0,$aHeadHeight=0,$aBodyHeight=0) {
52 $this->ih = $aBodyHeight;
53 $this->SetHeader($aHeader,$aHeadHeight);
54 $this->SetBody($aBody);
57 function Setheader($aTxt,$aHeight=0) {
58 $this->iheader_txt = $aTxt;
59 $this->ihh = $aHeight;
62 function SetBody($aTxtArr) {
65 for( $i=0; $i < $n; ++$i ) {
66 if( $i > 0 ) $t .= "\n";
69 $this->ibody_txt = $t;
72 function SetHeaderFont($aFam,$aSty,$aSize) {
73 $this->iheader_font = $aFam;
74 $this->iheader_style = $aSty;
75 $this->iheader_fontsize = $aSize;
78 function SetBodyFont($aFam,$aSty,$aSize) {
79 $this->ibody_font = $aFam;
80 $this->ibody_style = $aSty;
81 $this->ibody_fontsize = $aSize;
84 function StrokeAll($aImg,$aScale,$aTables) {
86 for($i=0; $i < $n; ++$i) {
87 $this->Set($aTables[$i][3], $aTables[$i][4], $aTables[$i][2]);
88 $this->Stroke($aImg,$aScale,$aTables[$i][0], $aTables[$i][1]);
92 function Stroke($aImg,$aScale,$ax=0,$ay=0) {
98 $t = new CanvasRectangleText();
101 $t->SetFont($this->iheader_font,$this->iheader_style,$this->iheader_fontsize);
102 $t->SetFillColor($this->iheader_fillcolor);
103 $t->SetFontColor($this->iheader_color);
104 $t->SetAutoMargin(7);
105 $t->ParagraphAlign('center');
106 $t->Set($this->iheader_txt,$this->ix,$this->iy,$this->iw,$this->ihh);
108 // In case of "auto-heighting" we need to find out the actual height
109 list($dummy,$this->ihh) = $t->Stroke($aImg,$aScale);
110 $this->iy = $aScale->TranslateY($this->iy);
112 // Use absolute coordinate (this is indicated by negative values)
113 $this->iy = -( $this->iy + $this->ihh );
115 $t->SetFont($this->ibody_font,$this->ibody_style,$this->ibody_fontsize);
116 $t->SetFillColor($this->ibody_fillcolor);
117 $t->SetFontColor($this->ibody_color);
118 $t->SetAutoMargin(8);
119 $t->ParagraphAlign('left');
120 $t->Set($this->ibody_txt,$this->ix,$this->iy,$this->iw,$this->ih);
121 $t->Stroke($aImg,$aScale);
128 //============================================================================
129 // Class: ImgDBSchema
130 // Generate an image of all tables in a specific DB
131 //============================================================================
135 var $iFormTblName = '';
136 var $iFormFldName = '';
137 var $iLeftMarg=2,$iTopMarg=4;
138 var $iTableWidth=13,$iTableAutoHeight=20;
141 // Initialize with DB name as well as possible callback methods for
142 // formatting the table header and fields
143 function ImgDBSchema($aDBName,$aTblNameFormat='',$aFldNameFormat='') {
144 $this->iFormTblName = $aTblNameFormat;
145 $this->iFormFldName = $aFldNameFormat;
146 $this->iDBSrv = new DBServer('root','');
147 $this->iTitle = new CanvasRectangleText();
148 $this->iDBSrv->SetDB($aDBName);
151 function SetMargin($aLeft,$aTop) {
152 $this->iLeftMarg = $aLeft;
153 $this->iTopMarg = $aTop;
156 function SetTableWIdth($aWidth) {
157 $this->iTableWidth=$aWidth;
160 // Stroke tables using a specific image context and scale.
161 // It is possible to manually set the position of each table if
162 // you specify the TblPos for each DB table.
163 function Stroke($aImg,$aScale,$aTblPos=null) {
165 $tables = $this->SetupFormatTables($aTblPos);
167 // Setup the DB table raster class
168 $dbi = new ImgDBTable();
171 $dbi->StrokeAll($aImg,$aScale,$tables);
176 // Get the information from the Database and setup
177 // the formatting array to help positioning and stroking the
178 // tables to the canvas
179 function SetupFormatTables($aTblPos) {
181 $tblflds = $this->iDBSrv->GetTablesFields();
183 $nt = count($tblflds);
186 $x = $this->iLeftMarg;
187 $y = $this->iTopMarg;
189 for( $i=0; $i < $nt; ++$i ) {
191 if( !isset($aTblPos[2*$i]) ) $aTblPos[2*$i]=-1;
192 if( !isset($aTblPos[2*$i+1]) ) $aTblPos[2*$i+1]=-1;
194 if( $this->iFormTblName != '' ) {
195 $f = $this->iFormTblName;
196 $tn = $f($tblflds[$i][0]);
199 $tn = $tblflds[$i][0];
201 $flds = $tblflds[$i][1];
202 if( $this->iFormFldName != '' ) {
203 $f = $this->iFormFldName;
205 for( $j=0; $j< $n; ++$j ) {
206 $flds[$j] = $f($flds[$j],$tblflds[$i][0]);
210 $tables[] = array($aTblPos[2*$i] >= 0 ? $this->iLeftMarg+$aTblPos[2*$i] : $x,
211 $aTblPos[2*$i+1] >= 0 ? $this->iTopMarg +$aTblPos[2*$i+1]: $y,
212 $this->iTableWidth,$tn,$flds);
213 $x += $this->iTableWidth + 1;
215 $x = $this->iLeftMarg;
216 $y += $this->iTableAutoHeight;