This is what I've come up with. I couldn't figure out how to go back to the search list, though. Sorry.
In your .cfg file, add the following:
Code:
$db_favorites = $db_script_path . "/favorite.db";
Create a blank file -- favorite.db -- in your directory. chmod to 666.
In db.cgi, with the other "elsif" statements, add
Code:
elsif ($in{'add_favorites'}) { if ($per_view) { &add_to_favorites; } else { &html_unauth; } }
elsif ($in{'view_favorites'}) { if ($per_view) { &view_favorites; } else { &html_unauth; } }
In db.cgi, add the following subroutines:
Code:
sub add_to_favorites {
#---------------------------------------
open (FAVE, "<$db_favorites") or &cgierr("error in add_to_favorites. unable to open favorites file: $db_favorites.
\nReason: $!");
if ($db_use_flock) { flock(FAVE, 1); }
LINE: while (<FAVE> ) {
(/^#/) and next LINE;
(/^\s*$/) and next LINE;
$line = $_; chomp ($line);
@data = &split_decode($line);
if ($data[0] eq "$in{$db_key}-$db_userid") {
$message = "That item is already in your favorites list";
}
}
close FAVE;
unless ($message) {
open (FAVE, ">>$db_favorites") or &cgierr("error in add_to_favorites. unable to open favorites file: $db_favorites.
\nReason: $!");
if ($db_use_flock) {
flock(FAVE, 2) or &cgierr("unable to get exclusive lock on $db_favorites.
\nReason: $!");
}
$date = &get_date();
print FAVE "$in{$db_key}-$db_userid|$date";
close FAVE; # automatically removes file lock
$message = "Item added to favorites list";
}
&html_favorite_success($message);
}
sub view_favorites {
# --------------------------------------------------------
open (FAVE, "<$db_favorites") or &cgierr("error in view_favorites. unable to open favorites file: $db_favorites.
\nReason: $!");
if ($db_use_flock) { flock(FAVE, 1); }
@flines = <FAVE>;
close FAVE;
foreach $fline (@flines) {
@data = &split_decode($fline);
if ($data[0] =~ /(.+)-$db_userid/) {
$in{$db_key} .= "$1|";
}
}
chop $in{$db_key};
$in{'re'} = 1;
$in{'ww'} = 1;
my ($status, @hits) = &query("view");
if ($status eq "ok") {
&html_view_success(@hits);
}
else {
&html_view_failure("No favorites on file");
}
}
In html.pl, sub footer, add
Code:
print qq!| <A HREF="$db_script_link_url&view_favorites=1">View Favorites</A> ! if ($per_view);
In html.pl, add the following subroutine:
Code:
sub html_favorite_success {
# --------------------------------------------------------
# This page let's the user know that the records were successfully
# deleted.
my $message = shift;
$page_title = "Favorite added";
&html_page_top;
# < -- Start page text -- >
print qq|
<$font>$message</font><P>
<$font>Please use the back button on your browser to return to your search results.</font>
|;
# < -- End page text -->
&html_footer;
&html_page_bottom;
}
(This is assuming you're using one or the other of my html.pl files. If not, you'll need to work out your own top and bottom of page formatting.)
In sub html_record, include a link
Code:
<a href="$db_script_link_url&$db_key=$rec{$db_key}&add_favorites=1">Add to your list of favorites</a>
The mod writes to a small database, which has only two fields --
the key, consisting of the $db_key value for the record and the userid
the date. (You didn't say you wanted the date, but I thought I'd go ahead and put it in there.)
When the user clicks the "View Favorites" link, the script looks through the "favorites.db" for anything with the userid as part of the field. When it finds the record, it takes out the part that is not part of the userid and adds it to a search term, with a |. It then sets
re=1, which causes the search to look for records whose key value is any one on the list.
At least that's what it's supposed to do. I haven't tested it.
------------------
JPD
[This message has been edited by JPDeni (edited August 29, 1999).]