#!/usr/bin/perl

# $rcs = ' $Id: edit.cgi,v 1.2 1998/06/20 04:00:04 fitz Exp $ ' ;

unshift(@INC, "lib");

require 'bib.pl';
require 'web.pl';
require 'cgi-lib.pl';


&ReadParse; # Read the script input
print "Content-type: text/html\n";

&bib::load("CATEGORY", "KEYWORDS");

# Determine if we are running as an administrator
$admin = ($ENV{SCRIPT_NAME} =~ m!/admin/! ? 1 : 0);

# If the ID is specified as part of the URL,
# then we are editing an existing file
($id = $ENV{PATH_INFO}) =~ s!.*edit\.cgi/?!!;
if ($id)
{
    $heading = "Edit Bibliography Entry";

    &error("Invalid format for ID '$id'.")
      unless $id =~ /^\d+$/;

    &error("Editing allowed only for administrators.")
      unless $admin;

    %entry = &db::read_entry($id);
    &error("Error reading bibliography entry for ID '$id': $db::error")
      unless defined %entry;
}
else
{
    $heading = ($admin ?
                "Create Bibliography Entry" :
                "Submit Bibliography Entry");
    %entry = ();
}

# Get the categories
@categories = &bib::get_categories();
&error($bib::error) unless defined @categories;

# Get the keywords
@keywords = &bib::get_keywords(1);
&error($bib::error) unless defined @keywords;

# If they clicked the "Erase Changes",
# revert to the last saved
undef %in if $in{REVERT};

if (%in)
{
    # If the Delete button was pressed, redirect to another CGI
    if ($in{delete})
    {
        $url = "$bib::admin_cgi/delete.cgi/$id";
        print "Location: $url\n\n",
        "This page has been redirected to<P><a href=\"$url\">$url</a>\n";
        exit 0;
    }

    # If they entered a new category,
    # override the category selected in the list
    $in{CATEGORY} = $in{NEW_CATEGORY} if $in{NEW_CATEGORY};


    # If they selected multiple keywords,
    # convert it into a single string,
    # and include any additional keywords
    if ($in{KEYWORDS})
    {
        my(@k) = &splitMultiple($in{KEYWORDS});
        $in{KEYWORDS} = "@k";
    }
    $in{KEYWORDS} .= " $in{NEW_KEYWORDS}";

    # If the user is not an administrator, list the entry as
    # preliminary 
    unless ($admin)
    {
        $in{META} = "submit";

        &error("You must enter a submitter name and email address.")
          unless ($in{SUBMITNAME} && $in{SUBMITADDR});
    }

    if ($id2 = &bib::write_entry($id, %in))
    {
        if ($id)
        {
            &success("Wrote entry ID=$id<P>\n",
                     "<A HREF=$bib::cgi/display.cgi/$id>Display Entry</A>\n");
        }
        elsif ($admin)
        {
            &success("Added new entry ID=$id2<P>\n",
                     "<A HREF=$bib::cgi/display.cgi/$id2>Display Entry</A>\n");
        }
        else
        {
            $link = "http://$ENV{SERVER_NAME}$bib::cgi/display.cgi/$id2";

            &bib::sendmail("The following entry was submitted:" .
                           "\n$link\n$in{TITLE}\n",
                           "To: $bib::email{admin}",
                           "Bcc: $bib::email{webmaster}",
                           "From: $in{SUBMITADDR} ($in{SUBMITNAME})",
                           "Subject: submission to SKEPTIC bibliography",)
              || ($mail_error = "<P>Mail error: $bib::error");

            &success("Submitted new entry: it will not appear ",
                     "in the bibliography until an administrator verifies it.<P>\n",
                     "<A HREF=$bib::cgi/display.cgi/$id2>Display Entry</A>\n",
                     $mail_error,
                    );
        }
    }
    else
    {
        # Validation or write error.
        # Display the error, and redisplay the entry form
        $vars = "<FONT COLOR=red SIZE=4><STRONG>Errors:</STRONG>"
          . "<UL>$bib::error</UL></FONT>";
        %entry = %in;

        $revert = "<INPUT TYPE=submit NAME=REVERT VALUE=\"Revert Entry to Last Saved\">" if $id;
    }
}


# Construct a list of all current categories
# and make sure the correct one is selected
# if we are editing an existing entry
foreach $category (@categories)
{
    $select = ($category eq $entry{CATEGORY}) ? "SELECTED" : "";
    $categories .= "<OPTION $select>$category";
}

# Construct a list of all current keywords
# and make sure the correct ones are selected
# if we are editing an existing entry
foreach $keyword (@keywords)
{
    $select = "";
    $select = "SELECTED"
      if ($entry{KEYWORDS} =~ /\b$keyword\b/ &&
          $entry{KEYWORDS} !~ /\b$keyword:/);
    $keywords .= "<OPTION $select>$keyword";
}


# Make a copy of the entry, but untaint the values
# so the special HTML characters are escaped
%entry_untaint = &untaint_assoc(%entry);
$entry_untaint{CATEGORY} = $categories;
$entry_untaint{KEYWORDS} = $keywords;

$delete = "<INPUT TYPE=submit NAME=delete VALUE=\"Delete Entry...\">"
  if $admin && $id;
$newcat = "<INPUT NAME=NEW_CATEGORY VALUE=\"$entry_untaint{NEW_CATEGORY}\">"
  if $admin;
$revert = "<INPUT TYPE=reset VALUE=\"Erase Changes\">" unless $revert;

$data = &bib::template("edit.html",
                       %entry_untaint,
                       "HEADING", $heading,
                       "VARS", $vars,
                       "DELETE", $delete,
                       "NEW_CATEGORY", $newcat,
                       "REVERT", $revert,);

print "\n", &bib::template("basic.html",
                           "TITLE", "Edit Entry $id",
                           "DATA", $data);

exit 0;
