#######################################
### A R C H I V E R E C O R D S ###
### ###
### Modified 7 May 2000 ###
### ###
### Modified 31 May 2000 by LoisC ###
### ###
#######################################
# Create a new, blank file -- archive.db
#######################################
# In default.cfg, after
# Full Path and File name of the database file.
$db_file_name = $db_script_path . "/default.db";
# add
# Full Path and File name of the database file.
$db_archive_name = $db_script_path . "/archive.db";
# ********** You must use a counter field for the $db_key! ****************
##########################################
# In db.cgi, sub main, after
elsif ($in{'delete_records'}) { if ($per_del) { &delete_records; } else { &html_unauth; } }
# add
elsif ($in{'archive_search'}) { if ($per_admin) { &html_archive_search; } else { &html_unauth; } }
elsif ($in{'archive_form'}) { if ($per_admin) { &html_archive_form; } else { &html_unauth; } }
elsif ($in{'archive_records'}) { if ($per_admin) { &archive_records; } else { &html_unauth; } }
elsif ($in{'unarchive_search'}) { if ($per_admin) { &html_unarchive_search; } else { &html_unauth; } }
elsif ($in{'unarchive_form'}) { if ($per_admin) { &html_unarchive_form; } else { &html_unauth; } }
elsif ($in{'unarchive_records'}) { if ($per_admin) { &unarchive_records; } else { &html_unauth; } }
###############################################
# In db.cgi, add two new subroutines:
sub archive_records {
# --------------------------------------------------------
# archives a single or multiple records. First the routine goes thrrough the form input and makes sure there are some records to archive. It then goes
# through the database deleting each entry and marking it archived. If there are any keys not archived, an error message will be returned saying which keys
# were not found and not archived, otherwise the user will go to the success page.
my ($key, %archive_list, $rec_to_archive, @lines, $line, @data, $errstr, $succstr, $output);
$rec_to_archive = 0;
foreach $key (keys %in) { # Build a hash of keys to archive.
if ($in{$key} eq "archive") {
$archive_list{$key} = 1;
$rec_to_archive = 1;
}
}
if (!$rec_to_archive) {
&html_archive_failure("no records specified.");
return;
}
open (DB, "<$db_file_name") or &cgierr("error in archive_records. unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = ;
close DB;
LINE: foreach $line (@lines) {
if ($line =~ /^$/) { next LINE; }
if ($line =~ /^#/) { $output .= $line; next LINE; }
chomp ($line);
@data = &split_decode($line);
if ($archive_list{$data[$db_key_pos]}) {
$archive_list{$data[$db_key_pos]} = 0;
open (ARC, ">>$db_archive_name") or &cgierr("error in archive_record. unable to open database: $db_archive_name.\nReason: $!");
if ($db_use_flock) {
flock(ARC, 2) or &cgierr("unable to get exclusive lock on $db_archive_name.\nReason: $!");
}
print ARC "$line\n";
close ARC; # automatically removes file lock
}
else {
$output .= $line . "\n";
}
}
foreach $key (keys %archive_list) {
$archive_list{$key} ? # Check to see if any items weren't archived
($errstr .= "$key,") : # that should have been.
($succstr .= "$key,"); # For logging, we'll remember the ones we archived.
}
chop($succstr); # Remove trailing delimeter
chop($errstr); # Remove trailing delimeter
open (DB, ">$db_file_name") or &cgierr("error in archive_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
&auth_logging("archived records: $succstr") if ($auth_logging);
$errstr ? # Do we have an error?
&html_archive_failure($errstr) : # If so, then let's report go to the failure page,
&html_archive_success($succstr); # else, everything went fine.
}
sub unarchive_records {
# --------------------------------------------------------
# unarchives a single or multiple records. First the routine goes through the form input and makes sure there are some records to unarchive. It then goes
# through the database deleting each entry and marking it unarchived. If there are any keys not unarchived, an error message will be returned saying which keys
# were not found and not unarchived, otherwise the user will go to the success page.
my ($key, %archive_list, $rec_to_archive, @lines, $line, @data, $errstr, $succstr, $output);
$rec_to_archive = 0;
foreach $key (keys %in) { # Build a hash of keys to archive.
if ($in{$key} eq "unarchive") {
$archive_list{$key} = 1;
$rec_to_archive = 1;
}
}
if (!$rec_to_archive) {
&html_unarchive_failure("no records specified.");
return;
}
open (ARC, "<$db_archive_name") or &cgierr("error in archive_records. unable to open db file: $db_archive_name.\nReason: $!");
if ($db_use_flock) { flock(ARC, 1); }
@lines = ;
close ARC;
LINE: foreach $line (@lines) {
if ($line =~ /^$/) { next LINE; }
if ($line =~ /^#/) { $output .= $line; next LINE; }
chomp ($line);
@data = &split_decode($line);
if ($archive_list{$data[$db_key_pos]}) {
$archive_list{$data[$db_key_pos]} = 0;
open (DB, ">>$db_file_name") or &cgierr("error in archive_record. unable to open database: $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 "$line\n";
close DB; # automatically removes file lock
}
else {
$output .= $line . "\n";
}
}
foreach $key (keys %archive_list) {
$archive_list{$key} ? # Check to see if any items weren't archived
($errstr .= "$key,") : # that should have been.
($succstr .= "$key,"); # For logging, we'll remember the ones we archived.
}
chop($succstr); # Remove trailing delimeter
chop($errstr); # Remove trailing delimeter
open (ARC, ">$db_archive_name") or &cgierr("error in archive_records. unable to open db file: $db_archive_name.\nReason: $!");
if ($db_use_flock) {
flock(ARC, 2) or &cgierr("unable to get exclusive lock on $db_archive_name.\nReason: $!");
}
print ARC $output;
close ARC; # automatically removes file lock
&auth_logging("unarchived records: $succstr") if ($auth_logging);
$errstr ? # Do we have an error?
&html_unarchive_failure($errstr) : # If so, then let's report go to the failure page,
&html_unarchive_success($succstr); # else, everything went fine.
}
##############################################################
# In html.pl, sub footer, add
print qq!| Archive Records ! if ($per_admin);
print qq!| UnArchive Records ! if ($per_admin);
##############################################################
++++++++++++++++++++++
# If you are *NOT* using the "user-friendly html.pl" or "short/long display" mod, add the following to html.pl.
#### Subroutines for "user-friendly html.pl" and "short/long display" are below.
##########################################################
## Archiving ##
##########################################################
sub html_archive_search {
# --------------------------------------------------------
# The page is displayed when a user wants to archive records. First the user has to search the database to pick which records to archive. That's handled by this form.
&html_print_headers;
print qq|
$html_title: Search the Database for Deletion.
| $html_title: Search the Database for Archiving |
<$font_title>Search the Database for Archiving
<$font>Search the database for the records you wish to archive or list all:
|;
&html_footer; print qq| |
|;
}
sub html_archive_form {
# --------------------------------------------------------
# The user has searched the database for deletion and must now pick which records to archive from the records returned. This page should produce a checkbox with name=ID value=archive for each record.
# We have to do a little work to convert the array @hits that contains the search results to a hash for printing.
my ($status, @hits) = &query("mod");
my ($numhits) = ($#hits+1) / ($#db_cols+1);
my ($maxhits); $in{'mh'} ? ($maxhits = $in{'mh'}) : ($maxhits = $db_max_hits);
my (%tmp);
&html_print_headers;
print qq|
$html_title: Archive Record(s).
| $html_title: Archive Record(s) |
|;
if ($status ne "ok") { # There was an error searching!
print qq|Error: $status
|;
}
else {
print qq|<$font>
Check which records you wish to archive and then press "Archive Records":
Your search returned $db_total_hits matches.|;
if ($db_next_hits) {
print "
<$font>Pages: $db_next_hits";
}
# Go through each hit and convert the array to hash and send to html_record for printing. Also add a checkbox with name=key and value=archive.
print qq|
|;
}
sub html_archive_success {
# --------------------------------------------------------
# This page let's the user know that the records were successfully archived.
my $message = shift;
&html_print_headers;
print qq|
$html_title: Record(s) Archived.
| $html_title: Record(s) Archived. |
<$font_title>Record(s) archived
<$font>The following records were archived: '$message'. |;
&html_footer; print qq| |
|;
}
sub html_archive_failure {
# --------------------------------------------------------
# This page let's the user know that some/all of the records were not archived (because they were not found in the database).
# $errstr contains a list of records not archived.
my ($errstr) = $_[0];
&html_print_headers;
print qq|
$html_title: Error: Record(s) Not Archived.
| $html_title: Error: Record(s) Not Archived |
<$font_title>Error: Record(s) Not archived
<$font>The records with the following keys were not found in the database: <$font_red>'$errstr'. |;
&html_footer; print qq|
|
|;
}
sub html_unarchive_search {
# --------------------------------------------------------
# The page is displayed when a user wants to archive records. First the user has to search the database to pick which records to archive.
# That's handled by this form.
&html_print_headers;
print qq|
$html_title: Search the Database for Deletion.
| $html_title: Search the Database for Archiving |
<$font_title>Search the Database for Un-Archiving
<$font>Search the database for the records you wish to un-archive or list all:
|;
&html_footer;
print qq| |
|;
}
sub html_unarchive_form {
# --------------------------------------------------------
# The user has searched the database for deletion and must now pick which records to archive from the records returned. This page
# should produce a checkbox with name=ID value=archive for each record. We have to do a little work to convert the array @hits that contains
# the search results to a hash for printing.
$db_file_name = $db_archive_name;
my ($status, @hits) = &query("mod");
my ($numhits) = ($#hits+1) / ($#db_cols+1);
my ($maxhits); $in{'mh'} ? ($maxhits = $in{'mh'}) : ($maxhits = $db_max_hits);
my (%tmp);
&html_print_headers;
print qq|
$html_title: UnArchive Record(s).
| $html_title: UnArchive Record(s) |
|;
if ($status ne "ok") { # There was an error searching!
print qq|Error: $status
|;
}
else {
print qq|<$font>Check which records you wish to archive and then press "UnArchive Records":
Your search returned $db_total_hits matches.|;
if ($db_next_hits) {
print "
<$font>Pages: $db_next_hits";
}
# Go through each hit and convert the array to hash and send to html_record for printing. Also add a checkbox with name=key and value=archive.
print qq|
|;
}
sub html_unarchive_success {
# --------------------------------------------------------
# This page let's the user know that the records were successfully archived.
my $message = shift;
&html_print_headers;
print qq| $html_title: Record(s) UnArchived.
| $html_title: Record(s) UnArchived. |
<$font_title>Record(s) archived <$font>The following records were unarchived: '$message'. |;
&html_footer; print qq|
|
|;
}
sub html_unarchive_failure {
# --------------------------------------------------------
# This page let's the user know that some/all of the records were not archived (because they were not found in the database).
# $errstr contains a list of records not archived.
my ($errstr) = $_[0];
&html_print_headers;
print qq|
$html_title: Error: Record(s) Not UnArchived.
| $html_title: Error: Record(s) Not UnArchived |
<$font_title>Error: Record(s) Not archived <$font>The records with the following keys were not found in the archive: <$font_red>'$errstr'. |;
&html_footer; print qq|
|
|;
}
#########################################
## Archiving ##
# USE THE SUBROUTINES BELOW IF YOU ARE USING THE "USER-FRIENDLY HTML.PL" OR "SHORT/LONG DISPLAY" MOD.
#########################################
sub html_archive_search {
# --------------------------------------------------------
# The page is displayed when a user wants to archive records. First the user has to search the database to pick which records to archive. That's handled by this form.
$page_title = "Search the Database for Archiving";
&html_page_top;
$submit_button = "Search";
$reset_button = "Reset Form";
print qq|<$font>Search the database for the records you wish to archive or list all:
|;
print qq|
|;
&html_footer;
&html_page_bottom;
}
sub html_archive_form {
# --------------------------------------------------------
# The user has searched the database for deletion and must now pick which records to archive from the records returned. This page should produce a checkbox with name=ID value=archive for each record. We have to do a little work to convert the array @hits that contains the search results to a hash for printing.
my ($status, @hits) = &query("mod");
my ($numhits) = ($#hits+1) / ($#db_cols+1);
my ($maxhits); $in{'mh'} ? ($maxhits = $in{'mh'}) : ($maxhits = $db_max_hits);
my (%tmp);
$page_title = "Archive Record(s)";
&html_page_top;
$submit_button = "Archive Checked Record(s)";
$reset_button = "Reset Form";
if ($status ne "ok") { # There was an error searching!
print qq|<$font_error>Error: $status
|;
&html_footer;
&html_page_bottom;
}
else {
print qq| <$font>Check which records you wish to archive and then press "archive Records":
Your search returned $db_total_hits matches. |;
if ($db_next_hits) { print "
<$font>Pages: $db_next_hits"; }
print qq|
|;
if ($db_next_hits) { print "
<$font>Pages: $db_next_hits"; }
&html_footer;
&html_page_bottom;
}
}
sub html_archive_success {
# --------------------------------------------------------
# This page let's the user know that the records were successfully archived.
my $message = shift;
$page_title = "Record(s) Archived";
&html_page_top;
print qq| <$font>The following records were archived: '$message'.
|;
&html_footer;
&html_page_bottom;
}
sub html_archive_failure {
# --------------------------------------------------------
# This page let's the user know that some/all of the records were not archived (because they were not found in the database).
# $errstr contains a list of records not archived.
my ($errstr) = $_[0];
$page_title = "Record(s) Not Archived";
&html_error_page_top;
print qq| <$font>The records with the following keys were not found in the database: <$error_color>'$errstr'. |;
&html_footer;
&html_page_bottom;
}
sub html_unarchive_search {
# --------------------------------------------------------
# The page is displayed when a user wants to unarchive records. First the user has to search the database to pick which records to unarchive. That's handled by this form.
$page_title = "Search the Database for UnArchiving";
&html_page_top;
$submit_button = "Search";
$reset_button = "Reset Form";
print qq| <$font>
Search the database for the records you wish to unarchive or list all:
|;
print qq|
|;
&html_footer;
&html_page_bottom;
}
sub html_unarchive_form {
# --------------------------------------------------------
# The user has searched the database for deletion and must now pick which records to unarchive from the records returned. This page should produce a checkbox with name=ID value=unarchive for each record. We have to do a little work to convert the array @hits that contains the search results to a hash for printing.
$db_file_name = $db_archive_name;
my ($status, @hits) = &query("mod");
my ($numhits) = ($#hits+1) / ($#db_cols+1);
my ($maxhits); $in{'mh'} ? ($maxhits = $in{'mh'}) : ($maxhits = $db_max_hits);
my (%tmp);
$page_title = "unarchive Record(s)";
&html_page_top;
$submit_button = "Unarchive Checked Record(s)";
$reset_button = "Reset Form";
if ($status ne "ok") { # There was an error searching!
print qq|<$font_error>Error: $status
|;
&html_footer;
&html_page_bottom;
}
else {
print qq| <$font>Check which records you wish to unarchive and then press "unarchive Records":
Your search returned $db_total_hits matches. |;
if ($db_next_hits) { print "
<$font>Pages: $db_next_hits"; }
print qq|
|;
if ($db_next_hits) { print "
<$font>Pages: $db_next_hits"; }
&html_footer;
&html_page_bottom;
}
}
sub html_unarchive_success {
# --------------------------------------------------------
# This page let's the user know that the records were successfully unarchived.
my $message = shift;
$page_title = "Record(s) unarchived";
&html_page_top;
print qq| <$font>The following records were unarchived: '$message'.
|;
&html_footer;
&html_page_bottom;
}
sub html_unarchive_failure {
# --------------------------------------------------------
# This page let's the user know that some/all of the records were not unarchived (because they were not found in the database).
# $errstr contains a list of records not unarchived.
my ($errstr) = $_[0];
$page_title = "Record(s) Not unarchived";
&html_error_page_top;
print qq| <$font>The records with the following keys were not found in the database: <$error_color>'$errstr'. |;
&html_footer;
&html_page_bottom;
}