2007-07-11 16:02:57 +00:00
< ? php
2009-08-29 13:57:50 +00:00
$TABLE = $_GET [ " foreign " ];
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 " ]) {
2010-05-11 14:45:04 +00:00
query_redirect ( " ALTER TABLE " . table ( $TABLE ) . " \n DROP " . ( $jush == " sql " ? " FOREIGN KEY " : " CONSTRAINT " ) . idf_escape ( $_GET [ " name " ]), ME . " table= " . urlencode ( $TABLE ), 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' );
2009-06-21 23:20:32 +00:00
ksort ( $source ); // enforce input order
2007-07-16 11:53:55 +00:00
$target = array ();
foreach ( $source as $key => $val ) {
$target [ $key ] = $_POST [ " target " ][ $key ];
}
2010-05-11 14:45:04 +00:00
query_redirect ( " ALTER TABLE " . table ( $TABLE )
2010-01-09 23:33:41 +00:00
. ( $_GET [ " name " ] != " " ? " \n DROP FOREIGN KEY " . idf_escape ( $_GET [ " name " ]) . " , " : " " )
2011-03-28 10:40:14 +00:00
. " \n ADD FOREIGN KEY ( " . implode ( " , " , array_map ( 'idf_escape' , $source )) . " ) REFERENCES " . table ( $_POST [ " table " ]) . " ( " . implode ( " , " , array_map ( 'idf_escape' , $target )) . " ) " //! reuse $_GET["name"] - check in older MySQL versions
2011-08-02 15:46:13 +00:00
. ( ereg ( " ^( $on_actions ) \$ " , $_POST [ " on_delete " ]) ? " ON DELETE $_POST[on_delete] " : " " )
. ( ereg ( " ^( $on_actions ) \$ " , $_POST [ " on_update " ]) ? " ON UPDATE $_POST[on_update] " : " " )
2010-01-09 23:33:41 +00:00
, ME . " table= " . urlencode ( $TABLE ), ( $_GET [ " name " ] != " " ? lang ( 'Foreign key has been altered.' ) : lang ( 'Foreign key has been created.' )));
2009-08-04 08:37:00 +00:00
$error = lang ( 'Source and target columns must have the same data type, there must be an index on the target columns and referenced data must exist.' ) . " <br> $error " ; //! no partitioning
2007-07-11 22:37:43 +00:00
}
}
2009-07-20 12:12:55 +00:00
2009-08-29 13:57:50 +00:00
page_header ( lang ( 'Foreign key' ), $error , array ( " table " => $TABLE ), $TABLE );
2007-07-11 16:02:57 +00:00
2009-08-29 13:57:50 +00:00
$row = array ( " table " => $TABLE , " source " => array ( " " ));
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 ();
}
2010-01-09 23:33:41 +00:00
} elseif ( $_GET [ " name " ] != " " ) {
2009-08-29 13:57:50 +00:00
$foreign_keys = foreign_keys ( $TABLE );
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
}
2007-07-14 07:18:10 +00:00
2009-10-16 12:26:16 +00:00
$source = array_keys ( fields ( $TABLE )); //! no text and blob
$target = ( $TABLE === $row [ " table " ] ? $source : array_keys ( fields ( $row [ " table " ])));
2010-04-21 12:01:32 +00:00
$referencable = array ();
foreach ( table_status () as $name => $table_status ) {
if ( fk_support ( $table_status )) {
$referencable [] = $name ;
}
}
2007-07-11 22:37:43 +00:00
?>
2007-08-04 20:26:58 +00:00
2007-07-11 22:37:43 +00:00
< form action = " " method = " post " >
< p >
2011-07-12 14:03:39 +00:00
< ? php if ( $row [ " db " ] == " " && $row [ " ns " ] == " " ) { ?>
2007-07-11 22:37:43 +00:00
< ? php echo lang ( 'Target table' ); ?> :
2011-04-23 12:25:07 +00:00
< ? php echo html_select ( " table " , $referencable , $row [ " table " ], " this.form['change-js'].value = '1'; if (!ajaxForm(this.form)) this.form.submit(); " ); ?>
2009-07-11 20:30:40 +00:00
< input type = " hidden " name = " change-js " value = " " >
< noscript >< p >< input type = " submit " name = " change " value = " <?php echo lang('Change'); ?> " ></ noscript >
2009-05-01 10:41:33 +00:00
< table cellspacing = " 0 " >
2009-07-11 20:30:40 +00:00
< thead >< tr >< th >< ? php echo lang ( 'Source' ); ?> <th><?php echo lang('Target'); ?></thead>
2007-07-11 22:37:43 +00:00
< ? 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> " ;
2010-10-22 21:36:56 +00:00
echo " <td> " . html_select ( " source[ " . ( + $key ) . " ] " , array ( - 1 => " " ) + $source , $val , ( $j == count ( $row [ " source " ]) - 1 ? " foreignAddRow(this); " : 1 ));
echo " <td> " . html_select ( " target[ " . ( + $key ) . " ] " , $target , $row [ " target " ][ $key ]);
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 >
2011-08-02 15:46:13 +00:00
< ? php echo lang ( 'ON DELETE' ); ?> : <?php echo html_select("on_delete", array(-1 => "") + explode("|", $on_actions), $row["on_delete"]); ?>
< ? php echo lang ( 'ON UPDATE' ); ?> : <?php echo html_select("on_update", array(-1 => "") + explode("|", $on_actions), $row["on_update"]); ?>
2007-07-25 17:12:43 +00:00
< p >
2009-07-11 20:30:40 +00:00
< input type = " submit " value = " <?php echo lang('Save'); ?> " >
< noscript >< p >< input type = " submit " name = " add " value = " <?php echo lang('Add column'); ?> " ></ noscript >
2009-11-19 20:04:55 +00:00
< ? php } ?>
2010-10-18 21:57:39 +00:00
< ? php if ( $_GET [ " name " ] != " " ) { ?> <input type="submit" name="drop" value="<?php echo lang('Drop'); ?>"<?php echo confirm(); ?>><?php } ?>
2009-11-19 20:04:55 +00:00
< input type = " hidden " name = " token " value = " <?php echo $token ; ?> " >
2007-07-11 22:37:43 +00:00
</ form >