2007-07-04 17:16:44 +00:00
< ? php
$index_types = array ( " PRIMARY " , " UNIQUE " , " INDEX " , " FULLTEXT " );
2007-07-05 05:09:01 +00:00
$indexes = indexes ( $_GET [ " indexes " ]);
2007-07-09 06:12:22 +00:00
if ( $_POST && ! $error && ! $_POST [ " add " ]) {
2007-07-05 05:09:01 +00:00
$alter = array ();
foreach ( $_POST [ " indexes " ] as $index ) {
if ( in_array ( $index [ " type " ], $index_types )) {
$columns = array ();
2007-07-14 07:06:53 +00:00
$lengths = array ();
$set = array ();
2007-07-05 05:09:01 +00:00
ksort ( $index [ " columns " ]);
2007-07-14 07:06:53 +00:00
foreach ( $index [ " columns " ] as $key => $column ) {
if ( strlen ( $column )) {
$length = $index [ " lengths " ][ $key ];
$set [] = idf_escape ( $column ) . ( $length ? " ( " . intval ( $length ) . " ) " : " " );
2007-07-05 05:09:01 +00:00
$columns [ count ( $columns ) + 1 ] = $column ;
2007-07-14 07:06:53 +00:00
$lengths [ count ( $lengths ) + 1 ] = ( $length ? $length : null );
2007-07-05 05:09:01 +00:00
}
}
if ( $columns ) {
foreach ( $indexes as $name => $existing ) {
2007-07-14 07:13:19 +00:00
ksort ( $existing [ " columns " ]);
ksort ( $existing [ " lengths " ]);
2007-07-14 07:06:53 +00:00
if ( $index [ " type " ] == $existing [ " type " ] && $existing [ " columns " ] === $columns && $existing [ " lengths " ] === $lengths ) {
2007-07-05 05:09:01 +00:00
unset ( $indexes [ $name ]);
continue 2 ;
}
}
2007-07-14 07:06:53 +00:00
$alter [] = " ADD $index[type] " . ( $index [ " type " ] == " PRIMARY " ? " KEY " : " " ) . " ( " . implode ( " , " , $set ) . " ) " ;
2007-07-05 05:09:01 +00:00
}
}
}
foreach ( $indexes as $name => $existing ) {
2007-07-14 07:16:02 +00:00
$alter [] = " DROP INDEX " . idf_escape ( $name );
2007-07-05 05:09:01 +00:00
}
2008-09-02 08:42:14 +00:00
if ( ! $alter ) {
redirect ( $SELF . " table= " . urlencode ( $_GET [ " indexes " ]));
2007-07-05 05:09:01 +00:00
}
2008-09-02 08:42:14 +00:00
query_redirect ( " ALTER TABLE " . idf_escape ( $_GET [ " indexes " ]) . " " . implode ( " , " , $alter ), $SELF . " table= " . urlencode ( $_GET [ " indexes " ]), lang ( 'Indexes has been altered.' ));
2007-07-04 17:16:44 +00:00
}
2008-04-10 14:37:10 +00:00
page_header ( lang ( 'Indexes' ), $error , array ( " table " => $_GET [ " indexes " ]), $_GET [ " indexes " ]);
2007-07-04 17:16:44 +00:00
2007-07-14 07:06:53 +00:00
$fields = array_keys ( fields ( $_GET [ " indexes " ]));
2008-07-10 15:39:24 +00:00
$row = array ( " indexes " => $indexes );
2007-07-04 17:16:44 +00:00
if ( $_POST ) {
2007-07-14 07:06:53 +00:00
$row = $_POST ;
2008-03-18 10:29:20 +00:00
if ( $_POST [ " add " ]) {
2007-07-14 07:06:53 +00:00
foreach ( $row [ " indexes " ] as $key => $index ) {
if ( strlen ( $index [ " columns " ][ count ( $index [ " columns " ])])) {
$row [ " indexes " ][ $key ][ " columns " ][] = " " ;
}
}
2007-08-04 19:37:22 +00:00
$index = end ( $row [ " indexes " ]);
2007-07-14 07:13:19 +00:00
if ( $index [ " type " ] || array_filter ( $index [ " columns " ], 'strlen' ) || array_filter ( $index [ " lengths " ], 'strlen' )) {
2007-07-14 07:06:53 +00:00
$row [ " indexes " ][] = array ( " columns " => array ( 1 => " " ));
}
2007-07-05 12:01:32 +00:00
}
2007-07-04 17:16:44 +00:00
} else {
2007-07-14 07:06:53 +00:00
foreach ( $row [ " indexes " ] as $key => $index ) {
$row [ " indexes " ][ $key ][ " columns " ][] = " " ;
}
$row [ " indexes " ][] = array ( " columns " => array ( 1 => " " ));
2007-07-04 17:16:44 +00:00
}
?>
2007-07-09 14:47:46 +00:00
2008-09-24 20:46:21 +00:00
< script type = " text/javascript " >// <! [ CDATA [
2007-07-24 15:44:27 +00:00
function add_row ( field ) {
var row = field . parentNode . parentNode . cloneNode ( true );
var spans = row . getElementsByTagName ( 'span' );
row . getElementsByTagName ( 'td' )[ 1 ] . innerHTML = '<span>' + spans [ spans . length - 1 ] . innerHTML + '</span>' ;
var selects = row . getElementsByTagName ( 'select' );
for ( var i = 0 ; i < selects . length ; i ++ ) {
selects [ i ] . name = selects [ i ] . name . replace ( / indexes\ [[ 0 - 9 ] +/ , '$&1' );
2007-08-23 15:38:00 +00:00
selects [ i ] . selectedIndex = 0 ;
2007-07-24 15:44:27 +00:00
}
var input = row . getElementsByTagName ( 'input' )[ 0 ];
input . name = input . name . replace ( / indexes\ [[ 0 - 9 ] +/ , '$&1' );
input . value = '' ;
field . parentNode . parentNode . parentNode . appendChild ( row );
field . onchange = function () { };
}
function add_column ( field ) {
var column = field . parentNode . cloneNode ( true );
var select = column . getElementsByTagName ( 'select' )[ 0 ];
select . name = select . name . replace ( / \ ] \ [[ 0 - 9 ] +/ , '$&1' );
2007-08-23 15:38:00 +00:00
select . selectedIndex = 0 ;
2007-07-24 15:44:27 +00:00
var input = column . getElementsByTagName ( 'input' )[ 0 ];
input . name = input . name . replace ( / \ ] \ [[ 0 - 9 ] +/ , '$&1' );
input . value = '' ;
field . parentNode . parentNode . appendChild ( column );
field . onchange = function () { };
}
2008-09-24 20:46:21 +00:00
// ]]></script>
2007-07-24 15:44:27 +00:00
2007-07-04 17:16:44 +00:00
< form action = " " method = " post " >
2009-05-01 10:41:33 +00:00
< table cellspacing = " 0 " >
2009-04-30 15:21:31 +00:00
< thead >< tr >< th >< ? php echo lang ( 'Index Type' ); ?> </th><th><?php echo lang('Column (length)'); ?></th></tr></thead>
2007-07-04 17:16:44 +00:00
< ? php
$j = 0 ;
2007-07-05 04:39:15 +00:00
foreach ( $row [ " indexes " ] as $index ) {
2009-04-30 15:21:31 +00:00
echo " <tr " . odd () . " ><td><select name='indexes[ $j ][type]' " . ( $j == count ( $row [ " indexes " ]) - 1 ? " onchange='add_row(this);' " : " " ) . " ><option></option> " . optionlist ( $index_types , $index [ " type " ]) . " </select></td><td> \n " ;
2007-07-14 07:06:53 +00:00
ksort ( $index [ " columns " ]);
foreach ( $index [ " columns " ] as $i => $column ) {
2007-07-24 15:44:27 +00:00
echo " <span><select name='indexes[ $j ][columns][ $i ]' " . ( $i == count ( $index [ " columns " ]) ? " onchange='add_column(this);' " : " " ) . " ><option></option> " . optionlist ( $fields , $column ) . " </select> " ;
echo " <input name='indexes[ $j ][lengths][ $i ]' size='2' value= \" " . htmlspecialchars ( $index [ " lengths " ][ $i ]) . " \" /></span> \n " ;
2007-07-04 17:16:44 +00:00
}
2007-07-14 07:06:53 +00:00
echo " </td></tr> \n " ;
$j ++ ;
2007-07-04 17:16:44 +00:00
}
?>
</ table >
2007-07-11 13:56:28 +00:00
< p >
2007-07-11 21:06:19 +00:00
< input type = " hidden " name = " token " value = " <?php echo $token ; ?> " />
< input type = " submit " value = " <?php echo lang('Alter indexes'); ?> " />
2007-07-11 13:56:28 +00:00
</ p >
2007-07-26 08:24:44 +00:00
< noscript >< p >< input type = " submit " name = " add " value = " <?php echo lang('Add next'); ?> " /></ p ></ noscript >
2007-07-04 17:16:44 +00:00
</ form >