action = $value; } /** * 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 * * $this->action tells what the Schema is supposed to do * create and select a page, generate schema etc * * @access public * @return void */ public function processUserChoice() { global $db, $cfgRelation; if (isset($this->action)) { switch ($this->action) { case 'selectpage': $this->chosenPage = $_REQUEST['chpage']; if ('1' == $_REQUEST['action_choose']) { $this->deleteCoordinates( $db, $cfgRelation, $this->chosenPage ); $this->deletePages( $db, $cfgRelation, $this->chosenPage ); $this->chosenPage = 0; } break; case 'createpage': $this->pageNumber = PMA_REL_createPage( $_POST['newpage'], $cfgRelation, $db ); $this->autoLayoutForeign = isset($_POST['auto_layout_foreign']) ? "1" : null; $this->autoLayoutInternal = isset($_POST['auto_layout_internal']) ? "1" : null; $this->processRelations( $db, $this->pageNumber, $cfgRelation ); break; case 'edcoord': $this->chosenPage = $_POST['chpage']; $this->c_table_rows = $_POST['c_table_rows']; $this->_editCoordinates($db, $cfgRelation); break; case 'delete_old_references': $this->_deleteTableRows( $_POST['delrow'], $cfgRelation, $db, $_POST['chpage'] ); break; case 'process_export': $this->_processExportSchema(); break; } // end switch } // end if (isset($do)) } /** * shows/displays the HTML FORM to create the page * * @param string $db name of the selected database * * @return void * @access public */ public function showCreatePageDialog($db) { $htmlString = '
' . '
' . '' . __('Create a page') . '' . PMA_URL_getHiddenInputs($db) . '' . '' . '' . '' . '' . '' . '' . __('Automatic layout based on') . '' . '' . '
' . '' . '
' . '
'; /* * Check to see whether INNODB and PBXT storage engines * are Available in MYSQL PACKAGE * If available, then provide AutoLayout for Foreign Keys in Schema View */ if (PMA_StorageEngine::isValid('InnoDB') || PMA_StorageEngine::isValid('PBXT') ) { $htmlString .= '' . '
'; } $htmlString .= '
' . '
' . '
' . '' . '
' . '
'; echo $htmlString; } /** * shows/displays the created page names in a drop down list * User can select any page number and edit it using dashboard etc * * @return void * @access public */ public function selectPage() { global $db,$table,$cfgRelation; $page_query = 'SELECT * FROM ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_Util::backquote($cfgRelation['pdf_pages']) . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\''; $page_rs = PMA_queryAsControlUser( $page_query, false, PMA_DatabaseInterface::QUERY_STORE ); if ($page_rs && $GLOBALS['dbi']->numRows($page_rs) > 0) { ?>
__('Edit'), '1' => __('Delete') ); echo PMA_Util::getRadioFields( 'action_choose', $choices, '0', false ); unset($choices); ?>

query( 'SHOW TABLES FROM ' . PMA_Util::backquote($db) . ';', null, PMA_DatabaseInterface::QUERY_STORE ); while ($val = @$GLOBALS['dbi']->fetchRow($alltab_rs)) { $selectboxall[] = $val[0]; } $tabExist = array(); /* * Now if we already have chosen a page number then we should * show the tables involved */ if (isset($this->chosenPage) && $this->chosenPage > 0) { echo "\n"; echo "

" . __('Select Tables') . "

"; $page_query = 'SELECT * FROM ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_Util::backquote($cfgRelation['table_coords']) . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\'' . ' AND pdf_page_number = \'' . PMA_Util::sqlAddSlashes($this->chosenPage) . '\''; $page_rs = PMA_queryAsControlUser($page_query, false); $array_sh_page = array(); while ($temp_sh_page = @$GLOBALS['dbi']->fetchAssoc($page_rs)) { $array_sh_page[] = $temp_sh_page; } /* * Display WYSIWYG parts */ if (! isset($_POST['with_field_names']) && ! isset($_POST['showwysiwyg']) ) { $with_field_names = true; } elseif (isset($_POST['with_field_names'])) { $with_field_names = true; } $this->_displayScratchboardTables($array_sh_page); echo '
'; echo PMA_URL_getHiddenInputs($db, $table); echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; if (isset($ctable)) { unset($ctable); } /* * Add one more empty row */ $array_sh_page[] = array( 'table_name' => '', 'x' => '0', 'y' => '0', ); $i = 0; $odd_row = true; foreach ($array_sh_page as $sh_page) { $_mtab = $sh_page['table_name']; if (! empty($_mtab)) { $tabExist[$_mtab] = false; } echo ''; $odd_row = ! $odd_row; echo ''; echo ''; echo ''; echo ''; echo ''; $i++; } echo '
' . __('Table') . '' . __('Delete') . 'XY
'; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
'; echo ''; echo ''; echo ''; echo '
'; echo ''; echo '
' . "\n\n"; } // end if if (isset($tabExist)) { $this->_deleteTables($db, $this->chosenPage, $tabExist); } } /** * show Export relational schema generation options * user can select export type of his own choice * and the attributes related to it * * @return void * @access public */ public function displaySchemaGenerationOptions() { global $cfg, $db, $test_rs, $chpage; $htmlString = '
' . '
' . '' . PMA_URL_getHiddenInputs($db); if (PMA_Util::showIcons('ActionLinksMode')) { $htmlString .= PMA_Util::getImage('b_views.png'); } $htmlString .= __('Display relational schema') . ':' . '' . '' . '
'; if (isset($test_rs)) { $htmlString .= '' . '
'; } else { $htmlString .= ''; } $htmlString .= '' . '' . '' . '
' . '' . '' . '
' . '' . '
' . '' . '
' . '' . '
' . '' . '
' . '' . '' . '
' . '' . '' . '
' . '
' . '' . '
' . '
'; echo $htmlString; } /** * Check if there are tables that need to be deleted in dashboard, * if there are, ask the user for allowance * * @param string $db name of database selected * @param integer $chpage selected page * @param array $tabExist array of booleans * * @return void * @access private */ private function _deleteTables($db, $chpage, $tabExist) { $_strtrans = ''; $_strname = ''; $shoot = false; if (empty($tabExist) || ! is_array($tabExist)) { return; } foreach ($tabExist as $key => $value) { if (! $value) { $_strtrans .= '' . "\n"; $_strname .= '
  • ' . htmlspecialchars($key) . '
  • ' . "\n"; $shoot = true; } } if (! $shoot) { return; } echo '
    ' . "\n" . PMA_URL_getHiddenInputs($db) . '' . "\n" . '' . "\n" . __( 'The current page has references to tables that no longer exist.' . ' Would you like to delete those references?' ) . '' . "\n" . $_strtrans . '' . "\n" . '
    '; } /** * Check if there are tables that need to be deleted in dashboard, * if there are, ask the user for allowance * * @param array $array_sh_page array of tables on page * * @return void * @access private */ private function _displayScratchboardTables($array_sh_page) { global $with_field_names, $db; echo '
    '; echo ''; echo ''; echo '
    '; echo ''; } /** * delete the table rows with table co-ordinates * * @param int $delrow delete selected table from list of tables * @param array $cfgRelation relation settings * @param string $db database name * @param integer $chpage selected page for adding relations etc * * @return void * @access private */ private function _deleteTableRows($delrow,$cfgRelation,$db,$chpage) { foreach ($delrow as $current_row) { $del_query = 'DELETE FROM ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_Util::backquote($cfgRelation['table_coords']) . ' ' . "\n" . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\'' . "\n" . ' AND table_name = \'' . PMA_Util::sqlAddSlashes($current_row) . '\'' . "\n" . ' AND pdf_page_number = \'' . PMA_Util::sqlAddSlashes($chpage) . '\''; PMA_queryAsControlUser($del_query, false); } } /** * get all the export options and verify * call and include the appropriate Schema Class depending on $export_type * * @return void * @access private */ private function _processExportSchema() { /** * Settings for relation stuff */ include_once './libraries/transformations.lib.php'; include_once './libraries/Index.class.php'; /** * default is PDF, otherwise validate it's only letters a-z */ global $db,$export_type; if (! isset($export_type) || ! preg_match('/^[a-zA-Z]+$/', $export_type)) { $export_type = 'pdf'; } $GLOBALS['dbi']->selectDb($db); $path = PMA_securePath(ucfirst($export_type)); $filename = 'libraries/schema/' . $path . '_Relation_Schema.class.php'; if (!file_exists($filename)) { PMA_Export_Relation_Schema::dieSchema( $_POST['chpage'], $export_type, __('File doesn\'t exist') ); } include $filename; $class_name = 'PMA_' . $path . '_Relation_Schema'; $obj_schema = new $class_name(); $obj_schema->showOutput(); } /** * delete X and Y coordinates * * @param string $db The database name * @param array $cfgRelation relation settings * @param integer $choosePage selected page for adding relations etc * * @return void * @access private */ public function deleteCoordinates($db, $cfgRelation, $choosePage) { $query = 'DELETE FROM ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_Util::backquote($cfgRelation['table_coords']) . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\'' . ' AND pdf_page_number = \'' . PMA_Util::sqlAddSlashes($choosePage) . '\''; PMA_queryAsControlUser($query, false); } /** * delete pages * * @param string $db The database name * @param array $cfgRelation relation settings * @param integer $choosePage selected page for adding relations etc * * @return void * @access private */ public function deletePages($db, $cfgRelation, $choosePage) { $query = 'DELETE FROM ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_Util::backquote($cfgRelation['pdf_pages']) . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\'' . ' AND page_nr = \'' . PMA_Util::sqlAddSlashes($choosePage) . '\''; PMA_queryAsControlUser($query, false); } /** * process internal and foreign key relations * * @param string $db The database name * @param integer $pageNumber document number/Id * @param array $cfgRelation relation settings * * @return void * @access private */ public function processRelations($db, $pageNumber, $cfgRelation) { /* * 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 = $GLOBALS['dbi']->getTablesFull($db); $foreignkey_tables = array(); foreach ($tables as $table_name => $table_properties) { if (PMA_Util::isForeignKeySupported($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_Util::backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_Util::backquote($cfgRelation['relation']) . ' WHERE master_db = \'' . PMA_Util::sqlAddSlashes($db) . '\'' . ' GROUP BY master_table' . ' ORDER BY COUNT(master_table) DESC'; $master_tables_rs = PMA_queryAsControlUser( $master_tables, false, PMA_DatabaseInterface::QUERY_STORE ); if ($master_tables_rs && $GLOBALS['dbi']->numRows($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) = $GLOBALS['dbi']->fetchRow($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->autoLayoutForeign)) { $this->addRelationCoordinates( $all_tables, $pageNumber, $db, $cfgRelation ); } $this->chosenPage = $pageNumber; } /** * Add X and Y coordinates for a table * * @param array $all_tables A list of all tables involved * @param integer $pageNumber document number/Id * @param string $db The database name * @param array $cfgRelation relation settings * * @return void * @access private */ public function addRelationCoordinates( $all_tables, $pageNumber, $db, $cfgRelation ) { /* * 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_Util::backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_Util::backquote($cfgRelation['table_coords']) . ' ' . '(db_name, table_name, pdf_page_number, x, y) ' . 'VALUES (\'' . PMA_Util::sqlAddSlashes($db) . '\', \'' . PMA_Util::sqlAddSlashes($current_table) . '\',' . $pageNumber . ',' . $pos_x . ',' . $pos_y . ')'; PMA_queryAsControlUser($insert_query, false); /* * 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; } } } /** * update X and Y coordinates for a table * * @param string $db The database name * @param array $cfgRelation relation settings * * @return void * @access private */ private function _editCoordinates($db, $cfgRelation) { for ($i = 0; $i < $this->c_table_rows; $i++) { $arrvalue = $_POST['c_table_' . $i]; 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_Util::backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_Util::backquote($cfgRelation['table_coords']) . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\'' . ' AND table_name = \'' . PMA_Util::sqlAddSlashes($arrvalue['name']) . '\'' . ' AND pdf_page_number = \'' . PMA_Util::sqlAddSlashes($this->chosenPage) . '\''; $test_rs = PMA_queryAsControlUser( $test_query, false, PMA_DatabaseInterface::QUERY_STORE ); //echo $test_query; if ($test_rs && $GLOBALS['dbi']->numRows($test_rs) > 0) { if (isset($arrvalue['delete']) && $arrvalue['delete'] == 'y') { $ch_query = 'DELETE FROM ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_Util::backquote($cfgRelation['table_coords']) . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\'' . ' AND table_name = \'' . PMA_Util::sqlAddSlashes($arrvalue['name']) . '\'' . ' AND pdf_page_number = \'' . PMA_Util::sqlAddSlashes($this->chosenPage) . '\''; } else { $ch_query = 'UPDATE ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_Util::backquote($cfgRelation['table_coords']) . ' ' . 'SET x = ' . $arrvalue['x'] . ', y= ' . $arrvalue['y'] . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\'' . ' AND table_name = \'' . PMA_Util::sqlAddSlashes($arrvalue['name']) . '\'' . ' AND pdf_page_number = \'' . PMA_Util::sqlAddSlashes($this->chosenPage) . '\''; } } else { $ch_query = 'INSERT INTO ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_Util::backquote($cfgRelation['table_coords']) . ' ' . '(db_name, table_name, pdf_page_number, x, y) ' . 'VALUES (\'' . PMA_Util::sqlAddSlashes($db) . '\', \'' . PMA_Util::sqlAddSlashes($arrvalue['name']) . '\', \'' . PMA_Util::sqlAddSlashes($this->chosenPage) . '\',' . $arrvalue['x'] . ',' . $arrvalue['y'] . ')'; } //echo $ch_query; PMA_queryAsControlUser($ch_query, false); } // end if } // end for } } ?>