Merge pull request #650 from scnakandala/refac_tbl_relation.php
Refactoring tbl_relation.php
This commit is contained in:
commit
cf8384ec73
@ -19,7 +19,9 @@ require_once 'libraries/Index.class.php';
|
||||
*/
|
||||
function PMA_getHtmlForDisplayIndexes()
|
||||
{
|
||||
$html_output = PMA_Util::getDivForSliderEffect(
|
||||
$html_output = '<div id="index_div" class="ajax" >';
|
||||
|
||||
$html_output .= PMA_Util::getDivForSliderEffect(
|
||||
'indexes', __('Indexes')
|
||||
);
|
||||
$html_output .= PMA_Index::getView($GLOBALS['table'], $GLOBALS['db']);
|
||||
|
||||
@ -155,4 +155,556 @@ function PMA_generateRelationalDropdown(
|
||||
$html_output .= '</select>';
|
||||
return $html_output;
|
||||
}
|
||||
?>
|
||||
|
||||
/**
|
||||
* Function to get html for the common form
|
||||
*
|
||||
* @param string $db current database
|
||||
* @param string $table current table
|
||||
* @param array $columns columns
|
||||
* @param array $cfgRelation configuration relation
|
||||
* @param string $tbl_storage_engine table storage engine
|
||||
* @param array $existrel db, table, column
|
||||
* @param array $existrel_foreign db, table, column
|
||||
* @param array $options_array options array
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function PMA_getHtmlForCommonForm($db, $table, $columns, $cfgRelation,
|
||||
$tbl_storage_engine, $existrel, $existrel_foreign, $options_array
|
||||
) {
|
||||
$html_output = PMA_getHtmlForCommonFormHeader($db, $table);
|
||||
|
||||
if (count($columns) > 0) {
|
||||
$html_output .= PMA_getHtmlForCommonFormRows(
|
||||
$columns, $cfgRelation, $tbl_storage_engine,
|
||||
$existrel, $existrel_foreign, $options_array, $db, $table
|
||||
);
|
||||
} // end if (we have columns in this table)
|
||||
|
||||
$html_output .= PMA_getHtmlForCommonFormFooter();
|
||||
|
||||
return $html_output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to get html for the common form rows
|
||||
*
|
||||
* @param array $columns columns
|
||||
* @param array $cfgRelation configuration relation
|
||||
* @param string $tbl_storage_engine table storage engine
|
||||
* @param array $existrel db, table, column
|
||||
* @param array $existrel_foreign db, table, column
|
||||
* @param array $options_array options array
|
||||
* @param string $db current database
|
||||
* @param string $table current table
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function PMA_getHtmlForCommonFormRows($columns, $cfgRelation, $tbl_storage_engine,
|
||||
$existrel, $existrel_foreign, $options_array, $db, $table
|
||||
) {
|
||||
foreach ($columns as $row) {
|
||||
$save_row[] = $row;
|
||||
}
|
||||
|
||||
$saved_row_cnt = count($save_row);
|
||||
|
||||
$html_output = '<fieldset>'
|
||||
. '<legend>' . __('Relations'). '</legend>'
|
||||
. '<table id="relationalTable">';
|
||||
|
||||
$html_output .= PMA_getHtmlForCommonFormTableHeaders(
|
||||
$cfgRelation, $tbl_storage_engine
|
||||
);
|
||||
|
||||
$odd_row = true;
|
||||
for ($i = 0; $i < $saved_row_cnt; $i++) {
|
||||
$html_output .= PMA_getHtmlForRow(
|
||||
$save_row, $i, $odd_row, $cfgRelation, $existrel, $db,
|
||||
$tbl_storage_engine, $existrel_foreign, $options_array
|
||||
);
|
||||
$odd_row = ! $odd_row;
|
||||
} // end for
|
||||
|
||||
$html_output .= '</table>' . "\n"
|
||||
. '</fieldset>' . "\n";
|
||||
|
||||
if ($cfgRelation['displaywork']) {
|
||||
$html_output .= PMA_getHtmlForDisplayFieldInfos($db, $table, $save_row);
|
||||
}
|
||||
|
||||
return $html_output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to get html for an entire row in common form
|
||||
*
|
||||
* @param array $save_row save row
|
||||
* @param int $i counter
|
||||
* @param bool $odd_row whether odd row or not
|
||||
* @param array $cfgRelation configuration relation
|
||||
* @param array $existrel db, table, column
|
||||
* @param string $db current db
|
||||
* @param string $tbl_storage_engine table storage engine
|
||||
* @param array $existrel_foreign db, table, column
|
||||
* @param array $options_array options array
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function PMA_getHtmlForRow($save_row, $i, $odd_row, $cfgRelation, $existrel, $db,
|
||||
$tbl_storage_engine, $existrel_foreign, $options_array
|
||||
) {
|
||||
$myfield = $save_row[$i]['Field'];
|
||||
// Use an md5 as array index to avoid having special characters
|
||||
// in the name atttibure (see bug #1746964 )
|
||||
$myfield_md5 = md5($myfield);
|
||||
$myfield_html = htmlspecialchars($myfield);
|
||||
|
||||
$html_output = '<tr class="' . ($odd_row ? 'odd' : 'even') . '">'
|
||||
. '<td class="center">'
|
||||
. '<strong>' . $myfield_html . '</strong>'
|
||||
. '<input type="hidden" name="fields_name[' . $myfield_md5 . ']"'
|
||||
. ' value="' . $myfield_html . '"/>'
|
||||
. '</td>';
|
||||
|
||||
if ($cfgRelation['relwork']) {
|
||||
$html_output .= '<td>';
|
||||
|
||||
$foreign_db = false;
|
||||
$foreign_table = false;
|
||||
$foreign_column = false;
|
||||
|
||||
// database dropdown
|
||||
if (isset($existrel[$myfield])) {
|
||||
$foreign_db = $existrel[$myfield]['foreign_db'];
|
||||
} else {
|
||||
$foreign_db = $db;
|
||||
}
|
||||
$html_output .= PMA_generateRelationalDropdown(
|
||||
'destination_db[' . $myfield_md5 . ']',
|
||||
$GLOBALS['pma']->databases,
|
||||
$foreign_db,
|
||||
__('Database')
|
||||
);
|
||||
// end of database dropdown
|
||||
|
||||
// table dropdown
|
||||
$tables = array();
|
||||
if ($foreign_db) {
|
||||
if (isset($existrel[$myfield])) {
|
||||
$foreign_table = $existrel[$myfield]['foreign_table'];
|
||||
}
|
||||
$tables_rs = $GLOBALS['dbi']->query(
|
||||
'SHOW TABLES FROM ' . PMA_Util::backquote($foreign_db),
|
||||
null,
|
||||
PMA_DatabaseInterface::QUERY_STORE
|
||||
);
|
||||
while ($row = $GLOBALS['dbi']->fetchRow($tables_rs)) {
|
||||
$tables[] = $row[0];
|
||||
}
|
||||
}
|
||||
$html_output .= PMA_generateRelationalDropdown(
|
||||
'destination_table[' . $myfield_md5 . ']',
|
||||
$tables,
|
||||
$foreign_table,
|
||||
__('Table')
|
||||
);
|
||||
// end of table dropdown
|
||||
|
||||
// column dropdown
|
||||
$columns = array();
|
||||
if ($foreign_db && $foreign_table) {
|
||||
if (isset($existrel[$myfield])) {
|
||||
$foreign_column = $existrel[$myfield]['foreign_field'];
|
||||
}
|
||||
$table_obj = new PMA_Table($foreign_table, $foreign_db);
|
||||
$columns = $table_obj->getUniqueColumns(false, false);
|
||||
}
|
||||
$html_output .= PMA_generateRelationalDropdown(
|
||||
'destination_column[' . $myfield_md5 . ']',
|
||||
$columns,
|
||||
$foreign_column,
|
||||
__('Column')
|
||||
);
|
||||
// end of column dropdown
|
||||
|
||||
$html_output .= '</td>';
|
||||
} // end if (internal relations)
|
||||
|
||||
if (PMA_Util::isForeignKeySupported($tbl_storage_engine)) {
|
||||
$html_output .= PMA_getHtmlForForeignKey(
|
||||
$save_row, $i, $existrel_foreign, $myfield, $db,
|
||||
$myfield_md5, $tbl_storage_engine, $options_array
|
||||
);
|
||||
} // end if (InnoDB)
|
||||
$html_output .= '</tr>';
|
||||
|
||||
return $html_output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to get html for the common form header
|
||||
*
|
||||
* @param string $db current database
|
||||
* @param string $table current table
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function PMA_getHtmlForCommonFormHeader($db, $table)
|
||||
{
|
||||
return '<form method="post" action="tbl_relation.php">' . "\n"
|
||||
. PMA_URL_getHiddenInputs($db, $table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to get html for the common form footer
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function PMA_getHtmlForCommonFormFooter()
|
||||
{
|
||||
return '<fieldset class="tblFooters">'
|
||||
. '<input type="submit" value="' . __('Save') . '" />'
|
||||
. '</fieldset>'
|
||||
. '</form>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to get html for display field infos
|
||||
*
|
||||
* @param string $db current database
|
||||
* @param string $table current table
|
||||
* @param array $save_row save row
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function PMA_getHtmlForDisplayFieldInfos($db, $table, $save_row)
|
||||
{
|
||||
$disp = PMA_getDisplayField($db, $table);
|
||||
$html_output = '<fieldset>'
|
||||
. '<label>' . __('Choose column to display:') . '</label>'
|
||||
. '<select name="display_field">'
|
||||
. '<option value="">---</option>';
|
||||
|
||||
foreach ($save_row as $row) {
|
||||
$html_output .= '<option value="'
|
||||
. htmlspecialchars($row['Field']) . '"';
|
||||
if (isset($disp) && $row['Field'] == $disp) {
|
||||
$html_output .= ' selected="selected"';
|
||||
}
|
||||
$html_output .= '>' . htmlspecialchars($row['Field'])
|
||||
. '</option>'. "\n";
|
||||
} // end while
|
||||
|
||||
$html_output .= '</select>'
|
||||
. '</fieldset>';
|
||||
|
||||
return $html_output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to get html for the common form title headers
|
||||
*
|
||||
* @param array $cfgRelation configuration relation
|
||||
* @param string $tbl_storage_engine table storage engine
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function PMA_getHtmlForCommonFormTableHeaders($cfgRelation, $tbl_storage_engine)
|
||||
{
|
||||
$html_output = '<tr><th>' . __('Column') . '</th>';
|
||||
|
||||
if ($cfgRelation['relwork']) {
|
||||
$html_output .= '<th>' . __('Internal relation');
|
||||
if (PMA_Util::isForeignKeySupported($tbl_storage_engine)) {
|
||||
$html_output .= PMA_Util::showHint(
|
||||
__(
|
||||
'An internal relation is not necessary when a corresponding'
|
||||
. ' FOREIGN KEY relation exists.'
|
||||
)
|
||||
);
|
||||
}
|
||||
$html_output .= '</th>';
|
||||
}
|
||||
|
||||
if (PMA_Util::isForeignKeySupported($tbl_storage_engine)) {
|
||||
// this does not have to be translated, it's part of the MySQL syntax
|
||||
$html_output .= '<th colspan="2">' . __('Foreign key constraint')
|
||||
. ' (' . $tbl_storage_engine . ')';
|
||||
$html_output .= '</th>';
|
||||
}
|
||||
$html_output .= '</tr>';
|
||||
|
||||
return $html_output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to get html for the foreign key
|
||||
*
|
||||
* @param array $save_row save row
|
||||
* @param int $i counter
|
||||
* @param array $existrel_foreign db, table, columns
|
||||
* @param string $myfield my field
|
||||
* @param string $db current database
|
||||
* @param string $myfield_md5 my field md5
|
||||
* @param string $tbl_storage_engine table storage engine
|
||||
* @param array $options_array options array
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function PMA_getHtmlForForeignKey($save_row, $i, $existrel_foreign, $myfield, $db,
|
||||
$myfield_md5, $tbl_storage_engine, $options_array
|
||||
) {
|
||||
$html_output = '<td>';
|
||||
if (! empty($save_row[$i]['Key'])) {
|
||||
|
||||
$foreign_db = false;
|
||||
$foreign_table = false;
|
||||
$foreign_column = false;
|
||||
|
||||
// foreign database dropdown
|
||||
if (isset($existrel_foreign[$myfield])) {
|
||||
$foreign_db = $existrel_foreign[$myfield]['foreign_db'];
|
||||
} else {
|
||||
$foreign_db = $db;
|
||||
}
|
||||
$html_output .= '<span class="formelement clearfloat">';
|
||||
$html_output .= PMA_generateRelationalDropdown(
|
||||
'destination_foreign_db[' . $myfield_md5 . ']',
|
||||
$GLOBALS['pma']->databases,
|
||||
$foreign_db,
|
||||
__('Database')
|
||||
);
|
||||
// end of foreign database dropdown
|
||||
|
||||
// foreign table dropdown
|
||||
$tables = array();
|
||||
if ($foreign_db) {
|
||||
if (isset($existrel_foreign[$myfield])) {
|
||||
$foreign_table = $existrel_foreign[$myfield]['foreign_table'];
|
||||
}
|
||||
$tables_rs = $GLOBALS['dbi']->query(
|
||||
'SHOW TABLE STATUS FROM ' . PMA_Util::backquote($foreign_db),
|
||||
null,
|
||||
PMA_DatabaseInterface::QUERY_STORE
|
||||
);
|
||||
while ($row = $GLOBALS['dbi']->fetchRow($tables_rs)) {
|
||||
if (isset($row[1])
|
||||
&& strtoupper($row[1]) == $tbl_storage_engine
|
||||
) {
|
||||
$tables[] = $row[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
$html_output .= PMA_generateRelationalDropdown(
|
||||
'destination_foreign_table[' . $myfield_md5 . ']',
|
||||
$tables,
|
||||
$foreign_table,
|
||||
__('Table')
|
||||
);
|
||||
// end of foreign table dropdown
|
||||
|
||||
// foreign column dropdown
|
||||
$columns = array();
|
||||
if ($foreign_db && $foreign_table) {
|
||||
if (isset($existrel_foreign[$myfield])) {
|
||||
$foreign_column = $existrel_foreign[$myfield]['foreign_field'];
|
||||
}
|
||||
$table_obj = new PMA_Table($foreign_table, $foreign_db);
|
||||
$columns = $table_obj->getUniqueColumns(false, false);
|
||||
}
|
||||
$html_output .= PMA_generateRelationalDropdown(
|
||||
'destination_foreign_column[' . $myfield_md5 . ']',
|
||||
$columns,
|
||||
$foreign_column,
|
||||
__('Column')
|
||||
);
|
||||
$html_output .= '</span>';
|
||||
// end of foreign column dropdown
|
||||
|
||||
// For constraint name
|
||||
$html_output .= '<span class="formelement clearfloat">';
|
||||
$constraint_name = isset($existrel_foreign[$myfield]['constraint'])
|
||||
? $existrel_foreign[$myfield]['constraint'] : '';
|
||||
$html_output .= __('Constraint name');
|
||||
$html_output .= '<input type="text" name="constraint_name['
|
||||
. $myfield_md5 . ']"'
|
||||
. ' value="' . $constraint_name . '"/>';
|
||||
$html_output .= '</span>' . "\n";
|
||||
|
||||
$html_output .= '<span class="formelement clearfloat">';
|
||||
// For ON DELETE and ON UPDATE, the default action
|
||||
// is RESTRICT as per MySQL doc; however, a SHOW CREATE TABLE
|
||||
// won't display the clause if it's set as RESTRICT.
|
||||
$on_delete = isset($existrel_foreign[$myfield]['on_delete'])
|
||||
? $existrel_foreign[$myfield]['on_delete'] : 'RESTRICT';
|
||||
$html_output .= PMA_generateDropdown(
|
||||
'ON DELETE',
|
||||
'on_delete[' . $myfield_md5 . ']',
|
||||
$options_array,
|
||||
$on_delete
|
||||
);
|
||||
$html_output .= '</span>' . "\n";
|
||||
|
||||
$html_output .= '<span class="formelement clearfloat">' . "\n";
|
||||
$on_update = isset($existrel_foreign[$myfield]['on_update'])
|
||||
? $existrel_foreign[$myfield]['on_update'] : 'RESTRICT';
|
||||
$html_output .= PMA_generateDropdown(
|
||||
'ON UPDATE',
|
||||
'on_update[' . $myfield_md5 . ']',
|
||||
$options_array,
|
||||
$on_update
|
||||
);
|
||||
$html_output .= '</span>' . "\n";
|
||||
} else {
|
||||
$html_output .= __('No index defined! Create one below');
|
||||
} // end if (a key exists)
|
||||
$html_output .= '</td>';
|
||||
|
||||
return $html_output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to send html for table or column dropdown list
|
||||
*
|
||||
* @retrun void
|
||||
*/
|
||||
function PMA_sendHtmlForTableOrColumnDropdownList()
|
||||
{
|
||||
if (isset($_REQUEST['foreignTable'])) { // if both db and table are selected
|
||||
PMA_sendHtmlForColumnDorpdownList();
|
||||
} else { // if only the db is selected
|
||||
PMA_sendHtmlForTableDropdownList();
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to send html for column dropdown list
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function PMA_sendHtmlForColumnDorpdownList()
|
||||
{
|
||||
$response = PMA_Response::getInstance();
|
||||
|
||||
$foreignTable = $_REQUEST['foreignTable'];
|
||||
$table_obj = new PMA_Table($foreignTable, $_REQUEST['foreignDb']);
|
||||
$columns = array();
|
||||
foreach ($table_obj->getUniqueColumns(false, false) as $column) {
|
||||
$columns[] = htmlspecialchars($column);
|
||||
}
|
||||
$response->addJSON('columns', $columns);
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to send html for table dropdown list
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function PMA_sendHtmlForTableDropdownList()
|
||||
{
|
||||
$response = PMA_Response::getInstance();
|
||||
|
||||
$foreign = isset($_REQUEST['foreign']) && $_REQUEST['foreign'] === 'true';
|
||||
if ($foreign) {
|
||||
$query = 'SHOW TABLE STATUS FROM '
|
||||
. PMA_Util::backquote($_REQUEST['foreignDb']);
|
||||
$tbl_storage_engine = strtoupper(
|
||||
PMA_Table::sGetStatusInfo(
|
||||
$_REQUEST['db'],
|
||||
$_REQUEST['table'],
|
||||
'Engine'
|
||||
)
|
||||
);
|
||||
} else {
|
||||
$query = 'SHOW TABLES FROM '
|
||||
. PMA_Util::backquote( $_REQUEST['foreignDb']);
|
||||
}
|
||||
$tables_rs = $GLOBALS['dbi']->query(
|
||||
$query,
|
||||
null,
|
||||
PMA_DatabaseInterface::QUERY_STORE
|
||||
);
|
||||
$tables = array();
|
||||
while ($row = $GLOBALS['dbi']->fetchRow($tables_rs)) {
|
||||
if ($foreign) {
|
||||
if (isset($row[1])
|
||||
&& strtoupper($row[1]) == $tbl_storage_engine
|
||||
) {
|
||||
$tables[] = htmlspecialchars($row[0]);
|
||||
}
|
||||
} else {
|
||||
$tables[] = htmlspecialchars($row[0]);
|
||||
}
|
||||
}
|
||||
$response->addJSON('tables', $tables);
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to handle update for display field
|
||||
*
|
||||
* @param string $disp field name
|
||||
* @param string $display_field display field
|
||||
* @param string $db current database
|
||||
* @param string $table current table
|
||||
* @param array $cfgRelation configuration relation
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function PMA_handleUpdateForDisplayField($disp, $display_field, $db, $table,
|
||||
$cfgRelation
|
||||
) {
|
||||
$upd_query = PMA_getQueryForDisplayUpdate(
|
||||
$disp, $display_field, $db, $table, $cfgRelation
|
||||
);
|
||||
if ($upd_query) {
|
||||
PMA_queryAsControlUser($upd_query);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to get display query for handlingdisplay update
|
||||
*
|
||||
* @param string $disp field name
|
||||
* @param string $display_field display field
|
||||
* @param string $db current database
|
||||
* @param string $table current table
|
||||
* @param array $cfgRelation configuration relation
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function PMA_getQueryForDisplayUpdate($disp, $display_field, $db, $table,
|
||||
$cfgRelation
|
||||
) {
|
||||
$upd_query = false;
|
||||
if ($disp) {
|
||||
if ($display_field != '') {
|
||||
$upd_query = 'UPDATE '
|
||||
. PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
|
||||
. '.' . PMA_Util::backquote($cfgRelation['table_info'])
|
||||
. ' SET display_field = \''
|
||||
. PMA_Util::sqlAddSlashes($display_field) . '\''
|
||||
. ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\''
|
||||
. ' AND table_name = \'' . PMA_Util::sqlAddSlashes($table) . '\'';
|
||||
} else {
|
||||
$upd_query = 'DELETE FROM '
|
||||
. PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
|
||||
. '.' . PMA_Util::backquote($cfgRelation['table_info'])
|
||||
. ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\''
|
||||
. ' AND table_name = \'' . PMA_Util::sqlAddSlashes($table) . '\'';
|
||||
}
|
||||
} elseif ($display_field != '') {
|
||||
$upd_query = 'INSERT INTO '
|
||||
. PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
|
||||
. '.' . PMA_Util::backquote($cfgRelation['table_info'])
|
||||
. '(db_name, table_name, display_field) VALUES('
|
||||
. '\'' . PMA_Util::sqlAddSlashes($db) . '\','
|
||||
. '\'' . PMA_Util::sqlAddSlashes($table) . '\','
|
||||
. '\'' . PMA_Util::sqlAddSlashes($display_field) . '\')';
|
||||
}
|
||||
|
||||
return $upd_query;
|
||||
}
|
||||
?>
|
||||
349
tbl_relation.php
349
tbl_relation.php
@ -28,51 +28,7 @@ $response = PMA_Response::getInstance();
|
||||
if (isset($_REQUEST['getDropdownValues'])
|
||||
&& $_REQUEST['getDropdownValues'] === 'true'
|
||||
) {
|
||||
$foreignDb = $_REQUEST['foreignDb'];
|
||||
|
||||
if (isset($_REQUEST['foreignTable'])) { // if both db and table are selected
|
||||
$foreignTable = $_REQUEST['foreignTable'];
|
||||
$table_obj = new PMA_Table($foreignTable, $foreignDb);
|
||||
$columns = array();
|
||||
foreach ($table_obj->getUniqueColumns(false, false) as $column) {
|
||||
$columns[] = htmlspecialchars($column);
|
||||
}
|
||||
$response->addJSON('columns', $columns);
|
||||
|
||||
} else { // if only the db is selected
|
||||
$foreign = isset($_REQUEST['foreign']) && $_REQUEST['foreign'] === 'true';
|
||||
if ($foreign) {
|
||||
$query = 'SHOW TABLE STATUS FROM ' . PMA_Util::backquote($foreignDb);
|
||||
$tbl_storage_engine = strtoupper(
|
||||
PMA_Table::sGetStatusInfo(
|
||||
$_REQUEST['db'],
|
||||
$_REQUEST['table'],
|
||||
'Engine'
|
||||
)
|
||||
);
|
||||
} else {
|
||||
$query = 'SHOW TABLES FROM ' . PMA_Util::backquote($foreignDb);
|
||||
}
|
||||
$tables_rs = $GLOBALS['dbi']->query(
|
||||
$query,
|
||||
null,
|
||||
PMA_DatabaseInterface::QUERY_STORE
|
||||
);
|
||||
$tables = array();
|
||||
while ($row = $GLOBALS['dbi']->fetchRow($tables_rs)) {
|
||||
if ($foreign) {
|
||||
if (isset($row[1])
|
||||
&& strtoupper($row[1]) == $tbl_storage_engine
|
||||
) {
|
||||
$tables[] = htmlspecialchars($row[0]);
|
||||
}
|
||||
} else {
|
||||
$tables[] = htmlspecialchars($row[0]);
|
||||
}
|
||||
}
|
||||
$response->addJSON('tables', $tables);
|
||||
}
|
||||
exit;
|
||||
PMA_sendHtmlForTableOrColumnDropdownList();
|
||||
}
|
||||
|
||||
$header = $response->getHeader();
|
||||
@ -324,36 +280,9 @@ if (isset($destination_foreign_db)) {
|
||||
// U p d a t e s f o r d i s p l a y f i e l d
|
||||
|
||||
if ($cfgRelation['displaywork'] && isset($display_field)) {
|
||||
$upd_query = false;
|
||||
if ($disp) {
|
||||
if ($display_field != '') {
|
||||
$upd_query = 'UPDATE '
|
||||
. PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
|
||||
. '.' . PMA_Util::backquote($cfgRelation['table_info'])
|
||||
. ' SET display_field = \''
|
||||
. PMA_Util::sqlAddSlashes($display_field) . '\''
|
||||
. ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\''
|
||||
. ' AND table_name = \'' . PMA_Util::sqlAddSlashes($table) . '\'';
|
||||
} else {
|
||||
$upd_query = 'DELETE FROM '
|
||||
. PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
|
||||
. '.' . PMA_Util::backquote($cfgRelation['table_info'])
|
||||
. ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\''
|
||||
. ' AND table_name = \'' . PMA_Util::sqlAddSlashes($table) . '\'';
|
||||
}
|
||||
} elseif ($display_field != '') {
|
||||
$upd_query = 'INSERT INTO '
|
||||
. PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
|
||||
. '.' . PMA_Util::backquote($cfgRelation['table_info'])
|
||||
. '(db_name, table_name, display_field) VALUES('
|
||||
. '\'' . PMA_Util::sqlAddSlashes($db) . '\','
|
||||
. '\'' . PMA_Util::sqlAddSlashes($table) . '\','
|
||||
. '\'' . PMA_Util::sqlAddSlashes($display_field) . '\')';
|
||||
}
|
||||
|
||||
if ($upd_query) {
|
||||
PMA_queryAsControlUser($upd_query);
|
||||
}
|
||||
PMA_handleUpdateForDisplayField(
|
||||
$disp, $display_field, $db, $table, $cfgRelation
|
||||
);
|
||||
} // end if
|
||||
|
||||
// If we did an update, refresh our data
|
||||
@ -374,273 +303,19 @@ if ($cfgRelation['displaywork']) {
|
||||
/**
|
||||
* Dialog
|
||||
*/
|
||||
|
||||
// common form
|
||||
$html_output .= '<form method="post" action="tbl_relation.php">' . "\n"
|
||||
. PMA_URL_getHiddenInputs($db, $table);
|
||||
|
||||
// Now find out the columns of our $table
|
||||
// need to use PMA_DatabaseInterface::QUERY_STORE with $GLOBALS['dbi']->numRows() in mysqli
|
||||
// need to use PMA_DatabaseInterface::QUERY_STORE with $GLOBALS['dbi']->numRows()
|
||||
// in mysqli
|
||||
$columns = $GLOBALS['dbi']->getColumns($db, $table);
|
||||
|
||||
if (count($columns) > 0) {
|
||||
|
||||
foreach ($columns as $row) {
|
||||
$save_row[] = $row;
|
||||
}
|
||||
|
||||
$saved_row_cnt = count($save_row);
|
||||
$html_output .= '<fieldset>'
|
||||
. '<legend>' . __('Relations'). '</legend>'
|
||||
. '<table id="relationalTable">'
|
||||
. '<tr><th>' . __('Column') . '</th>';
|
||||
|
||||
if ($cfgRelation['relwork']) {
|
||||
$html_output .= '<th>' . __('Internal relation');
|
||||
if (PMA_Util::isForeignKeySupported($tbl_storage_engine)) {
|
||||
$html_output .= PMA_Util::showHint(
|
||||
__(
|
||||
'An internal relation is not necessary when a corresponding'
|
||||
. ' FOREIGN KEY relation exists.'
|
||||
)
|
||||
);
|
||||
}
|
||||
$html_output .= '</th>';
|
||||
}
|
||||
|
||||
if (PMA_Util::isForeignKeySupported($tbl_storage_engine)) {
|
||||
// this does not have to be translated, it's part of the MySQL syntax
|
||||
$html_output .= '<th colspan="2">' . __('Foreign key constraint')
|
||||
. ' (' . $tbl_storage_engine . ')';
|
||||
$html_output .= '</th>';
|
||||
}
|
||||
$html_output .= '</tr>';
|
||||
|
||||
$odd_row = true;
|
||||
for ($i = 0; $i < $saved_row_cnt; $i++) {
|
||||
$myfield = $save_row[$i]['Field'];
|
||||
// Use an md5 as array index to avoid having special characters
|
||||
// in the name atttibure (see bug #1746964 )
|
||||
$myfield_md5 = md5($myfield);
|
||||
$myfield_html = htmlspecialchars($myfield);
|
||||
|
||||
$html_output .= '<tr class="' . ($odd_row ? 'odd' : 'even') . '">'
|
||||
. '<td class="center">'
|
||||
. '<strong>' . $myfield_html . '</strong>'
|
||||
. '<input type="hidden" name="fields_name[' . $myfield_md5 . ']"'
|
||||
. ' value="' . $myfield_html . '"/>'
|
||||
. '</td>';
|
||||
$odd_row = ! $odd_row;
|
||||
|
||||
if ($cfgRelation['relwork']) {
|
||||
$html_output .= '<td>';
|
||||
|
||||
$foreign_db = false;
|
||||
$foreign_table = false;
|
||||
$foreign_column = false;
|
||||
|
||||
// database dropdown
|
||||
if (isset($existrel[$myfield])) {
|
||||
$foreign_db = $existrel[$myfield]['foreign_db'];
|
||||
} else {
|
||||
$foreign_db = $db;
|
||||
}
|
||||
$html_output .= PMA_generateRelationalDropdown(
|
||||
'destination_db[' . $myfield_md5 . ']',
|
||||
$GLOBALS['pma']->databases,
|
||||
$foreign_db,
|
||||
__('Database')
|
||||
);
|
||||
// end of database dropdown
|
||||
|
||||
// table dropdown
|
||||
$tables = array();
|
||||
if ($foreign_db) {
|
||||
if (isset($existrel[$myfield])) {
|
||||
$foreign_table = $existrel[$myfield]['foreign_table'];
|
||||
}
|
||||
$tables_rs = $GLOBALS['dbi']->query(
|
||||
'SHOW TABLES FROM ' . PMA_Util::backquote($foreign_db),
|
||||
null,
|
||||
PMA_DatabaseInterface::QUERY_STORE
|
||||
);
|
||||
while ($row = $GLOBALS['dbi']->fetchRow($tables_rs)) {
|
||||
$tables[] = $row[0];
|
||||
}
|
||||
}
|
||||
$html_output .= PMA_generateRelationalDropdown(
|
||||
'destination_table[' . $myfield_md5 . ']',
|
||||
$tables,
|
||||
$foreign_table,
|
||||
__('Table')
|
||||
);
|
||||
// end of table dropdown
|
||||
|
||||
// column dropdown
|
||||
$columns = array();
|
||||
if ($foreign_db && $foreign_table) {
|
||||
if (isset($existrel[$myfield])) {
|
||||
$foreign_column = $existrel[$myfield]['foreign_field'];
|
||||
}
|
||||
$table_obj = new PMA_Table($foreign_table, $foreign_db);
|
||||
$columns = $table_obj->getUniqueColumns(false, false);
|
||||
}
|
||||
$html_output .= PMA_generateRelationalDropdown(
|
||||
'destination_column[' . $myfield_md5 . ']',
|
||||
$columns,
|
||||
$foreign_column,
|
||||
__('Column')
|
||||
);
|
||||
// end of column dropdown
|
||||
|
||||
$html_output .= '</td>';
|
||||
} // end if (internal relations)
|
||||
|
||||
if (PMA_Util::isForeignKeySupported($tbl_storage_engine)) {
|
||||
$html_output .= '<td>';
|
||||
if (! empty($save_row[$i]['Key'])) {
|
||||
|
||||
$foreign_db = false;
|
||||
$foreign_table = false;
|
||||
$foreign_column = false;
|
||||
|
||||
// foreign database dropdown
|
||||
if (isset($existrel_foreign[$myfield])) {
|
||||
$foreign_db = $existrel_foreign[$myfield]['foreign_db'];
|
||||
} else {
|
||||
$foreign_db = $db;
|
||||
}
|
||||
$html_output .= '<span class="formelement clearfloat">';
|
||||
$html_output .= PMA_generateRelationalDropdown(
|
||||
'destination_foreign_db[' . $myfield_md5 . ']',
|
||||
$GLOBALS['pma']->databases,
|
||||
$foreign_db,
|
||||
__('Database')
|
||||
);
|
||||
// end of foreign database dropdown
|
||||
|
||||
// foreign table dropdown
|
||||
$tables = array();
|
||||
if ($foreign_db) {
|
||||
if (isset($existrel_foreign[$myfield])) {
|
||||
$foreign_table = $existrel_foreign[$myfield]['foreign_table'];
|
||||
}
|
||||
$tables_rs = $GLOBALS['dbi']->query(
|
||||
'SHOW TABLE STATUS FROM ' . PMA_Util::backquote($foreign_db),
|
||||
null,
|
||||
PMA_DatabaseInterface::QUERY_STORE
|
||||
);
|
||||
while ($row = $GLOBALS['dbi']->fetchRow($tables_rs)) {
|
||||
if (isset($row[1])
|
||||
&& strtoupper($row[1]) == $tbl_storage_engine
|
||||
) {
|
||||
$tables[] = $row[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
$html_output .= PMA_generateRelationalDropdown(
|
||||
'destination_foreign_table[' . $myfield_md5 . ']',
|
||||
$tables,
|
||||
$foreign_table,
|
||||
__('Table')
|
||||
);
|
||||
// end of foreign table dropdown
|
||||
|
||||
// foreign column dropdown
|
||||
$columns = array();
|
||||
if ($foreign_db && $foreign_table) {
|
||||
if (isset($existrel_foreign[$myfield])) {
|
||||
$foreign_column = $existrel_foreign[$myfield]['foreign_field'];
|
||||
}
|
||||
$table_obj = new PMA_Table($foreign_table, $foreign_db);
|
||||
$columns = $table_obj->getUniqueColumns(false, false);
|
||||
}
|
||||
$html_output .= PMA_generateRelationalDropdown(
|
||||
'destination_foreign_column[' . $myfield_md5 . ']',
|
||||
$columns,
|
||||
$foreign_column,
|
||||
__('Column')
|
||||
);
|
||||
$html_output .= '</span>';
|
||||
// end of foreign column dropdown
|
||||
|
||||
// For constraint name
|
||||
$html_output .= '<span class="formelement clearfloat">';
|
||||
$constraint_name = isset($existrel_foreign[$myfield]['constraint'])
|
||||
? $existrel_foreign[$myfield]['constraint'] : '';
|
||||
$html_output .= __('Constraint name');
|
||||
$html_output .= '<input type="text" name="constraint_name['
|
||||
. $myfield_md5 . ']"'
|
||||
. ' value="' . $constraint_name . '"/>';
|
||||
$html_output .= '</span>' . "\n";
|
||||
|
||||
$html_output .= '<span class="formelement clearfloat">';
|
||||
// For ON DELETE and ON UPDATE, the default action
|
||||
// is RESTRICT as per MySQL doc; however, a SHOW CREATE TABLE
|
||||
// won't display the clause if it's set as RESTRICT.
|
||||
$on_delete = isset($existrel_foreign[$myfield]['on_delete'])
|
||||
? $existrel_foreign[$myfield]['on_delete'] : 'RESTRICT';
|
||||
$html_output .= PMA_generateDropdown(
|
||||
'ON DELETE',
|
||||
'on_delete[' . $myfield_md5 . ']',
|
||||
$options_array,
|
||||
$on_delete
|
||||
);
|
||||
$html_output .= '</span>' . "\n";
|
||||
|
||||
$html_output .= '<span class="formelement clearfloat">' . "\n";
|
||||
$on_update = isset($existrel_foreign[$myfield]['on_update'])
|
||||
? $existrel_foreign[$myfield]['on_update'] : 'RESTRICT';
|
||||
$html_output .= PMA_generateDropdown(
|
||||
'ON UPDATE',
|
||||
'on_update[' . $myfield_md5 . ']',
|
||||
$options_array,
|
||||
$on_update
|
||||
);
|
||||
$html_output .= '</span>' . "\n";
|
||||
} else {
|
||||
$html_output .= __('No index defined! Create one below');
|
||||
} // end if (a key exists)
|
||||
$html_output .= '</td>';
|
||||
} // end if (InnoDB)
|
||||
$html_output .= '</tr>';
|
||||
} // end for
|
||||
|
||||
unset( $myfield, $myfield_md5, $myfield_html);
|
||||
$html_output .= '</table>' . "\n"
|
||||
. '</fieldset>' . "\n";
|
||||
|
||||
if ($cfgRelation['displaywork']) {
|
||||
// Get "display_field" infos
|
||||
$disp = PMA_getDisplayField($db, $table);
|
||||
$html_output .= '<fieldset>'
|
||||
. '<label>' . __('Choose column to display:') . '</label>'
|
||||
. '<select name="display_field">'
|
||||
. '<option value="">---</option>';
|
||||
|
||||
foreach ($save_row as $row) {
|
||||
$html_output .= '<option value="'
|
||||
. htmlspecialchars($row['Field']) . '"';
|
||||
if (isset($disp) && $row['Field'] == $disp) {
|
||||
$html_output .= ' selected="selected"';
|
||||
}
|
||||
$html_output .= '>' . htmlspecialchars($row['Field'])
|
||||
. '</option>'. "\n";
|
||||
} // end while
|
||||
|
||||
$html_output .= '</select>'
|
||||
. '</fieldset>';
|
||||
} // end if (displayworks)
|
||||
|
||||
$html_output .= '<fieldset class="tblFooters">'
|
||||
. '<input type="submit" value="' . __('Save') . '" />'
|
||||
. '</fieldset>'
|
||||
. '</form>';
|
||||
} // end if (we have columns in this table)
|
||||
// common form
|
||||
$html_output .= PMA_getHtmlForCommonForm(
|
||||
$db, $table, $columns, $cfgRelation, $tbl_storage_engine, $existrel,
|
||||
$existrel_foreign, $options_array
|
||||
);
|
||||
|
||||
if (PMA_Util::isForeignKeySupported($tbl_storage_engine)) {
|
||||
$html_output .= '<div id="index_div" class="ajax" >'
|
||||
. PMA_getHtmlForDisplayIndexes();
|
||||
$html_output .= PMA_getHtmlForDisplayIndexes();
|
||||
}
|
||||
// Render HTML output
|
||||
PMA_Response::getInstance()->addHTML($html_output);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user