User Schema Class: big switch into several methods + reduce global variables

This commit is contained in:
Adnan
2010-07-09 14:39:03 +05:00
parent 1ca552a8e1
commit 07cee82af6
2 changed files with 411 additions and 377 deletions

View File

@@ -71,7 +71,7 @@ if ($cfgRelation['pdfwork']) {
* User object created for presenting the HTML options * User object created for presenting the HTML options
* so, user can interact with it and perform export of relations schema * so, user can interact with it and perform export of relations schema
*/ */
require_once './libraries/schema/User_Schema.class.php'; require_once './libraries/schema/User_Schema.class.php';
$user_schema = new PMA_User_Schema(); $user_schema = new PMA_User_Schema();
@@ -98,7 +98,7 @@ if ($cfgRelation['pdfwork']) {
* Create a new page where relations will be drawn * Create a new page where relations will be drawn
*/ */
$user_schema->createPage(); $user_schema->createPage($db);
/** /**
* After selection of page or creating a page * After selection of page or creating a page
@@ -110,8 +110,8 @@ if ($cfgRelation['pdfwork']) {
if (isset($do) if (isset($do)
&& ($do == 'edcoord' && ($do == 'edcoord'
|| ($do == 'selectpage' && isset($chpage) && $chpage != 0) || ($do == 'selectpage' && isset($user_schema->choosenPage) && $user_schema->choosenPage != 0)
|| ($do == 'createpage' && isset($chpage) && $chpage != 0))) { || ($do == 'createpage' && isset($user_schema->choosenPage) && $user_schema->choosenPage != 0))) {
/** /**
* show Export schema generation options * show Export schema generation options
@@ -119,12 +119,12 @@ if ($cfgRelation['pdfwork']) {
$user_schema->displaySchemaGenerationOptions(); $user_schema->displaySchemaGenerationOptions();
if ((isset($showwysiwyg) && $showwysiwyg == '1')) { if ((isset($showwysiwyg) && $showwysiwyg == '1')) {
?> ?>
<script type="text/javascript"> <script type="text/javascript">
//<![CDATA[ //<![CDATA[
ToggleDragDrop('pdflayout'); ToggleDragDrop('pdflayout');
//]]> //]]>
</script> </script>
<?php <?php
} }
} // end if } // end if

View File

@@ -11,250 +11,84 @@
* about their tables for which they want to export the relational schema * about their tables for which they want to export the relational schema
* export options are shown to user from they can choose * export options are shown to user from they can choose
* *
* @name * @name User_Schema
* @author Muhammad Adnan <hiddenpearls@gmail.com> * @author Muhammad Adnan <hiddenpearls@gmail.com>
* @copyright * @copyright
* @license * @license
*/ */
class PMA_User_Schema class PMA_User_Schema
{ {
public $choosenPage;
public $autoLayoutForeign;
public $autoLayoutInternal;
public $pageNumber;
public $c_table_rows;
/** /**
* This function will process the user defined pages * This function will process the user defined pages
* and tables which will be exported as Relational schema * and tables which will be exported as Relational schema
* you can set the table positions on the paper via scratchboard * you can set the table positions on the paper via scratchboard
* for table positions, put the x,y co-ordinates * for table positions, put the x,y co-ordinates
* *
* @param string $do It tells what the Schema is supposed to do * @param string $action It tells what the Schema is supposed to do
* create and select a page, generate schema etc * create and select a page, generate schema etc
* @access public * @access public
*/ */
public function processUserPreferences($do) public function processUserPreferences($action)
{ {
global $action_choose,$chpage,$db,$cfgRelation,$cfg,$auto_layout_foreign,$auto_layout_internal,$newpage,$c_table_rows,$query_default_option; global $action_choose,$db,$cfgRelation,$cfg,$query_default_option;
if (isset($do)) { if (isset($action)) {
switch ($do) { switch ($action) {
case 'selectpage': case 'selectpage':
if ($action_choose=="1") { $this->choosenPage = $_REQUEST['chpage'];
$ch_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) if ($action_choose=="1") {
. ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'' $this->deleteCoordinates($db, $cfgRelation, $this->choosenPage, $query_default_option);
. ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\''; $this->deletePages($db, $cfgRelation, $this->choosenPage, $query_default_option);
PMA_query_as_controluser($ch_query, FALSE, $query_default_option); $this->choosenPage = 0;
$ch_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
. ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
. ' AND page_nr = \'' . PMA_sqlAddslashes($chpage) . '\'';
PMA_query_as_controluser($ch_query, FALSE, $query_default_option);
unset($chpage);
}
break;
case 'createpage':
$pdf_page_number = PMA_REL_create_page($newpage, $cfgRelation, $db, $query_default_option);
/*
* A u t o m a t i c l a y o u t
*
* There are 2 kinds of relations in PMA
* 1) Internal Relations 2) Foreign Key Relations
*/
if (isset($auto_layout_internal) || isset($auto_layout_foreign)) {
$all_tables = array();
}
if (isset($auto_layout_foreign)) {
/*
* get the tables list
* who support FOREIGN KEY, it's not
* important that we group together InnoDB tables
* and PBXT tables, as this logic is just to put
* the tables on the layout, not to determine relations
*/
$tables = PMA_DBI_get_tables_full($db);
$foreignkey_tables = array();
foreach($tables as $table_name => $table_properties) {
if (PMA_foreignkey_supported($table_properties['ENGINE'])) {
$foreignkey_tables[] = $table_name;
}
} }
$all_tables = $foreignkey_tables; break;
// could be improved by finding the tables which have the case 'createpage':
// most references keys and placing them at the beginning $this->pageNumber = PMA_REL_create_page($_POST['newpage'], $cfgRelation, $db, $query_default_option);
// of the array (so that they are all center of schema) $this->autoLayoutForeign = $_POST['auto_layout_foreign'];
unset($tables, $foreignkey_tables); $this->autoLayoutInternal = $_POST['auto_layout_internal'];
} $this->processRelations($db, $this->pageNumber,$cfgRelation,$query_default_option);
break;
case 'edcoord':
$this->choosenPage = $_POST['chpage'];
$this->c_table_rows = $_POST['c_table_rows'];
$this->editCoordinates($db, $cfgRelation);
break;
case 'deleteCrap':
$this->_deleteTableRows($delrow,$cfgRelation,$db,$this->choosenPage);
break;
case 'process_export':
$this->_processExportSchema();
break;
if (isset($auto_layout_internal)) { } // end switch
/* } // end if (isset($do))
* get the tables list who support Internal Relations;
* This type of relations will be created when
* you setup the PMA tables correctly
*/
$master_tables = 'SELECT COUNT(master_table), master_table'
. ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
. ' WHERE master_db = \'' . $db . '\''
. ' GROUP BY master_table'
. ' ORDER BY ' . PMA_backquote('COUNT(master_table)') . ' DESC ';
$master_tables_rs = PMA_query_as_controluser($master_tables, FALSE, $query_default_option);
if ($master_tables_rs && PMA_DBI_num_rows($master_tables_rs) > 0) {
/* first put all the master tables at beginning
* of the list, so they are near the center of
* the schema
*/
while (list(, $master_table) = PMA_DBI_fetch_row($master_tables_rs)) {
$all_tables[] = $master_table;
}
/* Now for each master, add its foreigns into an array }
* of foreign tables, if not already there
* (a foreign might be foreign for more than
* one table, and might be a master itself)
*/
$foreign_tables = array();
foreach ($all_tables as $master_table) {
$foreigners = PMA_getForeigners($db, $master_table);
foreach ($foreigners as $foreigner) {
if (!in_array($foreigner['foreign_table'], $foreign_tables)) {
$foreign_tables[] = $foreigner['foreign_table'];
}
}
}
/*
* Now merge the master and foreign arrays/tables
*/
foreach ($foreign_tables as $foreign_table) {
if (!in_array($foreign_table, $all_tables)) {
$all_tables[] = $foreign_table;
}
}
} // endif there are master tables
} // endif auto_layout_internal
if (isset($auto_layout_internal) || isset($auto_layout_foreign)) {
/*
* Now generate the coordinates for the schema
* in a clockwise spiral
*/
$pos_x = 300;
$pos_y = 300;
$delta = 110;
$delta_mult = 1.10;
$direction = "right";
foreach ($all_tables as $current_table) {
/*
* save current table's coordinates
*/
$insert_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
. '(db_name, table_name, pdf_page_number, x, y) '
. 'VALUES (\'' . PMA_sqlAddslashes($db) . '\', \'' . PMA_sqlAddslashes($current_table) . '\',' . $pdf_page_number . ',' . $pos_x . ',' . $pos_y . ')';
PMA_query_as_controluser($insert_query, FALSE, $query_default_option);
/*
* compute for the next table
*/
switch ($direction) {
case 'right':
$pos_x += $delta;
$direction = "down";
$delta *= $delta_mult;
break;
case 'down':
$pos_y += $delta;
$direction = "left";
$delta *= $delta_mult;
break;
case 'left':
$pos_x -= $delta;
$direction = "up";
$delta *= $delta_mult;
break;
case 'up':
$pos_y -= $delta;
$direction = "right";
$delta *= $delta_mult;
break;
} // end switch
} // end foreach
} // end if some auto-layout to do
$chpage = $pdf_page_number;
break;
case 'edcoord':
for ($i = 0; $i < $c_table_rows; $i++) {
$arrvalue = 'c_table_' . $i;
global $$arrvalue;
$arrvalue = $$arrvalue;
if (!isset($arrvalue['x']) || $arrvalue['x'] == '') {
$arrvalue['x'] = 0;
}
if (!isset($arrvalue['y']) || $arrvalue['y'] == '') {
$arrvalue['y'] = 0;
}
if (isset($arrvalue['name']) && $arrvalue['name'] != '--') {
$test_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
. ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
. ' AND table_name = \'' . PMA_sqlAddslashes($arrvalue['name']) . '\''
. ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\'';
// echo $test_query;
$test_rs = PMA_query_as_controluser($test_query, FALSE, $query_default_option);
if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
if (isset($arrvalue['delete']) && $arrvalue['delete'] == 'y') {
$ch_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
. ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
. ' AND table_name = \'' . PMA_sqlAddslashes($arrvalue['name']) . '\''
. ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\'';
} else {
$ch_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
. 'SET x = ' . $arrvalue['x'] . ', y= ' . $arrvalue['y']
. ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
. ' AND table_name = \'' . PMA_sqlAddslashes($arrvalue['name']) . '\''
. ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\'';
}
} else {
$ch_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
. '(db_name, table_name, pdf_page_number, x, y) '
. 'VALUES (\'' . PMA_sqlAddslashes($db) . '\', \'' . PMA_sqlAddslashes($arrvalue['name']) . '\', \'' . PMA_sqlAddslashes($chpage) . '\',' . $arrvalue['x'] . ',' . $arrvalue['y'] . ')';
}
//echo $ch_query;
PMA_query_as_controluser($ch_query, FALSE, $query_default_option);
} // end if
} // end for
break;
case 'deleteCrap':
$this->_deleteTableRows();
break;
case 'process_export':
$this->_processExportSchema();
break;
} // end switch
} // end if (isset($do))
}
/** /**
* shows/displays the HTML FORM to create the page * shows/displays the HTML FORM to create the page
* *
* @access public * @access public
*/ */
public function createPage() public function createPage($db)
{ {
global $db,$table;
?> ?>
<form method="post" action="export_relation_schema.php" name="frm_create_page"> <form method="post" action="export_relation_schema.php" name="frm_create_page">
<fieldset> <fieldset>
<legend> <legend>
<?php echo __('Create a page !') . "\n"; ?> <?php echo __('Create a page !') . "\n"; ?>
</legend> </legend>
<?php echo PMA_generate_common_hidden_inputs($db, $table); ?> <?php echo PMA_generate_common_hidden_inputs($db); ?>
<input type="hidden" name="do" value="createpage" /> <input type="hidden" name="do" value="createpage" />
<table> <table>
<tr> <tr>
@@ -278,7 +112,7 @@ class PMA_User_Schema
<?php echo __('FOREIGN KEY'); ?></label><br /> <?php echo __('FOREIGN KEY'); ?></label><br />
<?php <?php
} }
?> ?>
</td></tr> </td></tr>
</table> </table>
</fieldset> </fieldset>
@@ -287,23 +121,23 @@ class PMA_User_Schema
</fieldset> </fieldset>
</form> </form>
<?php <?php
} }
/** /**
* shows/displays the created page names in a drop down list * shows/displays the created page names in a drop down list
* User can select any page number and edit it using dashboard etc * User can select any page number and edit it using dashboard etc
* *
* @access public * @access public
*/ */
public function selectPage() public function selectPage()
{ {
global $db,$table,$query_default_option,$cfgRelation,$chpage; global $db,$table,$query_default_option,$cfgRelation;
$page_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) $page_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
. ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''; . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
$page_rs = PMA_query_as_controluser($page_query, FALSE, $query_default_option); $page_rs = PMA_query_as_controluser($page_query, FALSE, $query_default_option);
if ($page_rs && PMA_DBI_num_rows($page_rs) > 0) { if ($page_rs && PMA_DBI_num_rows($page_rs) > 0) {
?> ?>
<form method="get" action="export_relation_schema.php" name="frm_select_page"> <form method="get" action="export_relation_schema.php" name="frm_select_page">
<fieldset> <fieldset>
<legend> <legend>
@@ -311,13 +145,13 @@ class PMA_User_Schema
</legend> </legend>
<?php echo PMA_generate_common_hidden_inputs($db, $table); ?> <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
<input type="hidden" name="do" value="selectpage" /> <input type="hidden" name="do" value="selectpage" />
<select name="chpage" onchange="this.form.submit()"> <select name="chpage" id="chpage" onchange="this.form.submit()">
<option value='0'><?php echo __('Select page'); ?></option> <option value="0"><?php echo __('Select page'); ?></option>
<?php <?php
while ($curr_page = PMA_DBI_fetch_assoc($page_rs)) { while ($curr_page = PMA_DBI_fetch_assoc($page_rs)) {
echo "\n" . ' ' echo "\n" . ' '
. '<option value="' . $curr_page['page_nr'] . '"'; . '<option value="' . $curr_page['page_nr'] . '"';
if (isset($chpage) && $chpage == $curr_page['page_nr']) { if (isset($this->choosenPage) && $this->choosenPage == $curr_page['page_nr']) {
echo ' selected="selected"'; echo ' selected="selected"';
} }
echo '>' . $curr_page['page_nr'] . ': ' . htmlspecialchars($curr_page['page_descr']) . '</option>'; echo '>' . $curr_page['page_nr'] . ': ' . htmlspecialchars($curr_page['page_descr']) . '</option>';
@@ -329,7 +163,7 @@ class PMA_User_Schema
$choices = array( $choices = array(
'0' => __('Edit'), '0' => __('Edit'),
'1' => __('Delete') '1' => __('Delete')
); );
PMA_display_html_radio('action_choose', $choices, '0', false); PMA_display_html_radio('action_choose', $choices, '0', false);
unset($choices); unset($choices);
?> ?>
@@ -341,17 +175,17 @@ class PMA_User_Schema
<?php <?php
} // end IF } // end IF
echo "\n"; echo "\n";
} // end function } // end function
/** /**
* A dashboard is displayed to AutoLayout the position of tables * A dashboard is displayed to AutoLayout the position of tables
* users can drag n drop the tables and change their positions * users can drag n drop the tables and change their positions
* *
*/ */
public function showTableDashBoard() public function showTableDashBoard()
{ {
global $db,$cfgRelation,$table,$cfg,$with_field_names,$chpage; global $db,$cfgRelation,$table,$cfg,$with_field_names;
/* /*
* We will need an array of all tables in this db * We will need an array of all tables in this db
*/ */
@@ -366,14 +200,14 @@ class PMA_User_Schema
* show the tables involved * show the tables involved
*/ */
if (isset($chpage) && $chpage > 0) { if (isset($this->choosenPage) && $this->choosenPage > 0) {
echo "\n"; echo "\n";
?> ?>
<h2><?php echo __('Select Tables') ;?></h2> <h2><?php echo __('Select Tables') ;?></h2>
<?php <?php
$page_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) $page_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
. ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'' . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
. ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\''; . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($this->choosenPage) . '\'';
$page_rs = PMA_query_as_controluser($page_query, FALSE, $query_default_option); $page_rs = PMA_query_as_controluser($page_query, FALSE, $query_default_option);
$array_sh_page = array(); $array_sh_page = array();
$draginit = ''; $draginit = '';
@@ -382,57 +216,86 @@ class PMA_User_Schema
while ($temp_sh_page = @PMA_DBI_fetch_assoc($page_rs)) { while ($temp_sh_page = @PMA_DBI_fetch_assoc($page_rs)) {
$array_sh_page[] = $temp_sh_page; $array_sh_page[] = $temp_sh_page;
} }
/*
* Display WYSIWYG-PDF parts?
*/
/* if ($cfg['WYSIWYG-PDF']) {
* Display WYSIWYG-PDF parts? if (!isset($_POST['with_field_names']) && !isset($_POST['showwysiwyg'])) {
*/ $with_field_names = TRUE;
}
$this->_displayScratchboardTables($array_sh_page,$draginit,$reset_draginit);
}
?>
if ($cfg['WYSIWYG-PDF']) { <form method="post" action="export_relation_schema.php" name="edcoord">
if (!isset($_POST['with_field_names']) && !isset($_POST['showwysiwyg'])) { <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
$with_field_names = TRUE; <input type="hidden" name="chpage" value="<?php echo htmlspecialchars($this->choosenPage); ?>" />
} <input type="hidden" name="do" value="edcoord" />
$this->_displayScratchboardTables($array_sh_page,$draginit,$reset_draginit); <table border="0">
} <tr>
?> <th><?php echo __('Table'); ?></th>
<th><?php echo __('Delete'); ?></th>
<th>X</th>
<th>Y</th>
</tr>
<?php
if (isset($ctable)) {
unset($ctable);
}
<form method="post" action="export_relation_schema.php" name="edcoord"> $i = 0;
<?php echo PMA_generate_common_hidden_inputs($db, $table); ?> $odd_row = true;
<input type="hidden" name="chpage" value="<?php echo htmlspecialchars($chpage); ?>" /> foreach ($array_sh_page as $dummy_sh_page => $sh_page) {
<input type="hidden" name="do" value="edcoord" /> $_mtab = $sh_page['table_name'];
<table border="0"> $tabExist[$_mtab] = FALSE;
<tr> echo "\n" . ' <tr class="';
<th><?php echo __('Table'); ?></th> if ($odd_row) {
<th><?php echo __('Delete'); ?></th> echo 'odd';
<th>X</th> } else {
<th>Y</th> echo 'even';
</tr> }
<?php echo '">';
if (isset($ctable)) { $odd_row != $odd_row;
unset($ctable); echo "\n" . ' <td>'
} . "\n" . ' <select name="c_table_' . $i . '[name]">';
foreach ($selectboxall as $key => $value) {
$i = 0; echo "\n" . ' <option value="' . htmlspecialchars($value) . '"';
$odd_row = true; if ($value == $sh_page['table_name']) {
foreach ($array_sh_page as $dummy_sh_page => $sh_page) { echo ' selected="selected"';
$_mtab = $sh_page['table_name']; $tabExist[$_mtab] = TRUE;
$tabExist[$_mtab] = FALSE; }
echo '>' . htmlspecialchars($value) . '</option>';
}
echo "\n" . ' </select>'
. "\n" . ' </td>';
echo "\n" . ' <td>'
. "\n" . ' <input type="checkbox" name="c_table_' . $i . '[delete]" value="y" />' . __('Delete');
echo "\n" . ' </td>';
echo "\n" . ' <td>'
. "\n" . ' <input type="text" ' . ($cfg['WYSIWYG-PDF'] ? 'onchange="dragPlace(' . $i . ', \'x\', this.value)"' : '') . ' name="c_table_' . $i . '[x]" value="' . $sh_page['x'] . '" />';
echo "\n" . ' </td>';
echo "\n" . ' <td>'
. "\n" . ' <input type="text" ' . ($cfg['WYSIWYG-PDF'] ? 'onchange="dragPlace(' . $i . ', \'y\', this.value)"' : '') . ' name="c_table_' . $i . '[y]" value="' . $sh_page['y'] . '" />';
echo "\n" . ' </td>';
echo "\n" . ' </tr>';
$i++;
}
/*
* Add one more empty row
*/
echo "\n" . ' <tr class="'; echo "\n" . ' <tr class="';
if ($odd_row) { if ($odd_row) {
echo 'odd'; echo 'odd';
} else { } else {
echo 'even'; echo 'even';
} }
echo '">';
$odd_row != $odd_row; $odd_row != $odd_row;
echo '">';
echo "\n" . ' <td>' echo "\n" . ' <td>'
. "\n" . ' <select name="c_table_' . $i . '[name]">'; . "\n" . ' <select name="c_table_' . $i . '[name]">';
foreach ($selectboxall as $key => $value) { foreach ($selectboxall as $key => $value) {
echo "\n" . ' <option value="' . htmlspecialchars($value) . '"'; echo "\n" . ' <option value="' . htmlspecialchars($value) . '">' . htmlspecialchars($value) . '</option>';
if ($value == $sh_page['table_name']) {
echo ' selected="selected"';
$tabExist[$_mtab] = TRUE;
}
echo '>' . htmlspecialchars($value) . '</option>';
} }
echo "\n" . ' </select>' echo "\n" . ' </select>'
. "\n" . ' </td>'; . "\n" . ' </td>';
@@ -440,60 +303,30 @@ class PMA_User_Schema
. "\n" . ' <input type="checkbox" name="c_table_' . $i . '[delete]" value="y" />' . __('Delete'); . "\n" . ' <input type="checkbox" name="c_table_' . $i . '[delete]" value="y" />' . __('Delete');
echo "\n" . ' </td>'; echo "\n" . ' </td>';
echo "\n" . ' <td>' echo "\n" . ' <td>'
. "\n" . ' <input type="text" ' . ($cfg['WYSIWYG-PDF'] ? 'onchange="dragPlace(' . $i . ', \'x\', this.value)"' : '') . ' name="c_table_' . $i . '[x]" value="' . $sh_page['x'] . '" />'; . "\n" . ' <input type="text" name="c_table_' . $i . '[x]" value="' . (isset($sh_page['x'])?$sh_page['x']:'') . '" />';
echo "\n" . ' </td>'; echo "\n" . ' </td>';
echo "\n" . ' <td>' echo "\n" . ' <td>'
. "\n" . ' <input type="text" ' . ($cfg['WYSIWYG-PDF'] ? 'onchange="dragPlace(' . $i . ', \'y\', this.value)"' : '') . ' name="c_table_' . $i . '[y]" value="' . $sh_page['y'] . '" />'; . "\n" . ' <input type="text" name="c_table_' . $i . '[y]" value="' . (isset($sh_page['y'])?$sh_page['y']:'') . '" />';
echo "\n" . ' </td>'; echo "\n" . ' </td>';
echo "\n" . ' </tr>'; echo "\n" . ' </tr>';
$i++; echo "\n" . ' </table>' . "\n";
}
/*
* Add one more empty row
*/
echo "\n" . ' <tr class="';
if ($odd_row) {
echo 'odd';
} else {
echo 'even';
}
$odd_row != $odd_row;
echo '">';
echo "\n" . ' <td>'
. "\n" . ' <select name="c_table_' . $i . '[name]">';
foreach ($selectboxall as $key => $value) {
echo "\n" . ' <option value="' . htmlspecialchars($value) . '">' . htmlspecialchars($value) . '</option>';
}
echo "\n" . ' </select>'
. "\n" . ' </td>';
echo "\n" . ' <td>'
. "\n" . ' <input type="checkbox" name="c_table_' . $i . '[delete]" value="y" />' . __('Delete');
echo "\n" . ' </td>';
echo "\n" . ' <td>'
. "\n" . ' <input type="text" name="c_table_' . $i . '[x]" value="' . (isset($sh_page['x'])?$sh_page['x']:'') . '" />';
echo "\n" . ' </td>';
echo "\n" . ' <td>'
. "\n" . ' <input type="text" name="c_table_' . $i . '[y]" value="' . (isset($sh_page['y'])?$sh_page['y']:'') . '" />';
echo "\n" . ' </td>';
echo "\n" . ' </tr>';
echo "\n" . ' </table>' . "\n";
echo "\n" . ' <input type="hidden" name="c_table_rows" value="' . ($i + 1) . '" />'; echo "\n" . ' <input type="hidden" name="c_table_rows" value="' . ($i + 1) . '" />';
echo ($cfg['WYSIWYG-PDF'] ? "\n" . ' <input type="hidden" id="showwysiwyg" name="showwysiwyg" value="' . ((isset($showwysiwyg) && $showwysiwyg == '1') ? '1' : '0') . '" />' : ''); echo ($cfg['WYSIWYG-PDF'] ? "\n" . ' <input type="hidden" id="showwysiwyg" name="showwysiwyg" value="' . ((isset($showwysiwyg) && $showwysiwyg == '1') ? '1' : '0') . '" />' : '');
echo "\n" . ' <input type="checkbox" name="with_field_names" ' . (isset($with_field_names) ? 'checked="checked"' : ''). ' />' . __('Column names') . '<br />'; echo "\n" . ' <input type="checkbox" name="with_field_names" ' . (isset($with_field_names) ? 'checked="checked"' : ''). ' />' . __('Column names') . '<br />';
echo "\n" . ' <input type="submit" value="' . __('Save') . '" />'; echo "\n" . ' <input type="submit" value="' . __('Save') . '" />';
echo "\n" . '</form>' . "\n\n"; echo "\n" . '</form>' . "\n\n";
} // end if } // end if
$this->_deleteTables($chpage); $this->_deleteTables($db, $this->choosenPage, $tabExist);
} }
/** /**
* show Export relational schema generation options * show Export relational schema generation options
* user can select export type of his own choice * user can select export type of his own choice
* and the attributes related to it * and the attributes related to it
*/ */
public function displaySchemaGenerationOptions() public function displaySchemaGenerationOptions()
{ {
global $cfg,$pmaThemeImage,$db,$test_rs,$chpage; global $cfg,$pmaThemeImage,$db,$test_rs,$chpage;
@@ -501,15 +334,15 @@ class PMA_User_Schema
<form method="post" action="export_relation_schema.php"> <form method="post" action="export_relation_schema.php">
<fieldset> <fieldset>
<legend> <legend>
<?php <?php
echo PMA_generate_common_hidden_inputs($db); echo PMA_generate_common_hidden_inputs($db);
if ($cfg['PropertiesIconic']) { if ($cfg['PropertiesIconic']) {
echo '<img class="icon" src="' . $pmaThemeImage . 'b_view.png"' echo '<img class="icon" src="' . $pmaThemeImage . 'b_view.png"'
.' alt="" width="16" height="16" />'; .' alt="" width="16" height="16" />';
} }
echo __('Display Relational Schema'); echo __('Display Relational Schema');
?>: ?>:
</legend> </legend>
<select name="export_type" id="export_type"> <select name="export_type" id="export_type">
<option value="pdf" selected="selected"><?php echo __('PDF');?></option> <option value="pdf" selected="selected"><?php echo __('PDF');?></option>
<option value="svg"><?php echo __('SVG');?></option> <option value="svg"><?php echo __('SVG');?></option>
@@ -533,7 +366,7 @@ class PMA_User_Schema
?> ?>
</select><br /> </select><br />
<?php } else { ?> <?php } else { ?>
<input type="hidden" name="pdf_page_number" value="<?php echo htmlspecialchars($chpage); ?>" /> <input type="hidden" name="pdf_page_number" value="<?php echo htmlspecialchars($this->choosenPage); ?>" />
<?php } ?> <?php } ?>
<input type="hidden" name="do" value="process_export" /> <input type="hidden" name="do" value="process_export" />
<input type="checkbox" name="show_grid" id="show_grid_opt" /> <input type="checkbox" name="show_grid" id="show_grid_opt" />
@@ -543,8 +376,8 @@ class PMA_User_Schema
<input type="checkbox" name="show_table_dimension" id="show_table_dim_opt" /> <input type="checkbox" name="show_table_dimension" id="show_table_dim_opt" />
<label for="show_table_dim_opt"><?php echo __('Show dimension of tables'); ?> <label for="show_table_dim_opt"><?php echo __('Show dimension of tables'); ?>
</label><br /> </label><br />
<input type="checkbox" name="all_tab_same_wide" id="all_tab_same_wide" /> <input type="checkbox" name="all_table_same_wide" id="all_table_same_wide" />
<label for="all_tab_same_wide"><?php echo __('Display all tables with the same width'); ?> <label for="all_table_same_wide"><?php echo __('Display all tables with the same width'); ?>
</label><br /> </label><br />
<input type="checkbox" name="with_doc" id="with_doc" checked="checked" /> <input type="checkbox" name="with_doc" id="with_doc" checked="checked" />
<label for="with_doc"><?php echo __('Data Dictionary'); ?></label><br /> <label for="with_doc"><?php echo __('Data Dictionary'); ?></label><br />
@@ -575,44 +408,43 @@ class PMA_User_Schema
</fieldset> </fieldset>
</form> </form>
<?php <?php
}// end function }
/** /**
* Check if there are tables that need to be deleted in dashboard, * Check if there are tables that need to be deleted in dashboard,
* if there are, ask the user for allowance * if there are, ask the user for allowance
* @param string $chpage selected page * @param string $chpage selected page
* @access private * @access private
*/ */
private function _deleteTables($chpage) private function _deleteTables($db, $chpage, $tabExist)
{ {
global $db, $table,$tabExist; $_strtrans = '';
$_strname = '';
$_strtrans = ''; $shoot = FALSE;
$_strname = ''; if (!empty($tabExist) && is_array($tabExist)) {
$shoot = FALSE; foreach ($tabExist as $key => $value) {
if (!empty($tabExist) && is_array($tabExist)) { if (!$value) {
foreach ($tabExist as $key => $value) { $_strtrans .= '<input type="hidden" name="delrow[]" value="' . htmlspecialchars($key) . '" />' . "\n";
if (!$value) { $_strname .= '<li>' . htmlspecialchars($key) . '</li>' . "\n";
$_strtrans .= '<input type="hidden" name="delrow[]" value="' . htmlspecialchars($key) . '" />' . "\n"; $shoot = TRUE;
$_strname .= '<li>' . htmlspecialchars($key) . '</li>' . "\n"; }
$shoot = TRUE; }
} if ($shoot) {
} echo '<form action="export_relation_schema.php" method="post">' . "\n"
if ($shoot) { . PMA_generate_common_hidden_inputs($db, $table)
echo '<form action="export_relation_schema.php" method="post">' . "\n" . '<input type="hidden" name="do" value="deleteCrap" />' . "\n"
. PMA_generate_common_hidden_inputs($db, $table) . '<input type="hidden" name="chpage" value="' . htmlspecialchars($chpage) . '" />' . "\n"
. '<input type="hidden" name="do" value="deleteCrap" />' . "\n" . __('The current page has references to tables that no longer exist. Would you like to delete those references?')
. '<input type="hidden" name="chpage" value="' . htmlspecialchars($chpage) . '" />' . "\n" . '<ul>' . "\n"
. __('The current page has references to tables that no longer exist. Would you like to delete those references?') . $_strname
. '<ul>' . "\n" . '</ul>' . "\n"
. $_strname . $_strtrans
. '</ul>' . "\n" . '<input type="submit" value="' . __('Go') . '" />' . "\n"
. $_strtrans . '</form>';
. '<input type="submit" value="' . __('Go') . '" />' . "\n" }
. '</form>'; }
}
} }
} // end function
/** /**
* Check if there are tables that need to be deleted in dashboard, * Check if there are tables that need to be deleted in dashboard,
@@ -630,6 +462,7 @@ class PMA_User_Schema
</form> </form>
<div id="pdflayout" class="pdflayout" style="visibility: hidden;"> <div id="pdflayout" class="pdflayout" style="visibility: hidden;">
<?php <?php
$i = 0;
foreach ($array_sh_page as $key => $temp_sh_page) { foreach ($array_sh_page as $key => $temp_sh_page) {
$drag_x = $temp_sh_page['x']; $drag_x = $temp_sh_page['x'];
$drag_y = $temp_sh_page['y']; $drag_y = $temp_sh_page['y'];
@@ -642,14 +475,14 @@ class PMA_User_Schema
$reset_draginit .= ' getElement("table_' . $i . '").style.top = "' . (15 * $i) . 'px";' . "\n"; $reset_draginit .= ' getElement("table_' . $i . '").style.top = "' . (15 * $i) . 'px";' . "\n";
$reset_draginit .= ' document.edcoord.elements["c_table_' . $i . '[x]"].value = "2"' . "\n"; $reset_draginit .= ' document.edcoord.elements["c_table_' . $i . '[x]"].value = "2"' . "\n";
$reset_draginit .= ' document.edcoord.elements["c_table_' . $i . '[y]"].value = "' . (15 * $i) . '"' . "\n"; $reset_draginit .= ' document.edcoord.elements["c_table_' . $i . '[y]"].value = "' . (15 * $i) . '"' . "\n";
$local_query = 'SHOW FIELDS FROM ' $local_query = 'SHOW FIELDS FROM '
. PMA_backquote($temp_sh_page['table_name']) . PMA_backquote($temp_sh_page['table_name'])
. ' FROM ' . PMA_backquote($db); . ' FROM ' . PMA_backquote($db);
$fields_rs = PMA_DBI_query($local_query); $fields_rs = PMA_DBI_query($local_query);
unset($local_query); unset($local_query);
$fields_cnt = PMA_DBI_num_rows($fields_rs); $fields_cnt = PMA_DBI_num_rows($fields_rs);
echo '<div id="table_' . $i . '" class="pdflayout_table"><u>' . $temp_sh_page['table_name'] . '</u>'; echo '<div id="table_' . $i . '" class="pdflayout_table"><u>' . $temp_sh_page['table_name'] . '</u>';
if (isset($with_field_names)) { if (isset($with_field_names)) {
while ($row = PMA_DBI_fetch_assoc($fields_rs)) { while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
@@ -685,13 +518,14 @@ class PMA_User_Schema
* @access private * @access private
*/ */
private function _deleteTableRows() private function _deleteTableRows($delrow,$cfgRelation,$db,$chpage)
{ {
foreach ($delrow as $current_row) { foreach ($delrow as $current_row) {
$del_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' ' . "\n" $del_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' ' . "\n"
. ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'' . "\n" . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'' . "\n"
. ' AND table_name = \'' . PMA_sqlAddslashes($current_row) . '\'' . "\n" . ' AND table_name = \'' . PMA_sqlAddslashes($current_row) . '\'' . "\n"
. ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\''; . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\'';
echo $del_query;
PMA_query_as_controluser($del_query, FALSE, $query_default_option); PMA_query_as_controluser($del_query, FALSE, $query_default_option);
} }
} }
@@ -704,7 +538,7 @@ class PMA_User_Schema
*/ */
private function _processExportSchema() private function _processExportSchema()
{ {
/** /**
* Settings for relation stuff * Settings for relation stuff
*/ */
@@ -713,11 +547,211 @@ class PMA_User_Schema
/** /**
* default is PDF * default is PDF
*/ */
global $db,$export_type;
$export_type = isset($export_type) ? $export_type : 'pdf'; $export_type = isset($export_type) ? $export_type : 'pdf';
PMA_DBI_select_db($db); PMA_DBI_select_db($db);
include("./libraries/schema/".ucfirst($export_type)."_Relation_Schema.class.php"); include("./libraries/schema/".ucfirst($export_type)."_Relation_Schema.class.php");
$obj_schema = eval("new PMA_".ucfirst($export_type)."_Relation_Schema();"); $obj_schema = eval("new PMA_".ucfirst($export_type)."_Relation_Schema();");
} }
public function deleteCoordinates($db, $cfgRelation, $choosePage, $query_default_option)
{
$query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
. ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
. ' AND pdf_page_number = \'' . PMA_sqlAddslashes($choosePage) . '\'';
PMA_query_as_controluser($query, FALSE, $query_default_option);
}
public function deletePages($db, $cfgRelation, $choosePage, $query_default_option)
{
$query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
. ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
. ' AND page_nr = \'' . PMA_sqlAddslashes($choosePage) . '\'';
PMA_query_as_controluser($query, FALSE, $query_default_option);
}
public function processRelations($db, $pageNumber, $cfgRelation, $query_default_option)
{
/*
* A u t o m a t i c l a y o u t
*
* There are 2 kinds of relations in PMA
* 1) Internal Relations 2) Foreign Key Relations
*/
if (isset($this->autoLayoutInternal) || isset($this->autoLayoutForeign)) {
$all_tables = array();
}
if (isset($this->autoLayoutForeign)) {
/*
* get the tables list
* who support FOREIGN KEY, it's not
* important that we group together InnoDB tables
* and PBXT tables, as this logic is just to put
* the tables on the layout, not to determine relations
*/
$tables = PMA_DBI_get_tables_full($db);
$foreignkey_tables = array();
foreach($tables as $table_name => $table_properties) {
if (PMA_foreignkey_supported($table_properties['ENGINE'])) {
$foreignkey_tables[] = $table_name;
}
}
$all_tables = $foreignkey_tables;
/*
* could be improved by finding the tables which have the
* most references keys and placing them at the beginning
* of the array (so that they are all center of schema)
*/
unset($tables, $foreignkey_tables);
}
if (isset($this->autoLayoutInternal)) {
/*
* get the tables list who support Internal Relations;
* This type of relations will be created when
* you setup the PMA tables correctly
*/
$master_tables = 'SELECT COUNT(master_table), master_table'
. ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
. ' WHERE master_db = \'' . $db . '\''
. ' GROUP BY master_table'
. ' ORDER BY ' . PMA_backquote('COUNT(master_table)') . ' DESC ';
$master_tables_rs = PMA_query_as_controluser($master_tables, FALSE, $query_default_option);
if ($master_tables_rs && PMA_DBI_num_rows($master_tables_rs) > 0) {
/* first put all the master tables at beginning
* of the list, so they are near the center of
* the schema
*/
while (list(, $master_table) = PMA_DBI_fetch_row($master_tables_rs)) {
$all_tables[] = $master_table;
}
/* Now for each master, add its foreigns into an array
* of foreign tables, if not already there
* (a foreign might be foreign for more than
* one table, and might be a master itself)
*/
$foreign_tables = array();
foreach ($all_tables as $master_table) {
$foreigners = PMA_getForeigners($db, $master_table);
foreach ($foreigners as $foreigner) {
if (!in_array($foreigner['foreign_table'], $foreign_tables)) {
$foreign_tables[] = $foreigner['foreign_table'];
}
}
}
/*
* Now merge the master and foreign arrays/tables
*/
foreach ($foreign_tables as $foreign_table) {
if (!in_array($foreign_table, $all_tables)) {
$all_tables[] = $foreign_table;
}
}
}
}
if (isset($this->autoLayoutInternal) || isset($this->auto_layout_foreign)) {
$this->addRelationCoordinates($all_tables,$pageNumber,$db, $cfgRelation,$query_default_option);
}
$this->choosenPage = $pageNumber;
}
public function addRelationCoordinates($all_tables,$pageNumber,$db, $cfgRelation,$query_default_option)
{
/*
* Now generate the coordinates for the schema
* in a clockwise spiral and add to co-ordinates table
*/
$pos_x = 300;
$pos_y = 300;
$delta = 110;
$delta_mult = 1.10;
$direction = "right";
foreach ($all_tables as $current_table) {
/*
* save current table's coordinates
*/
$insert_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
. '(db_name, table_name, pdf_page_number, x, y) '
. 'VALUES (\'' . PMA_sqlAddslashes($db) . '\', \'' . PMA_sqlAddslashes($current_table) . '\',' . $pageNumber . ',' . $pos_x . ',' . $pos_y . ')';
PMA_query_as_controluser($insert_query, FALSE, $query_default_option);
/*
* compute for the next table
*/
switch ($direction) {
case 'right':
$pos_x += $delta;
$direction = "down";
$delta *= $delta_mult;
break;
case 'down':
$pos_y += $delta;
$direction = "left";
$delta *= $delta_mult;
break;
case 'left':
$pos_x -= $delta;
$direction = "up";
$delta *= $delta_mult;
break;
case 'up':
$pos_y -= $delta;
$direction = "right";
$delta *= $delta_mult;
break;
}
}
}
public function editCoordinates($db, $cfgRelation)
{
for ($i = 0; $i < $this->c_table_rows; $i++) {
$arrvalue = 'c_table_' . $i;
global $$arrvalue;
$arrvalue = $$arrvalue;
if (!isset($arrvalue['x']) || $arrvalue['x'] == '') {
$arrvalue['x'] = 0;
}
if (!isset($arrvalue['y']) || $arrvalue['y'] == '') {
$arrvalue['y'] = 0;
}
if (isset($arrvalue['name']) && $arrvalue['name'] != '--') {
$test_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
. ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
. ' AND table_name = \'' . PMA_sqlAddslashes($arrvalue['name']) . '\''
. ' AND pdf_page_number = \'' . PMA_sqlAddslashes($this->choosenPage) . '\'';
$test_rs = PMA_query_as_controluser($test_query, FALSE, $query_default_option);
//echo $test_query;
if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
if (isset($arrvalue['delete']) && $arrvalue['delete'] == 'y') {
$ch_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
. ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
. ' AND table_name = \'' . PMA_sqlAddslashes($arrvalue['name']) . '\''
. ' AND pdf_page_number = \'' . PMA_sqlAddslashes($this->choosenPage) . '\'';
} else {
$ch_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
. 'SET x = ' . $arrvalue['x'] . ', y= ' . $arrvalue['y']
. ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
. ' AND table_name = \'' . PMA_sqlAddslashes($arrvalue['name']) . '\''
. ' AND pdf_page_number = \'' . PMA_sqlAddslashes($this->choosenPage) . '\'';
}
} else {
$ch_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
. '(db_name, table_name, pdf_page_number, x, y) '
. 'VALUES (\'' . PMA_sqlAddslashes($db) . '\', \'' . PMA_sqlAddslashes($arrvalue['name']) . '\', \'' . PMA_sqlAddslashes($this->choosenPage) . '\',' . $arrvalue['x'] . ',' . $arrvalue['y'] . ')';
}
//echo $ch_query;
PMA_query_as_controluser($ch_query, FALSE, $query_default_option);
} // end if
} // end for
}
} }
?> ?>