Comments
[phpmyadmin.git] / libraries / schema / User_Schema.class.php
blobaa39ec5a991e1e3e13aeeb83c9bc613c54d9b5c5
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,$query_default_option;
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($db, $cfgRelation, $this->chosenPage, $query_default_option);
49 $this->deletePages($db, $cfgRelation, $this->chosenPage, $query_default_option);
50 $this->chosenPage = 0;
52 break;
53 case 'createpage':
54 $this->pageNumber = PMA_REL_create_page($_POST['newpage'], $cfgRelation, $db, $query_default_option);
55 $this->autoLayoutForeign = isset($_POST['auto_layout_foreign']) ? "1":NULL;
56 $this->autoLayoutInternal = isset($_POST['auto_layout_internal']) ? "1":NULL;
57 $this->processRelations($db, $this->pageNumber,$cfgRelation,$query_default_option);
58 break;
59 case 'edcoord':
60 $this->chosenPage = $_POST['chpage'];
61 $this->c_table_rows = $_POST['c_table_rows'];
62 $this->_editCoordinates($db, $cfgRelation,$query_default_option);
63 break;
64 case 'delete_old_references':
65 $this->_deleteTableRows($delrow,$cfgRelation,$db,$this->chosenPage);
66 break;
67 case 'process_export':
68 $this->_processExportSchema();
69 break;
71 } // end switch
72 } // end if (isset($do))
76 /**
77 * shows/displays the HTML FORM to create the page
79 * @param string db name of the selected database
80 * @return void
81 * @access public
83 public function showCreatePageDialog($db)
86 <form method="post" action="schema_edit.php" name="frm_create_page">
87 <fieldset>
88 <legend>
89 <?php echo __('Create a page') . "\n"; ?>
90 </legend>
91 <?php echo PMA_generate_common_hidden_inputs($db); ?>
92 <input type="hidden" name="do" value="createpage" />
93 <table>
94 <tr>
95 <td><label for="id_newpage"><?php echo __('Page name'); ?></label></td>
96 <td><input type="text" name="newpage" id="id_newpage" size="20" maxlength="50" /></td>
97 </tr>
98 <tr>
99 <td><?php echo __('Automatic layout based on'); ?></td>
100 <td>
101 <input type="checkbox" name="auto_layout_internal" id="id_auto_layout_internal" /><label for="id_auto_layout_internal">
102 <?php echo __('Internal relations'); ?></label><br />
103 <?php
105 * Check to see whether INNODB and PBXT storage engines are Available in MYSQL PACKAGE
106 * If available, then provide AutoLayout for Foreign Keys in Schema View
109 if (PMA_StorageEngine::isValid('InnoDB') || PMA_StorageEngine::isValid('PBXT')) {
111 <input type="checkbox" name="auto_layout_foreign" id="id_auto_layout_foreign" /><label for="id_auto_layout_foreign">
112 <?php echo __('FOREIGN KEY'); ?></label><br />
113 <?php
116 </td></tr>
117 </table>
118 </fieldset>
119 <fieldset class="tblFooters">
120 <input type="submit" value="<?php echo __('Go'); ?>" />
121 </fieldset>
122 </form>
123 <?php
127 * shows/displays the created page names in a drop down list
128 * User can select any page number and edit it using dashboard etc
130 * @return void
131 * @access public
133 public function selectPage()
135 global $db,$table,$query_default_option,$cfgRelation;
136 $page_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
137 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\'';
138 $page_rs = PMA_query_as_controluser($page_query, false, $query_default_option);
139 if ($page_rs && PMA_DBI_num_rows($page_rs) > 0) {
141 <form method="get" action="schema_edit.php" name="frm_select_page">
142 <fieldset>
143 <legend>
144 <?php echo __('Please choose a page to edit') . "\n"; ?>
145 </legend>
146 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
147 <input type="hidden" name="do" value="selectpage" />
148 <select name="chpage" id="chpage" onchange="this.form.submit()">
149 <option value="0"><?php echo __('Select page'); ?></option>
150 <?php
151 while ($curr_page = PMA_DBI_fetch_assoc($page_rs)) {
152 echo "\n" . ' '
153 . '<option value="' . $curr_page['page_nr'] . '"';
154 if (isset($this->chosenPage) && $this->chosenPage == $curr_page['page_nr']) {
155 echo ' selected="selected"';
157 echo '>' . $curr_page['page_nr'] . ': ' . htmlspecialchars($curr_page['page_descr']) . '</option>';
158 } // end while
159 echo "\n";
161 </select>
162 <?php
163 $choices = array(
164 '0' => __('Edit'),
165 '1' => __('Delete')
167 PMA_display_html_radio('action_choose', $choices, '0', false);
168 unset($choices);
170 </fieldset>
171 <fieldset class="tblFooters">
172 <input type="submit" value="<?php echo __('Go'); ?>" /><br />
173 </fieldset>
174 </form>
175 <?php
176 } // end IF
177 echo "\n";
178 } // end function
181 * A dashboard is displayed to AutoLayout the position of tables
182 * users can drag n drop the tables and change their positions
184 * @return void
185 * @access public
187 public function showTableDashBoard()
189 global $db,$cfgRelation,$table,$cfg,$with_field_names,$query_default_option;
191 * We will need an array of all tables in this db
193 $selectboxall = array('--');
194 $alltab_rs = PMA_DBI_query('SHOW TABLES FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE);
195 while ($val = @PMA_DBI_fetch_row($alltab_rs)) {
196 $selectboxall[] = $val[0];
200 * Now if we already have chosen a page number then we should
201 * show the tables involved
204 if (isset($this->chosenPage) && $this->chosenPage > 0) {
205 echo "\n";
207 <h2><?php echo __('Select Tables') ;?></h2>
208 <?php
209 $page_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
210 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
211 . ' AND pdf_page_number = \'' . PMA_sqlAddSlashes($this->chosenPage) . '\'';
212 $page_rs = PMA_query_as_controluser($page_query, false, $query_default_option);
213 $array_sh_page = array();
214 while ($temp_sh_page = @PMA_DBI_fetch_assoc($page_rs)) {
215 $array_sh_page[] = $temp_sh_page;
218 * Display WYSIWYG parts
221 if (! isset($_POST['with_field_names']) && ! isset($_POST['showwysiwyg'])) {
222 $with_field_names = true;
224 $this->_displayScratchboardTables($array_sh_page);
227 <form method="post" action="schema_edit.php" name="edcoord">
228 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
229 <input type="hidden" name="chpage" value="<?php echo htmlspecialchars($this->chosenPage); ?>" />
230 <input type="hidden" name="do" value="edcoord" />
231 <table border="0">
232 <tr>
233 <th><?php echo __('Table'); ?></th>
234 <th><?php echo __('Delete'); ?></th>
235 <th>X</th>
236 <th>Y</th>
237 </tr>
238 <?php
239 if (isset($ctable)) {
240 unset($ctable);
243 $i = 0;
244 $odd_row = true;
245 foreach ($array_sh_page as $dummy_sh_page => $sh_page) {
246 $_mtab = $sh_page['table_name'];
247 $tabExist[$_mtab] = false;
248 echo "\n" . ' <tr class="noclick ';
249 if ($odd_row) {
250 echo 'odd';
251 } else {
252 echo 'even';
254 echo '">';
255 $odd_row != $odd_row;
256 echo "\n" . ' <td>'
257 . "\n" . ' <select name="c_table_' . $i . '[name]">';
258 foreach ($selectboxall as $key => $value) {
259 echo "\n" . ' <option value="' . htmlspecialchars($value) . '"';
260 if ($value == $sh_page['table_name']) {
261 echo ' selected="selected"';
262 $tabExist[$_mtab] = true;
264 echo '>' . htmlspecialchars($value) . '</option>';
266 echo "\n" . ' </select>'
267 . "\n" . ' </td>';
268 echo "\n" . ' <td>'
269 . "\n" . ' <input type="checkbox" id="id_c_table_' . $i .'" name="c_table_' . $i . '[delete]" value="y" /><label for="id_c_table_' . $i .'">' . __('Delete') . '</label>';
270 echo "\n" . ' </td>';
271 echo "\n" . ' <td>'
272 . "\n" . ' <input type="text" onchange="dragPlace(' . $i . ', \'x\', this.value)" name="c_table_' . $i . '[x]" value="' . $sh_page['x'] . '" />';
273 echo "\n" . ' </td>';
274 echo "\n" . ' <td>'
275 . "\n" . ' <input type="text" onchange="dragPlace(' . $i . ', \'y\', this.value)" name="c_table_' . $i . '[y]" value="' . $sh_page['y'] . '" />';
276 echo "\n" . ' </td>';
277 echo "\n" . ' </tr>';
278 $i++;
281 * Add one more empty row
283 echo "\n" . ' <tr class="noclick ';
284 if ($odd_row) {
285 echo 'odd';
286 } else {
287 echo 'even';
289 $odd_row != $odd_row;
290 echo '">';
291 echo "\n" . ' <td>'
292 . "\n" . ' <select name="c_table_' . $i . '[name]">';
293 foreach ($selectboxall as $key => $value) {
294 echo "\n" . ' <option value="' . htmlspecialchars($value) . '">' . htmlspecialchars($value) . '</option>';
296 echo "\n" . ' </select>'
297 . "\n" . ' </td>';
298 echo "\n" . ' <td>'
299 . "\n" . ' <input type="checkbox" id="id_c_table_' . $i .'" name="c_table_' . $i . '[delete]" value="y" /><label for="id_c_table_' . $i .'">' . __('Delete') . '</label>';
300 echo "\n" . ' </td>';
301 echo "\n" . ' <td>'
302 . "\n" . ' <input type="text" name="c_table_' . $i . '[x]" value="' . (isset($sh_page['x'])?$sh_page['x']:'') . '" />';
303 echo "\n" . ' </td>';
304 echo "\n" . ' <td>'
305 . "\n" . ' <input type="text" name="c_table_' . $i . '[y]" value="' . (isset($sh_page['y'])?$sh_page['y']:'') . '" />';
306 echo "\n" . ' </td>';
307 echo "\n" . ' </tr>';
308 echo "\n" . ' </table>' . "\n";
310 echo "\n" . ' <input type="hidden" name="c_table_rows" value="' . ($i + 1) . '" />';
311 echo "\n" . ' <input type="hidden" id="showwysiwyg" name="showwysiwyg" value="' . ((isset($showwysiwyg) && $showwysiwyg == '1') ? '1' : '0') . '" />';
312 echo "\n" . ' <input type="checkbox" name="with_field_names" ' . (isset($with_field_names) ? 'checked="checked"' : ''). ' />' . __('Column names') . '<br />';
313 echo "\n" . ' <input type="submit" value="' . __('Save') . '" />';
314 echo "\n" . '</form>' . "\n\n";
315 } // end if
317 $this->_deleteTables($db, $this->chosenPage, isset($tabExist));
321 * show Export relational schema generation options
322 * user can select export type of his own choice
323 * and the attributes related to it
325 * @return void
326 * @access public
329 public function displaySchemaGenerationOptions()
331 global $cfg,$pmaThemeImage,$db,$test_rs,$chpage;
333 <form method="post" action="schema_export.php">
334 <fieldset>
335 <legend>
336 <?php
337 echo PMA_generate_common_hidden_inputs($db);
338 if ($cfg['PropertiesIconic']) {
339 echo '<img class="icon ic_b_views" src="themes/dot.gif" />';
341 echo __('Display relational schema');
343 </legend>
344 <select name="export_type" id="export_type">
345 <option value="pdf" selected="selected">PDF</option>
346 <option value="svg">SVG</option>
347 <option value="dia">DIA</option>
348 <option value="visio">Visio</option>
349 <option value="eps">EPS</option>
350 </select>
351 <label><?php echo __('Select Export Relational Type');?></label><br />
352 <?php
353 if (isset($test_rs)) {
355 <label for="pdf_page_number_opt"><?php echo __('Page number:'); ?></label>
356 <select name="pdf_page_number" id="pdf_page_number_opt">
357 <?php
358 while ($pages = @PMA_DBI_fetch_assoc($test_rs)) {
359 echo ' <option value="' . $pages['page_nr'] . '">'
360 . $pages['page_nr'] . ': ' . htmlspecialchars($pages['page_descr']) . '</option>' . "\n";
361 } // end while
362 PMA_DBI_free_result($test_rs);
363 unset($test_rs);
365 </select><br />
366 <?php } else { ?>
367 <input type="hidden" name="pdf_page_number" value="<?php echo htmlspecialchars($this->chosenPage); ?>" />
368 <?php } ?>
369 <input type="hidden" name="do" value="process_export" />
370 <input type="hidden" name="chpage" value="<?php echo $chpage; ?>" />
371 <input type="checkbox" name="show_grid" id="show_grid_opt" />
372 <label for="show_grid_opt"><?php echo __('Show grid'); ?></label><br />
373 <input type="checkbox" name="show_color" id="show_color_opt" checked="checked" />
374 <label for="show_color_opt"><?php echo __('Show color'); ?></label><br />
375 <input type="checkbox" name="show_table_dimension" id="show_table_dim_opt" />
376 <label for="show_table_dim_opt"><?php echo __('Show dimension of tables'); ?>
377 </label><br />
378 <input type="checkbox" name="all_table_same_wide" id="all_table_same_wide" />
379 <label for="all_table_same_wide"><?php echo __('Display all tables with the same width'); ?>
380 </label><br />
381 <input type="checkbox" name="with_doc" id="with_doc" checked="checked" />
382 <label for="with_doc"><?php echo __('Data Dictionary'); ?></label><br />
383 <input type="checkbox" name="show_keys" id="show_keys" />
384 <label for="show_keys"><?php echo __('Only show keys'); ?></label><br />
385 <select name="orientation" id="orientation_opt" onchange="refreshDragOption('pdflayout');" >
386 <option value="L"><?php echo __('Landscape');?></option>
387 <option value="P"><?php echo __('Portrait');?></option>
388 </select>
389 <label for="orientation_opt"><?php echo __('Orientation'); ?></label>
390 <br />
391 <select name="paper" id="paper_opt" onchange="refreshDragOption('pdflayout');">
392 <?php
393 foreach ($cfg['PDFPageSizes'] as $key => $val) {
394 echo '<option value="' . $val . '"';
395 if ($val == $cfg['PDFDefaultPageSize']) {
396 echo ' selected="selected"';
398 echo ' >' . $val . '</option>' . "\n";
401 </select>
402 <label for="paper_opt"><?php echo __('Paper size'); ?></label>
403 </fieldset>
404 <fieldset class="tblFooters">
405 <input type="submit" value="<?php echo __('Go'); ?>" />
406 </fieldset>
407 </form>
408 <?php
412 * Check if there are tables that need to be deleted in dashboard,
413 * if there are, ask the user for allowance
415 * @param string db name of database selected
416 * @param integer chpage selected page
417 * @param array tabExist
418 * @return void
419 * @access private
421 private function _deleteTables($db, $chpage, $tabExist)
423 $_strtrans = '';
424 $_strname = '';
425 $shoot = false;
426 if (!empty($tabExist) && is_array($tabExist)) {
427 foreach ($tabExist as $key => $value) {
428 if (!$value) {
429 $_strtrans .= '<input type="hidden" name="delrow[]" value="' . htmlspecialchars($key) . '" />' . "\n";
430 $_strname .= '<li>' . htmlspecialchars($key) . '</li>' . "\n";
431 $shoot = true;
434 if ($shoot) {
435 echo '<form action="schema_edit.php" method="post">' . "\n"
436 . PMA_generate_common_hidden_inputs($db, $table)
437 . '<input type="hidden" name="do" value="delete_old_references" />' . "\n"
438 . '<input type="hidden" name="chpage" value="' . htmlspecialchars($chpage) . '" />' . "\n"
439 . __('The current page has references to tables that no longer exist. Would you like to delete those references?')
440 . '<ul>' . "\n"
441 . $_strname
442 . '</ul>' . "\n"
443 . $_strtrans
444 . '<input type="submit" value="' . __('Go') . '" />' . "\n"
445 . '</form>';
452 * Check if there are tables that need to be deleted in dashboard,
453 * if there are, ask the user for allowance
455 * @return void
456 * @access private
458 private function _displayScratchboardTables($array_sh_page)
460 global $with_field_names,$cfg,$db;
462 <script type="text/javascript" src="./js/dom-drag.js"></script>
463 <form method="post" action="schema_edit.php" name="dragdrop">
464 <input type="button" name="dragdrop" value="<?php echo __('Toggle scratchboard'); ?>" onclick="ToggleDragDrop('pdflayout');" />
465 <input type="button" name="dragdropreset" value="<?php echo __('Reset'); ?>" onclick="resetDrag();" />
466 </form>
467 <div id="pdflayout" class="pdflayout" style="visibility: hidden;">
468 <?php
469 $draginit = '';
470 $draginit2 = '';
471 $reset_draginit = '';
472 $i = 0;
473 foreach ($array_sh_page as $key => $temp_sh_page) {
474 $drag_x = $temp_sh_page['x'];
475 $drag_y = $temp_sh_page['y'];
477 $draginit2 .= ' Drag.init(getElement("table_' . $i . '"), null, 0, parseInt(myid.style.width)-2, 0, parseInt(myid.style.height)-5);' . "\n";
478 $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";
479 $draginit .= ' getElement("table_' . $i . '").style.left = "' . $drag_x . 'px";' . "\n";
480 $draginit .= ' getElement("table_' . $i . '").style.top = "' . $drag_y . 'px";' . "\n";
481 $reset_draginit .= ' getElement("table_' . $i . '").style.left = "2px";' . "\n";
482 $reset_draginit .= ' getElement("table_' . $i . '").style.top = "' . (15 * $i) . 'px";' . "\n";
483 $reset_draginit .= ' document.edcoord.elements["c_table_' . $i . '[x]"].value = "2"' . "\n";
484 $reset_draginit .= ' document.edcoord.elements["c_table_' . $i . '[y]"].value = "' . (15 * $i) . '"' . "\n";
486 $local_query = 'SHOW FIELDS FROM '
487 . PMA_backquote($temp_sh_page['table_name'])
488 . ' FROM ' . PMA_backquote($db);
489 $fields_rs = PMA_DBI_query($local_query);
490 unset($local_query);
491 $fields_cnt = PMA_DBI_num_rows($fields_rs);
493 echo '<div id="table_' . $i . '" class="pdflayout_table"><u>' . $temp_sh_page['table_name'] . '</u>';
494 if (isset($with_field_names)) {
495 while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
496 echo '<br />' . htmlspecialchars($row['Field']) . "\n";
499 echo '</div>' . "\n";
500 PMA_DBI_free_result($fields_rs);
501 unset($fields_rs);
502 $i++;
505 </div>
506 <script type="text/javascript">
507 //<![CDATA[
508 function PDFinit() {
509 refreshLayout();
510 myid = getElement('pdflayout');
511 <?php echo $draginit; ?>
512 TableDragInit();
515 function TableDragInit() {
516 myid = getElement('pdflayout');
517 <?php echo $draginit2; ?>
520 function resetDrag() {
521 <?php echo $reset_draginit; ?>
523 //]]>
524 </script>
525 <?php
529 * delete the table rows with table co-ordinates
531 * @param int delrow delete selected table from list of tables
532 * @param array cfgRelation relation settings
533 * @param string db database name
534 * @param integer chpage selected page for adding relations etc
535 * @return void
536 * @access private
538 private function _deleteTableRows($delrow,$cfgRelation,$db,$chpage)
540 foreach ($delrow as $current_row) {
541 $del_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' ' . "\n"
542 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\'' . "\n"
543 . ' AND table_name = \'' . PMA_sqlAddSlashes($current_row) . '\'' . "\n"
544 . ' AND pdf_page_number = \'' . PMA_sqlAddSlashes($chpage) . '\'';
545 echo $del_query;
546 PMA_query_as_controluser($del_query, false, $query_default_option);
551 * get all the export options and verify
552 * call and include the appropriate Schema Class depending on $export_type
554 * @return void
555 * @access private
557 private function _processExportSchema()
560 * Settings for relation stuff
562 require_once './libraries/transformations.lib.php';
563 require_once './libraries/Index.class.php';
565 * default is PDF, otherwise validate it's only letters a-z
567 global $db,$export_type;
568 if (!isset($export_type) || !preg_match('/^[a-zA-Z]+$/', $export_type)) {
569 $export_type = 'pdf';
572 PMA_DBI_select_db($db);
574 include("./libraries/schema/".ucfirst($export_type)."_Relation_Schema.class.php");
575 $obj_schema = eval("new PMA_".ucfirst($export_type)."_Relation_Schema();");
579 * delete X and Y coordinates
581 * @param string db The database name
582 * @param array cfgRelation relation settings
583 * @param integer choosePage selected page for adding relations etc
584 * @return void
585 * @access private
587 public function deleteCoordinates($db, $cfgRelation, $choosePage, $query_default_option)
589 $query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
590 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
591 . ' AND pdf_page_number = \'' . PMA_sqlAddSlashes($choosePage) . '\'';
592 PMA_query_as_controluser($query, false, $query_default_option);
596 * delete pages
598 * @param string db The database name
599 * @param array cfgRelation relation settings
600 * @param integer choosePage selected page for adding relations etc
601 * @return void
602 * @access private
604 public function deletePages($db, $cfgRelation, $choosePage, $query_default_option)
606 $query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
607 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
608 . ' AND page_nr = \'' . PMA_sqlAddSlashes($choosePage) . '\'';
609 PMA_query_as_controluser($query, false, $query_default_option);
613 * process internal and foreign key relations
615 * @param string db The database name
616 * @param array cfgRelation relation settings
617 * @param integer pageNumber document number/Id
618 * @return void
619 * @access private
621 public function processRelations($db, $pageNumber, $cfgRelation, $query_default_option)
624 * A u t o m a t i c l a y o u t
626 * There are 2 kinds of relations in PMA
627 * 1) Internal Relations 2) Foreign Key Relations
629 if (isset($this->autoLayoutInternal) || isset($this->autoLayoutForeign)) {
630 $all_tables = array();
633 if (isset($this->autoLayoutForeign)) {
635 * get the tables list
636 * who support FOREIGN KEY, it's not
637 * important that we group together InnoDB tables
638 * and PBXT tables, as this logic is just to put
639 * the tables on the layout, not to determine relations
641 $tables = PMA_DBI_get_tables_full($db);
642 $foreignkey_tables = array();
643 foreach ($tables as $table_name => $table_properties) {
644 if (PMA_foreignkey_supported($table_properties['ENGINE'])) {
645 $foreignkey_tables[] = $table_name;
648 $all_tables = $foreignkey_tables;
650 * could be improved by finding the tables which have the
651 * most references keys and placing them at the beginning
652 * of the array (so that they are all center of schema)
654 unset($tables, $foreignkey_tables);
657 if (isset($this->autoLayoutInternal)) {
659 * get the tables list who support Internal Relations;
660 * This type of relations will be created when
661 * you setup the PMA tables correctly
663 $master_tables = 'SELECT COUNT(master_table), master_table'
664 . ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
665 . ' WHERE master_db = \'' . $db . '\''
666 . ' GROUP BY master_table'
667 . ' ORDER BY ' . PMA_backquote('COUNT(master_table)') . ' DESC ';
668 $master_tables_rs = PMA_query_as_controluser($master_tables, false, $query_default_option);
669 if ($master_tables_rs && PMA_DBI_num_rows($master_tables_rs) > 0) {
670 /* first put all the master tables at beginning
671 * of the list, so they are near the center of
672 * the schema
674 while (list(, $master_table) = PMA_DBI_fetch_row($master_tables_rs)) {
675 $all_tables[] = $master_table;
678 /* Now for each master, add its foreigns into an array
679 * of foreign tables, if not already there
680 * (a foreign might be foreign for more than
681 * one table, and might be a master itself)
684 $foreign_tables = array();
685 foreach ($all_tables as $master_table) {
686 $foreigners = PMA_getForeigners($db, $master_table);
687 foreach ($foreigners as $foreigner) {
688 if (!in_array($foreigner['foreign_table'], $foreign_tables)) {
689 $foreign_tables[] = $foreigner['foreign_table'];
695 * Now merge the master and foreign arrays/tables
697 foreach ($foreign_tables as $foreign_table) {
698 if (!in_array($foreign_table, $all_tables)) {
699 $all_tables[] = $foreign_table;
705 if (isset($this->autoLayoutInternal) || isset($this->autoLayoutForeign)) {
706 $this->addRelationCoordinates($all_tables,$pageNumber,$db, $cfgRelation,$query_default_option);
709 $this->chosenPage = $pageNumber;
713 * Add X and Y coordinates for a table
715 * @param string db The database name
716 * @param array cfgRelation relation settings
717 * @param integer pageNumber document number/Id
718 * @param array all_tables A list of all tables involved
719 * @return void
720 * @access private
722 public function addRelationCoordinates($all_tables,$pageNumber,$db, $cfgRelation,$query_default_option)
725 * Now generate the coordinates for the schema
726 * in a clockwise spiral and add to co-ordinates table
728 $pos_x = 300;
729 $pos_y = 300;
730 $delta = 110;
731 $delta_mult = 1.10;
732 $direction = "right";
733 foreach ($all_tables as $current_table) {
735 * save current table's coordinates
737 $insert_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
738 . '(db_name, table_name, pdf_page_number, x, y) '
739 . 'VALUES (\'' . PMA_sqlAddSlashes($db) . '\', \'' . PMA_sqlAddSlashes($current_table) . '\',' . $pageNumber . ',' . $pos_x . ',' . $pos_y . ')';
740 PMA_query_as_controluser($insert_query, false, $query_default_option);
743 * compute for the next table
745 switch ($direction) {
746 case 'right':
747 $pos_x += $delta;
748 $direction = "down";
749 $delta *= $delta_mult;
750 break;
751 case 'down':
752 $pos_y += $delta;
753 $direction = "left";
754 $delta *= $delta_mult;
755 break;
756 case 'left':
757 $pos_x -= $delta;
758 $direction = "up";
759 $delta *= $delta_mult;
760 break;
761 case 'up':
762 $pos_y -= $delta;
763 $direction = "right";
764 $delta *= $delta_mult;
765 break;
771 * update X and Y coordinates for a table
773 * @param string db The database name
774 * @param array cfgRelation relation settings
775 * @return void
776 * @access private
778 private function _editCoordinates($db, $cfgRelation,$query_default_option)
780 for ($i = 0; $i < $this->c_table_rows; $i++) {
781 $arrvalue = 'c_table_' . $i;
782 global $$arrvalue;
783 $arrvalue = $$arrvalue;
784 if (! isset($arrvalue['x']) || $arrvalue['x'] == '') {
785 $arrvalue['x'] = 0;
787 if (! isset($arrvalue['y']) || $arrvalue['y'] == '') {
788 $arrvalue['y'] = 0;
790 if (isset($arrvalue['name']) && $arrvalue['name'] != '--') {
791 $test_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
792 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
793 . ' AND table_name = \'' . PMA_sqlAddSlashes($arrvalue['name']) . '\''
794 . ' AND pdf_page_number = \'' . PMA_sqlAddSlashes($this->chosenPage) . '\'';
795 $test_rs = PMA_query_as_controluser($test_query, false, $query_default_option);
796 //echo $test_query;
797 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
798 if (isset($arrvalue['delete']) && $arrvalue['delete'] == 'y') {
799 $ch_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
800 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
801 . ' AND table_name = \'' . PMA_sqlAddSlashes($arrvalue['name']) . '\''
802 . ' AND pdf_page_number = \'' . PMA_sqlAddSlashes($this->chosenPage) . '\'';
803 } else {
804 $ch_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
805 . 'SET x = ' . $arrvalue['x'] . ', y= ' . $arrvalue['y']
806 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
807 . ' AND table_name = \'' . PMA_sqlAddSlashes($arrvalue['name']) . '\''
808 . ' AND pdf_page_number = \'' . PMA_sqlAddSlashes($this->chosenPage) . '\'';
810 } else {
811 $ch_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
812 . '(db_name, table_name, pdf_page_number, x, y) '
813 . 'VALUES (\'' . PMA_sqlAddSlashes($db) . '\', \'' . PMA_sqlAddSlashes($arrvalue['name']) . '\', \'' . PMA_sqlAddSlashes($this->chosenPage) . '\',' . $arrvalue['x'] . ',' . $arrvalue['y'] . ')';
815 //echo $ch_query;
816 PMA_query_as_controluser($ch_query, false, $query_default_option);
817 } // end if
818 } // end for