Merge remote-tracking branch 'origin/master' into drizzle
[phpmyadmin.git] / libraries / schema / User_Schema.class.php
blobd866329c386ff59685199d25c36ef5b38fb34bb4
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @package phpMyAdmin
6 */
8 /**
9 * This Class interacts with the user to gather the information
10 * about their tables for which they want to export the relational schema
11 * export options are shown to user from they can choose
14 class PMA_User_Schema
17 public $chosenPage;
18 public $autoLayoutForeign;
19 public $autoLayoutInternal;
20 public $pageNumber;
21 public $c_table_rows;
22 public $action;
24 public function setAction($value)
26 $this->action = $value;
28 /**
29 * This function will process the user defined pages
30 * and tables which will be exported as Relational schema
31 * you can set the table positions on the paper via scratchboard
32 * for table positions, put the x,y co-ordinates
34 * @param string $this->action It tells what the Schema is supposed to do
35 * create and select a page, generate schema etc
36 * @access public
39 public function processUserChoice()
41 global $action_choose,$db,$cfgRelation,$cfg;
43 if (isset($this->action)) {
44 switch ($this->action) {
45 case 'selectpage':
46 $this->chosenPage = $_REQUEST['chpage'];
47 if ($action_choose=="1") {
48 $this->deleteCoordinates(
49 $db,
50 $cfgRelation,
51 $this->chosenPage
53 $this->deletePages(
54 $db,
55 $cfgRelation,
56 $this->chosenPage
58 $this->chosenPage = 0;
60 break;
61 case 'createpage':
62 $this->pageNumber = PMA_REL_create_page(
63 $_POST['newpage'],
64 $cfgRelation,
65 $db
67 $this->autoLayoutForeign = isset($_POST['auto_layout_foreign']) ? "1":NULL;
68 $this->autoLayoutInternal = isset($_POST['auto_layout_internal']) ? "1":NULL;
69 $this->processRelations(
70 $db,
71 $this->pageNumber,
72 $cfgRelation
74 break;
75 case 'edcoord':
76 $this->chosenPage = $_POST['chpage'];
77 $this->c_table_rows = $_POST['c_table_rows'];
78 $this->_editCoordinates($db, $cfgRelation);
79 break;
80 case 'delete_old_references':
81 $this->_deleteTableRows(
82 $delrow,
83 $cfgRelation,
84 $db,
85 $this->chosenPage
87 break;
88 case 'process_export':
89 $this->_processExportSchema();
90 break;
92 } // end switch
93 } // end if (isset($do))
97 /**
98 * shows/displays the HTML FORM to create the page
100 * @param string db name of the selected database
101 * @return void
102 * @access public
104 public function showCreatePageDialog($db)
107 <form method="post" action="schema_edit.php" name="frm_create_page">
108 <fieldset>
109 <legend>
110 <?php echo __('Create a page') . "\n"; ?>
111 </legend>
112 <?php echo PMA_generate_common_hidden_inputs($db); ?>
113 <input type="hidden" name="do" value="createpage" />
114 <table>
115 <tr>
116 <td><label for="id_newpage"><?php echo __('Page name'); ?></label></td>
117 <td><input type="text" name="newpage" id="id_newpage" size="20" maxlength="50" /></td>
118 </tr>
119 <tr>
120 <td><?php echo __('Automatic layout based on'); ?></td>
121 <td>
122 <input type="checkbox" name="auto_layout_internal" id="id_auto_layout_internal" /><label for="id_auto_layout_internal">
123 <?php echo __('Internal relations'); ?></label><br />
124 <?php
126 * Check to see whether INNODB and PBXT storage engines are Available in MYSQL PACKAGE
127 * If available, then provide AutoLayout for Foreign Keys in Schema View
130 if (PMA_StorageEngine::isValid('InnoDB') || PMA_StorageEngine::isValid('PBXT')) {
132 <input type="checkbox" name="auto_layout_foreign" id="id_auto_layout_foreign" /><label for="id_auto_layout_foreign">
133 <?php echo __('FOREIGN KEY'); ?></label><br />
134 <?php
137 </td></tr>
138 </table>
139 </fieldset>
140 <fieldset class="tblFooters">
141 <input type="submit" value="<?php echo __('Go'); ?>" />
142 </fieldset>
143 </form>
144 <?php
148 * shows/displays the created page names in a drop down list
149 * User can select any page number and edit it using dashboard etc
151 * @return void
152 * @access public
154 public function selectPage()
156 global $db,$table,$cfgRelation;
157 $page_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
158 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\'';
159 $page_rs = PMA_query_as_controluser($page_query, false, PMA_DBI_QUERY_STORE);
160 if ($page_rs && PMA_DBI_num_rows($page_rs) > 0) {
162 <form method="get" action="schema_edit.php" name="frm_select_page">
163 <fieldset>
164 <legend>
165 <?php echo __('Please choose a page to edit') . "\n"; ?>
166 </legend>
167 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
168 <input type="hidden" name="do" value="selectpage" />
169 <select name="chpage" id="chpage" class="autosubmit">
170 <option value="0"><?php echo __('Select page'); ?></option>
171 <?php
172 while ($curr_page = PMA_DBI_fetch_assoc($page_rs)) {
173 echo "\n" . ' '
174 . '<option value="' . $curr_page['page_nr'] . '"';
175 if (isset($this->chosenPage) && $this->chosenPage == $curr_page['page_nr']) {
176 echo ' selected="selected"';
178 echo '>' . $curr_page['page_nr'] . ': ' . htmlspecialchars($curr_page['page_descr']) . '</option>';
179 } // end while
180 echo "\n";
182 </select>
183 <?php
184 $choices = array(
185 '0' => __('Edit'),
186 '1' => __('Delete')
188 PMA_display_html_radio('action_choose', $choices, '0', false);
189 unset($choices);
191 </fieldset>
192 <fieldset class="tblFooters">
193 <input type="submit" value="<?php echo __('Go'); ?>" /><br />
194 </fieldset>
195 </form>
196 <?php
197 } // end IF
198 echo "\n";
199 } // end function
202 * A dashboard is displayed to AutoLayout the position of tables
203 * users can drag n drop the tables and change their positions
205 * @return void
206 * @access public
208 public function showTableDashBoard()
210 global $db,$cfgRelation,$table,$cfg,$with_field_names;
212 * We will need an array of all tables in this db
214 $selectboxall = array('--');
215 $alltab_rs = PMA_DBI_query('SHOW TABLES FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE);
216 while ($val = @PMA_DBI_fetch_row($alltab_rs)) {
217 $selectboxall[] = $val[0];
221 * Now if we already have chosen a page number then we should
222 * show the tables involved
225 if (isset($this->chosenPage) && $this->chosenPage > 0) {
226 echo "\n";
228 <h2><?php echo __('Select Tables') ;?></h2>
229 <?php
230 $page_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
231 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
232 . ' AND pdf_page_number = \'' . PMA_sqlAddSlashes($this->chosenPage) . '\'';
233 $page_rs = PMA_query_as_controluser($page_query, false);
234 $array_sh_page = array();
235 while ($temp_sh_page = @PMA_DBI_fetch_assoc($page_rs)) {
236 $array_sh_page[] = $temp_sh_page;
239 * Display WYSIWYG parts
242 if (! isset($_POST['with_field_names']) && ! isset($_POST['showwysiwyg'])) {
243 $with_field_names = true;
245 $this->_displayScratchboardTables($array_sh_page);
248 <form method="post" action="schema_edit.php" name="edcoord">
249 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
250 <input type="hidden" name="chpage" value="<?php echo htmlspecialchars($this->chosenPage); ?>" />
251 <input type="hidden" name="do" value="edcoord" />
252 <table border="0">
253 <tr>
254 <th><?php echo __('Table'); ?></th>
255 <th><?php echo __('Delete'); ?></th>
256 <th>X</th>
257 <th>Y</th>
258 </tr>
259 <?php
260 if (isset($ctable)) {
261 unset($ctable);
264 $i = 0;
265 $odd_row = true;
266 foreach ($array_sh_page as $dummy_sh_page => $sh_page) {
267 $_mtab = $sh_page['table_name'];
268 $tabExist[$_mtab] = false;
269 echo "\n" . ' <tr class="noclick ';
270 if ($odd_row) {
271 echo 'odd';
272 } else {
273 echo 'even';
275 echo '">';
276 $odd_row != $odd_row;
277 echo "\n" . ' <td>'
278 . "\n" . ' <select name="c_table_' . $i . '[name]">';
279 foreach ($selectboxall as $key => $value) {
280 echo "\n" . ' <option value="' . htmlspecialchars($value) . '"';
281 if ($value == $sh_page['table_name']) {
282 echo ' selected="selected"';
283 $tabExist[$_mtab] = true;
285 echo '>' . htmlspecialchars($value) . '</option>';
287 echo "\n" . ' </select>'
288 . "\n" . ' </td>';
289 echo "\n" . ' <td>'
290 . "\n" . ' <input type="checkbox" id="id_c_table_' . $i .'" name="c_table_' . $i . '[delete]" value="y" /><label for="id_c_table_' . $i .'">' . __('Delete') . '</label>';
291 echo "\n" . ' </td>';
292 echo "\n" . ' <td>'
293 . "\n" . ' <input type="text" onchange="dragPlace(' . $i . ', \'x\', this.value)" name="c_table_' . $i . '[x]" value="' . $sh_page['x'] . '" />';
294 echo "\n" . ' </td>';
295 echo "\n" . ' <td>'
296 . "\n" . ' <input type="text" onchange="dragPlace(' . $i . ', \'y\', this.value)" name="c_table_' . $i . '[y]" value="' . $sh_page['y'] . '" />';
297 echo "\n" . ' </td>';
298 echo "\n" . ' </tr>';
299 $i++;
302 * Add one more empty row
304 echo "\n" . ' <tr class="noclick ';
305 if ($odd_row) {
306 echo 'odd';
307 } else {
308 echo 'even';
310 $odd_row != $odd_row;
311 echo '">';
312 echo "\n" . ' <td>'
313 . "\n" . ' <select name="c_table_' . $i . '[name]">';
314 foreach ($selectboxall as $key => $value) {
315 echo "\n" . ' <option value="' . htmlspecialchars($value) . '">' . htmlspecialchars($value) . '</option>';
317 echo "\n" . ' </select>'
318 . "\n" . ' </td>';
319 echo "\n" . ' <td>'
320 . "\n" . ' <input type="checkbox" id="id_c_table_' . $i .'" name="c_table_' . $i . '[delete]" value="y" /><label for="id_c_table_' . $i .'">' . __('Delete') . '</label>';
321 echo "\n" . ' </td>';
322 echo "\n" . ' <td>'
323 . "\n" . ' <input type="text" name="c_table_' . $i . '[x]" value="' . (isset($sh_page['x'])?$sh_page['x']:'') . '" />';
324 echo "\n" . ' </td>';
325 echo "\n" . ' <td>'
326 . "\n" . ' <input type="text" name="c_table_' . $i . '[y]" value="' . (isset($sh_page['y'])?$sh_page['y']:'') . '" />';
327 echo "\n" . ' </td>';
328 echo "\n" . ' </tr>';
329 echo "\n" . ' </table>' . "\n";
331 echo "\n" . ' <input type="hidden" name="c_table_rows" value="' . ($i + 1) . '" />';
332 echo "\n" . ' <input type="hidden" id="showwysiwyg" name="showwysiwyg" value="' . ((isset($showwysiwyg) && $showwysiwyg == '1') ? '1' : '0') . '" />';
333 echo "\n" . ' <input type="checkbox" name="with_field_names" ' . (isset($with_field_names) ? 'checked="checked"' : ''). ' />' . __('Column names') . '<br />';
334 echo "\n" . ' <input type="submit" value="' . __('Save') . '" />';
335 echo "\n" . '</form>' . "\n\n";
336 } // end if
338 $this->_deleteTables($db, $this->chosenPage, isset($tabExist));
342 * show Export relational schema generation options
343 * user can select export type of his own choice
344 * and the attributes related to it
346 * @return void
347 * @access public
350 public function displaySchemaGenerationOptions()
352 global $cfg,$pmaThemeImage,$db,$test_rs,$chpage;
354 <form method="post" action="schema_export.php">
355 <fieldset>
356 <legend>
357 <?php
358 echo PMA_generate_common_hidden_inputs($db);
359 if ($cfg['PropertiesIconic']) {
360 echo '<img class="icon ic_b_views" src="themes/dot.gif" />';
362 echo __('Display relational schema');
364 </legend>
365 <select name="export_type" id="export_type">
366 <option value="pdf" selected="selected">PDF</option>
367 <option value="svg">SVG</option>
368 <option value="dia">DIA</option>
369 <option value="visio">Visio</option>
370 <option value="eps">EPS</option>
371 </select>
372 <label><?php echo __('Select Export Relational Type');?></label><br />
373 <?php
374 if (isset($test_rs)) {
376 <label for="pdf_page_number_opt"><?php echo __('Page number:'); ?></label>
377 <select name="pdf_page_number" id="pdf_page_number_opt">
378 <?php
379 while ($pages = @PMA_DBI_fetch_assoc($test_rs)) {
380 echo ' <option value="' . $pages['page_nr'] . '">'
381 . $pages['page_nr'] . ': ' . htmlspecialchars($pages['page_descr']) . '</option>' . "\n";
382 } // end while
383 PMA_DBI_free_result($test_rs);
384 unset($test_rs);
386 </select><br />
387 <?php } else { ?>
388 <input type="hidden" name="pdf_page_number" value="<?php echo htmlspecialchars($this->chosenPage); ?>" />
389 <?php } ?>
390 <input type="hidden" name="do" value="process_export" />
391 <input type="hidden" name="chpage" value="<?php echo $chpage; ?>" />
392 <input type="checkbox" name="show_grid" id="show_grid_opt" />
393 <label for="show_grid_opt"><?php echo __('Show grid'); ?></label><br />
394 <input type="checkbox" name="show_color" id="show_color_opt" checked="checked" />
395 <label for="show_color_opt"><?php echo __('Show color'); ?></label><br />
396 <input type="checkbox" name="show_table_dimension" id="show_table_dim_opt" />
397 <label for="show_table_dim_opt"><?php echo __('Show dimension of tables'); ?>
398 </label><br />
399 <input type="checkbox" name="all_table_same_wide" id="all_table_same_wide" />
400 <label for="all_table_same_wide"><?php echo __('Display all tables with the same width'); ?>
401 </label><br />
402 <input type="checkbox" name="with_doc" id="with_doc" checked="checked" />
403 <label for="with_doc"><?php echo __('Data Dictionary'); ?></label><br />
404 <input type="checkbox" name="show_keys" id="show_keys" />
405 <label for="show_keys"><?php echo __('Only show keys'); ?></label><br />
406 <select name="orientation" id="orientation_opt" onchange="refreshDragOption('pdflayout');" >
407 <option value="L"><?php echo __('Landscape');?></option>
408 <option value="P"><?php echo __('Portrait');?></option>
409 </select>
410 <label for="orientation_opt"><?php echo __('Orientation'); ?></label>
411 <br />
412 <select name="paper" id="paper_opt" onchange="refreshDragOption('pdflayout');">
413 <?php
414 foreach ($cfg['PDFPageSizes'] as $key => $val) {
415 echo '<option value="' . $val . '"';
416 if ($val == $cfg['PDFDefaultPageSize']) {
417 echo ' selected="selected"';
419 echo ' >' . $val . '</option>' . "\n";
422 </select>
423 <label for="paper_opt"><?php echo __('Paper size'); ?></label>
424 </fieldset>
425 <fieldset class="tblFooters">
426 <input type="submit" value="<?php echo __('Go'); ?>" />
427 </fieldset>
428 </form>
429 <?php
433 * Check if there are tables that need to be deleted in dashboard,
434 * if there are, ask the user for allowance
436 * @param string db name of database selected
437 * @param integer chpage selected page
438 * @param array tabExist
439 * @return void
440 * @access private
442 private function _deleteTables($db, $chpage, $tabExist)
444 $_strtrans = '';
445 $_strname = '';
446 $shoot = false;
447 if (!empty($tabExist) && is_array($tabExist)) {
448 foreach ($tabExist as $key => $value) {
449 if (!$value) {
450 $_strtrans .= '<input type="hidden" name="delrow[]" value="' . htmlspecialchars($key) . '" />' . "\n";
451 $_strname .= '<li>' . htmlspecialchars($key) . '</li>' . "\n";
452 $shoot = true;
455 if ($shoot) {
456 echo '<form action="schema_edit.php" method="post">' . "\n"
457 . PMA_generate_common_hidden_inputs($db, $table)
458 . '<input type="hidden" name="do" value="delete_old_references" />' . "\n"
459 . '<input type="hidden" name="chpage" value="' . htmlspecialchars($chpage) . '" />' . "\n"
460 . __('The current page has references to tables that no longer exist. Would you like to delete those references?')
461 . '<ul>' . "\n"
462 . $_strname
463 . '</ul>' . "\n"
464 . $_strtrans
465 . '<input type="submit" value="' . __('Go') . '" />' . "\n"
466 . '</form>';
473 * Check if there are tables that need to be deleted in dashboard,
474 * if there are, ask the user for allowance
476 * @return void
477 * @access private
479 private function _displayScratchboardTables($array_sh_page)
481 global $with_field_names,$cfg,$db;
483 <script type="text/javascript" src="./js/dom-drag.js"></script>
484 <form method="post" action="schema_edit.php" name="dragdrop">
485 <input type="button" name="dragdrop" value="<?php echo __('Toggle scratchboard'); ?>" onclick="ToggleDragDrop('pdflayout');" />
486 <input type="button" name="dragdropreset" value="<?php echo __('Reset'); ?>" onclick="resetDrag();" />
487 </form>
488 <div id="pdflayout" class="pdflayout" style="visibility: hidden;">
489 <?php
490 $draginit = '';
491 $draginit2 = '';
492 $reset_draginit = '';
493 $i = 0;
494 foreach ($array_sh_page as $key => $temp_sh_page) {
495 $drag_x = $temp_sh_page['x'];
496 $drag_y = $temp_sh_page['y'];
498 $draginit2 .= ' Drag.init(getElement("table_' . $i . '"), null, 0, parseInt(myid.style.width)-2, 0, parseInt(myid.style.height)-5);' . "\n";
499 $draginit2 .= ' getElement("table_' . $i . '").onDrag = function (x, y) { document.edcoord.elements["c_table_' . $i . '[x]"].value = parseInt(x); document.edcoord.elements["c_table_' . $i . '[y]"].value = parseInt(y) }' . "\n";
500 $draginit .= ' getElement("table_' . $i . '").style.left = "' . $drag_x . 'px";' . "\n";
501 $draginit .= ' getElement("table_' . $i . '").style.top = "' . $drag_y . 'px";' . "\n";
502 $reset_draginit .= ' getElement("table_' . $i . '").style.left = "2px";' . "\n";
503 $reset_draginit .= ' getElement("table_' . $i . '").style.top = "' . (15 * $i) . 'px";' . "\n";
504 $reset_draginit .= ' document.edcoord.elements["c_table_' . $i . '[x]"].value = "2"' . "\n";
505 $reset_draginit .= ' document.edcoord.elements["c_table_' . $i . '[y]"].value = "' . (15 * $i) . '"' . "\n";
507 $local_query = PMA_DBI_get_columns_sql($db, $temp_sh_page['table_name']);
508 $fields_rs = PMA_DBI_query($local_query);
509 unset($local_query);
510 $fields_cnt = PMA_DBI_num_rows($fields_rs);
512 echo '<div id="table_' . $i . '" class="pdflayout_table"><u>' . $temp_sh_page['table_name'] . '</u>';
513 if (isset($with_field_names)) {
514 while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
515 echo '<br />' . htmlspecialchars($row['Field']) . "\n";
518 echo '</div>' . "\n";
519 PMA_DBI_free_result($fields_rs);
520 unset($fields_rs);
521 $i++;
524 </div>
525 <script type="text/javascript">
526 //<![CDATA[
527 function PDFinit() {
528 refreshLayout();
529 myid = getElement('pdflayout');
530 <?php echo $draginit; ?>
531 TableDragInit();
534 function TableDragInit() {
535 myid = getElement('pdflayout');
536 <?php echo $draginit2; ?>
539 function resetDrag() {
540 <?php echo $reset_draginit; ?>
542 //]]>
543 </script>
544 <?php
548 * delete the table rows with table co-ordinates
550 * @param int delrow delete selected table from list of tables
551 * @param array cfgRelation relation settings
552 * @param string db database name
553 * @param integer chpage selected page for adding relations etc
554 * @return void
555 * @access private
557 private function _deleteTableRows($delrow,$cfgRelation,$db,$chpage)
559 foreach ($delrow as $current_row) {
560 $del_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' ' . "\n"
561 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\'' . "\n"
562 . ' AND table_name = \'' . PMA_sqlAddSlashes($current_row) . '\'' . "\n"
563 . ' AND pdf_page_number = \'' . PMA_sqlAddSlashes($chpage) . '\'';
564 echo $del_query;
565 PMA_query_as_controluser($del_query, false);
570 * get all the export options and verify
571 * call and include the appropriate Schema Class depending on $export_type
573 * @return void
574 * @access private
576 private function _processExportSchema()
579 * Settings for relation stuff
581 require_once './libraries/transformations.lib.php';
582 require_once './libraries/Index.class.php';
584 * default is PDF, otherwise validate it's only letters a-z
586 global $db,$export_type;
587 if (!isset($export_type) || !preg_match('/^[a-zA-Z]+$/', $export_type)) {
588 $export_type = 'pdf';
591 PMA_DBI_select_db($db);
593 include("./libraries/schema/".ucfirst($export_type)."_Relation_Schema.class.php");
594 $obj_schema = eval("new PMA_".ucfirst($export_type)."_Relation_Schema();");
598 * delete X and Y coordinates
600 * @param string db The database name
601 * @param array cfgRelation relation settings
602 * @param integer choosePage selected page for adding relations etc
603 * @return void
604 * @access private
606 public function deleteCoordinates($db, $cfgRelation, $choosePage)
608 $query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
609 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
610 . ' AND pdf_page_number = \'' . PMA_sqlAddSlashes($choosePage) . '\'';
611 PMA_query_as_controluser($query, false);
615 * delete pages
617 * @param string db The database name
618 * @param array cfgRelation relation settings
619 * @param integer choosePage selected page for adding relations etc
620 * @return void
621 * @access private
623 public function deletePages($db, $cfgRelation, $choosePage)
625 $query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
626 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
627 . ' AND page_nr = \'' . PMA_sqlAddSlashes($choosePage) . '\'';
628 PMA_query_as_controluser($query, false);
632 * process internal and foreign key relations
634 * @param string db The database name
635 * @param array cfgRelation relation settings
636 * @param integer pageNumber document number/Id
637 * @return void
638 * @access private
640 public function processRelations($db, $pageNumber, $cfgRelation)
643 * A u t o m a t i c l a y o u t
645 * There are 2 kinds of relations in PMA
646 * 1) Internal Relations 2) Foreign Key Relations
648 if (isset($this->autoLayoutInternal) || isset($this->autoLayoutForeign)) {
649 $all_tables = array();
652 if (isset($this->autoLayoutForeign)) {
654 * get the tables list
655 * who support FOREIGN KEY, it's not
656 * important that we group together InnoDB tables
657 * and PBXT tables, as this logic is just to put
658 * the tables on the layout, not to determine relations
660 $tables = PMA_DBI_get_tables_full($db);
661 $foreignkey_tables = array();
662 foreach ($tables as $table_name => $table_properties) {
663 if (PMA_foreignkey_supported($table_properties['ENGINE'])) {
664 $foreignkey_tables[] = $table_name;
667 $all_tables = $foreignkey_tables;
669 * could be improved by finding the tables which have the
670 * most references keys and placing them at the beginning
671 * of the array (so that they are all center of schema)
673 unset($tables, $foreignkey_tables);
676 if (isset($this->autoLayoutInternal)) {
678 * get the tables list who support Internal Relations;
679 * This type of relations will be created when
680 * you setup the PMA tables correctly
682 $master_tables = 'SELECT COUNT(master_table), master_table'
683 . ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
684 . ' WHERE master_db = \'' . $db . '\''
685 . ' GROUP BY master_table'
686 . ' ORDER BY ' . PMA_backquote('COUNT(master_table)') . ' DESC ';
687 $master_tables_rs = PMA_query_as_controluser($master_tables, false, PMA_DBI_QUERY_STORE);
688 if ($master_tables_rs && PMA_DBI_num_rows($master_tables_rs) > 0) {
689 /* first put all the master tables at beginning
690 * of the list, so they are near the center of
691 * the schema
693 while (list(, $master_table) = PMA_DBI_fetch_row($master_tables_rs)) {
694 $all_tables[] = $master_table;
697 /* Now for each master, add its foreigns into an array
698 * of foreign tables, if not already there
699 * (a foreign might be foreign for more than
700 * one table, and might be a master itself)
703 $foreign_tables = array();
704 foreach ($all_tables as $master_table) {
705 $foreigners = PMA_getForeigners($db, $master_table);
706 foreach ($foreigners as $foreigner) {
707 if (!in_array($foreigner['foreign_table'], $foreign_tables)) {
708 $foreign_tables[] = $foreigner['foreign_table'];
714 * Now merge the master and foreign arrays/tables
716 foreach ($foreign_tables as $foreign_table) {
717 if (!in_array($foreign_table, $all_tables)) {
718 $all_tables[] = $foreign_table;
724 if (isset($this->autoLayoutInternal) || isset($this->autoLayoutForeign)) {
725 $this->addRelationCoordinates($all_tables,$pageNumber,$db, $cfgRelation);
728 $this->chosenPage = $pageNumber;
732 * Add X and Y coordinates for a table
734 * @param string db The database name
735 * @param array cfgRelation relation settings
736 * @param integer pageNumber document number/Id
737 * @param array all_tables A list of all tables involved
738 * @return void
739 * @access private
741 public function addRelationCoordinates($all_tables,$pageNumber,$db, $cfgRelation)
744 * Now generate the coordinates for the schema
745 * in a clockwise spiral and add to co-ordinates table
747 $pos_x = 300;
748 $pos_y = 300;
749 $delta = 110;
750 $delta_mult = 1.10;
751 $direction = "right";
752 foreach ($all_tables as $current_table) {
754 * save current table's coordinates
756 $insert_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
757 . '(db_name, table_name, pdf_page_number, x, y) '
758 . 'VALUES (\'' . PMA_sqlAddSlashes($db) . '\', \'' . PMA_sqlAddSlashes($current_table) . '\',' . $pageNumber . ',' . $pos_x . ',' . $pos_y . ')';
759 PMA_query_as_controluser($insert_query, false);
762 * compute for the next table
764 switch ($direction) {
765 case 'right':
766 $pos_x += $delta;
767 $direction = "down";
768 $delta *= $delta_mult;
769 break;
770 case 'down':
771 $pos_y += $delta;
772 $direction = "left";
773 $delta *= $delta_mult;
774 break;
775 case 'left':
776 $pos_x -= $delta;
777 $direction = "up";
778 $delta *= $delta_mult;
779 break;
780 case 'up':
781 $pos_y -= $delta;
782 $direction = "right";
783 $delta *= $delta_mult;
784 break;
790 * update X and Y coordinates for a table
792 * @param string db The database name
793 * @param array cfgRelation relation settings
794 * @return void
795 * @access private
797 private function _editCoordinates($db, $cfgRelation)
799 for ($i = 0; $i < $this->c_table_rows; $i++) {
800 $arrvalue = 'c_table_' . $i;
801 global $$arrvalue;
802 $arrvalue = $$arrvalue;
803 if (! isset($arrvalue['x']) || $arrvalue['x'] == '') {
804 $arrvalue['x'] = 0;
806 if (! isset($arrvalue['y']) || $arrvalue['y'] == '') {
807 $arrvalue['y'] = 0;
809 if (isset($arrvalue['name']) && $arrvalue['name'] != '--') {
810 $test_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
811 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
812 . ' AND table_name = \'' . PMA_sqlAddSlashes($arrvalue['name']) . '\''
813 . ' AND pdf_page_number = \'' . PMA_sqlAddSlashes($this->chosenPage) . '\'';
814 $test_rs = PMA_query_as_controluser($test_query, false, PMA_DBI_QUERY_STORE);
815 //echo $test_query;
816 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
817 if (isset($arrvalue['delete']) && $arrvalue['delete'] == 'y') {
818 $ch_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
819 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
820 . ' AND table_name = \'' . PMA_sqlAddSlashes($arrvalue['name']) . '\''
821 . ' AND pdf_page_number = \'' . PMA_sqlAddSlashes($this->chosenPage) . '\'';
822 } else {
823 $ch_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
824 . 'SET x = ' . $arrvalue['x'] . ', y= ' . $arrvalue['y']
825 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
826 . ' AND table_name = \'' . PMA_sqlAddSlashes($arrvalue['name']) . '\''
827 . ' AND pdf_page_number = \'' . PMA_sqlAddSlashes($this->chosenPage) . '\'';
829 } else {
830 $ch_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
831 . '(db_name, table_name, pdf_page_number, x, y) '
832 . 'VALUES (\'' . PMA_sqlAddSlashes($db) . '\', \'' . PMA_sqlAddSlashes($arrvalue['name']) . '\', \'' . PMA_sqlAddSlashes($this->chosenPage) . '\',' . $arrvalue['x'] . ',' . $arrvalue['y'] . ')';
834 //echo $ch_query;
835 PMA_query_as_controluser($ch_query, false);
836 } // end if
837 } // end for