% 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:
| <%= 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?
%>
<% end %>
|