1#!../../src/xotclsh
2# $Id: FormsWithState.xotcl,v 1.1 2004/05/23 22:50:39 neumann Exp $
3#
4# Demo Program for Multi-Page Forms implemented via hidden
5# Form Fields  -gn
6#
7package require XOTcl; namespace import -force xotcl::*
8package require xotcl::actiweb::htmlPlace
9package require xotcl::actiweb::webDocument
10HtmlPlace receiver -port 8086
11
12# The following class is used to define HTML-pages
13Class HTMLPage -superclass HtmlDocument -parameter {
14  {pageContent ""}
15  {pageTitle ""}
16  {pageHeader ""}
17  endCmd
18}
19
20HTMLPage instproc init args {
21  receiver exportObjs [self]
22  next
23}
24
25# instproc default is evoked whenever an HTMLDocument object
26# is called without a specific method.
27HTMLPage instproc default args {
28  my instvar content
29  # creates HTML page content
30  set content <HTML>
31  append content \
32      <TITLE>[my set pageTitle]</TITLE> <BODY> \
33      [my set pageHeader] \n \
34      [my set pageContent] </BODY> </HTML>
35  # substitutes all embedded commands and variables in content
36  set content [subst $content]
37
38  # An HTMLPage can provide a command, which is executed after 
39  # the content is computed; this mechanism is used here for deleting 
40  # the context object
41  if {[my exists endCmd]} {
42    eval [my set endCmd]
43  }
44  # next sends content to the client
45  next
46}
47
48# The form state is kept in a context object. The method varname
49# accesses variables from the context object or returns empty values.
50HTMLPage instproc get varname {
51  my instvar context
52  if {[$context exists $varname]} {
53    return [$context set $varname]
54  } else {
55    return ""
56  }
57}
58
59# The final page will contain a table with all the variables in
60# the context object
61HTMLPage instproc printAll {} {
62  my instvar context
63  set result " <TABLE BORDER='0' WIDTH='600' CELLSPACING='0' CELLPADDING='0'>
64   <TR><TD WIDTH='50'>&nbsp; <TD WIDTH='500' VALIGN='TOP'>
65     <TABLE BORDER='0' cellpadding='0' cellspacing='1'>
66     <TR> <TD COLSPAN=2 ALIGN='LEFT' VALIGN='TOP'>
67             <FONT SIZE='+1' COLOR='#000099'><B>Submission</B></font><br>
68             You submitted the following values:
69    </TD></TR> 
70     <TR > <TD>&nbsp;</TD> <TD>&nbsp;</TD></TR>\n"
71  # creates a table row for each variable and 
72  # it's value of the current context object
73  foreach var [$context info vars] {
74    append result <tr bgcolor='#DDDDDD'><td>$var:</td> \
75      <td>[$context set $var]</td></tr>\n
76  }
77  return "$result</table>\n</table>"
78} 
79
80HTMLPage form1
81form1 proc default {} {
82  set values {}
83  # process the form data from post method
84  foreach a [my getFormData] {
85    set name [$a set name]
86    switch -glob $name {
87      *.html {set nextPage $name}
88      context {set context [$a set content]}
89      default {lappend values $name [$a set content]}
90    }
91  }
92  # save the form data in the context object
93  foreach {name value} $values {
94    $context set $name $value
95  }
96  if {![info exists nextPage]} {
97    # We assume, we are called the first time...
98    # Create a new context object and call the first page
99    set context [Object new]
100    set nextPage [self]/page1.html
101  } 
102  $nextPage set context $context
103  # delegeate to the actual form page
104  $nextPage default
105}
106
107HTMLPage form1/page1.html -pageTitle "First Page" -pageContent {
108   <FORM METHOD="POST" action="form1">
109
110  <!-- assigns the current context object to HTML page-->
111   <INPUT TYPE="HIDDEN" NAME="context" VALUE="[my set context]">
112   <TABLE BORDER="0" WIDTH="600" CELLSPACING="0" CELLPADDING="0">
113   <TR><TD WIDTH="50">&nbsp;<!-- COLUMN FILLER --></TD>
114       <TD WIDTH="500" VALIGN="TOP">
115     <TABLE BORDER="0" cellpadding="0" cellspacing="0" width="500">
116     <TR> <TD COLSPAN=2 ALIGN="LEFT" VALIGN="TOP">
117             <FONT SIZE="+1" COLOR="#000099">
118             <B>Primary Contact information</B></font><br>
119              Please supply us with full details of 
120              the person who will act as the
121             person we will contact about this paper.
122          </TD>
123     </TR>
124     <TR> <TD>&nbsp;</TD> <TD>&nbsp;</TD></TR>
125     <TR> <TD ALIGN="LEFT" VALIGN="TOP">
126             <FONT SIZE="+1">First Name:<BR></FONT>
127  <!-- fills field FIRST_NAME with corresponding value from the context object-->
128             <INPUT TYPE="text" NAME="FIRST_NAME" 
129             VALUE="[my get FIRST_NAME]" SIZE=25 MAXLENGTH=30><BR>
130          </TD>
131          <TD ALIGN="LEFT" VALIGN="TOP">
132             <FONT SIZE="+1">Second Name:<BR>
133  <!--fills field SECOND_NAME with corresponding value from the context object-->
134             </FONT><INPUT TYPE="text" NAME="SECOND_NAME" 
135             VALUE="[my get SECOND_NAME]" SIZE=25 MAXLENGTH=30><BR>
136          </TD>
137     </TR>
138  <TR><TD ALIGN="LEFT" VALIGN="TOP">
139       <INPUT type="SUBMIT" name="form1/page2.html" VALUE="Second Page">
140      </TD>
141  </TR>
142  </TABLE>
143</TABLE>
144}
145
146
147HTMLPage form1/page2.html -pageTitle "Second Page" -pageContent {
148   <FORM METHOD="POST" action="form1">
149   <INPUT TYPE="HIDDEN" NAME="context" VALUE="[my set context]">
150   <TABLE BORDER="0" WIDTH="600" CELLSPACING="0" CELLPADDING="0">
151   <TR><TD WIDTH="50">&nbsp;
152       <TD WIDTH="500" VALIGN="TOP">
153     <TABLE BORDER="0" cellpadding="0" cellspacing="0" width="500">
154     <TR> <TD COLSPAN=2 ALIGN="LEFT" VALIGN="TOP">
155             <FONT SIZE="+1" COLOR="#000099"><B>Title and Autors</B></font><br>
156             Please supply full title of the paper and the contributing authors.
157          </TD>
158     </TR>
159     <TR><TD >&nbsp;</TD><TD>&nbsp;</TD></TR>
160     <TR> <TD ALIGN="LEFT" VALIGN="TOP">
161             <FONT SIZE="+1" >Title:<BR>
162  <!--fills field TITLE with corresponding value from the context object-->
163             </FONT><INPUT TYPE="text" NAME="TITLE" 
164               VALUE="[my get TITLE]" SIZE=25 MAXLENGTH=30><BR>
165          </TD>
166          <TD ALIGN="LEFT" VALIGN="TOP">
167             <FONT SIZE="+1" >Authors:<BR>
168  <!-- fills field AUTHORS with corresponding value from the context object-->
169             </FONT><INPUT TYPE="text" NAME="AUTHORS" 
170               VALUE="[my get AUTHORS]" SIZE=25 MAXLENGTH=30><BR>
171          </TD>
172     </TR>
173    
174  <TR>
175  <TD ALIGN="LEFT" VALIGN="TOP">
176       <INPUT type="SUBMIT" name="form1/page1.html" VALUE="Back to first Page">
177  </TD>
178  <TD ALIGN="LEFT" VALIGN="TOP">
179      <INPUT type="SUBMIT" name="form1/submit.html" VALUE="Submit">
180  </TD>
181  </TR>
182  </TABLE>
183</TABLE>
184}
185
186###  submit.html ist the final page; the context object is destroyed here
187HTMLPage form1/submit.html \
188    -endCmd {[my set context] destroy} \
189    -pageTitle "Third Page" -pageContent {
190  [my printAll] 
191 <p> Many thanks for your submission!</p> 
192    }
193
194### provide a default page for the receiver
195receiver proc default args {
196  return {
197  <HTML><BODY>
198  <center><h1>Welcome to the paper provision page</h1></center>
199  In order to fill out the form, press 
200  <a href="form1">here</a>
201  </BODY></HTML>
202  }
203}
204
205receiver startEventLoop 
206