#!/usr/local/bin/perl 
#
# MODULE: hd-assign.pl 
# AUTHOR: Ron Shalhoup (ron@captech.com)
# PURPOSE: Generate and/or process form for viewing, prioritizing,
#          and assigning unassigned CallTracks.
#

require "./hd-lib.pl";

MAIN:
{
    # Get cgi data
    &ReadParse(%in);

    $priority = "";
    $comments = "";
    $keywords = "";
    $cclist   = "";

    $date = &GetDate;

    # If a CallTrack was selected, then we know to process form.
    if (defined($in{'CALLTRACK'})) {
        &process_unassigned_form;
    } else {         
        # If form was submitted without selecting a CallTrack, gather any
        # other data that may have been inputted so user won't need to 
        # retype it.
        if (defined($in{'PRIORITY'})) {
            $priority = $in{'PRIORITY'};
        }
        if (defined($in{'COMMENTS'})) {
            $comments = $in{'COMMENTS'};
        }
        if (defined($in{'KEYWORDS'})) {
            $keywords = $in{'KEYWORDS'};
        }
        if (defined($in{'CCLIST'})) {
            $cclist = $in{'CCLIST'};
        }
    }
    #Generate html form for prioritizing and assigning CallTracks
    print &PrintHeader;
    &Generate_HTML_Header("assign");
    &Generate_Unassigned_CallTrack_Table;
    print "Assigned Engineer:\n";
    &Generate_Engineer_PickList("ENGINEER",0);
    print "<P>\n";
    &Generate_Priority_PickList;
    &Generate_CC_TextInput;
    &Generate_Keyword_TextInput;
    &Generate_Comments_TextArea(3);
    print "<INPUT TYPE=SUBMIT VALUE=ASSIGN><BR>\n";
    &Generate_HTML_Footer;   
}

sub process_unassigned_form {

    # The value contained in the cgi variable CALLTRACK is the file 
    # name of the selected CallTrack.
    $fname = $in{'CALLTRACK'};
    if (-e "$DATADIR/$fname") {

        # Parse fields from file name.
    ($subject,$user,$id,$submitted_date) = (split(/\./, $fname));
        $submitter = $user;

        # If the subitter's mail address was not local and contained
        # "." that were transformed to "__" for file storage, convert
        # the "__" back to "." to have a valid mail address.
        $submitter =~ s/__/./g;

        # Generate a string containing fields from the form.
        $message =   "CallTrack ID:     $id\n" .
                     "Subject Keywords: $in{'KEYWORDS'}\n" .
                     "Priority:         $in{'PRIORITY'}\n" .
                     "Status:           ASSIGNED\n" .
                     "Date Submitted:   $submitted_date\n" .
                     "Submitted By:     $submitter\n" .
                     "Date Assigned:    $date\n" .
                     "Assigned To:      $in{'ENGINEER'}\n";
        if (defined($in{'CCLIST'})) {
            $message = $message . "CC:               $in{'CCLIST'}\n";
        }
        if (defined($in{'COMMENTS'}) && $in{'COMMENTS'}) {
            $message = $message . 
                       "\nOperator Comments:\n" .  
                       $in{'COMMENTS'};
        }
        $message = $message . 
                "\n------------------------------------------------------\n";
        $message = $message . "\nOriginal Mail Message:\n";
        open(INFILE, "<$DATADIR/$fname") || 
            die "Unable to open $DATADIR/$fname\n";

        # Read in file CallTrack file.
        while(<INFILE>) {
            if (/^<[^\/]/) {            # <HTML>,<BODY>,etc.
                $header = $header . $_;
            } elsif (/^<[\/]/) {        # </HTML>,</BODY>,etc.
                $footer = $footer . $_;
            } else {                    # Non-html codes; actual request
                $message = $message . $_; 
            }
        }
        close(INFILE);
        
        # Generate new file by adding assigned engineer to filename.
        $new_ctfile = \
          "$DATADIR/$subject.$user.$id.$date.$in{'ENGINEER'}.html";
        # Send notification to requestor, assigned engineer, and anyone 
        # specified on CC list.
        open(CTFILE, ">$new_ctfile");
        if (defined $in{'CCLIST'}) {
            open(MAILER, "|$MAILER -s 'CT#$id $subject' -c $in{'CCLIST'} \
                 $submitter $in{'ENGINEER'}");
        } else {
            open(MAILER, "|$MAILER -s 'CT#$id $subject' $submitter \
                 $in{'ENGINEER'}");
        }
        print CTFILE $header;
        print CTFILE $message;
        print CTFILE $footer;
        print MAILER $message;
        close(CTFILE);
        close(MAILER);
        # Generate new file by adding assigned engineer to filename.
        unlink("$DATADIR/$fname");
    }
}

