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
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-05-29 16:14:54 +00:00
echo " <tr><td><select name='indexes[ $j ][type]' " . ( $j == count ( $row [ " indexes " ]) - 1 ? " onchange='indexes_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 ) {
2009-05-29 16:14:54 +00:00
echo " <span><select name='indexes[ $j ][columns][ $i ]' " . ( $i == count ( $index [ " columns " ]) ? " onchange='indexes_add_column(this);' " : " " ) . " ><option></option> " . optionlist ( $fields , $column ) . " </select> " ;
2007-07-24 15:44:27 +00:00
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 >