2007-07-02 05:51:26 +00:00
< ? php
$fields = fields ( $_GET [ " edit " ]);
2007-07-05 10:41:53 +00:00
$where = array ();
2007-07-06 13:48:25 +00:00
foreach (( array ) $_GET [ " where " ] as $key => $val ) {
$where [] = idf_escape ( $key ) . " = BINARY ' " . mysql_real_escape_string ( $val ) . " ' " ; //! enum and set
2007-07-05 10:41:53 +00:00
}
2007-07-06 13:48:25 +00:00
foreach (( array ) $_GET [ " null " ] as $key ) {
$where [] = idf_escape ( $key ) . " IS NULL " ;
2007-07-05 10:41:53 +00:00
}
2007-07-02 05:51:26 +00:00
if ( $_POST ) {
if ( isset ( $_POST [ " delete " ])) {
$query = " DELETE FROM " . idf_escape ( $_GET [ " edit " ]) . " WHERE " . implode ( " AND " , $where ) . " LIMIT 1 " ;
$message = lang ( 'Item has been deleted.' );
} else {
$set = array ();
2007-07-06 13:48:25 +00:00
foreach ( $_POST [ " fields " ] as $key => $val ) {
$name = bracket_escape ( $key , " back " );
$field = $fields [ $name ];
if ( preg_match ( '~char|text|set~' , $field [ " type " ]) ? $_POST [ " null " ][ $key ] : ! strlen ( $val )) {
$val = " NULL " ;
2007-07-02 05:51:26 +00:00
} elseif ( $field [ " type " ] == " enum " ) {
2007-07-06 13:48:25 +00:00
$val = intval ( $val );
2007-07-02 05:51:26 +00:00
} elseif ( $field [ " type " ] == " set " ) {
2007-07-06 13:48:25 +00:00
$val = array_sum (( array ) $val );
2007-07-02 05:51:26 +00:00
} else {
2007-07-06 13:48:25 +00:00
$val = " ' " . mysql_real_escape_string ( $val ) . " ' " ;
2007-07-02 05:51:26 +00:00
}
2007-07-06 13:48:25 +00:00
$set [] = idf_escape ( $name ) . " = $val " ;
2007-07-02 05:51:26 +00:00
}
if ( $where ) {
$query = " UPDATE " . idf_escape ( $_GET [ " edit " ]) . " SET " . implode ( " , " , $set ) . " WHERE " . implode ( " AND " , $where ) . " LIMIT 1 " ;
$message = lang ( 'Item has been updated.' );
} else {
$query = " INSERT INTO " . idf_escape ( $_GET [ " edit " ]) . " SET " . implode ( " , " , $set );
$message = lang ( 'Item has been inserted.' );
}
}
if ( mysql_query ( $query )) {
2007-07-04 14:37:54 +00:00
redirect ( $SELF . ( $_POST [ " insert " ] ? " edit= " : " select= " ) . urlencode ( $_GET [ " edit " ]), $message );
2007-07-02 05:51:26 +00:00
}
2007-07-02 16:42:42 +00:00
$error = mysql_error ();
2007-07-02 05:51:26 +00:00
}
page_header (( $_GET [ " where " ] ? lang ( 'Edit' ) : lang ( 'Insert' )) . " : " . htmlspecialchars ( $_GET [ " edit " ]));
if ( $_POST ) {
2007-07-02 16:42:42 +00:00
echo " <p class='error'> " . lang ( 'Error during saving' ) . " : " . htmlspecialchars ( $error ) . " </p> \n " ;
2007-07-02 05:51:26 +00:00
$data = $_POST [ " fields " ];
2007-07-06 13:48:25 +00:00
foreach ( $_POST [ " null " ] as $key => $val ) {
2007-07-02 05:51:26 +00:00
$data [ $key ] = null ;
}
} 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 ) {
2007-07-06 13:48:25 +00:00
if ( in_array ( " select " , $field [ " privileges " ]) && in_array (( $where ? " update " : " insert " ), $field [ " privileges " ])) {
$select [] = ( $field [ " type " ] == " enum " || $field [ " type " ] == " set " ? " 1* " . idf_escape ( $name ) . " AS " : " " ) . idf_escape ( $name );
2007-07-02 05:51:26 +00:00
}
}
2007-07-06 13:48:25 +00:00
$data = ( $select ? mysql_fetch_assoc ( mysql_query ( " SELECT " . implode ( " , " , $select ) . " FROM " . idf_escape ( $_GET [ " edit " ]) . " WHERE " . implode ( " AND " , $where ) . " LIMIT 1 " )) : array ());
2007-07-02 05:51:26 +00:00
} else {
$data = array ();
}
?>
< form action = " " method = " post " >
2007-07-06 13:48:25 +00:00
< table border = " 0 " cellspacing = " 0 " cellpadding = " 2 " >
2007-07-02 05:51:26 +00:00
< ? php
2007-07-04 14:37:54 +00:00
$types = types ();
2007-07-02 05:51:26 +00:00
foreach ( $fields as $name => $field ) {
2007-07-06 13:48:25 +00:00
if ( in_array (( $where ? " update " : " insert " ), $field [ " privileges " ])) {
echo " <tr><th> " . htmlspecialchars ( $name ) . " </th><td> " ;
$value = ( $data ? $data [ $name ] : $field [ " default " ]);
$name = htmlspecialchars ( bracket_escape ( $name ));
if ( $field [ " type " ] == " enum " ) {
echo '<input type="radio" name="fields[' . $name . ']" value="0"' . ( $value == " 0 " ? ' checked="checked"' : '' ) . ' />' ;
preg_match_all ( " ~'((?:[^']*|'')+)'~ " , $field [ " length " ], $matches );
foreach ( $matches [ 1 ] as $i => $val ) {
$id = " field- $name - " . ( $i + 1 );
echo ' <input type="radio" name="fields[' . $name . ']" id="' . $id . '" value="' . ( $i + 1 ) . '"' . ( $value == $i + 1 ? ' checked="checked"' : '' ) . ' /><label for="' . $id . '">' . htmlspecialchars ( str_replace ( " '' " , " ' " , $val )) . '</label>' ;
}
if ( $field [ " null " ]) {
$id = " field- $name - " ;
echo '<input type="radio" name="fields[' . $name . ']" id="' . $id . '" value=""' . ( strlen ( $value ) ? '' : ' checked="checked"' ) . ' /><label for="' . $id . '">' . lang ( 'NULL' ) . '</label> ' ;
}
} elseif ( $field [ " type " ] == " set " ) { //! 64 bits
preg_match_all ( " ~'((?:[^']*|'')+)'~ " , $field [ " length " ], $matches );
foreach ( $matches [ 1 ] as $i => $val ) {
$id = " $name - " . ( $i + 1 );
echo ' <input type="checkbox" name="fields[' . $name . '][]" id="' . $id . '" value="' . ( 1 << $i ) . '"' . (( $value >> $i ) & 1 ? ' checked="checked"' : '' ) . ' /><label for="' . $id . '">' . htmlspecialchars ( str_replace ( " '' " , " ' " , $val )) . '</label>' ;
}
} elseif ( strpos ( $field [ " type " ], " text " ) !== false ) {
echo '<textarea name="fields[' . $name . ']" cols="50" rows="12">' . htmlspecialchars ( $value ) . '</textarea>' ;
} else { //! binary
echo '<input name="fields[' . $name . ']" value="' . htmlspecialchars ( $value ) . '"' . ( strlen ( $field [ " length " ]) ? " maxlength=' $field[length] ' " : ( $types [ $field [ " type " ]] ? " maxlength=' " . $types [ $field [ " type " ]] . " ' " : '' )) . ' />' ;
2007-07-02 05:51:26 +00:00
}
2007-07-06 13:48:25 +00:00
if ( $field [ " null " ] && preg_match ( '~char|text|set~' , $field [ " type " ])) {
echo '<input type="checkbox" name="null[' . $name . ']" value="1" id="null-' . $name . '"' . ( isset ( $value ) ? '' : ' checked="checked"' ) . ' /><label for="null-' . $name . '">' . lang ( 'NULL' ) . '</label>' ;
2007-07-02 05:51:26 +00:00
}
2007-07-06 13:48:25 +00:00
echo " </td></tr> \n " ;
2007-07-02 05:51:26 +00:00
}
}
?>
</ table >
2007-07-06 13:48:25 +00:00
< p >< input type = " hidden " name = " sent " value = " 1 " /></ th >< td >< input type = " submit " value = " <?php echo lang('Save'); ?> " /> < input type = " submit " name = " insert " value = " <?php echo lang('Save and insert'); ?> " />< ? php if ( $where ) { ?> <input type="submit" name="delete" value="<?php echo lang('Delete'); ?>" /><?php } ?></p>
2007-07-02 05:51:26 +00:00
</ form >