sub Generate_Unassigned_CallTrack_Table {

    print "<TABLE BORDER=4 CELLPADDING=4>\n";
    print "<TH ALIGN=CENTER COLSPAN=4><H3>- OPEN CALLTRACKS -</H3>";
    
    print "<TR><TH ALIGN=LEFT>CT ID#<TH ALIGN=CENTER>Description
           <TH ALIGN=CENTER>Submitted By <TH ALIGN=CENTER>Date
           Submitted\n";

    # Gather all unassigned calltracks from data directory.
    # Unassigned CallTracks only contain 4 fields in filename.
    @filelist = `/bin/ls $DATADIR`;
    foreach $i (@filelist) {
        chop($i);
        ($subject,$user,$id,$date,$assigned) = split(/\./, $i);
        if (defined $assigned && $assigned ne "html") {
            next;
        } 
        $ct_open{$id} = "$subject.$user.$id.$date.html";
    }
    # Sort CallTracks by ID and display in a table with radio buttons
    # preceeding each CallTrack.
    for (sort {$a <=> $b} keys %ct_open) {
        ($subject,$user,$id,$date) = split(/\./, $ct_open{$_});
        $subject =~ tr/_/ /;
        $submitter = $user;
        # Display submitter's correct mail address.
        $submitter =~ s/__/./g;
        print "<TR><TD ALIGN=LEFT><INPUT TYPE=RADIO NAME=CALLTRACK 
               VALUE=$ct_open{$_}>\t\t$id <TD ALIGN=CENTER>
               <A HREF=\"$APP_DATADIR/$ct_open{$_}\">$subject </A>
               <TD ALIGN=CENTER>$submitter<TD ALIGN=CENTER>$date\n";
    }
    print "</TABLE>\n";
    print "<BR>\n";
}

sub Generate_Priority_PickList {
    # Allow administrator to assign a priority to the CallTrack.
    print "Select Priority:<BR>\n";
    print "<SELECT NAME=PRIORITY>\n";
    # If priority previously selected we want to remember that value and
    # set it accordingly.
    if (defined $in{'PRIORITY'}) {
        if (substr($priority,0,1) eq '1') {
            print "<OPTION SELECTED> 1-Requires Immediate Attention \
                   (System(s) Down)";
            print "<OPTION> 2-Urgent (Multiple Users Affected)";
            print "<OPTION> 3-High (One or More Users Affected)";
            print "<OPTION> 4-Low (One or More Users Affected)";
            print "<OPTION> 5-Non-urgent (can be scheduled)";
        } elsif (substr($priority,0,1) eq '2') {
            print "<OPTION> 1-Requires Immediate Attention (System(s) Down)";
            print "<OPTION SELECTED> 2-Urgent (Multiple Users Affected)";
            print "<OPTION> 3-High (One or More Users Affected)";
            print "<OPTION> 4-Low (One or More Users Affected)";
            print "<OPTION> 5-Non-urgent (can be scheduled)";
        } elsif (substr($priority,0,1) eq '3') {
            print "<OPTION> 1-Requires Immediate Attention (System(s) Down)";
            print "<OPTION> 2-Urgent (Multiple Users Affected)";
            print "<OPTION SELECTED> 3-High (One or More Users Affected)";
            print "<OPTION> 4-Low (One or More Users Affected)";
            print "<OPTION> 5-Non-urgent (can be scheduled)";
        } elsif (substr($priority,0,1) eq '4') {
            print "<OPTION> 1-Requires Immediate Attention (System(s) Down)";
            print "<OPTION> 2-Urgent (Multiple Users Affected)";
            print "<OPTION> 3-High (One or More Users Affected)";
            print "<OPTION SELECTED> 4-Low (One or More Users Affected)";
            print "<OPTION> 5-Non-urgent (can be scheduled)";
        } else {
            print "<OPTION> 1-Requires Immediate Attention (System(s) Down)";
            print "<OPTION> 2-Urgent (Multiple Users Affected)";
            print "<OPTION> 3-High (One or More Users Affected)";
            print "<OPTION> 4-Low (One or More Users Affected)";
            print "<OPTION SELECTED> 5-Non-urgent (can be scheduled)";
        }
    } else {
        print "<OPTION> 1-Requires Immediate Attention (System(s) Down)";
        print "<OPTION> 2-Urgent (Multiple Users Affected)";
        print "<OPTION> 3-High (One or More Users Affected)";
        print "<OPTION> 4-Low (One or More Users Affected)";
        print "<OPTION SELECTED> 5-Non-urgent (can be scheduled)";
    }
    print "</SELECT><P>\n";
}

sub Generate_CC_TextInput {
    print "List Other Parties to be CC'ed in Mail Message:<BR>\n";
    print "<INPUT TYPE=TEXT NAME=CCLIST VALUE=\"$cclist\" SIZE=60></INPUT><P>\n";
}

sub Generate_Keyword_TextInput {
    print "Enter Subject Heading Keywords:<BR>\n";
    print "<INPUT TYPE=TEXT NAME=KEYWORDS VALUE=\"$keywords\" SIZE=60></INPUT>\n<P>\n";
}

# End of File


