#!/usr/bin/xotclsh
# $Id: FormsWithState.xotcl,v 1.1 2004/05/23 22:50:39 neumann Exp $
#
# Demo Program for Multi-Page Forms implemented via hidden
# Form Fields  -gn
#
package require XOTcl 1; namespace import -force xotcl::*
package require xotcl::actiweb::htmlPlace
package require xotcl::actiweb::webDocument
HtmlPlace receiver -port 8086

# The following class is used to define HTML-pages
Class HTMLPage -superclass HtmlDocument -parameter {
  {pageContent ""}
  {pageTitle ""}
  {pageHeader ""}
  endCmd
}

HTMLPage instproc init args {
  receiver exportObjs [self]
  next
}

# instproc default is evoked whenever an HTMLDocument object
# is called without a specific method.
HTMLPage instproc default args {
  my instvar content
  # creates HTML page content
  set content 
  append content \
      [my set pageTitle]  \
      [my set pageHeader] \n \
      [my set pageContent]  
  # substitutes all embedded commands and variables in content
  set content [subst $content]

  # An HTMLPage can provide a command, which is executed after 
  # the content is computed; this mechanism is used here for deleting 
  # the context object
  if {[my exists endCmd]} {
    eval [my set endCmd]
  }
  # next sends content to the client
  next
}

# The form state is kept in a context object. The method varname
# accesses variables from the context object or returns empty values.
HTMLPage instproc get varname {
  my instvar context
  if {[$context exists $varname]} {
    return [$context set $varname]
  } else {
    return ""
  }
}

# The final page will contain a table with all the variables in
# the context object
HTMLPage instproc printAll {} {
  my instvar context
  set result " 
            
                    Submission
             You submitted the following values:
    
           
\n"
  # creates a table row for each variable and 
  # it's value of the current context object
  foreach var [$context info vars] {
    append result  $var: \
       [$context set $var]
\n
  }
  return "$result
\n
"
} 

HTMLPage form1
form1 proc default {} {
  set values {}
  # process the form data from post method
  foreach a [my getFormData] {
    set name [$a set name]
    switch -glob $name {
      *.html {set nextPage $name}
      context {set context [$a set content]}
      default {lappend values $name [$a set content]}
    }
  }
  # save the form data in the context object
  foreach {name value} $values {
    $context set $name $value
  }
  if {![info exists nextPage]} {
    # We assume, we are called the first time...
    # Create a new context object and call the first page
    set context [Object new]
    set nextPage [self]/page1.html
  } 
  $nextPage set context $context
  # delegeate to the actual form page
  $nextPage default
}

HTMLPage form1/page1.html -pageTitle "First Page" -pageContent {

     

             Primary Contact information
              Please supply us with full details of 
              the person who will act as the
             person we will contact about this paper.

           
                    First Name:

                        Second Name:

}

HTMLPage form1/page2.html -pageTitle "Second Page" -pageContent {

     
             
                    Title and Autors
             Please supply full title of the paper and the contributing authors.

         
                    Title:

                        Authors:

}

###  submit.html ist the final page; the context object is destroyed here
HTMLPage form1/submit.html \
    -endCmd {[my set context] destroy} \
    -pageTitle "Third Page" -pageContent {
  [my printAll] 
 
 Many thanks for your submission!
    }

### provide a default page for the receiver
receiver proc default args {
  return {

Welcome to the paper provision page

  In order to fill out the form, press 
  here
  
  }
}

receiver startEventLoop 
