bug #3392150 [schema] PMA_User_Schema::processUserChoice() is broken
[phpmyadmin/alexukf.git] / libraries / schema / User_Schema.class.php
blob310ffecc73b33c95e59ad379ce6ad60c4507d628
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
13 * @name User_Schema
14 * @copyright
15 * @license
18 class PMA_User_Schema
21 public $chosenPage;
22 public $autoLayoutForeign;
23 public $autoLayoutInternal;
24 public $pageNumber;
25 public $c_table_rows;
26 public $action;
28 public function setAction($value)
30 $this->action = $value;
32 /**
33 * This function will process the user defined pages
34 * and tables which will be exported as Relational schema
35 * you can set the table positions on the paper via scratchboard
36 * for table positions, put the x,y co-ordinates
38 * @param string $this->action It tells what the Schema is supposed to do
39 * create and select a page, generate schema etc
40 * @access public
43 public function processUserChoice()
45 global $action_choose,$db,$cfgRelation,$cfg,$query_default_option;
47 if (isset($this->action)) {
48 switch ($this->action) {
49 case 'selectpage':
50 $this->chosenPage = $_REQUEST['chpage'];
51 if ($action_choose=="1") {
52 $this->deleteCoordinates($db, $cfgRelation, $this->chosenPage, $query_default_option);
53 $this->deletePages($db, $cfgRelation, $this->chosenPage, $query_default_option);
54 $this->chosenPage = 0;
56 break;
57 case 'createpage':
58 $this->pageNumber = PMA_REL_create_page($_POST['newpage'], $cfgRelation, $db, $query_default_option);
59 $this->autoLayoutForeign = isset($_POST['auto_layout_foreign']) ? "1":NULL;
60 $this->autoLayoutInternal = isset($_POST['auto_layout_internal']) ? "1":NULL;
61 $this->processRelations($db, $this->pageNumber,$cfgRelation,$query_default_option);
62 break;
63 case 'edcoord':
64 $this->chosenPage = $_POST['chpage'];
65 $this->c_table_rows = $_POST['c_table_rows'];
66 $this->_editCoordinates($db, $cfgRelation,$query_default_option);
67 break;
68 case 'delete_old_references':
69 $this->_deleteTableRows($_POST['delrow'], $cfgRelation, $db, $_POST['chpage']);
70 break;
71 case 'process_export':
72 $this->_processExportSchema();
73 break;
75 } // end switch
76 } // end if (isset($do))
80 /**
81 * shows/displays the HTML FORM to create the page
83 * @param string db name of the selected database
84 * @return void
85 * @access public
87 public function showCreatePageDialog($db)
90 <form method="post" action="schema_edit.php" name="frm_create_page">
91 <fieldset>
92 <legend>
93 <?php echo __('Create a page') . "\n"; ?>
94 </legend>
95 <?php echo PMA_generate_common_hidden_inputs($db); ?>
96 <input type="hidden" name="do" value="createpage" />
97 <table>
98 <tr>
99 <td><label for="id_newpage"><?php echo __('Page name'); ?></label></td>
100 <td><input type="text" name="newpage" id="id_newpage" size="20" maxlength="50" /></td>
101 </tr>
102 <tr>
103 <td><?php echo __('Automatic layout based on'); ?></td>
104 <td>
105 <input type="checkbox" name="auto_layout_internal" id="id_auto_layout_internal" /><label for="id_auto_layout_internal">
106 <?php echo __('Internal relations'); ?></label><br />
107 <?php
109 * Check to see whether INNODB and PBXT storage engines are Available in MYSQL PACKAGE
110 * If available, then provide AutoLayout for Foreign Keys in Schema View
113 if (PMA_StorageEngine::isValid('InnoDB') || PMA_StorageEngine::isValid('PBXT')) {
115 <input type="checkbox" name="auto_layout_foreign" id="id_auto_layout_foreign" /><label for="id_auto_layout_foreign">
116 <?php echo __('FOREIGN KEY'); ?></label><br />
117 <?php
120 </td></tr>
121 </table>
122 </fieldset>
123 <fieldset class="tblFooters">
124 <input type="submit" value="<?php echo __('Go'); ?>" />
125 </fieldset>
126 </form>
127 <?php
131 * shows/displays the created page names in a drop down list
132 * User can select any page number and edit it using dashboard etc
134 * @return void
135 * @access public
137 public function selectPage()
139 global $db,$table,$query_default_option,$cfgRelation;
140 $page_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
141 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
142 $page_rs = PMA_query_as_controluser($page_query, FALSE, $query_default_option);
143 if ($page_rs && PMA_DBI_num_rows($page_rs) > 0) {
145 <form method="get" action="schema_edit.php" name="frm_select_page">
146 <fieldset>
147 <legend>
148 <?php echo __('Please choose a page to edit') . "\n"; ?>
149 </legend>
150 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
151 <input type="hidden" name="do" value="selectpage" />
152 <select name="chpage" id="chpage" onchange="this.form.submit()">
153 <option value="0"><?php echo __('Select page'); ?></option>
154 <?php
155 while ($curr_page = PMA_DBI_fetch_assoc($page_rs)) {
156 echo "\n" . ' '
157 . '<option value="' . $curr_page['page_nr'] . '"';
158 if (isset($this->chosenPage) && $this->chosenPage == $curr_page['page_nr']) {
159 echo ' selected="selected"';
161 echo '>' . $curr_page['page_nr'] . ': ' . htmlspecialchars($curr_page['page_descr']) . '</option>';
162 } // end while
163 echo "\n";
165 </select>
166 <?php
167 $choices = array(
168 '0' => __('Edit'),
169 '1' => __('Delete')
171 PMA_display_html_radio('action_choose', $choices, '0', false);
172 unset($choices);
174 </fieldset>
175 <fieldset class="tblFooters">
176 <input type="submit" value="<?php echo __('Go'); ?>" /><br />
177 </fieldset>
178 </form>
179 <?php
180 } // end IF
181 echo "\n";
182 } // end function
185 * A dashboard is displayed to AutoLayout the position of tables
186 * users can drag n drop the tables and change their positions
188 * @return void
189 * @access public
191 public function showTableDashBoard()
193 global $db,$cfgRelation,$table,$cfg,$with_field_names,$query_default_option;
195 * We will need an array of all tables in this db
197 $selectboxall = array('--');
198 $alltab_rs = PMA_DBI_query('SHOW TABLES FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE);
199 while ($val = @PMA_DBI_fetch_row($alltab_rs)) {
200 $selectboxall[] = $val[0];
204 * Now if we already have chosen a page number then we should
205 * show the tables involved
208 if (isset($this->chosenPage) && $this->chosenPage > 0) {
209 echo "\n";
211 <h2><?php echo __('Select Tables') ;?></h2>
212 <?php
213 $page_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
214 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
215 . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($this->chosenPage) . '\'';
216 $page_rs = PMA_query_as_controluser($page_query, FALSE, $query_default_option);
217 $array_sh_page = array();
218 while ($temp_sh_page = @PMA_DBI_fetch_assoc($page_rs)) {
219 $array_sh_page[] = $temp_sh_page;
222 * Display WYSIWYG parts
225 if (!isset($_POST['with_field_names']) && !isset($_POST['showwysiwyg'])) {
226 $with_field_names = TRUE;
228 $this->_displayScratchboardTables($array_sh_page);
231 <form method="post" action="schema_edit.php" name="edcoord">
232 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
233 <input type="hidden" name="chpage" value="<?php echo htmlspecialchars($this->chosenPage); ?>" />
234 <input type="hidden" name="do" value="edcoord" />
235 <table border="0">
236 <tr>
237 <th><?php echo __('Table'); ?></th>
238 <th><?php echo __('Delete'); ?></th>
239 <th>X</th>
240 <th>Y</th>
241 </tr>
242 <?php
243 if (isset($ctable)) {
244 unset($ctable);
247 $i = 0;
248 $odd_row = true;
249 foreach ($array_sh_page as $dummy_sh_page => $sh_page) {
250 $_mtab = $sh_page['table_name'];
251 $tabExist[$_mtab] = FALSE;
252 echo "\n" . ' <tr class="noclick ';
253 if ($odd_row) {
254 echo 'odd';
255 } else {
256 echo 'even';
258 echo '">';
259 $odd_row != $odd_row;
260 echo "\n" . ' <td>'
261 . "\n" . ' <select name="c_table_' . $i . '[name]">';
262 foreach ($selectboxall as $key => $value) {
263 echo "\n" . ' <option value="' . htmlspecialchars($value) . '"';
264 if ($value == $sh_page['table_name']) {
265 echo ' selected="selected"';
266 $tabExist[$_mtab] = TRUE;
268 echo '>' . htmlspecialchars($value) . '</option>';
270 echo "\n" . ' </select>'
271 . "\n" . ' </td>';
272 echo "\n" . ' <td>'
273 . "\n" . ' <input type="checkbox" id="id_c_table_' . $i .'" name="c_table_' . $i . '[delete]" value="y" /><label for="id_c_table_' . $i .'">' . __('Delete') . '</label>';
274 echo "\n" . ' </td>';
275 echo "\n" . ' <td>'
276 . "\n" . ' <input type="text" onchange="dragPlace(' . $i . ', \'x\', this.value)" name="c_table_' . $i . '[x]" value="' . $sh_page['x'] . '" />';
277 echo "\n" . ' </td>';
278 echo "\n" . ' <td>'
279 . "\n" . ' <input type="text" onchange="dragPlace(' . $i . ', \'y\', this.value)" name="c_table_' . $i . '[y]" value="' . $sh_page['y'] . '" />';
280 echo "\n" . ' </td>';
281 echo "\n" . ' </tr>';
282 $i++;
285 * Add one more empty row
287 echo "\n" . ' <tr class="noclick ';
288 if ($odd_row) {
289 echo 'odd';
290 } else {
291 echo 'even';
293 $odd_row != $odd_row;
294 echo '">';
295 echo "\n" . ' <td>'
296 . "\n" . ' <select name="c_table_' . $i . '[name]">';
297 foreach ($selectboxall as $key => $value) {
298 echo "\n" . ' <option value="' . htmlspecialchars($value) . '">' . htmlspecialchars($value) . '</option>';
300 echo "\n" . ' </select>'
301 . "\n" . ' </td>';
302 echo "\n" . ' <td>'
303 . "\n" . ' <input type="checkbox" id="id_c_table_' . $i .'" name="c_table_' . $i . '[delete]" value="y" /><label for="id_c_table_' . $i .'">' . __('Delete') . '</label>';
304 echo "\n" . ' </td>';
305 echo "\n" . ' <td>'
306 . "\n" . ' <input type="text" name="c_table_' . $i . '[x]" value="' . (isset($sh_page['x'])?$sh_page['x']:'') . '" />';
307 echo "\n" . ' </td>';
308 echo "\n" . ' <td>'
309 . "\n" . ' <input type="text" name="c_table_' . $i . '[y]" value="' . (isset($sh_page['y'])?$sh_page['y']:'') . '" />';
310 echo "\n" . ' </td>';
311 echo "\n" . ' </tr>';
312 echo "\n" . ' </table>' . "\n";
314 echo "\n" . ' <input type="hidden" name="c_table_rows" value="' . ($i + 1) . '" />';
315 echo "\n" . ' <input type="hidden" id="showwysiwyg" name="showwysiwyg" value="' . ((isset($showwysiwyg) && $showwysiwyg == '1') ? '1' : '0') . '" />';
316 echo "\n" . ' <input type="checkbox" name="with_field_names" ' . (isset($with_field_names) ? 'checked="checked"' : ''). ' />' . __('Column names') . '<br />';
317 echo "\n" . ' <input type="submit" value="' . __('Save') . '" />';
318 echo "\n" . '</form>' . "\n\n";
319 } // end if
321 $this->_deleteTables($db, $this->chosenPage, $tabExist);
325 * show Export relational schema generation options
326 * user can select export type of his own choice
327 * and the attributes related to it
329 * @return void
330 * @access public
333 public function displaySchemaGenerationOptions()
335 global $cfg,$pmaThemeImage,$db,$test_rs,$chpage;
337 <form method="post" action="schema_export.php">
338 <fieldset>
339 <legend>
340 <?php
341 echo PMA_generate_common_hidden_inputs($db);
342 if ($cfg['PropertiesIconic']) {
343 echo '<img class="icon" src="' . $pmaThemeImage . 'b_view.png"'
344 .' alt="" width="16" height="16" />';
346 echo __('Display relational schema');
348 </legend>
349 <select name="export_type" id="export_type">
350 <option value="pdf" selected="selected">PDF</option>
351 <option value="svg">SVG</option>
352 <option value="dia">DIA</option>
353 <option value="visio">Visio</option>
354 <option value="eps">EPS</option>
355 </select>
356 <label><?php echo __('Select Export Relational Type');?></label><br />
357 <?php
358 if (isset($test_rs)) {
360 <label for="pdf_page_number_opt"><?php echo __('Page number:'); ?></label>
361 <select name="pdf_page_number" id="pdf_page_number_opt">
362 <?php
363 while ($pages = @PMA_DBI_fetch_assoc($test_rs)) {
364 echo ' <option value="' . $pages['page_nr'] . '">'
365 . $pages['page_nr'] . ': ' . htmlspecialchars($pages['page_descr']) . '</option>' . "\n";
366 } // end while
367 PMA_DBI_free_result($test_rs);
368 unset($test_rs);
370 </select><br />
371 <?php } else { ?>
372 <input type="hidden" name="pdf_page_number" value="<?php echo htmlspecialchars($this->chosenPage); ?>" />
373 <?php } ?>
374 <input type="hidden" name="do" value="process_export" />
375 <input type="hidden" name="chpage" value="<?php echo $chpage; ?>" />
376 <input type="checkbox" name="show_grid" id="show_grid_opt" />
377 <label for="show_grid_opt"><?php echo __('Show grid'); ?></label><br />
378 <input type="checkbox" name="show_color" id="show_color_opt" checked="checked" />
379 <label for="show_color_opt"><?php echo __('Show color'); ?></label><br />
380 <input type="checkbox" name="show_table_dimension" id="show_table_dim_opt" />
381 <label for="show_table_dim_opt"><?php echo __('Show dimension of tables'); ?>
382 </label><br />
383 <input type="checkbox" name="all_table_same_wide" id="all_table_same_wide" />
384 <label for="all_table_same_wide"><?php echo __('Display all tables with the same width'); ?>
385 </label><br />
386 <input type="checkbox" name="with_doc" id="with_doc" checked="checked" />
387 <label for="with_doc"><?php echo __('Data Dictionary'); ?></label><br />
388 <input type="checkbox" name="show_keys" id="show_keys" />
389 <label for="show_keys"><?php echo __('Only show keys'); ?></label><br />
390 <select name="orientation" id="orientation_opt" onchange="refreshDragOption('pdflayout');" >
391 <option value="L"><?php echo __('Landscape');?></option>
392 <option value="P"><?php echo __('Portrait');?></option>
393 </select>
394 <label for="orientation_opt"><?php echo __('Orientation'); ?></label>
395 <br />
396 <select name="paper" id="paper_opt" onchange="refreshDragOption('pdflayout');">
397 <?php
398 foreach ($cfg['PDFPageSizes'] as $key => $val) {
399 echo '<option value="' . $val . '"';
400 if ($val == $cfg['PDFDefaultPageSize']) {
401 echo ' selected="selected"';
403 echo ' >' . $val . '</option>' . "\n";
406 </select>
407 <label for="paper_opt"><?php echo __('Paper size'); ?></label>
408 </fieldset>
409 <fieldset class="tblFooters">
410 <input type="submit" value="<?php echo __('Go'); ?>" />
411 </fieldset>
412 </form>
413 <?php
417 * Check if there are tables that need to be deleted in dashboard,
418 * if there are, ask the user for allowance
420 * @param string db name of database selected
421 * @param integer chpage selected page
422 * @param array tabExist
423 * @return void
424 * @access private
426 private function _deleteTables($db, $chpage, $tabExist)
428 $_strtrans = '';
429 $_strname = '';
430 $shoot = FALSE;
431 if (!empty($tabExist) && is_array($tabExist)) {
432 foreach ($tabExist as $key => $value) {
433 if (!$value) {
434 $_strtrans .= '<input type="hidden" name="delrow[]" value="' . htmlspecialchars($key) . '" />' . "\n";
435 $_strname .= '<li>' . htmlspecialchars($key) . '</li>' . "\n";
436 $shoot = TRUE;
439 if ($shoot) {
440 echo '<form action="schema_edit.php" method="post">' . "\n"
441 . PMA_generate_common_hidden_inputs($db)
442 . '<input type="hidden" name="do" value="delete_old_references" />' . "\n"
443 . '<input type="hidden" name="chpage" value="' . htmlspecialchars($chpage) . '" />' . "\n"
444 . __('The current page has references to tables that no longer exist. Would you like to delete those references?')
445 . '<ul>' . "\n"
446 . $_strname
447 . '</ul>' . "\n"
448 . $_strtrans
449 . '<input type="submit" value="' . __('Go') . '" />' . "\n"
450 . '</form>';
457 * Check if there are tables that need to be deleted in dashboard,
458 * if there are, ask the user for allowance
460 * @return void
461 * @access private
463 private function _displayScratchboardTables($array_sh_page)
465 global $with_field_names,$cfg,$db;
467 <script type="text/javascript" src="./js/dom-drag.js"></script>
468 <form method="post" action="schema_edit.php" name="dragdrop">
469 <input type="button" name="dragdrop" value="<?php echo __('Toggle scratchboard'); ?>" onclick="ToggleDragDrop('pdflayout');" />
470 <input type="button" name="dragdropreset" value="<?php echo __('Reset'); ?>" onclick="resetDrag();" />
471 </form>
472 <div id="pdflayout" class="pdflayout" style="visibility: hidden;">
473 <?php
474 $draginit = '';
475 $draginit2 = '';
476 $reset_draginit = '';
477 $i = 0;
478 foreach ($array_sh_page as $key => $temp_sh_page) {
479 $drag_x = $temp_sh_page['x'];
480 $drag_y = $temp_sh_page['y'];
482 $draginit2 .= ' Drag.init(getElement("table_' . $i . '"), null, 0, parseInt(myid.style.width)-2, 0, parseInt(myid.style.height)-5);' . "\n";
483 $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";
484 $draginit .= ' getElement("table_' . $i . '").style.left = "' . $drag_x . 'px";' . "\n";
485 $draginit .= ' getElement("table_' . $i . '").style.top = "' . $drag_y . 'px";' . "\n";
486 $reset_draginit .= ' getElement("table_' . $i . '").style.left = "2px";' . "\n";
487 $reset_draginit .= ' getElement("table_' . $i . '").style.top = "' . (15 * $i) . 'px";' . "\n";
488 $reset_draginit .= ' document.edcoord.elements["c_table_' . $i . '[x]"].value = "2"' . "\n";
489 $reset_draginit .= ' document.edcoord.elements["c_table_' . $i . '[y]"].value = "' . (15 * $i) . '"' . "\n";
491 $local_query = 'SHOW FIELDS FROM '
492 . PMA_backquote($temp_sh_page['table_name'])
493 . ' FROM ' . PMA_backquote($db);
494 $fields_rs = PMA_DBI_try_query($local_query);
495 unset($local_query);
496 // the table has been dropped from outside phpMyAdmin
497 if (PMA_DBI_getError()) {
498 continue;
500 echo '<div id="table_' . $i . '" class="pdflayout_table"><u>' . $temp_sh_page['table_name'] . '</u>';
501 if (isset($with_field_names)) {
502 while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
503 echo '<br />' . htmlspecialchars($row['Field']) . "\n";
506 echo '</div>' . "\n";
507 PMA_DBI_free_result($fields_rs);
508 unset($fields_rs);
509 $i++;
512 </div>
513 <script type="text/javascript">
514 //<![CDATA[
515 function PDFinit() {
516 refreshLayout();
517 myid = getElement('pdflayout');
518 <?php echo $draginit; ?>
519 TableDragInit();
522 function TableDragInit() {
523 myid = getElement('pdflayout');
524 <?php echo $draginit2; ?>
527 function resetDrag() {
528 <?php echo $reset_draginit; ?>
530 //]]>
531 </script>
532 <?php
536 * delete the table rows with table co-ordinates
538 * @param int delrow delete selected table from list of tables
539 * @param array cfgRelation relation settings
540 * @param string db database name
541 * @param integer chpage selected page for adding relations etc
542 * @return void
543 * @access private
545 private function _deleteTableRows($delrow,$cfgRelation,$db,$chpage)
547 foreach ($delrow as $current_row) {
548 $del_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' ' . "\n"
549 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'' . "\n"
550 . ' AND table_name = \'' . PMA_sqlAddslashes($current_row) . '\'' . "\n"
551 . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\'';
552 echo $del_query;
553 PMA_query_as_controluser($del_query, FALSE, $query_default_option);
558 * get all the export options and verify
559 * call and include the appropriate Schema Class depending on $export_type
561 * @return void
562 * @access private
564 private function _processExportSchema()
567 * Settings for relation stuff
569 require_once './libraries/transformations.lib.php';
570 require_once './libraries/Index.class.php';
572 * default is PDF, otherwise validate it's only letters a-z
574 global $db,$export_type;
575 if (!isset($export_type) || !preg_match('/^[a-zA-Z]+$/', $export_type)) {
576 $export_type = 'pdf';
579 PMA_DBI_select_db($db);
581 include("./libraries/schema/".ucfirst($export_type)."_Relation_Schema.class.php");
582 $obj_schema = eval("new PMA_".ucfirst($export_type)."_Relation_Schema();");
586 * delete X and Y coordinates
588 * @param string db The database name
589 * @param array cfgRelation relation settings
590 * @param integer choosePage selected page for adding relations etc
591 * @return void
592 * @access private
594 public function deleteCoordinates($db, $cfgRelation, $choosePage, $query_default_option)
596 $query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
597 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
598 . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($choosePage) . '\'';
599 PMA_query_as_controluser($query, FALSE, $query_default_option);
603 * delete pages
605 * @param string db The database name
606 * @param array cfgRelation relation settings
607 * @param integer choosePage selected page for adding relations etc
608 * @return void
609 * @access private
611 public function deletePages($db, $cfgRelation, $choosePage, $query_default_option)
613 $query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
614 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
615 . ' AND page_nr = \'' . PMA_sqlAddslashes($choosePage) . '\'';
616 PMA_query_as_controluser($query, FALSE, $query_default_option);
620 * process internal and foreign key relations
622 * @param string db The database name
623 * @param array cfgRelation relation settings
624 * @param integer pageNumber document number/Id
625 * @return void
626 * @access private
628 public function processRelations($db, $pageNumber, $cfgRelation, $query_default_option)
631 * A u t o m a t i c l a y o u t
633 * There are 2 kinds of relations in PMA
634 * 1) Internal Relations 2) Foreign Key Relations
636 if (isset($this->autoLayoutInternal) || isset($this->autoLayoutForeign)) {
637 $all_tables = array();
640 if (isset($this->autoLayoutForeign)) {
642 * get the tables list
643 * who support FOREIGN KEY, it's not
644 * important that we group together InnoDB tables
645 * and PBXT tables, as this logic is just to put
646 * the tables on the layout, not to determine relations
648 $tables = PMA_DBI_get_tables_full($db);
649 $foreignkey_tables = array();
650 foreach($tables as $table_name => $table_properties) {
651 if (PMA_foreignkey_supported($table_properties['ENGINE'])) {
652 $foreignkey_tables[] = $table_name;
655 $all_tables = $foreignkey_tables;
657 * could be improved by finding the tables which have the
658 * most references keys and placing them at the beginning
659 * of the array (so that they are all center of schema)
661 unset($tables, $foreignkey_tables);
664 if (isset($this->autoLayoutInternal)) {
666 * get the tables list who support Internal Relations;
667 * This type of relations will be created when
668 * you setup the PMA tables correctly
670 $master_tables = 'SELECT COUNT(master_table), master_table'
671 . ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
672 . ' WHERE master_db = \'' . $db . '\''
673 . ' GROUP BY master_table'
674 . ' ORDER BY ' . PMA_backquote('COUNT(master_table)') . ' DESC ';
675 $master_tables_rs = PMA_query_as_controluser($master_tables, FALSE, $query_default_option);
676 if ($master_tables_rs && PMA_DBI_num_rows($master_tables_rs) > 0) {
677 /* first put all the master tables at beginning
678 * of the list, so they are near the center of
679 * the schema
681 while (list(, $master_table) = PMA_DBI_fetch_row($master_tables_rs)) {
682 $all_tables[] = $master_table;
685 /* Now for each master, add its foreigns into an array
686 * of foreign tables, if not already there
687 * (a foreign might be foreign for more than
688 * one table, and might be a master itself)
691 $foreign_tables = array();
692 foreach ($all_tables as $master_table) {
693 $foreigners = PMA_getForeigners($db, $master_table);
694 foreach ($foreigners as $foreigner) {
695 if (!in_array($foreigner['foreign_table'], $foreign_tables)) {
696 $foreign_tables[] = $foreigner['foreign_table'];
702 * Now merge the master and foreign arrays/tables
704 foreach ($foreign_tables as $foreign_table) {
705 if (!in_array($foreign_table, $all_tables)) {
706 $all_tables[] = $foreign_table;
712 if (isset($this->autoLayoutInternal) || isset($this->autoLayoutForeign)) {
713 $this->addRelationCoordinates($all_tables,$pageNumber,$db, $cfgRelation,$query_default_option);
716 $this->chosenPage = $pageNumber;
720 * Add X and Y coordinates for a table
722 * @param string db The database name
723 * @param array cfgRelation relation settings
724 * @param integer pageNumber document number/Id
725 * @param array all_tables A list of all tables involved
726 * @return void
727 * @access private
729 public function addRelationCoordinates($all_tables,$pageNumber,$db, $cfgRelation,$query_default_option)
732 * Now generate the coordinates for the schema
733 * in a clockwise spiral and add to co-ordinates table
735 $pos_x = 300;
736 $pos_y = 300;
737 $delta = 110;
738 $delta_mult = 1.10;
739 $direction = "right";
740 foreach ($all_tables as $current_table) {
742 * save current table's coordinates
744 $insert_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
745 . '(db_name, table_name, pdf_page_number, x, y) '
746 . 'VALUES (\'' . PMA_sqlAddslashes($db) . '\', \'' . PMA_sqlAddslashes($current_table) . '\',' . $pageNumber . ',' . $pos_x . ',' . $pos_y . ')';
747 PMA_query_as_controluser($insert_query, FALSE, $query_default_option);
750 * compute for the next table
752 switch ($direction) {
753 case 'right':
754 $pos_x += $delta;
755 $direction = "down";
756 $delta *= $delta_mult;
757 break;
758 case 'down':
759 $pos_y += $delta;
760 $direction = "left";
761 $delta *= $delta_mult;
762 break;
763 case 'left':
764 $pos_x -= $delta;
765 $direction = "up";
766 $delta *= $delta_mult;
767 break;
768 case 'up':
769 $pos_y -= $delta;
770 $direction = "right";
771 $delta *= $delta_mult;
772 break;
778 * update X and Y coordinates for a table
780 * @param string db The database name
781 * @param array cfgRelation relation settings
782 * @return void
783 * @access private
785 private function _editCoordinates($db, $cfgRelation,$query_default_option)
787 for ($i = 0; $i < $this->c_table_rows; $i++) {
788 $arrvalue = 'c_table_' . $i;
789 global $$arrvalue;
790 $arrvalue = $$arrvalue;
791 if (!isset($arrvalue['x']) || $arrvalue['x'] == '') {
792 $arrvalue['x'] = 0;
794 if (!isset($arrvalue['y']) || $arrvalue['y'] == '') {
795 $arrvalue['y'] = 0;
797 if (isset($arrvalue['name']) && $arrvalue['name'] != '--') {
798 $test_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
799 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
800 . ' AND table_name = \'' . PMA_sqlAddslashes($arrvalue['name']) . '\''
801 . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($this->chosenPage) . '\'';
802 $test_rs = PMA_query_as_controluser($test_query, FALSE, $query_default_option);
803 //echo $test_query;
804 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
805 if (isset($arrvalue['delete']) && $arrvalue['delete'] == 'y') {
806 $ch_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
807 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
808 . ' AND table_name = \'' . PMA_sqlAddslashes($arrvalue['name']) . '\''
809 . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($this->chosenPage) . '\'';
810 } else {
811 $ch_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
812 . 'SET x = ' . $arrvalue['x'] . ', y= ' . $arrvalue['y']
813 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
814 . ' AND table_name = \'' . PMA_sqlAddslashes($arrvalue['name']) . '\''
815 . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($this->chosenPage) . '\'';
817 } else {
818 $ch_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
819 . '(db_name, table_name, pdf_page_number, x, y) '
820 . 'VALUES (\'' . PMA_sqlAddslashes($db) . '\', \'' . PMA_sqlAddslashes($arrvalue['name']) . '\', \'' . PMA_sqlAddslashes($this->chosenPage) . '\',' . $arrvalue['x'] . ',' . $arrvalue['y'] . ')';
822 //echo $ch_query;
823 PMA_query_as_controluser($ch_query, FALSE, $query_default_option);
824 } // end if
825 } // end for