Remove JS function: getElement
[phpmyadmin.git] / libraries / schema / User_Schema.class.php
blob95d60e127b0b7b7988d86c3ed2a1a5e672c56df1
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 global $table;
445 $_strtrans = '';
446 $_strname = '';
447 $shoot = false;
448 if (!empty($tabExist) && is_array($tabExist)) {
449 foreach ($tabExist as $key => $value) {
450 if (!$value) {
451 $_strtrans .= '<input type="hidden" name="delrow[]" value="' . htmlspecialchars($key) . '" />' . "\n";
452 $_strname .= '<li>' . htmlspecialchars($key) . '</li>' . "\n";
453 $shoot = true;
456 if ($shoot) {
457 echo '<form action="schema_edit.php" method="post">' . "\n"
458 . PMA_generate_common_hidden_inputs($db, $table)
459 . '<input type="hidden" name="do" value="delete_old_references" />' . "\n"
460 . '<input type="hidden" name="chpage" value="' . htmlspecialchars($chpage) . '" />' . "\n"
461 . __('The current page has references to tables that no longer exist. Would you like to delete those references?')
462 . '<ul>' . "\n"
463 . $_strname
464 . '</ul>' . "\n"
465 . $_strtrans
466 . '<input type="submit" value="' . __('Go') . '" />' . "\n"
467 . '</form>';
474 * Check if there are tables that need to be deleted in dashboard,
475 * if there are, ask the user for allowance
477 * @return void
478 * @access private
480 private function _displayScratchboardTables($array_sh_page)
482 global $with_field_names,$cfg,$db;
484 <script type="text/javascript" src="./js/dom-drag.js"></script>
485 <form method="post" action="schema_edit.php" name="dragdrop">
486 <input type="button" name="dragdrop" value="<?php echo __('Toggle scratchboard'); ?>" onclick="ToggleDragDrop('pdflayout');" />
487 <input type="button" name="dragdropreset" value="<?php echo __('Reset'); ?>" onclick="resetDrag();" />
488 </form>
489 <div id="pdflayout" class="pdflayout" style="visibility: hidden;">
490 <?php
491 $draginit = '';
492 $draginit2 = '';
493 $reset_draginit = '';
494 $i = 0;
495 foreach ($array_sh_page as $key => $temp_sh_page) {
496 $drag_x = $temp_sh_page['x'];
497 $drag_y = $temp_sh_page['y'];
499 $draginit2 .= ' Drag.init($("#table_' . $i . '")[0], null, 0, parseInt(myid.style.width)-2, 0, parseInt(myid.style.height)-5);' . "\n";
500 $draginit2 .= ' $("#table_' . $i . '")[0].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";
501 $draginit .= ' $("#table_' . $i . '")[0].style.left = "' . $drag_x . 'px";' . "\n";
502 $draginit .= ' $("#table_' . $i . '")[0].style.top = "' . $drag_y . 'px";' . "\n";
503 $reset_draginit .= ' $("#table_' . $i . '")[0].style.left = "2px";' . "\n";
504 $reset_draginit .= ' $("#table_' . $i . '")[0].style.top = "' . (15 * $i) . 'px";' . "\n";
505 $reset_draginit .= ' document.edcoord.elements["c_table_' . $i . '[x]"].value = "2"' . "\n";
506 $reset_draginit .= ' document.edcoord.elements["c_table_' . $i . '[y]"].value = "' . (15 * $i) . '"' . "\n";
508 $local_query = 'SHOW FIELDS FROM '
509 . PMA_backquote($temp_sh_page['table_name'])
510 . ' FROM ' . PMA_backquote($db);
511 $fields_rs = PMA_DBI_query($local_query);
512 unset($local_query);
513 $fields_cnt = PMA_DBI_num_rows($fields_rs);
515 echo '<div id="table_' . $i . '" class="pdflayout_table"><u>' . $temp_sh_page['table_name'] . '</u>';
516 if (isset($with_field_names)) {
517 while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
518 echo '<br />' . htmlspecialchars($row['Field']) . "\n";
521 echo '</div>' . "\n";
522 PMA_DBI_free_result($fields_rs);
523 unset($fields_rs);
524 $i++;
527 </div>
528 <script type="text/javascript">
529 //<![CDATA[
530 function PDFinit() {
531 refreshLayout();
532 myid = $('#pdflayout')[0];
533 <?php echo $draginit; ?>
534 TableDragInit();
537 function TableDragInit() {
538 myid = $('#pdflayout')[0];
539 <?php echo $draginit2; ?>
542 function resetDrag() {
543 <?php echo $reset_draginit; ?>
545 //]]>
546 </script>
547 <?php
551 * delete the table rows with table co-ordinates
553 * @param int delrow delete selected table from list of tables
554 * @param array cfgRelation relation settings
555 * @param string db database name
556 * @param integer chpage selected page for adding relations etc
557 * @return void
558 * @access private
560 private function _deleteTableRows($delrow,$cfgRelation,$db,$chpage)
562 foreach ($delrow as $current_row) {
563 $del_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' ' . "\n"
564 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\'' . "\n"
565 . ' AND table_name = \'' . PMA_sqlAddSlashes($current_row) . '\'' . "\n"
566 . ' AND pdf_page_number = \'' . PMA_sqlAddSlashes($chpage) . '\'';
567 echo $del_query;
568 PMA_query_as_controluser($del_query, false);
573 * get all the export options and verify
574 * call and include the appropriate Schema Class depending on $export_type
576 * @return void
577 * @access private
579 private function _processExportSchema()
582 * Settings for relation stuff
584 require_once './libraries/transformations.lib.php';
585 require_once './libraries/Index.class.php';
587 * default is PDF, otherwise validate it's only letters a-z
589 global $db,$export_type;
590 if (!isset($export_type) || !preg_match('/^[a-zA-Z]+$/', $export_type)) {
591 $export_type = 'pdf';
594 PMA_DBI_select_db($db);
596 include("./libraries/schema/".ucfirst($export_type)."_Relation_Schema.class.php");
597 $obj_schema = eval("new PMA_".ucfirst($export_type)."_Relation_Schema();");
601 * delete X and Y coordinates
603 * @param string db The database name
604 * @param array cfgRelation relation settings
605 * @param integer choosePage selected page for adding relations etc
606 * @return void
607 * @access private
609 public function deleteCoordinates($db, $cfgRelation, $choosePage)
611 $query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
612 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
613 . ' AND pdf_page_number = \'' . PMA_sqlAddSlashes($choosePage) . '\'';
614 PMA_query_as_controluser($query, false);
618 * delete pages
620 * @param string db The database name
621 * @param array cfgRelation relation settings
622 * @param integer choosePage selected page for adding relations etc
623 * @return void
624 * @access private
626 public function deletePages($db, $cfgRelation, $choosePage)
628 $query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
629 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
630 . ' AND page_nr = \'' . PMA_sqlAddSlashes($choosePage) . '\'';
631 PMA_query_as_controluser($query, false);
635 * process internal and foreign key relations
637 * @param string db The database name
638 * @param array cfgRelation relation settings
639 * @param integer pageNumber document number/Id
640 * @return void
641 * @access private
643 public function processRelations($db, $pageNumber, $cfgRelation)
646 * A u t o m a t i c l a y o u t
648 * There are 2 kinds of relations in PMA
649 * 1) Internal Relations 2) Foreign Key Relations
651 if (isset($this->autoLayoutInternal) || isset($this->autoLayoutForeign)) {
652 $all_tables = array();
655 if (isset($this->autoLayoutForeign)) {
657 * get the tables list
658 * who support FOREIGN KEY, it's not
659 * important that we group together InnoDB tables
660 * and PBXT tables, as this logic is just to put
661 * the tables on the layout, not to determine relations
663 $tables = PMA_DBI_get_tables_full($db);
664 $foreignkey_tables = array();
665 foreach ($tables as $table_name => $table_properties) {
666 if (PMA_foreignkey_supported($table_properties['ENGINE'])) {
667 $foreignkey_tables[] = $table_name;
670 $all_tables = $foreignkey_tables;
672 * could be improved by finding the tables which have the
673 * most references keys and placing them at the beginning
674 * of the array (so that they are all center of schema)
676 unset($tables, $foreignkey_tables);
679 if (isset($this->autoLayoutInternal)) {
681 * get the tables list who support Internal Relations;
682 * This type of relations will be created when
683 * you setup the PMA tables correctly
685 $master_tables = 'SELECT COUNT(master_table), master_table'
686 . ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
687 . ' WHERE master_db = \'' . PMA_sqlAddSlashes($db) . '\''
688 . ' GROUP BY master_table'
689 . ' ORDER BY COUNT(master_table) DESC';
690 $master_tables_rs = PMA_query_as_controluser($master_tables, false, PMA_DBI_QUERY_STORE);
691 if ($master_tables_rs && PMA_DBI_num_rows($master_tables_rs) > 0) {
692 /* first put all the master tables at beginning
693 * of the list, so they are near the center of
694 * the schema
696 while (list(, $master_table) = PMA_DBI_fetch_row($master_tables_rs)) {
697 $all_tables[] = $master_table;
700 /* Now for each master, add its foreigns into an array
701 * of foreign tables, if not already there
702 * (a foreign might be foreign for more than
703 * one table, and might be a master itself)
706 $foreign_tables = array();
707 foreach ($all_tables as $master_table) {
708 $foreigners = PMA_getForeigners($db, $master_table);
709 foreach ($foreigners as $foreigner) {
710 if (!in_array($foreigner['foreign_table'], $foreign_tables)) {
711 $foreign_tables[] = $foreigner['foreign_table'];
717 * Now merge the master and foreign arrays/tables
719 foreach ($foreign_tables as $foreign_table) {
720 if (!in_array($foreign_table, $all_tables)) {
721 $all_tables[] = $foreign_table;
727 if (isset($this->autoLayoutInternal) || isset($this->autoLayoutForeign)) {
728 $this->addRelationCoordinates($all_tables,$pageNumber,$db, $cfgRelation);
731 $this->chosenPage = $pageNumber;
735 * Add X and Y coordinates for a table
737 * @param string db The database name
738 * @param array cfgRelation relation settings
739 * @param integer pageNumber document number/Id
740 * @param array all_tables A list of all tables involved
741 * @return void
742 * @access private
744 public function addRelationCoordinates($all_tables,$pageNumber,$db, $cfgRelation)
747 * Now generate the coordinates for the schema
748 * in a clockwise spiral and add to co-ordinates table
750 $pos_x = 300;
751 $pos_y = 300;
752 $delta = 110;
753 $delta_mult = 1.10;
754 $direction = "right";
755 foreach ($all_tables as $current_table) {
757 * save current table's coordinates
759 $insert_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
760 . '(db_name, table_name, pdf_page_number, x, y) '
761 . 'VALUES (\'' . PMA_sqlAddSlashes($db) . '\', \'' . PMA_sqlAddSlashes($current_table) . '\',' . $pageNumber . ',' . $pos_x . ',' . $pos_y . ')';
762 PMA_query_as_controluser($insert_query, false);
765 * compute for the next table
767 switch ($direction) {
768 case 'right':
769 $pos_x += $delta;
770 $direction = "down";
771 $delta *= $delta_mult;
772 break;
773 case 'down':
774 $pos_y += $delta;
775 $direction = "left";
776 $delta *= $delta_mult;
777 break;
778 case 'left':
779 $pos_x -= $delta;
780 $direction = "up";
781 $delta *= $delta_mult;
782 break;
783 case 'up':
784 $pos_y -= $delta;
785 $direction = "right";
786 $delta *= $delta_mult;
787 break;
793 * update X and Y coordinates for a table
795 * @param string db The database name
796 * @param array cfgRelation relation settings
797 * @return void
798 * @access private
800 private function _editCoordinates($db, $cfgRelation)
802 for ($i = 0; $i < $this->c_table_rows; $i++) {
803 $arrvalue = 'c_table_' . $i;
804 global $$arrvalue;
805 $arrvalue = $$arrvalue;
806 if (! isset($arrvalue['x']) || $arrvalue['x'] == '') {
807 $arrvalue['x'] = 0;
809 if (! isset($arrvalue['y']) || $arrvalue['y'] == '') {
810 $arrvalue['y'] = 0;
812 if (isset($arrvalue['name']) && $arrvalue['name'] != '--') {
813 $test_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
814 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
815 . ' AND table_name = \'' . PMA_sqlAddSlashes($arrvalue['name']) . '\''
816 . ' AND pdf_page_number = \'' . PMA_sqlAddSlashes($this->chosenPage) . '\'';
817 $test_rs = PMA_query_as_controluser($test_query, false, PMA_DBI_QUERY_STORE);
818 //echo $test_query;
819 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
820 if (isset($arrvalue['delete']) && $arrvalue['delete'] == 'y') {
821 $ch_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
822 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
823 . ' AND table_name = \'' . PMA_sqlAddSlashes($arrvalue['name']) . '\''
824 . ' AND pdf_page_number = \'' . PMA_sqlAddSlashes($this->chosenPage) . '\'';
825 } else {
826 $ch_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
827 . 'SET x = ' . $arrvalue['x'] . ', y= ' . $arrvalue['y']
828 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
829 . ' AND table_name = \'' . PMA_sqlAddSlashes($arrvalue['name']) . '\''
830 . ' AND pdf_page_number = \'' . PMA_sqlAddSlashes($this->chosenPage) . '\'';
832 } else {
833 $ch_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
834 . '(db_name, table_name, pdf_page_number, x, y) '
835 . 'VALUES (\'' . PMA_sqlAddSlashes($db) . '\', \'' . PMA_sqlAddSlashes($arrvalue['name']) . '\', \'' . PMA_sqlAddSlashes($this->chosenPage) . '\',' . $arrvalue['x'] . ',' . $arrvalue['y'] . ')';
837 //echo $ch_query;
838 PMA_query_as_controluser($ch_query, false);
839 } // end if
840 } // end for