From 07cee82af681ca5602c704e438eff39c83825db3 Mon Sep 17 00:00:00 2001 From: Adnan Date: Fri, 9 Jul 2010 14:39:03 +0500 Subject: [PATCH] User Schema Class: big switch into several methods + reduce global variables --- export_relation_schema.php | 20 +- libraries/schema/User_Schema.class.php | 768 +++++++++++++------------ 2 files changed, 411 insertions(+), 377 deletions(-) diff --git a/export_relation_schema.php b/export_relation_schema.php index 1c135e5b7..667845801 100644 --- a/export_relation_schema.php +++ b/export_relation_schema.php @@ -71,7 +71,7 @@ if ($cfgRelation['pdfwork']) { * User object created for presenting the HTML options * so, user can interact with it and perform export of relations schema */ - + require_once './libraries/schema/User_Schema.class.php'; $user_schema = new PMA_User_Schema(); @@ -98,7 +98,7 @@ if ($cfgRelation['pdfwork']) { * Create a new page where relations will be drawn */ - $user_schema->createPage(); + $user_schema->createPage($db); /** * After selection of page or creating a page @@ -110,8 +110,8 @@ if ($cfgRelation['pdfwork']) { if (isset($do) && ($do == 'edcoord' - || ($do == 'selectpage' && isset($chpage) && $chpage != 0) - || ($do == 'createpage' && isset($chpage) && $chpage != 0))) { + || ($do == 'selectpage' && isset($user_schema->choosenPage) && $user_schema->choosenPage != 0) + || ($do == 'createpage' && isset($user_schema->choosenPage) && $user_schema->choosenPage != 0))) { /** * show Export schema generation options @@ -119,12 +119,12 @@ if ($cfgRelation['pdfwork']) { $user_schema->displaySchemaGenerationOptions(); if ((isset($showwysiwyg) && $showwysiwyg == '1')) { - ?> - + ?> + * @copyright * @license */ class PMA_User_Schema -{ +{ + + public $choosenPage; + public $autoLayoutForeign; + public $autoLayoutInternal; + public $pageNumber; + public $c_table_rows; + /** * This function will process the user defined pages * and tables which will be exported as Relational schema * you can set the table positions on the paper via scratchboard * for table positions, put the x,y co-ordinates * - * @param string $do It tells what the Schema is supposed to do - * create and select a page, generate schema etc + * @param string $action It tells what the Schema is supposed to do + * create and select a page, generate schema etc * @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)) { - switch ($do) { - case 'selectpage': - if ($action_choose=="1") { - $ch_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($chpage) . '\''; - PMA_query_as_controluser($ch_query, FALSE, $query_default_option); - - $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; - } + if (isset($action)) { + switch ($action) { + case 'selectpage': + $this->choosenPage = $_REQUEST['chpage']; + if ($action_choose=="1") { + $this->deleteCoordinates($db, $cfgRelation, $this->choosenPage, $query_default_option); + $this->deletePages($db, $cfgRelation, $this->choosenPage, $query_default_option); + $this->choosenPage = 0; } - $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); - } + break; + case 'createpage': + $this->pageNumber = PMA_REL_create_page($_POST['newpage'], $cfgRelation, $db, $query_default_option); + $this->autoLayoutForeign = $_POST['auto_layout_foreign']; + $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)) { - /* - * 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; - } + } // end switch + } // end if (isset($do)) - /* 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 * * @access public */ - public function createPage() - { - global $db,$table; + public function createPage($db) + { ?>
- + @@ -278,7 +112,7 @@ class PMA_User_Schema
+ ?>
@@ -287,23 +121,23 @@ class PMA_User_Schema
0) { - ?> + if ($page_rs && PMA_DBI_num_rows($page_rs) > 0) { + ?>
@@ -311,13 +145,13 @@ class PMA_User_Schema - + choosenPage) && $this->choosenPage == $curr_page['page_nr']) { echo ' selected="selected"'; } echo '>' . $curr_page['page_nr'] . ': ' . htmlspecialchars($curr_page['page_descr']) . ''; @@ -329,7 +163,7 @@ class PMA_User_Schema $choices = array( '0' => __('Edit'), '1' => __('Delete') - ); + ); PMA_display_html_radio('action_choose', $choices, '0', false); unset($choices); ?> @@ -341,17 +175,17 @@ class PMA_User_Schema 0) { + if (isset($this->choosenPage) && $this->choosenPage > 0) { echo "\n"; ?> -

+

choosenPage) . '\''; $page_rs = PMA_query_as_controluser($page_query, FALSE, $query_default_option); $array_sh_page = array(); $draginit = ''; @@ -382,57 +216,86 @@ class PMA_User_Schema while ($temp_sh_page = @PMA_DBI_fetch_assoc($page_rs)) { $array_sh_page[] = $temp_sh_page; } + /* + * Display WYSIWYG-PDF parts? + */ - /* - * Display WYSIWYG-PDF parts? - */ + if ($cfg['WYSIWYG-PDF']) { + 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']) { - if (!isset($_POST['with_field_names']) && !isset($_POST['showwysiwyg'])) { - $with_field_names = TRUE; - } - $this->_displayScratchboardTables($array_sh_page,$draginit,$reset_draginit); - } - ?> + + + + + + + + + + + + - - - -
XY
- - - - - - - $sh_page) { - $_mtab = $sh_page['table_name']; - $tabExist[$_mtab] = FALSE; + $i = 0; + $odd_row = true; + foreach ($array_sh_page as $dummy_sh_page => $sh_page) { + $_mtab = $sh_page['table_name']; + $tabExist[$_mtab] = FALSE; + echo "\n" . ' '; + $odd_row != $odd_row; + echo "\n" . ' '; + echo "\n" . ' '; + echo "\n" . ' '; + echo "\n" . ' '; + echo "\n" . ' '; + $i++; + } + /* + * Add one more empty row + */ echo "\n" . ' '; $odd_row != $odd_row; + echo '">'; echo "\n" . ' '; @@ -440,60 +303,30 @@ class PMA_User_Schema . "\n" . ' ' . __('Delete'); echo "\n" . ' '; echo "\n" . ' '; echo "\n" . ' '; echo "\n" . ' '; - $i++; - } - /* - * Add one more empty row - */ - echo "\n" . ' '; - echo "\n" . ' '; - echo "\n" . ' '; - echo "\n" . ' '; - echo "\n" . ' '; - echo "\n" . ' '; - echo "\n" . '
XY
' + . "\n" . ' ' + . "\n" . ' ' + . "\n" . ' ' . __('Delete'); + echo "\n" . ' ' + . "\n" . ' '; + echo "\n" . ' ' + . "\n" . ' '; + echo "\n" . '
' . "\n" . ' ' . "\n" . ' ' - . "\n" . ' '; + . "\n" . ' '; echo "\n" . ' ' - . "\n" . ' '; + . "\n" . ' '; echo "\n" . '
' - . "\n" . ' ' - . "\n" . ' ' - . "\n" . ' ' . __('Delete'); - echo "\n" . ' ' - . "\n" . ' '; - echo "\n" . ' ' - . "\n" . ' '; - echo "\n" . '
' . "\n"; + echo "\n" . ' ' . "\n"; - echo "\n" . ' '; - echo ($cfg['WYSIWYG-PDF'] ? "\n" . ' ' : ''); - echo "\n" . ' ' . __('Column names') . '
'; - echo "\n" . ' '; - echo "\n" . '' . "\n\n"; - } // end if - - $this->_deleteTables($chpage); + echo "\n" . ' '; + echo ($cfg['WYSIWYG-PDF'] ? "\n" . ' ' : ''); + echo "\n" . ' ' . __('Column names') . '
'; + echo "\n" . ' '; + echo "\n" . '' . "\n\n"; + } // end if + + $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 * and the attributes related to it - */ - + */ + public function displaySchemaGenerationOptions() { global $cfg,$pmaThemeImage,$db,$test_rs,$chpage; @@ -501,15 +334,15 @@ class PMA_User_Schema
- '; - } - echo __('Display Relational Schema'); - ?>: - + '; + } + echo __('Display Relational Schema'); + ?>: +
- + @@ -543,8 +376,8 @@ class PMA_User_Schema
- -
$value) { - if (!$value) { - $_strtrans .= '' . "\n"; - $_strname .= '
  • ' . htmlspecialchars($key) . '
  • ' . "\n"; - $shoot = TRUE; - } - } - if ($shoot) { - echo '
    ' . "\n" - . PMA_generate_common_hidden_inputs($db, $table) - . '' . "\n" - . '' . "\n" - . __('The current page has references to tables that no longer exist. Would you like to delete those references?') - . '
      ' . "\n" - . $_strname - . '
    ' . "\n" - . $_strtrans - . '' . "\n" - . '
    '; - } - } - } // end function + /** + * Check if there are tables that need to be deleted in dashboard, + * if there are, ask the user for allowance + * @param string $chpage selected page + * @access private + */ + private function _deleteTables($db, $chpage, $tabExist) + { + $_strtrans = ''; + $_strname = ''; + $shoot = FALSE; + if (!empty($tabExist) && is_array($tabExist)) { + foreach ($tabExist as $key => $value) { + if (!$value) { + $_strtrans .= '' . "\n"; + $_strname .= '
  • ' . htmlspecialchars($key) . '
  • ' . "\n"; + $shoot = TRUE; + } + } + if ($shoot) { + echo '
    ' . "\n" + . PMA_generate_common_hidden_inputs($db, $table) + . '' . "\n" + . '' . "\n" + . __('The current page has references to tables that no longer exist. Would you like to delete those references?') + . '
      ' . "\n" + . $_strname + . '
    ' . "\n" + . $_strtrans + . '' . "\n" + . '
    '; + } + } + + } /** * Check if there are tables that need to be deleted in dashboard, @@ -630,6 +462,7 @@ class PMA_User_Schema