PageRenderTime 33ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/re-alpine-2.02/web/src/cgi.tcl-1.10/example/vote.cgi

#
TCL | 190 lines | 171 code | 14 blank | 5 comment | 2 complexity | 8df64a1b9427a8772f35a52f8013ffb8 MD5 | raw file
Possible License(s): Apache-2.0
  1. #!/depot/path/tclsh
  2. package require cgi
  3. cgi_eval {
  4. source example.tcl
  5. cgi_input
  6. cgi_title "Vote for a t-shirt design!"
  7. cgi_uid_check http
  8. set BarURL $EXPECT_ART/pink.gif
  9. set Q(filename) "$DATADIR/vote.cnt"
  10. proc votes_read {} {
  11. global Q
  12. set Q(Max) 0
  13. set Q(Votes) 0
  14. set Q(Indices) ""
  15. set fid [open $Q(filename) r]
  16. while {-1!=[gets $fid i]} {
  17. gets $fid buf
  18. foreach "Q(Votes,$i) Q(Entry,$i) Q(Name,$i) Q(Email,$i)" $buf {}
  19. lappend Q(Indices) $i
  20. set Q(Unvotable,$i) [catch {incr Q(Votes) $Q(Votes,$i)}]
  21. set Q(Max) $i
  22. }
  23. close $fid
  24. }
  25. proc votes_write {} {
  26. global Q
  27. # No file locking (or real database) handy so we can't guarantee that
  28. # simultaneous votes aren't dropped, but we'll at least avoid
  29. # corruption of the vote file by working on a private copy.
  30. set tmpfile $Q(filename).[pid]
  31. set fid [open $tmpfile w]
  32. foreach i $Q(Indices) {
  33. set data [list $Q(Votes,$i) $Q(Entry,$i) $Q(Name,$i) $Q(Email,$i)]
  34. # simplify votes_read by making each entry a single line
  35. regsub -all \n $data <br> data
  36. puts $fid $i\n$data
  37. }
  38. close $fid
  39. exec mv $tmpfile $Q(filename)
  40. }
  41. proc vote_other_show {} {
  42. global Q
  43. h3 "Other suggestions"
  44. table border=2 {
  45. table_row {
  46. th width=300 "Entry"
  47. th width=300 "Judge's Comments"
  48. }
  49. foreach i $Q(Indices) {
  50. if {!$Q(Unvotable,$i)} continue
  51. table_row {
  52. td width=300 "$Q(Entry,$i)"
  53. td width=300 "$Q(Votes,$i)"
  54. }
  55. }
  56. }
  57. }
  58. cgi_body {
  59. votes_read
  60. if {[regexp "Vote(\[0-9]+)" [cgi_import_list] dummy i]} {
  61. if {[catch {incr Q(Votes,$i)}]} {
  62. user_error "There is no such entry to vote for."
  63. }
  64. incr Q(Votes)
  65. votes_write
  66. h3 "Thanks for voting! See you at the next Tcl conference!"
  67. set ShowVotes 1
  68. }
  69. catch {cgi_import ShowVotes}
  70. if {[info exists ShowVotes]} {
  71. table border=2 {
  72. table_row {
  73. th width=300 "Entry"
  74. th "Votes"
  75. th width=140 "Percent" ;# 100 + room for pct
  76. }
  77. foreach i $Q(Indices) {
  78. if {!$Q(Unvotable,$i)} {
  79. table_row {
  80. td width=300 "$Q(Entry,$i)"
  81. td align=right "$Q(Votes,$i)"
  82. table_data width=140 {
  83. table {
  84. table_row {
  85. set pct [expr 100*$Q(Votes,$i)/$Q(Votes)]
  86. # avoid 0-width Netscape bug
  87. set pct_bar [expr $pct==0?1:$pct]
  88. td [img $BarURL align=left width=$pct_bar height=15]
  89. td $pct
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. form vote {
  98. submit_button "=Submit entry or vote"
  99. submit_button "ShowVotes=Show votes"
  100. }
  101. vote_other_show
  102. return
  103. }
  104. if 0==[catch {import Entry}] {
  105. if {[string length $Entry] == 0} {
  106. user_error "No entry found."
  107. }
  108. if {[string length $Entry] > 500} {
  109. user_error "Your entry is too long. Keep it under 500 characters!"
  110. }
  111. set Name ""
  112. catch {import Name}
  113. if 0==[string length $Name] {
  114. user_error "You must supply your name. How are we going to know who you are if you win?"
  115. }
  116. set Email ""
  117. catch {import Email}
  118. if 0==[string length $Email] {
  119. user_error "You must supply your email. How are we going to contact you if you win?"
  120. }
  121. set i [incr Q(Max)]
  122. set Q(Entry,$i) $Entry
  123. set Q(Name,$i) $Name
  124. set Q(Email,$i) $Email
  125. set Q(Votes,$i) 1
  126. lappend Q(Indices) $i
  127. votes_write
  128. h3 "Thanks for your new entry!"
  129. p "No need to go back and vote for it - a vote has already
  130. been recorded for you."
  131. form vote {
  132. submit_button "=Submit another entry or vote"
  133. submit_button "ShowVotes=Show votes"
  134. }
  135. return
  136. }
  137. p "Vote for what will go on the next Tcl conference t-shirt! (Feel free to vote for several entries.)"
  138. cgi_form vote {
  139. table border=2 {
  140. foreach i $Q(Indices) {
  141. if {$Q(Unvotable,$i)} continue
  142. table_row {
  143. table_data {
  144. cgi_submit_button Vote$i=Vote
  145. }
  146. td "$Q(Entry,$i)"
  147. }
  148. }
  149. }
  150. br
  151. cgi_submit_button "ShowVotes=Just show me the votes"
  152. hr
  153. p "The author of the winning entry gets fame and glory (and a free t-shirt)! Submit a new entry:"
  154. cgi_text Entry= size=80
  155. p "Entries may use embedded HTML. Images or concepts are fine - for artwork, we have the same artist who did the [url "'96 conference shirt" $MSID_STAFF/libes/t.html]). The [url judges mailto:tclchairs@usenix.org] reserve the right to delete entries. (Do us a favor and use common sense and good taste!)"
  156. puts "Name: "
  157. cgi_text Name=
  158. br
  159. puts "Email: "
  160. cgi_text Email=
  161. br
  162. cgi_submit_button "=Submit new entry"
  163. vote_other_show
  164. }
  165. }
  166. }