Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / schema / User_Schema.class.php
blob95321b3d6c0311c6cc9046b17ba25479c0aaf471
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Schema support library
6 * @package PhpMyAdmin-schema
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * This Class interacts with the user to gather the information
14 * about their tables for which they want to export the relational schema
15 * export options are shown to user from they can choose
17 * @package PhpMyAdmin-schema
20 class PMA_User_Schema
23 public $chosenPage;
24 public $autoLayoutForeign;
25 public $autoLayoutInternal;
26 public $pageNumber;
27 public $c_table_rows;
28 public $action;
30 /**
31 * Sets action to be performed with schema.
33 * @param string $value action name
35 * @return void
37 public function setAction($value)
39 $this->action = $value;
42 /**
43 * This function will process the user defined pages
44 * and tables which will be exported as Relational schema
45 * you can set the table positions on the paper via scratchboard
46 * for table positions, put the x,y co-ordinates
48 * $this->action tells what the Schema is supposed to do
49 * create and select a page, generate schema etc
51 * @access public
52 * @return void
54 public function processUserChoice()
56 global $db, $cfgRelation;
58 if (isset($this->action)) {
59 switch ($this->action) {
60 case 'selectpage':
61 $this->chosenPage = $_REQUEST['chpage'];
62 if ('1' == $_REQUEST['action_choose']) {
63 $this->deleteCoordinates(
64 $db,
65 $cfgRelation,
66 $this->chosenPage
68 $this->deletePages(
69 $db,
70 $cfgRelation,
71 $this->chosenPage
73 $this->chosenPage = 0;
75 break;
76 case 'createpage':
77 $this->pageNumber = PMA_REL_createPage(
78 $_POST['newpage'],
79 $cfgRelation,
80 $db
82 $this->autoLayoutForeign = isset($_POST['auto_layout_foreign'])
83 ? "1"
84 : null;
85 $this->autoLayoutInternal = isset($_POST['auto_layout_internal'])
86 ? "1"
87 : null;
88 $this->processRelations(
89 $db,
90 $this->pageNumber,
91 $cfgRelation
93 break;
94 case 'edcoord':
95 $this->chosenPage = $_POST['chpage'];
96 $this->c_table_rows = $_POST['c_table_rows'];
97 $this->_editCoordinates($db, $cfgRelation);
98 break;
99 case 'delete_old_references':
100 $this->_deleteTableRows(
101 $_POST['delrow'],
102 $cfgRelation,
103 $db,
104 $_POST['chpage']
106 break;
107 case 'process_export':
108 $this->_processExportSchema();
109 break;
111 } // end switch
112 } // end if (isset($do))
117 * shows/displays the HTML FORM to create the page
119 * @param string $db name of the selected database
121 * @return void
122 * @access public
124 public function showCreatePageDialog($db)
127 <form method="post" action="schema_edit.php" name="frm_create_page">
128 <fieldset>
129 <legend>
130 <?php echo __('Create a page') . "\n"; ?>
131 </legend>
132 <?php echo PMA_generate_common_hidden_inputs($db); ?>
133 <input type="hidden" name="do" value="createpage" />
134 <table>
135 <tr>
136 <td><label for="id_newpage"><?php echo __('Page name'); ?></label></td>
137 <td>
138 <input type="text" name="newpage" id="id_newpage" size="20" maxlength="50" />
139 </td>
140 </tr>
141 <tr>
142 <td><?php echo __('Automatic layout based on'); ?></td>
143 <td>
144 <input type="checkbox" name="auto_layout_internal" id="id_auto_layout_internal" /><label for="id_auto_layout_internal">
145 <?php echo __('Internal relations'); ?></label><br />
146 <?php
148 * Check to see whether INNODB and PBXT storage engines
149 * are Available in MYSQL PACKAGE
150 * If available, then provide AutoLayout for Foreign Keys in Schema View
153 if (PMA_StorageEngine::isValid('InnoDB') || PMA_StorageEngine::isValid('PBXT')) {
155 <input type="checkbox" name="auto_layout_foreign" id="id_auto_layout_foreign" /><label for="id_auto_layout_foreign">
156 <?php echo __('FOREIGN KEY'); ?></label><br />
157 <?php
160 </td></tr>
161 </table>
162 </fieldset>
163 <fieldset class="tblFooters">
164 <input type="submit" value="<?php echo __('Go'); ?>" />
165 </fieldset>
166 </form>
167 <?php
171 * shows/displays the created page names in a drop down list
172 * User can select any page number and edit it using dashboard etc
174 * @return void
175 * @access public
177 public function selectPage()
179 global $db,$table,$cfgRelation;
180 $page_query = 'SELECT * FROM '
181 . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.'
182 . PMA_Util::backquote($cfgRelation['pdf_pages'])
183 . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\'';
184 $page_rs = PMA_queryAsControlUser(
185 $page_query, false, PMA_DBI_QUERY_STORE
188 if ($page_rs && PMA_DBI_num_rows($page_rs) > 0) {
190 <form method="get" action="schema_edit.php" name="frm_select_page">
191 <fieldset>
192 <legend>
193 <?php echo __('Please choose a page to edit') . "\n"; ?>
194 </legend>
195 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
196 <input type="hidden" name="do" value="selectpage" />
197 <select name="chpage" id="chpage" class="autosubmit">
198 <option value="0"><?php echo __('Select page'); ?></option>
199 <?php
200 while ($curr_page = PMA_DBI_fetch_assoc($page_rs)) {
201 echo "\n" . ' '
202 . '<option value="' . $curr_page['page_nr'] . '"';
203 if (isset($this->chosenPage)
204 && $this->chosenPage == $curr_page['page_nr']
206 echo ' selected="selected"';
208 echo '>' . $curr_page['page_nr'] . ': '
209 . htmlspecialchars($curr_page['page_descr']) . '</option>';
210 } // end while
211 echo "\n";
213 </select>
214 <?php
215 $choices = array(
216 '0' => __('Edit'),
217 '1' => __('Delete')
219 echo PMA_Util::getRadioFields(
220 'action_choose', $choices, '0', false
222 unset($choices);
224 </fieldset>
225 <fieldset class="tblFooters">
226 <input type="submit" value="<?php echo __('Go'); ?>" /><br />
227 </fieldset>
228 </form>
229 <?php
230 } // end IF
231 echo "\n";
232 } // end function
235 * A dashboard is displayed to AutoLayout the position of tables
236 * users can drag n drop the tables and change their positions
238 * @return void
239 * @access public
241 public function showTableDashBoard()
243 global $db, $cfgRelation, $table, $with_field_names;
245 * We will need an array of all tables in this db
247 $selectboxall = array('--');
248 $alltab_rs = PMA_DBI_query(
249 'SHOW TABLES FROM ' . PMA_Util::backquote($db) . ';',
250 null,
251 PMA_DBI_QUERY_STORE
253 while ($val = @PMA_DBI_fetch_row($alltab_rs)) {
254 $selectboxall[] = $val[0];
257 $tabExist = array();
260 * Now if we already have chosen a page number then we should
261 * show the tables involved
263 if (isset($this->chosenPage) && $this->chosenPage > 0) {
264 echo "\n";
265 echo "<h2>" . __('Select Tables') . "</h2>";
266 $page_query = 'SELECT * FROM '
267 . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
268 . '.' . PMA_Util::backquote($cfgRelation['table_coords'])
269 . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\''
270 . ' AND pdf_page_number = \''
271 . PMA_Util::sqlAddSlashes($this->chosenPage) . '\'';
272 $page_rs = PMA_queryAsControlUser($page_query, false);
273 $array_sh_page = array();
274 while ($temp_sh_page = @PMA_DBI_fetch_assoc($page_rs)) {
275 $array_sh_page[] = $temp_sh_page;
278 * Display WYSIWYG parts
281 if (! isset($_POST['with_field_names'])
282 && ! isset($_POST['showwysiwyg'])
284 $with_field_names = true;
285 } elseif (isset($_POST['with_field_names'])) {
286 $with_field_names = true;
288 $this->_displayScratchboardTables($array_sh_page);
290 echo '<form method="post" action="schema_edit.php" name="edcoord">';
292 echo PMA_generate_common_hidden_inputs($db, $table);
293 echo '<input type="hidden" name="chpage" '
294 . 'value="' . htmlspecialchars($this->chosenPage) . '" />';
295 echo '<input type="hidden" name="do" value="edcoord" />';
296 echo '<table>';
297 echo '<tr>';
298 echo '<th>' . __('Table') . '</th>';
299 echo '<th>' . __('Delete') . '</th>';
300 echo '<th>X</th>';
301 echo '<th>Y</th>';
302 echo '</tr>';
304 if (isset($ctable)) {
305 unset($ctable);
309 * Add one more empty row
311 $array_sh_page[] = array(
312 'table_name' => '',
313 'x' => '0',
314 'y' => '0',
317 $i = 0;
318 $odd_row = true;
319 foreach ($array_sh_page as $sh_page) {
320 $_mtab = $sh_page['table_name'];
321 if (! empty($_mtab)) {
322 $tabExist[$_mtab] = false;
325 echo '<tr class="noclick ';
326 if ($odd_row) {
327 echo 'odd';
328 } else {
329 echo 'even';
331 echo '">';
332 $odd_row = !$odd_row;
334 echo '<td>';
335 echo '<select name="c_table_' . $i . '[name]">';
337 foreach ($selectboxall as $value) {
338 echo '<option value="' . htmlspecialchars($value) . '"';
339 if (! empty($_mtab) && $value == $_mtab) {
340 echo ' selected="selected"';
341 $tabExist[$_mtab] = true;
343 echo '>' . htmlspecialchars($value) . '</option>';
345 echo '</select>';
346 echo '</td>';
348 echo '<td>';
349 echo '<input type="checkbox" id="id_c_table_' . $i .'" '
350 . 'name="c_table_' . $i . '[delete]" value="y" />';
351 echo '<label for="id_c_table_' . $i .'">'
352 . __('Delete') . '</label>';
353 echo '</td>';
355 echo '<td>';
356 echo '<input type="text" class="position-change" data-axis="left" '
357 . 'data-number="' . $i . '" id="c_table_' . $i . '_x" '
358 . 'name="c_table_' . $i . '[x]" value="'
359 . $sh_page['x'] . '" />';
360 echo '</td>';
362 echo '<td>';
363 echo '<input type="text" class="position-change" data-axis="top" '
364 . 'data-number="' . $i . '" id="c_table_' . $i . '_y" '
365 . 'name="c_table_' . $i . '[y]" value="'
366 . $sh_page['y'] . '" />';
367 echo '</td>';
368 echo '</tr>';
369 $i++;
372 echo '</table>';
374 echo '<input type="hidden" name="c_table_rows" value="' . $i . '" />';
375 echo '<input type="hidden" id="showwysiwyg" name="showwysiwyg" value="'
376 . ((isset($showwysiwyg) && $showwysiwyg == '1') ? '1' : '0')
377 . '" />';
378 echo '<input type="checkbox" id="id_with_field_names" '
379 . 'name="with_field_names" '
380 . (isset($with_field_names) ? 'checked="checked"' : ''). ' />';
381 echo '<label for="id_with_field_names">'
382 . __('Column names') . '</label><br />';
383 echo '<input type="submit" value="' . __('Save') . '" />';
384 echo '</form>' . "\n\n";
385 } // end if
387 if (isset($tabExist)) {
388 $this->_deleteTables($db, $this->chosenPage, $tabExist);
393 * show Export relational schema generation options
394 * user can select export type of his own choice
395 * and the attributes related to it
397 * @return void
398 * @access public
401 public function displaySchemaGenerationOptions()
403 global $cfg,$db,$test_rs,$chpage;
405 <form method="post" action="schema_export.php" class="disableAjax">
406 <fieldset>
407 <legend>
408 <?php
409 echo PMA_generate_common_hidden_inputs($db);
410 if ($cfg['PropertiesIconic']) {
411 echo PMA_Util::getImage('b_views.png');
413 echo __('Display relational schema');
415 </legend>
416 <select name="export_type" id="export_type">
417 <option value="pdf" selected="selected">PDF</option>
418 <option value="svg">SVG</option>
419 <option value="dia">DIA</option>
420 <option value="eps">EPS</option>
421 </select>
422 <label><?php echo __('Select Export Relational Type');?></label><br />
423 <?php
424 if (isset($test_rs)) {
426 <label for="pdf_page_number_opt"><?php echo __('Page number:'); ?></label>
427 <select name="pdf_page_number" id="pdf_page_number_opt">
428 <?php
429 while ($pages = @PMA_DBI_fetch_assoc($test_rs)) {
430 echo ' <option value="' . $pages['page_nr'] . '">'
431 . $pages['page_nr'] . ': ' . htmlspecialchars($pages['page_descr']) . '</option>' . "\n";
432 } // end while
433 PMA_DBI_free_result($test_rs);
434 unset($test_rs);
436 </select><br />
437 <?php
438 } else {
440 <input type="hidden" name="pdf_page_number" value="<?php echo htmlspecialchars($this->chosenPage); ?>" />
441 <?php
444 <input type="hidden" name="do" value="process_export" />
445 <input type="hidden" name="chpage" value="<?php echo $chpage; ?>" />
446 <input type="checkbox" name="show_grid" id="show_grid_opt" />
447 <label for="show_grid_opt"><?php echo __('Show grid'); ?></label><br />
448 <input type="checkbox" name="show_color" id="show_color_opt" checked="checked" />
449 <label for="show_color_opt"><?php echo __('Show color'); ?></label>
450 <br />
451 <input type="checkbox" name="show_table_dimension" id="show_table_dim_opt" />
452 <label for="show_table_dim_opt">
453 <?php echo __('Show dimension of tables'); ?>
454 </label><br />
455 <input type="checkbox" name="all_tables_same_width" id="all_tables_same_width" />
456 <label for="all_tables_same_width">
457 <?php echo __('Display all tables with the same width'); ?>
458 </label><br />
459 <input type="checkbox" name="with_doc" id="with_doc" checked="checked" />
460 <label for="with_doc"><?php echo __('Data Dictionary'); ?></label><br />
461 <input type="checkbox" name="show_keys" id="show_keys" />
462 <label for="show_keys"><?php echo __('Only show keys'); ?></label><br />
463 <select name="orientation" id="orientation_opt" class="paper-change">
464 <option value="L"><?php echo __('Landscape');?></option>
465 <option value="P"><?php echo __('Portrait');?></option>
466 </select>
467 <label for="orientation_opt"><?php echo __('Orientation'); ?></label>
468 <br />
469 <select name="paper" id="paper_opt" class="paper-change">
470 <?php
471 foreach ($cfg['PDFPageSizes'] as $val) {
472 echo '<option value="' . $val . '"';
473 if ($val == $cfg['PDFDefaultPageSize']) {
474 echo ' selected="selected"';
476 echo ' >' . $val . '</option>' . "\n";
479 </select>
480 <label for="paper_opt"><?php echo __('Paper size'); ?></label>
481 </fieldset>
482 <fieldset class="tblFooters">
483 <input type="submit" value="<?php echo __('Go'); ?>" />
484 </fieldset>
485 </form>
486 <?php
490 * Check if there are tables that need to be deleted in dashboard,
491 * if there are, ask the user for allowance
493 * @param string $db name of database selected
494 * @param integer $chpage selected page
495 * @param array $tabExist array of booleans
497 * @return void
498 * @access private
500 private function _deleteTables($db, $chpage, $tabExist)
502 $_strtrans = '';
503 $_strname = '';
504 $shoot = false;
505 if (empty($tabExist) || ! is_array($tabExist)) {
506 return;
508 foreach ($tabExist as $key => $value) {
509 if (! $value) {
510 $_strtrans .= '<input type="hidden" name="delrow[]" value="'
511 . htmlspecialchars($key) . '" />' . "\n";
512 $_strname .= '<li>' . htmlspecialchars($key) . '</li>' . "\n";
513 $shoot = true;
516 if (!$shoot) {
517 return;
519 echo '<br /><form action="schema_edit.php" method="post">' . "\n"
520 . PMA_generate_common_hidden_inputs($db)
521 . '<input type="hidden" name="do" value="delete_old_references" />'
522 . "\n"
523 . '<input type="hidden" name="chpage" value="'
524 . htmlspecialchars($chpage) . '" />' . "\n"
525 . __(
526 'The current page has references to tables that no longer exist.'
527 . ' Would you like to delete those references?'
529 . '<ul>' . "\n"
530 . $_strname
531 . '</ul>' . "\n"
532 . $_strtrans
533 . '<input type="submit" value="' . __('Go') . '" />' . "\n"
534 . '</form>';
538 * Check if there are tables that need to be deleted in dashboard,
539 * if there are, ask the user for allowance
541 * @param array $array_sh_page array of tables on page
543 * @return void
544 * @access private
546 private function _displayScratchboardTables($array_sh_page)
548 global $with_field_names, $db;
550 echo '<form method="post" action="schema_edit.php" name="dragdrop">';
551 echo '<input type="button" name="dragdrop" id="toggle-dragdrop" '
552 . 'value="' . __('Toggle scratchboard') . '" />';
553 echo '<input type="button" name="dragdropreset" id="reset-dragdrop" '
554 . 'value="' . __('Reset') . '" />';
555 echo '</form>';
556 echo '<div id="pdflayout" class="pdflayout" style="visibility: hidden;">';
558 $i = 0;
560 foreach ($array_sh_page as $temp_sh_page) {
561 $drag_x = $temp_sh_page['x'];
562 $drag_y = $temp_sh_page['y'];
564 echo '<div id="table_' . $i . '" '
565 . 'data-number="' . $i .'" '
566 . 'data-x="' . $drag_x . '" '
567 . 'data-y="' . $drag_y . '" '
568 . 'class="pdflayout_table"'
569 . '>'
570 . '<u>'
571 . htmlspecialchars($temp_sh_page['table_name'])
572 . '</u>';
574 if (isset($with_field_names)) {
575 $fields = PMA_DBI_get_columns($db, $temp_sh_page['table_name']);
576 // if the table has been dropped from outside phpMyAdmin,
577 // we can no longer obtain its columns list
578 if ($fields) {
579 foreach ($fields as $row) {
580 echo '<br />' . htmlspecialchars($row['Field']) . "\n";
584 echo '</div>' . "\n";
585 $i++;
588 echo '</div>';
592 * delete the table rows with table co-ordinates
594 * @param int $delrow delete selected table from list of tables
595 * @param array $cfgRelation relation settings
596 * @param string $db database name
597 * @param integer $chpage selected page for adding relations etc
599 * @return void
600 * @access private
602 private function _deleteTableRows($delrow,$cfgRelation,$db,$chpage)
604 foreach ($delrow as $current_row) {
605 $del_query = 'DELETE FROM '
606 . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.'
607 . PMA_Util::backquote($cfgRelation['table_coords']) . ' ' . "\n"
608 . ' WHERE db_name = \''
609 . PMA_Util::sqlAddSlashes($db) . '\'' . "\n"
610 . ' AND table_name = \''
611 . PMA_Util::sqlAddSlashes($current_row) . '\'' . "\n"
612 . ' AND pdf_page_number = \''
613 . PMA_Util::sqlAddSlashes($chpage) . '\'';
614 PMA_queryAsControlUser($del_query, false);
619 * get all the export options and verify
620 * call and include the appropriate Schema Class depending on $export_type
622 * @return void
623 * @access private
625 private function _processExportSchema()
628 * Settings for relation stuff
630 include_once './libraries/transformations.lib.php';
631 include_once './libraries/Index.class.php';
633 * default is PDF, otherwise validate it's only letters a-z
635 global $db,$export_type;
636 if (!isset($export_type) || !preg_match('/^[a-zA-Z]+$/', $export_type)) {
637 $export_type = 'pdf';
640 PMA_DBI_select_db($db);
642 include "libraries/schema/" . ucfirst($export_type)
643 . "_Relation_Schema.class.php";
644 eval("new PMA_" . ucfirst($export_type) . "_Relation_Schema();");
648 * delete X and Y coordinates
650 * @param string $db The database name
651 * @param array $cfgRelation relation settings
652 * @param integer $choosePage selected page for adding relations etc
654 * @return void
655 * @access private
657 public function deleteCoordinates($db, $cfgRelation, $choosePage)
659 $query = 'DELETE FROM '
660 . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.'
661 . PMA_Util::backquote($cfgRelation['table_coords'])
662 . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\''
663 . ' AND pdf_page_number = \''
664 . PMA_Util::sqlAddSlashes($choosePage) . '\'';
665 PMA_queryAsControlUser($query, false);
669 * delete pages
671 * @param string $db The database name
672 * @param array $cfgRelation relation settings
673 * @param integer $choosePage selected page for adding relations etc
675 * @return void
676 * @access private
678 public function deletePages($db, $cfgRelation, $choosePage)
680 $query = 'DELETE FROM '
681 . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.'
682 . PMA_Util::backquote($cfgRelation['pdf_pages'])
683 . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\''
684 . ' AND page_nr = \'' . PMA_Util::sqlAddSlashes($choosePage) . '\'';
685 PMA_queryAsControlUser($query, false);
689 * process internal and foreign key relations
691 * @param string $db The database name
692 * @param integer $pageNumber document number/Id
693 * @param array $cfgRelation relation settings
695 * @return void
696 * @access private
698 public function processRelations($db, $pageNumber, $cfgRelation)
701 * A u t o m a t i c l a y o u t
703 * There are 2 kinds of relations in PMA
704 * 1) Internal Relations 2) Foreign Key Relations
706 if (isset($this->autoLayoutInternal) || isset($this->autoLayoutForeign)) {
707 $all_tables = array();
710 if (isset($this->autoLayoutForeign)) {
712 * get the tables list
713 * who support FOREIGN KEY, it's not
714 * important that we group together InnoDB tables
715 * and PBXT tables, as this logic is just to put
716 * the tables on the layout, not to determine relations
718 $tables = PMA_DBI_get_tables_full($db);
719 $foreignkey_tables = array();
720 foreach ($tables as $table_name => $table_properties) {
721 if (PMA_Util::isForeignKeySupported($table_properties['ENGINE'])) {
722 $foreignkey_tables[] = $table_name;
725 $all_tables = $foreignkey_tables;
727 * could be improved by finding the tables which have the
728 * most references keys and placing them at the beginning
729 * of the array (so that they are all center of schema)
731 unset($tables, $foreignkey_tables);
734 if (isset($this->autoLayoutInternal)) {
736 * get the tables list who support Internal Relations;
737 * This type of relations will be created when
738 * you setup the PMA tables correctly
740 $master_tables = 'SELECT COUNT(master_table), master_table'
741 . ' FROM ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.'
742 . PMA_Util::backquote($cfgRelation['relation'])
743 . ' WHERE master_db = \'' . PMA_Util::sqlAddSlashes($db) . '\''
744 . ' GROUP BY master_table'
745 . ' ORDER BY COUNT(master_table) DESC';
746 $master_tables_rs = PMA_queryAsControlUser(
747 $master_tables, false, PMA_DBI_QUERY_STORE
749 if ($master_tables_rs && PMA_DBI_num_rows($master_tables_rs) > 0) {
750 /* first put all the master tables at beginning
751 * of the list, so they are near the center of
752 * the schema
754 while (list(, $master_table) = PMA_DBI_fetch_row($master_tables_rs)) {
755 $all_tables[] = $master_table;
758 /* Now for each master, add its foreigns into an array
759 * of foreign tables, if not already there
760 * (a foreign might be foreign for more than
761 * one table, and might be a master itself)
764 $foreign_tables = array();
765 foreach ($all_tables as $master_table) {
766 $foreigners = PMA_getForeigners($db, $master_table);
767 foreach ($foreigners as $foreigner) {
768 if (! in_array($foreigner['foreign_table'], $foreign_tables)) {
769 $foreign_tables[] = $foreigner['foreign_table'];
775 * Now merge the master and foreign arrays/tables
777 foreach ($foreign_tables as $foreign_table) {
778 if (! in_array($foreign_table, $all_tables)) {
779 $all_tables[] = $foreign_table;
785 if (isset($this->autoLayoutInternal) || isset($this->autoLayoutForeign)) {
786 $this->addRelationCoordinates(
787 $all_tables, $pageNumber, $db, $cfgRelation
791 $this->chosenPage = $pageNumber;
795 * Add X and Y coordinates for a table
797 * @param array $all_tables A list of all tables involved
798 * @param integer $pageNumber document number/Id
799 * @param string $db The database name
800 * @param array $cfgRelation relation settings
802 * @return void
803 * @access private
805 public function addRelationCoordinates(
806 $all_tables, $pageNumber, $db, $cfgRelation
809 * Now generate the coordinates for the schema
810 * in a clockwise spiral and add to co-ordinates table
812 $pos_x = 300;
813 $pos_y = 300;
814 $delta = 110;
815 $delta_mult = 1.10;
816 $direction = "right";
817 foreach ($all_tables as $current_table) {
819 * save current table's coordinates
821 $insert_query = 'INSERT INTO '
822 . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.'
823 . PMA_Util::backquote($cfgRelation['table_coords']) . ' '
824 . '(db_name, table_name, pdf_page_number, x, y) '
825 . 'VALUES (\'' . PMA_Util::sqlAddSlashes($db) . '\', \''
826 . PMA_Util::sqlAddSlashes($current_table) . '\',' . $pageNumber
827 . ',' . $pos_x . ',' . $pos_y . ')';
828 PMA_queryAsControlUser($insert_query, false);
831 * compute for the next table
833 switch ($direction) {
834 case 'right':
835 $pos_x += $delta;
836 $direction = "down";
837 $delta *= $delta_mult;
838 break;
839 case 'down':
840 $pos_y += $delta;
841 $direction = "left";
842 $delta *= $delta_mult;
843 break;
844 case 'left':
845 $pos_x -= $delta;
846 $direction = "up";
847 $delta *= $delta_mult;
848 break;
849 case 'up':
850 $pos_y -= $delta;
851 $direction = "right";
852 $delta *= $delta_mult;
853 break;
859 * update X and Y coordinates for a table
861 * @param string $db The database name
862 * @param array $cfgRelation relation settings
864 * @return void
865 * @access private
867 private function _editCoordinates($db, $cfgRelation)
869 for ($i = 0; $i < $this->c_table_rows; $i++) {
870 $arrvalue = $_POST['c_table_' . $i];
872 if (! isset($arrvalue['x']) || $arrvalue['x'] == '') {
873 $arrvalue['x'] = 0;
875 if (! isset($arrvalue['y']) || $arrvalue['y'] == '') {
876 $arrvalue['y'] = 0;
878 if (isset($arrvalue['name']) && $arrvalue['name'] != '--') {
879 $test_query = 'SELECT * FROM '
880 . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.'
881 . PMA_Util::backquote($cfgRelation['table_coords'])
882 . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\''
883 . ' AND table_name = \''
884 . PMA_Util::sqlAddSlashes($arrvalue['name']) . '\''
885 . ' AND pdf_page_number = \''
886 . PMA_Util::sqlAddSlashes($this->chosenPage) . '\'';
887 $test_rs = PMA_queryAsControlUser(
888 $test_query, false, PMA_DBI_QUERY_STORE
890 //echo $test_query;
891 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
892 if (isset($arrvalue['delete']) && $arrvalue['delete'] == 'y') {
893 $ch_query = 'DELETE FROM '
894 . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
895 . '.'
896 . PMA_Util::backquote($cfgRelation['table_coords'])
897 . ' WHERE db_name = \''
898 . PMA_Util::sqlAddSlashes($db) . '\''
899 . ' AND table_name = \''
900 . PMA_Util::sqlAddSlashes($arrvalue['name']) . '\''
901 . ' AND pdf_page_number = \''
902 . PMA_Util::sqlAddSlashes($this->chosenPage) . '\'';
903 } else {
904 $ch_query = 'UPDATE '
905 . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
906 . '.' . PMA_Util::backquote($cfgRelation['table_coords'])
907 . ' '
908 . 'SET x = ' . $arrvalue['x'] . ', y= ' . $arrvalue['y']
909 . ' WHERE db_name = \''
910 . PMA_Util::sqlAddSlashes($db) . '\''
911 . ' AND table_name = \''
912 . PMA_Util::sqlAddSlashes($arrvalue['name']) . '\''
913 . ' AND pdf_page_number = \''
914 . PMA_Util::sqlAddSlashes($this->chosenPage) . '\'';
916 } else {
917 $ch_query = 'INSERT INTO '
918 . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
919 . '.' . PMA_Util::backquote($cfgRelation['table_coords'])
920 . ' '
921 . '(db_name, table_name, pdf_page_number, x, y) '
922 . 'VALUES (\'' . PMA_Util::sqlAddSlashes($db) . '\', \''
923 . PMA_Util::sqlAddSlashes($arrvalue['name']) . '\', \''
924 . PMA_Util::sqlAddSlashes($this->chosenPage) . '\','
925 . $arrvalue['x'] . ',' . $arrvalue['y'] . ')';
927 //echo $ch_query;
928 PMA_queryAsControlUser($ch_query, false);
929 } // end if
930 } // end for