#!/bin/perl

#  First line above should point to your system's perl compiler
####################################################################
#                   perl script SvyData.pl                         #
#                                                                  #
#                      2.0E Rev 5/27/98                            #
#                                                                  #
#   Captures raw html from form submit by POST into an .SDH file   #
#     Basic data save and acknowledgment, single questionnaire     #
#                                                                  #
#             Survey Pro and Net.collect sample file               #
#                         Apian Software                           #
####################################################################

######################## INSTALLATION ##############################

# In the exported Net.collect .htm file, make the FORM's ACTION=
# equal to this script's URL or filename as installed.  This must be in a
# format the Web Server can use to find this perl script.

# Set this savefile name to be the right .SDH file name for collecting
# the answers.  This is the server file pathname this perl script has
# permission to create, open and write.  If you use backslashes remember
# to do double \\.  Be sure to keep the >> so each set of answers are
# appended to the file.

   $savefile = ">> /www/administration/plant/survey/MySvy.sdh"  ;

# Set this CRLF to the correct newline on your server system so that a
# \r\n pair is emitted.  It is currently set to just "\n" for an
# NT server since it adds the \r itself.  UNIX is normally explicit "\r\n".

   $CRLF = "\n"  ;

# Modify the Thank You, Form Error and File Error replies below
# as needed to fit your application.

# Before attempting to submit forms over the web, run this script
# from the server's command line.  If the script has no syntax
# errors, the perl compiler link in line 1 is correct and the CRLF setting
# is correct, the Form Error message should appear on the console like:
#
#   Content-type: text/html
#
#   <HTML>
#   <HEAD>
#   <TITLE>Form Error</TITLE>
#      ... and so forth
#
# When testing the form submit from a browser, be sure the .HTM page
# was obtained from the web server with a URL.  The submit button
# does not work when browsers open the page directly as a file,
# but browsers give very misleading indications in this case.

# See the Net.collect User Guide Chapter 5 and the NCReadMe.wri
# for more on installation.  Application notes are available on
# www.apian.com for advanced hosting techniques.

####################### PROCESS ANSWERS ###########################

# Verify that POST was used submit a form's answers

if ($ENV{"REQUEST_METHOD"} eq "POST")
  {
   # Read the answers into a text string

   read(STDIN, $buffer, $ENV{"CONTENT_LENGTH"})  ;

   # Optionally append FSRC or FDOC fields here (advanced usage,
   # see documentation for why and how).

   # Append current date/time to form in .SDH file coding

   $buffer .= &time_stamp( )  ;

   # Terminate the record with a \r\n for Survey Pro's .SDH file

   $buffer .= $CRLF  ;

   # Validate the input and handle any errors if needed at this
   # point (see Part1.pl for an example of validation)

   # Append the latest answers to the .SDH savefile.  The open will
   # create the file if it does not exist yet on most systems.

   if (open(ANSWERS,  $savefile) &&
          print(ANSWERS  $buffer) &&
             close(ANSWERS))
     {
      # Acknowledge the responses were accepted OK.  Edit this to suit
      # your application or link to another URL (see Part1.pl for
      # a example of a URL anchor link).

      # This should be replaced with a &goto_url( ) when you design a
      # pretty web page with something like PageMill or FrontPage as
      # acknowledgement (see User Guide).

      &html_header("Thank You")  ;
      print "<HR><P>"  ;
      print $CRLF  ;
      print "Your answers have been collected for processing."  ;
      print $CRLF  ;
      print "<P><HR>"  ;
      print "Thanks for participating in this survey!"  ;
      print $CRLF  ;
      &html_trailer  ;
     }
     else
     {
      #  Or send a File Error to the browser if .SDH open/print/close fails

      &html_header("File Error")  ;
      print "<HR><P>"  ;
      print $CRLF  ;
      print "Please try sending again, or contact your system administrator"  ;
      print " and tell them SvyData.pl was unable to save in the .SDH file"  ;
      print " because $!."  ;
      &html_trailer  ;
     }
  }
  else
  {
   #  Or send a Form Error to the browser if the POST is invalid (this
   #  also happens running from the server command line):

   &html_header("Form Error")  ;
   print "<HR><P>"  ;
   print $CRLF  ;
   print "Please contact your system administrator.<BR>"  ;
   print "Tell them SvyData.pl did not receive a posted message."  ;
   &html_trailer  ;
  }

#  End of answer processing program.

################################################################
#  &html_header("Title String")  ;                             #
#  ... print HTML ...                                          #
#  &html_trailer  ;                                            #
#     Subroutines to generate an HTML page header and trailer  #
#     for an HTML page computed within this script             #
################################################################

sub html_header
  {$doc_title = $_[0]  ;
   print "Content-type: text/html"  ;
   # Not a misprint -- two lines are required here
   print $CRLF  ;
   print $CRLF  ;
   print "<HTML>"  ;
   print $CRLF  ;
   print "<HEAD>"  ;
   print $CRLF  ;
   print "<TITLE>$doc_title</TITLE>"  ;
   print $CRLF  ;

print "<script language=\"JavaScript\">" ;
print "      function CLOSEIT(){" ;
print "         self.close();" ;
print "      }" ;
print "</script>" ;

   print "</HEAD>"  ;
   print $CRLF  ;
   print "<BODY onUnload=\"CLOSEIT()\">"  ;
   print $CRLF  ;
   print "<H1>$doc_title</H1>"  ;
   print $CRLF  ;
   print "<P>"  ;
   print $CRLF  ;
  }

sub html_trailer
  {print "</BODY>"  ;
   print $CRLF  ;
   print "</HTML>"  ;
   print $CRLF  ;
  }

################################################################
#  &goto_url("next_absolute_url")  ;                           #
#     Sends the next URL as the next web page, which must a    #
#        relative or absolute URL accessible by the browser.   #
################################################################

sub goto_url
  {$ToLoc = $_[0]  ;
   print "Location: "  ;
   print $ToLoc  ;
   # Not a misprint -- two lines are required here
   print $CRLF  ;
   print $CRLF  ;
  }

################################################################
#  &time_stamp( )  ;                                           #
#     Returns current local date/time coded as .SDH data:      #
#         Formats date for import into US Windows settings     #
#         as mmm dd yy hh:mm.  Metric import settings would    #
#         be dd mm yy hh:mm.  Seconds are not included.        #
################################################################

sub time_stamp
  {$DT = "&FDT="  ;
   ($sec, $min, $hr, $day, $mon, $yr) = localtime(time)  ;
   $DT .= (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[$mon]  ;
   $DT .= "+" . $day . "+" . $yr . "+" . $hr . "%3A" . $min  ;
  }
