<% BASE = "/Library/Sites/tonybenda.com" $: << File.join(BASE, "public") require "tb.rb" require "cgi" require 'net/smtp' LOG = File.join(BASE, "registrations", "register.log") class Details attr_reader :name, :address, :phone, :email attr_reader :will_golf, :teetime, :partners attr_reader :will_party def Details.from_cgi(cgi) details = Details.new details.from_cgi(cgi) details end def from_cgi(cgi) @name = cgi.val("name").strip @phone = cgi.val("phone").strip @address = cgi.val("address").strip @email = cgi.val("email").strip @will_golf = cgi.val("willgolf") == "on" @teetime = cgi.val("teetime") @partners = cgi.val("partners").strip @will_party = cgi.val("willparty") == "on" end def initialize @name = "" @address = "" @phone = "" @email = "" @teetime = "don't care" @partners = "" @will_golf = false @will_party = true end def error_list errs = [] errs << "No name was entered" if @name.empty? if @phone.empty? && @email.empty? errs << "We need to have either a phone number or an email address" end errs end def to_s [ @name, @phone, @email, @address, @will_golf, @teetime, @partners, @will_party]. map {|v| v.inspect }. join(",") + "\n" end end def checked(flag) flag ? "checked" : "" end def say_thank_you puts %{

Thank you! We've received your registration.

Click here to return to the top-level page.

} #' end def send_email(details) msg = %{From: dave@thomases.com To: juliet@pragprog.com Subject: Golf Registration - #{details.name} Date: #{Time.now.to_s} Name: #{details.name} Phone: #{details.phone} EMail: #{details.email} Address: #{details.address.split("\n").join("\n ")} } if details.will_golf msg << "GOLF: YES, Tee time: #{details.teetime}\n" if details.partners.empty? msg << " No team preference" else msg << "Team pref: " << details.partners.split("\n").join("\n ") msg << "\n" end else msg << "GOLF: NO" end msg << "\n" if details.will_party msg << "PARTY: YES\n" else msg << "PARTY: NO\n" end # puts "
#{msg}
" open("|/usr/sbin/sendmail -oi -f dave@thomases.com juliet@thomases.com", "w") do |sm| sm.write msg end end def log_registration(details) File.open(LOG, "a") {|f| f.puts Time.now.strftime("%Y-%m-%d") + "," + details.to_s} end def show_greeting puts %{

If you're planning to come to either the golf or the party, please register below. This will let us plan for food, and help us make up the golf teams. If there are other folks you'd like on your team, put their names down in the form too.

The entry fee for the golf tournament is $75, which covers everything: golf, cart, prizes, after party food, drinks, and all the fun. If you're not golfing, then the party is free.

} end #' def report_errors(errs) puts %{

I'm sorry, but it looks like some information was missing from your registration:

" end cgi = CGI.new("html4") def cgi.val(name) if has_key?(name) CGI.escapeHTML(self[name][0]) else "" end end if cgi.has_key?("register") details = Details.from_cgi(cgi) errs = details.error_list first_time = false else details = Details.new errs = [] first_time = true end # puts cgi.header("Cache-Control" => "no-cache", # "Pragma" => "no-cache") new_page("2008 Tony Benda Golf Classic", "tonyteesmall.jpg", "2008 Tony Benda Golf Classic", "golf2008.png") subtitle("Hit some balls, remember some good times...") %>
<%= nav_menu %>  

Registration

<% # puts "
"
#  cgi.keys.each do |k|
#   puts "#{k} = #{cgi[k][0]}"
#  end
#  puts "
" if first_time show_greeting else if errs.empty? log_registration(details) send_email(details) say_thank_you else report_errors(errs) end end %> <% if first_time || !errs.empty? %>

1. All about you...

Name:
Address:
Phone:
E-mail:

2. Golf Tournament

name="willgolf"> Yes! I'd like to play in the tournament.
Preferred tee time: <% ["don't care", "9-10am", "10-11am", "11-12am"].each do |tee| puts %{ #{tee}
} end %>
We'll be playing in teams of four. If there are folks you'd like to play with, please give their names below. (We can't guarantee to match everyone up, but we'll try.)

3. After-Golf Party

name="willparty"> Yes! I'll be coming to the party.

4. Finally...

Remember to push the button below to complete your registration.

<% end %>