Fixed: Not selecting a datalabel used to issue a notice(undefined offset)
[phpmyadmin/ammaryasirr.git] / libraries / schema / User_Schema.class.php
blob41a2b9e6fe2b5c375bbc794852452057b592ff72
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;
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, $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, $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 echo '<div id="table_' . $i . '" class="pdflayout_table"><u>' . $temp_sh_page['table_name'] . '</u>';
509 if (isset($with_field_names)) {
510 $fields = PMA_DBI_get_columns($db, $temp_sh_page['table_name']);
511 foreach ($fields as $row) {
512 echo '<br />' . htmlspecialchars($row['Field']) . "\n";
515 echo '</div>' . "\n";
516 $i++;
519 </div>
520 <script type="text/javascript">
521 //<![CDATA[
522 function PDFinit() {
523 refreshLayout();
524 myid = $('#pdflayout')[0];
525 <?php echo $draginit; ?>
526 TableDragInit();
529 function TableDragInit() {
530 myid = $('#pdflayout')[0];
531 <?php echo $draginit2; ?>
534 function resetDrag() {
535 <?php echo $reset_draginit; ?>
537 //]]>
538 </script>
539 <?php
543 * delete the table rows with table co-ordinates
545 * @param int delrow delete selected table from list of tables
546 * @param array cfgRelation relation settings
547 * @param string db database name
548 * @param integer chpage selected page for adding relations etc
549 * @return void
550 * @access private
552 private function _deleteTableRows($delrow,$cfgRelation,$db,$chpage)
554 foreach ($delrow as $current_row) {
555 $del_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' ' . "\n"
556 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\'' . "\n"
557 . ' AND table_name = \'' . PMA_sqlAddSlashes($current_row) . '\'' . "\n"
558 . ' AND pdf_page_number = \'' . PMA_sqlAddSlashes($chpage) . '\'';
559 echo $del_query;
560 PMA_query_as_controluser($del_query, false);
565 * get all the export options and verify
566 * call and include the appropriate Schema Class depending on $export_type
568 * @return void
569 * @access private
571 private function _processExportSchema()
574 * Settings for relation stuff
576 require_once './libraries/transformations.lib.php';
577 require_once './libraries/Index.class.php';
579 * default is PDF, otherwise validate it's only letters a-z
581 global $db,$export_type;
582 if (!isset($export_type) || !preg_match('/^[a-zA-Z]+$/', $export_type)) {
583 $export_type = 'pdf';
586 PMA_DBI_select_db($db);
588 include("./libraries/schema/".ucfirst($export_type)."_Relation_Schema.class.php");
589 $obj_schema = eval("new PMA_".ucfirst($export_type)."_Relation_Schema();");
593 * delete X and Y coordinates
595 * @param string db The database name
596 * @param array cfgRelation relation settings
597 * @param integer choosePage selected page for adding relations etc
598 * @return void
599 * @access private
601 public function deleteCoordinates($db, $cfgRelation, $choosePage)
603 $query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
604 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
605 . ' AND pdf_page_number = \'' . PMA_sqlAddSlashes($choosePage) . '\'';
606 PMA_query_as_controluser($query, false);
610 * delete pages
612 * @param string db The database name
613 * @param array cfgRelation relation settings
614 * @param integer choosePage selected page for adding relations etc
615 * @return void
616 * @access private
618 public function deletePages($db, $cfgRelation, $choosePage)
620 $query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
621 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
622 . ' AND page_nr = \'' . PMA_sqlAddSlashes($choosePage) . '\'';
623 PMA_query_as_controluser($query, false);
627 * process internal and foreign key relations
629 * @param string db The database name
630 * @param array cfgRelation relation settings
631 * @param integer pageNumber document number/Id
632 * @return void
633 * @access private
635 public function processRelations($db, $pageNumber, $cfgRelation)
638 * A u t o m a t i c l a y o u t
640 * There are 2 kinds of relations in PMA
641 * 1) Internal Relations 2) Foreign Key Relations
643 if (isset($this->autoLayoutInternal) || isset($this->autoLayoutForeign)) {
644 $all_tables = array();
647 if (isset($this->autoLayoutForeign)) {
649 * get the tables list
650 * who support FOREIGN KEY, it's not
651 * important that we group together InnoDB tables
652 * and PBXT tables, as this logic is just to put
653 * the tables on the layout, not to determine relations
655 $tables = PMA_DBI_get_tables_full($db);
656 $foreignkey_tables = array();
657 foreach ($tables as $table_name => $table_properties) {
658 if (PMA_foreignkey_supported($table_properties['ENGINE'])) {
659 $foreignkey_tables[] = $table_name;
662 $all_tables = $foreignkey_tables;
664 * could be improved by finding the tables which have the
665 * most references keys and placing them at the beginning
666 * of the array (so that they are all center of schema)
668 unset($tables, $foreignkey_tables);
671 if (isset($this->autoLayoutInternal)) {
673 * get the tables list who support Internal Relations;
674 * This type of relations will be created when
675 * you setup the PMA tables correctly
677 $master_tables = 'SELECT COUNT(master_table), master_table'
678 . ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
679 . ' WHERE master_db = \'' . PMA_sqlAddSlashes($db) . '\''
680 . ' GROUP BY master_table'
681 . ' ORDER BY COUNT(master_table) DESC';
682 $master_tables_rs = PMA_query_as_controluser($master_tables, false, PMA_DBI_QUERY_STORE);
683 if ($master_tables_rs && PMA_DBI_num_rows($master_tables_rs) > 0) {
684 /* first put all the master tables at beginning
685 * of the list, so they are near the center of
686 * the schema
688 while (list(, $master_table) = PMA_DBI_fetch_row($master_tables_rs)) {
689 $all_tables[] = $master_table;
692 /* Now for each master, add its foreigns into an array
693 * of foreign tables, if not already there
694 * (a foreign might be foreign for more than
695 * one table, and might be a master itself)
698 $foreign_tables = array();
699 foreach ($all_tables as $master_table) {
700 $foreigners = PMA_getForeigners($db, $master_table);
701 foreach ($foreigners as $foreigner) {
702 if (!in_array($foreigner['foreign_table'], $foreign_tables)) {
703 $foreign_tables[] = $foreigner['foreign_table'];
709 * Now merge the master and foreign arrays/tables
711 foreach ($foreign_tables as $foreign_table) {
712 if (!in_array($foreign_table, $all_tables)) {
713 $all_tables[] = $foreign_table;
719 if (isset($this->autoLayoutInternal) || isset($this->autoLayoutForeign)) {
720 $this->addRelationCoordinates($all_tables,$pageNumber,$db, $cfgRelation);
723 $this->chosenPage = $pageNumber;
727 * Add X and Y coordinates for a table
729 * @param string db The database name
730 * @param array cfgRelation relation settings
731 * @param integer pageNumber document number/Id
732 * @param array all_tables A list of all tables involved
733 * @return void
734 * @access private
736 public function addRelationCoordinates($all_tables,$pageNumber,$db, $cfgRelation)
739 * Now generate the coordinates for the schema
740 * in a clockwise spiral and add to co-ordinates table
742 $pos_x = 300;
743 $pos_y = 300;
744 $delta = 110;
745 $delta_mult = 1.10;
746 $direction = "right";
747 foreach ($all_tables as $current_table) {
749 * save current table's coordinates
751 $insert_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
752 . '(db_name, table_name, pdf_page_number, x, y) '
753 . 'VALUES (\'' . PMA_sqlAddSlashes($db) . '\', \'' . PMA_sqlAddSlashes($current_table) . '\',' . $pageNumber . ',' . $pos_x . ',' . $pos_y . ')';
754 PMA_query_as_controluser($insert_query, false);
757 * compute for the next table
759 switch ($direction) {
760 case 'right':
761 $pos_x += $delta;
762 $direction = "down";
763 $delta *= $delta_mult;
764 break;
765 case 'down':
766 $pos_y += $delta;
767 $direction = "left";
768 $delta *= $delta_mult;
769 break;
770 case 'left':
771 $pos_x -= $delta;
772 $direction = "up";
773 $delta *= $delta_mult;
774 break;
775 case 'up':
776 $pos_y -= $delta;
777 $direction = "right";
778 $delta *= $delta_mult;
779 break;
785 * update X and Y coordinates for a table
787 * @param string db The database name
788 * @param array cfgRelation relation settings
789 * @return void
790 * @access private
792 private function _editCoordinates($db, $cfgRelation)
794 for ($i = 0; $i < $this->c_table_rows; $i++) {
795 $arrvalue = 'c_table_' . $i;
796 global $$arrvalue;
797 $arrvalue = $$arrvalue;
798 if (! isset($arrvalue['x']) || $arrvalue['x'] == '') {
799 $arrvalue['x'] = 0;
801 if (! isset($arrvalue['y']) || $arrvalue['y'] == '') {
802 $arrvalue['y'] = 0;
804 if (isset($arrvalue['name']) && $arrvalue['name'] != '--') {
805 $test_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
806 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
807 . ' AND table_name = \'' . PMA_sqlAddSlashes($arrvalue['name']) . '\''
808 . ' AND pdf_page_number = \'' . PMA_sqlAddSlashes($this->chosenPage) . '\'';
809 $test_rs = PMA_query_as_controluser($test_query, false, PMA_DBI_QUERY_STORE);
810 //echo $test_query;
811 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
812 if (isset($arrvalue['delete']) && $arrvalue['delete'] == 'y') {
813 $ch_query = 'DELETE 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 } else {
818 $ch_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
819 . 'SET x = ' . $arrvalue['x'] . ', y= ' . $arrvalue['y']
820 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
821 . ' AND table_name = \'' . PMA_sqlAddSlashes($arrvalue['name']) . '\''
822 . ' AND pdf_page_number = \'' . PMA_sqlAddSlashes($this->chosenPage) . '\'';
824 } else {
825 $ch_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
826 . '(db_name, table_name, pdf_page_number, x, y) '
827 . 'VALUES (\'' . PMA_sqlAddSlashes($db) . '\', \'' . PMA_sqlAddSlashes($arrvalue['name']) . '\', \'' . PMA_sqlAddSlashes($this->chosenPage) . '\',' . $arrvalue['x'] . ',' . $arrvalue['y'] . ')';
829 //echo $ch_query;
830 PMA_query_as_controluser($ch_query, false);
831 } // end if
832 } // end for