2007-07-02 05:51:26 +00:00
< ? php
2008-10-01 11:39:13 +00:00
$where = ( isset ( $_GET [ " select " ]) ? array () : where ( $_GET ));
2008-09-27 08:52:18 +00:00
$update = ( $where && ! $_GET [ " clone " ]);
2007-07-25 15:18:59 +00:00
$fields = fields ( $_GET [ " edit " ]);
foreach ( $fields as $name => $field ) {
2008-09-27 08:52:18 +00:00
if ( isset ( $_GET [ " default " ]) ? $field [ " auto_increment " ] || preg_match ( '~text|blob~' , $field [ " type " ]) : ! isset ( $field [ " privileges " ][ $update ? " update " : " insert " ])) {
2007-07-25 15:18:59 +00:00
unset ( $fields [ $name ]);
2007-07-09 06:12:22 +00:00
}
2007-07-05 10:41:53 +00:00
}
2008-10-01 11:39:13 +00:00
if ( $_POST && ! $error && ! isset ( $_GET [ " select " ])) {
2008-09-27 01:51:14 +00:00
$location = ( $_POST [ " insert " ] ? $_SERVER [ " REQUEST_URI " ] : $SELF . ( isset ( $_GET [ " default " ]) ? " table= " : " select= " ) . urlencode ( $_GET [ " edit " ]));
2007-07-02 05:51:26 +00:00
if ( isset ( $_POST [ " delete " ])) {
2008-09-02 08:42:14 +00:00
query_redirect ( " DELETE FROM " . idf_escape ( $_GET [ " edit " ]) . " WHERE " . implode ( " AND " , $where ) . " LIMIT 1 " , $location , lang ( 'Item has been deleted.' ));
2007-07-02 05:51:26 +00:00
} else {
2008-09-02 08:42:14 +00:00
$set = array ();
2007-07-09 06:12:22 +00:00
foreach ( $fields as $name => $field ) {
2007-07-09 14:45:11 +00:00
$val = process_input ( $name , $field );
if ( $val !== false ) {
2007-07-25 15:18:59 +00:00
if ( ! isset ( $_GET [ " default " ])) {
$set [] = idf_escape ( $name ) . " = $val " ;
} elseif ( $field [ " type " ] == " timestamp " ) {
2007-07-25 17:10:03 +00:00
$set [] = " MODIFY " . idf_escape ( $name ) . " timestamp " . ( $field [ " null " ] ? " NULL " : " " ) . " DEFAULT $val " . ( $_POST [ " on_update " ][ bracket_escape ( $name )] ? " ON UPDATE CURRENT_TIMESTAMP " : " " );
2007-07-25 15:18:59 +00:00
} else {
2008-10-06 15:41:43 +00:00
$set [] = " ALTER " . idf_escape ( $name ) . ( $val == ( $field [ " null " ] || $field [ " type " ] == " enum " ? " NULL " : " '' " ) ? " DROP DEFAULT " : " SET DEFAULT $val " );
2007-07-25 15:18:59 +00:00
}
2007-07-02 05:51:26 +00:00
}
}
2008-09-02 08:42:14 +00:00
if ( ! $set ) {
redirect ( $location );
}
2007-07-09 06:12:22 +00:00
if ( isset ( $_GET [ " default " ])) {
2008-09-02 08:42:14 +00:00
query_redirect ( " ALTER TABLE " . idf_escape ( $_GET [ " edit " ]) . implode ( " , " , $set ), $location , lang ( 'Default values has been set.' ));
2008-09-27 08:52:18 +00:00
} elseif ( $update ) {
2008-09-02 08:42:14 +00:00
query_redirect ( " UPDATE " . idf_escape ( $_GET [ " edit " ]) . " SET " . implode ( " , " , $set ) . " WHERE " . implode ( " AND " , $where ) . " LIMIT 1 " , $location , lang ( 'Item has been updated.' ));
2007-07-02 05:51:26 +00:00
} else {
2008-09-02 08:42:14 +00:00
query_redirect ( " INSERT INTO " . idf_escape ( $_GET [ " edit " ]) . " SET " . implode ( " , " , $set ), $location , lang ( 'Item has been inserted.' ));
2007-07-02 05:51:26 +00:00
}
}
}
2008-10-01 11:39:13 +00:00
page_header (( isset ( $_GET [ " default " ]) ? lang ( 'Default values' ) : ( $_GET [ " where " ] || isset ( $_GET [ " select " ]) ? lang ( 'Edit' ) : lang ( 'Insert' ))), $error , array (( isset ( $_GET [ " default " ]) ? " table " : " select " ) => $_GET [ " edit " ]), $_GET [ " edit " ]);
2007-07-02 05:51:26 +00:00
2008-07-10 15:39:24 +00:00
unset ( $row );
2008-03-18 10:29:20 +00:00
if ( $_POST ) {
2007-07-16 14:01:28 +00:00
$row = ( array ) $_POST [ " fields " ];
2007-07-02 05:51:26 +00:00
} elseif ( $where ) {
2007-07-06 13:48:25 +00:00
$select = array ();
2007-07-02 05:51:26 +00:00
foreach ( $fields as $name => $field ) {
2008-09-27 01:51:14 +00:00
if ( isset ( $field [ " privileges " ][ " select " ]) && ! preg_match ( '~binary|blob~' , $field [ " type " ]) && ( ! $_GET [ " clone " ] || ! $field [ " auto_increment " ])) {
2007-07-06 13:48:25 +00:00
$select [] = ( $field [ " type " ] == " enum " || $field [ " type " ] == " set " ? " 1* " . idf_escape ( $name ) . " AS " : " " ) . idf_escape ( $name );
2007-07-02 05:51:26 +00:00
}
}
2008-10-09 13:23:54 +00:00
$row = array ();
2007-07-10 13:30:42 +00:00
if ( $select ) {
$result = $mysql -> query ( " SELECT " . implode ( " , " , $select ) . " FROM " . idf_escape ( $_GET [ " edit " ]) . " WHERE " . implode ( " AND " , $where ) . " LIMIT 1 " );
2007-07-16 14:01:28 +00:00
$row = $result -> fetch_assoc ();
2008-09-01 16:09:04 +00:00
$result -> free ();
2007-07-10 13:30:42 +00:00
}
2007-07-02 05:51:26 +00:00
}
?>
2007-07-09 14:45:11 +00:00
2007-07-09 06:12:22 +00:00
< form action = " " method = " post " enctype = " multipart/form-data " >
2007-07-02 05:51:26 +00:00
< ? php
2007-07-10 20:26:17 +00:00
if ( $fields ) {
2007-07-25 17:10:03 +00:00
unset ( $create );
2007-07-10 20:26:17 +00:00
echo " <table border='0' cellspacing='0' cellpadding='2'> \n " ;
foreach ( $fields as $name => $field ) {
echo " <tr><th> " . htmlspecialchars ( $name ) . " </th><td> " ;
2008-10-09 13:23:54 +00:00
$value = ( ! isset ( $row ) ? $field [ " default " ] :
( strlen ( $row [ $name ]) && ( $field [ " type " ] == " enum " || $field [ " type " ] == " set " ) ? intval ( $row [ $name ]) :
( $_POST [ " clone " ] && $field [ " auto_increment " ] ? " " :
$row [ $name ]
)));
2007-07-10 20:26:17 +00:00
input ( $name , $field , $value );
2007-07-25 17:10:03 +00:00
if ( isset ( $_GET [ " default " ]) && $field [ " type " ] == " timestamp " ) {
if ( ! isset ( $create ) && ! $_POST ) {
2007-08-11 06:43:01 +00:00
//! disable sql_mode NO_FIELD_OPTIONS
2007-07-25 17:10:03 +00:00
$create = $mysql -> result ( $mysql -> query ( " SHOW CREATE TABLE " . idf_escape ( $_GET [ " edit " ])), 1 );
}
$checked = ( $_POST ? $_POST [ " on_update " ][ bracket_escape ( $name )] : preg_match ( " ~ \n \\ s* " . preg_quote ( idf_escape ( $name ), '~' ) . " timestamp.* on update CURRENT_TIMESTAMP~i " , $create ));
2008-10-01 11:39:13 +00:00
echo '<label><input type="checkbox" name="on_update[' . htmlspecialchars ( bracket_escape ( $name )) . ']" value="1"' . ( $checked ? ' checked="checked"' : '' ) . ' />' . lang ( 'ON UPDATE CURRENT_TIMESTAMP' ) . '</label>' ;
2007-07-25 17:10:03 +00:00
}
2007-07-10 20:26:17 +00:00
echo " </td></tr> \n " ;
2007-07-02 05:51:26 +00:00
}
2007-07-10 20:26:17 +00:00
echo " </table> \n " ;
2007-07-02 05:51:26 +00:00
}
?>
2007-07-09 06:12:22 +00:00
< p >
< input type = " hidden " name = " token " value = " <?php echo $token ; ?> " />
2008-10-01 11:39:13 +00:00
< ? php
if ( isset ( $_GET [ " select " ])) {
2008-10-09 13:23:54 +00:00
hidden_fields ( array ( " check " => ( array ) $_POST [ " check " ], " clone " => $_POST [ " clone " ], " all " => $_POST [ " all " ]));
echo " <input type='hidden' name='save' value='1' /> \n " ;
2008-10-01 11:39:13 +00:00
}
if ( $fields ) {
?>
2007-07-09 06:12:22 +00:00
< input type = " submit " value = " <?php echo lang('Save'); ?> " />
2008-10-01 11:39:13 +00:00
< ? php if ( ! isset ( $_GET [ " default " ]) && ! isset ( $_GET [ " select " ])) { ?> <input type="submit" name="insert" value="<?php echo ($update ? lang('Save and continue edit') : lang('Save and insert next')); ?>" /><?php } ?>
2007-07-09 06:12:22 +00:00
< ? php } ?>
2008-09-27 08:52:18 +00:00
< ? php if ( $update ) { ?> <input type="submit" name="delete" value="<?php echo lang('Delete'); ?>" onclick="return confirm('<?php echo lang('Are you sure?'); ?>');" /><?php } ?>
2007-07-09 06:12:22 +00:00
</ p >
2007-07-02 05:51:26 +00:00
</ form >