UPDATE 4.4.0.0
[phpmyadmin.git] / libraries / plugins / schema / svg / Svg_Relation_Schema.class.php
blobef6af6d43d33795a9fbff2f5e7f192e2beb85cb7
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Classes to create relation schema in SVG format.
6 * @package PhpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 require_once 'libraries/plugins/schema/Export_Relation_Schema.class.php';
13 require_once 'libraries/plugins/schema/svg/RelationStatsSvg.class.php';
14 require_once 'libraries/plugins/schema/svg/TableStatsSvg.class.php';
15 require_once 'libraries/Font.class.php';
17 /**
18 * This Class inherits the XMLwriter class and
19 * helps in developing structure of SVG Schema Export
21 * @package PhpMyAdmin
22 * @access public
23 * @see http://php.net/manual/en/book.xmlwriter.php
25 class PMA_SVG extends XMLWriter
27 public $title;
28 public $author;
29 public $font;
30 public $fontSize;
32 /**
33 * The "PMA_SVG" constructor
35 * Upon instantiation This starts writing the Svg XML document
37 * @see XMLWriter::openMemory(),XMLWriter::setIndent(),XMLWriter::startDocument()
39 function __construct()
41 $this->openMemory();
43 * Set indenting using three spaces,
44 * so output is formatted
47 $this->setIndent(true);
48 $this->setIndentString(' ');
50 * Create the XML document
53 $this->startDocument('1.0', 'UTF-8');
54 $this->startDtd(
55 'svg', '-//W3C//DTD SVG 1.1//EN',
56 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'
58 $this->endDtd();
61 /**
62 * Set document title
64 * @param string $value sets the title text
66 * @return void
67 * @access public
69 function setTitle($value)
71 $this->title = $value;
74 /**
75 * Set document author
77 * @param string $value sets the author
79 * @return void
80 * @access public
82 function setAuthor($value)
84 $this->author = $value;
87 /**
88 * Set document font
90 * @param string $value sets the font e.g Arial, Sans-serif etc
92 * @return void
93 * @access public
95 function setFont($value)
97 $this->font = $value;
101 * Get document font
103 * @return string returns the font name
104 * @access public
106 function getFont()
108 return $this->font;
112 * Set document font size
114 * @param string $value sets the font size in pixels
116 * @return void
117 * @access public
119 function setFontSize($value)
121 $this->fontSize = $value;
125 * Get document font size
127 * @return string returns the font size
128 * @access public
130 function getFontSize()
132 return $this->fontSize;
136 * Starts Svg Document
138 * svg document starts by first initializing svg tag
139 * which contains all the attributes and namespace that needed
140 * to define the svg document
142 * @param integer $width total width of the Svg document
143 * @param integer $height total height of the Svg document
145 * @return void
146 * @access public
148 * @see XMLWriter::startElement(),XMLWriter::writeAttribute()
150 function startSvgDoc($width,$height)
152 $this->startElement('svg');
153 $this->writeAttribute('width', $width);
154 $this->writeAttribute('height', $height);
155 $this->writeAttribute('xmlns', 'http://www.w3.org/2000/svg');
156 $this->writeAttribute('version', '1.1');
160 * Ends Svg Document
162 * @return void
163 * @access public
164 * @see XMLWriter::endElement(),XMLWriter::endDocument()
166 function endSvgDoc()
168 $this->endElement();
169 $this->endDocument();
173 * output Svg Document
175 * svg document prompted to the user for download
176 * Svg document saved in .svg extension and can be
177 * easily changeable by using any svg IDE
179 * @param string $fileName file name
181 * @return void
182 * @access public
183 * @see XMLWriter::startElement(),XMLWriter::writeAttribute()
185 function showOutput($fileName)
187 //ob_get_clean();
188 $output = $this->flush();
189 PMA_Response::getInstance()->disable();
190 PMA_downloadHeader(
191 $fileName,
192 'image/svg+xml',
193 /*overload*/mb_strlen($output)
195 print $output;
199 * Draws Svg elements
201 * SVG has some predefined shape elements like rectangle & text
202 * and other elements who have x,y co-ordinates are drawn.
203 * specify their width and height and can give styles too.
205 * @param string $name Svg element name
206 * @param int $x The x attr defines the left position of the element
207 * (e.g. x="0" places the element 0 pixels from the left of the browser window)
208 * @param integer $y The y attribute defines the top position of the
209 * element (e.g. y="0" places the element 0 pixels from the top of the browser
210 * window)
211 * @param int|string $width The width attribute defines the width the element
212 * @param int|string $height The height attribute defines the height the element
213 * @param string $text The text attribute defines the text the element
214 * @param string $styles The style attribute defines the style the element
215 * styles can be defined like CSS styles
217 * @return void
218 * @access public
220 * @see XMLWriter::startElement(), XMLWriter::writeAttribute(),
221 * XMLWriter::text(), XMLWriter::endElement()
223 function printElement($name, $x, $y, $width = '', $height = '',
224 $text = '', $styles = ''
226 $this->startElement($name);
227 $this->writeAttribute('width', $width);
228 $this->writeAttribute('height', $height);
229 $this->writeAttribute('x', $x);
230 $this->writeAttribute('y', $y);
231 $this->writeAttribute('style', $styles);
232 if (isset($text)) {
233 $this->writeAttribute('font-family', $this->font);
234 $this->writeAttribute('font-size', $this->fontSize);
235 $this->text($text);
237 $this->endElement();
241 * Draws Svg Line element
243 * Svg line element is drawn for connecting the tables.
244 * arrows are also drawn by specify its start and ending
245 * co-ordinates
247 * @param string $name Svg element name i.e line
248 * @param integer $x1 Defines the start of the line on the x-axis
249 * @param integer $y1 Defines the start of the line on the y-axis
250 * @param integer $x2 Defines the end of the line on the x-axis
251 * @param integer $y2 Defines the end of the line on the y-axis
252 * @param string $styles The style attribute defines the style the element
253 * styles can be defined like CSS styles
255 * @return void
256 * @access public
258 * @see XMLWriter::startElement(), XMLWriter::writeAttribute(),
259 * XMLWriter::endElement()
261 function printElementLine($name,$x1,$y1,$x2,$y2,$styles)
263 $this->startElement($name);
264 $this->writeAttribute('x1', $x1);
265 $this->writeAttribute('y1', $y1);
266 $this->writeAttribute('x2', $x2);
267 $this->writeAttribute('y2', $y2);
268 $this->writeAttribute('style', $styles);
269 $this->endElement();
274 * Svg Relation Schema Class
276 * Purpose of this class is to generate the SVG XML Document because
277 * SVG defines the graphics in XML format which is used for representing
278 * the database diagrams as vector image. This class actually helps
279 * in preparing SVG XML format.
281 * SVG XML is generated by using XMLWriter php extension and this class
282 * inherits Export_Relation_Schema class has common functionality added
283 * to this class
285 * @package PhpMyAdmin
286 * @name Svg_Relation_Schema
288 class PMA_Svg_Relation_Schema extends PMA_Export_Relation_Schema
291 private $_tables = array();
292 private $_relations = array();
293 private $_xMax = 0;
294 private $_yMax = 0;
295 private $_xMin = 100000;
296 private $_yMin = 100000;
297 private $_tablewidth;
300 * The "PMA_Svg_Relation_Schema" constructor
302 * Upon instantiation This starts writing the SVG XML document
303 * user will be prompted for download as .svg extension
305 * @see PMA_SVG
307 function __construct()
309 parent::__construct();
311 global $svg;
313 $this->setShowColor(isset($_REQUEST['svg_show_color']));
314 $this->setShowKeys(isset($_REQUEST['svg_show_keys']));
315 $this->setTableDimension(isset($_REQUEST['svg_show_table_dimension']));
316 $this->setAllTablesSameWidth(isset($_REQUEST['svg_all_tables_same_width']));
318 $svg = new PMA_SVG();
319 $svg->setTitle(
320 sprintf(
321 __('Schema of the %s database - Page %s'),
322 $GLOBALS['db'],
323 $this->pageNumber
326 $svg->SetAuthor('phpMyAdmin ' . PMA_VERSION);
327 $svg->setFont('Arial');
328 $svg->setFontSize('16px');
329 $svg->startSvgDoc('1000px', '1000px');
331 $alltables = $this->getTablesFromRequest();
333 foreach ($alltables as $table) {
334 if (! isset($this->_tables[$table])) {
335 $this->_tables[$table] = new Table_Stats_Svg(
336 $table, $svg->getFont(), $svg->getFontSize(), $this->pageNumber,
337 $this->_tablewidth, $this->showKeys, $this->tableDimension,
338 $this->offline
342 if ($this->sameWide) {
343 $this->_tables[$table]->width = $this->_tablewidth;
345 $this->_setMinMax($this->_tables[$table]);
347 $seen_a_relation = false;
348 foreach ($alltables as $one_table) {
349 $exist_rel = PMA_getForeigners($GLOBALS['db'], $one_table, '', 'both');
350 if (!$exist_rel) {
351 continue;
354 $seen_a_relation = true;
355 foreach ($exist_rel as $master_field => $rel) {
356 /* put the foreign table on the schema only if selected
357 * by the user
358 * (do not use array_search() because we would have to
359 * to do a === false and this is not PHP3 compatible)
361 if ($master_field != 'foreign_keys_data') {
362 if (in_array($rel['foreign_table'], $alltables)) {
363 $this->_addRelation(
364 $one_table, $svg->getFont(), $svg->getFontSize(),
365 $master_field, $rel['foreign_table'],
366 $rel['foreign_field'], $this->tableDimension
369 continue;
372 foreach ($rel as $one_key) {
373 if (!in_array($one_key['ref_table_name'], $alltables)) {
374 continue;
377 foreach ($one_key['index_list']
378 as $index => $one_field
380 $this->_addRelation(
381 $one_table, $svg->getFont(),
382 $svg->getFontSize(),
383 $one_field, $one_key['ref_table_name'],
384 $one_key['ref_index_list'][$index],
385 $this->tableDimension
391 if ($seen_a_relation) {
392 $this->_drawRelations();
395 $this->_drawTables();
396 $svg->endSvgDoc();
400 * Output Svg Document for download
402 * @return void
403 * @access public
405 function showOutput()
407 global $svg;
408 $svg->showOutput($this->getFileName('.svg'));
413 * Sets X and Y minimum and maximum for a table cell
415 * @param string $table The table name
417 * @return void
418 * @access private
420 private function _setMinMax($table)
422 $this->_xMax = max($this->_xMax, $table->x + $table->width);
423 $this->_yMax = max($this->_yMax, $table->y + $table->height);
424 $this->_xMin = min($this->_xMin, $table->x);
425 $this->_yMin = min($this->_yMin, $table->y);
429 * Defines relation objects
431 * @param string $masterTable The master table name
432 * @param string $font The font face
433 * @param int $fontSize Font size
434 * @param string $masterField The relation field in the master table
435 * @param string $foreignTable The foreign table name
436 * @param string $foreignField The relation field in the foreign table
437 * @param boolean $tableDimension Whether to display table position or not
439 * @access private
440 * @return void
442 * @see _setMinMax,Table_Stats_Svg::__construct(),
443 * Relation_Stats_Svg::__construct()
445 private function _addRelation(
446 $masterTable,$font,$fontSize, $masterField,
447 $foreignTable, $foreignField, $tableDimension
449 if (! isset($this->_tables[$masterTable])) {
450 $this->_tables[$masterTable] = new Table_Stats_Svg(
451 $masterTable, $font, $fontSize, $this->pageNumber,
452 $this->_tablewidth, false, $tableDimension
454 $this->_setMinMax($this->_tables[$masterTable]);
456 if (! isset($this->_tables[$foreignTable])) {
457 $this->_tables[$foreignTable] = new Table_Stats_Svg(
458 $foreignTable, $font, $fontSize, $this->pageNumber,
459 $this->_tablewidth, false, $tableDimension
461 $this->_setMinMax($this->_tables[$foreignTable]);
463 $this->_relations[] = new Relation_Stats_Svg(
464 $this->_tables[$masterTable], $masterField,
465 $this->_tables[$foreignTable], $foreignField
470 * Draws relation arrows and lines
471 * connects master table's master field to
472 * foreign table's foreign field
474 * @return void
475 * @access private
477 * @see Relation_Stats_Svg::relationDraw()
479 private function _drawRelations()
481 foreach ($this->_relations as $relation) {
482 $relation->relationDraw($this->showColor);
487 * Draws tables
489 * @return void
490 * @access private
492 * @see Table_Stats_Svg::Table_Stats_tableDraw()
494 private function _drawTables()
496 foreach ($this->_tables as $table) {
497 $table->tableDraw($this->showColor);