sub modify_mult_record { # -------------------------------------------------------- # This routine will update multiple records at once. It expects # to find in %in a series of records to update. They will be of the # form field_name-key. # my ($key, %modify_list, %modify_rec, $rec_to_modify, @data, $key, $errstr, $succstr, $output, %errors); ### new line 20151219 ########################################################## my (&modify_list2); ######################################################### # First let's pick which records to modify and then separate them and store # them in their own hashes. $rec_to_modify = 0; foreach $key (keys %in) { # Build a hash of keys to modify. if ($in{$key} eq "modify") { $modify_list{$key} = 1; $rec_to_modify = 1; } ($key =~ /^(.*)-(.+)$/) and (${$modify_rec{$2}}{$1} = $in{$key}); } # Choke if we don't have anything to do. $rec_to_modify or (&html_modify_failure("no records specified.") and return); ### new line 20151219 &modify_list2 = &modify_list; #### open (DB, "<$db_file_name") or &cgierr("error in modify_records. unable to open db file: $db_file_name.\nReason: $!"); if ($db_use_flock) { flock(DB, 1); } LINE: while () { (/^#/) and ($output .= $_ and next LINE); (/^\s*$/) and next LINE; chomp; @data = &split_decode($_); $key = $data[$db_key_pos]; # Now we check if this record is something we want to modify. If so, then # we make sure the new record is ok, if so we replace it. if ($modify_list{$key}) { $status = &validate_record(%{$modify_rec{$key}}); if ($status eq "ok") { $output .= &join_encode(%{$modify_rec{$key}}); $modify_list{$key} = 0; } else { $errors{$key} = $status; $output .= "$_\n"; } } else { $output .= "$_\n"; } } close DB; # Reprint out the database. open (DB, ">$db_file_name") or &cgierr("error in modify_records. unable to open db file: $db_file_name.\nReason: $!"); if ($db_use_flock) { flock(DB, 2) or &cgierr("unable to get exclusive lock on $db_file_name.\nReason: $!"); } print DB $output; close DB; # automatically removes file lock # Let's display an error message if we were unable to modify a record # for some reason. foreach $key (keys %modify_list) { if ($modify_list{$key}) { ($errors{$key}) ? ($errstr .= "$key: $errors{$key}") : ($errstr .= "$key: not found"); } else { $succstr .= qq~$key,~; } } chop($succstr); # Remove trailing delimeter ########## new lines 20151219 ############################################################# if ($db_after_modify_record) { my ($db2_file_name) = "$db_after_add_db"; ## i don't understand this but if it works for adding ## it should work here my ($output2); open (DB, "<$db2_file_name") or &cgierr("error in modify_records. unable to open db file: $db2_file_name.\nReason: $!"); if ($db_use_flock) { flock(DB, 1); } LINE: while () { (/^#/) and ($output2 .= $_ and next LINE); (/^\s*$/) and next LINE; chomp; @data = &split_decode($_); $key = $data[$db_key_pos]; ##### 20151219 change this if key pos not same in db and db2!!! # Now we check if this record is something we want to modify. If so, then # we make sure the new record is ok, if so we replace it. if ($modify_list2{$key}) { # not validating records in db2 $output2 .= &join_encode(%{$modify_rec{$key}}); $modify_list2{$key} = 0; } else { $output2 .= "$_\n"; } } close DB; # Reprint out the database. open (DB, ">$db2_file_name") or &cgierr("error in modify_records. unable to open db file: $db2_file_name.\nReason: $!"); if ($db_use_flock) { flock(DB, 2) or &cgierr("unable to get exclusive lock on $db2_file_name.\nReason: $!"); } print DB $output2; close DB; # automatically removes file lock } #################################################### &html_modify_mult_results($succstr, $errstr); }