2007-07-11 16:02:57 +00:00
< ? php
2007-07-11 22:37:43 +00:00
if ( $_POST && ! $error && ! $_POST [ " add " ] && ! $_POST [ " change " ] && ! $_POST [ " change-js " ]) {
2007-08-04 20:26:58 +00:00
if ( $_POST [ " drop " ]) {
2008-09-02 08:42:14 +00:00
query_redirect ( " ALTER TABLE " . idf_escape ( $_GET [ " foreign " ]) . " DROP FOREIGN KEY " . idf_escape ( $_GET [ " name " ]), $SELF . " table= " . urlencode ( $_GET [ " foreign " ]), lang ( 'Foreign key has been dropped.' ));
2007-08-04 20:26:58 +00:00
} else {
2007-07-16 11:53:55 +00:00
$source = array_filter ( $_POST [ " source " ], 'strlen' );
ksort ( $source );
$target = array ();
foreach ( $source as $key => $val ) {
$target [ $key ] = $_POST [ " target " ][ $key ];
}
2008-09-02 08:42:14 +00:00
query_redirect ( " ALTER TABLE " . idf_escape ( $_GET [ " foreign " ])
. ( strlen ( $_GET [ " name " ]) ? " DROP FOREIGN KEY " . idf_escape ( $_GET [ " name " ]) . " , " : " " )
. " ADD FOREIGN KEY ( " . implode ( " , " , array_map ( 'idf_escape' , $source )) . " ) REFERENCES " . idf_escape ( $_POST [ " table " ]) . " ( " . implode ( " , " , array_map ( 'idf_escape' , $target )) . " ) "
2007-08-04 20:26:58 +00:00
. ( in_array ( $_POST [ " on_delete " ], $on_actions ) ? " ON DELETE $_POST[on_delete] " : " " )
. ( in_array ( $_POST [ " on_update " ], $on_actions ) ? " ON UPDATE $_POST[on_update] " : " " )
2008-09-02 08:42:14 +00:00
, $SELF . " table= " . urlencode ( $_GET [ " foreign " ]), ( strlen ( $_GET [ " name " ]) ? lang ( 'Foreign key has been altered.' ) : lang ( 'Foreign key has been created.' )));
2007-07-11 22:37:43 +00:00
}
}
2008-04-10 14:37:10 +00:00
page_header ( lang ( 'Foreign key' ), $error , array ( " table " => $_GET [ " foreign " ]), $_GET [ " foreign " ]);
2007-07-11 16:02:57 +00:00
2007-07-14 07:14:06 +00:00
$tables = array ();
$result = $mysql -> query ( " SHOW TABLE STATUS " );
while ( $row = $result -> fetch_assoc ()) {
if ( $row [ " Engine " ] == " InnoDB " ) {
$tables [] = $row [ " Name " ];
}
}
$result -> free ();
2007-07-11 22:37:43 +00:00
if ( $_POST ) {
$row = $_POST ;
ksort ( $row [ " source " ]);
if ( $_POST [ " add " ]) {
$row [ " source " ][] = " " ;
} elseif ( $_POST [ " change " ] || $_POST [ " change-js " ]) {
$row [ " target " ] = array ();
}
} elseif ( strlen ( $_GET [ " name " ])) {
2007-07-11 16:02:57 +00:00
$foreign_keys = foreign_keys ( $_GET [ " foreign " ]);
2007-07-11 22:37:43 +00:00
$row = $foreign_keys [ $_GET [ " name " ]];
2007-08-04 20:26:58 +00:00
$row [ " source " ][] = " " ;
2007-07-11 22:37:43 +00:00
} else {
$row = array ( " table " => $_GET [ " foreign " ], " source " => array ( " " ));
}
2007-07-14 07:18:10 +00:00
$source = get_vals ( " SHOW COLUMNS FROM " . idf_escape ( $_GET [ " foreign " ])); //! no text and blob
$target = ( $_GET [ " foreign " ] === $row [ " table " ] ? $source : get_vals ( " SHOW COLUMNS FROM " . idf_escape ( $row [ " table " ])));
2007-07-11 22:37:43 +00:00
?>
2007-08-04 20:26:58 +00:00
2008-09-24 20:46:21 +00:00
< script type = " text/javascript " >// <! [ CDATA [
2007-08-04 20:26:58 +00:00
function add_row ( field ) {
var row = field . parentNode . parentNode . cloneNode ( true );
var selects = row . getElementsByTagName ( 'select' );
for ( var i = 0 ; i < selects . length ; i ++ ) {
selects [ i ] . name = selects [ i ] . name . replace ( / \ ] / , '1$&' );
2007-08-23 15:38:00 +00:00
selects [ i ] . selectedIndex = 0 ;
2007-08-04 20:26:58 +00:00
}
field . parentNode . parentNode . parentNode . appendChild ( row );
field . onchange = function () { };
}
2008-09-24 20:46:21 +00:00
// ]]></script>
2007-08-04 20:26:58 +00:00
2007-07-11 22:37:43 +00:00
< form action = " " method = " post " >
< p >
< ? php echo lang ( 'Target table' ); ?> :
2007-07-14 07:14:06 +00:00
< select name = " table " onchange = " this.form['change-js'].value = '1'; this.form.submit(); " >< ? php echo optionlist ( $tables , $row [ " table " ]); ?> </select>
2007-07-11 22:37:43 +00:00
< input type = " hidden " name = " change-js " value = " " />
</ p >
< noscript >< p >< input type = " submit " name = " change " value = " <?php echo lang('Change'); ?> " /></ p ></ noscript >
2009-05-01 10:41:33 +00:00
< table cellspacing = " 0 " >
2007-07-11 22:37:43 +00:00
< thead >< tr >< th >< ? php echo lang ( 'Source' ); ?> </th><th><?php echo lang('Target'); ?></th></tr></thead>
< ? php
2007-08-04 20:26:58 +00:00
$j = 0 ;
2007-07-11 22:37:43 +00:00
foreach ( $row [ " source " ] as $key => $val ) {
echo " <tr> " ;
2007-08-04 20:26:58 +00:00
echo " <td><select name='source[ " . intval ( $key ) . " ]' " . ( $j == count ( $row [ " source " ]) - 1 ? " onchange='add_row(this);' " : " " ) . " ><option></option> " . optionlist ( $source , $val ) . " </select></td> " ;
2007-07-11 22:37:43 +00:00
echo " <td><select name='target[ " . intval ( $key ) . " ]'> " . optionlist ( $target , $row [ " target " ][ $key ]) . " </select></td> " ;
echo " </tr> \n " ;
2007-08-04 20:26:58 +00:00
$j ++ ;
2007-07-11 16:02:57 +00:00
}
2007-07-11 22:37:43 +00:00
?>
</ table >
< p >
2007-07-25 17:12:43 +00:00
< ? php echo lang ( 'ON DELETE' ); ?> : <select name="on_delete"><option></option><?php echo optionlist($on_actions, $row["on_delete"]); ?></select>
< ? php echo lang ( 'ON UPDATE' ); ?> : <select name="on_update"><option></option><?php echo optionlist($on_actions, $row["on_update"]); ?></select>
</ p >
< p >
2007-07-11 22:37:43 +00:00
< input type = " hidden " name = " token " value = " <?php echo $token ; ?> " />
< input type = " submit " value = " <?php echo lang('Save'); ?> " />
2008-10-09 14:13:17 +00:00
< ? php if ( strlen ( $_GET [ " name " ])) { ?> <input type="submit" name="drop" value="<?php echo lang('Drop'); ?>"<?php echo $confirm; ?> /><?php } ?>
2007-07-11 22:37:43 +00:00
</ p >
2007-08-04 20:26:58 +00:00
< noscript >< p >< input type = " submit " name = " add " value = " <?php echo lang('Add column'); ?> " /></ p ></ noscript >
2007-07-11 22:37:43 +00:00
</ form >