2013-01-07 09:04:09 +01:00
class ConferenceRegistrationController < ApplicationController
before_filter :verify_user
def register
# TODO Figure out how to change the route's id from :id to :conference_id
@conference = Conference . find_all_by_short_title ( params [ :id ] ) . first
2013-07-11 07:43:52 +03:00
@workshops = @conference . events . where ( " require_registration = ? AND state LIKE ? " , true , 'confirmed' )
2013-01-07 09:04:09 +01:00
@person = current_user . person
2013-02-06 18:42:12 +01:00
if @person . first_name . blank? || @person . last_name . blank?
2013-02-06 18:43:10 +01:00
redirect_to ( edit_user_registration_path , :alert = > " Please fill in your first and last name before registering. " )
2013-02-06 18:42:12 +01:00
return
end
2013-01-07 09:04:09 +01:00
@registration = @person . registrations . where ( :conference_id = > @conference . id ) . first
@registered = true
if @registration . nil?
@registered = false
@registration = @person . registrations . new ( :conference_id = > @conference . id )
end
2013-02-02 16:43:47 +01:00
2013-02-06 07:23:35 +01:00
# Check if there's an existing SupporterRegistration for this email and link it when appropriate
@registration . supporter_registration || = @conference . supporter_registrations . where ( :email = > @person . email ) . first
2013-02-03 18:12:44 +01:00
@registration . supporter_registration || = SupporterRegistration . new ( :conference_id = > @conference . id )
2013-01-07 09:04:09 +01:00
end
2013-01-14 08:49:49 +01:00
# TODO this is ugly
2013-01-07 09:04:09 +01:00
def update
conference = Conference . find_all_by_short_title ( params [ :id ] ) . first
person = current_user . person
registration = person . registrations . where ( :conference_id = > conference . id ) . first
update_registration = true
2013-02-06 07:23:35 +01:00
# First verify that the supporter code is legit
if ! params [ :registration ] [ :supporter_registration_attributes ] . nil? && ! params [ :registration ] [ :supporter_registration_attributes ] [ :code ] . empty?
regs = conference . supporter_registrations . where ( :code = > params [ :registration ] [ :supporter_registration_attributes ] [ :code ] )
if regs . count != 0
if regs . where ( :email = > person . email ) . count == 0
redirect_to ( register_conference_path ( :id = > conference . short_title ) , :alert = > " This code is already in use. Please contact #{ conference . contact_email } for assistance. " )
return
end
end
end
2013-01-07 09:04:09 +01:00
begin
if registration . nil?
update_registration = false
person . update_attributes ( params [ :registration ] [ :person_attributes ] )
params [ :registration ] . delete :person_attributes
2013-02-06 07:23:35 +01:00
supporter_reg = params [ :registration ] [ :supporter_registration_attributes ]
params [ :registration ] . delete :supporter_registration_attributes
2013-01-07 09:04:09 +01:00
registration = person . registrations . new ( params [ :registration ] )
2013-02-06 18:12:13 +01:00
if conference . use_supporter_levels? && ! supporter_reg . nil?
if ! supporter_reg [ :id ] . blank?
# This means that their supporter registration was entered ahead of time, probably by an admin
registration . supporter_registration = SupporterRegistration . find ( supporter_reg [ :id ] )
if registration . supporter_registration . email != person . email
raise " Invalid code "
end
else
registration . supporter_registration = conference . supporter_registrations . new ( supporter_reg )
2013-02-06 07:23:35 +01:00
end
end
2013-01-07 09:04:09 +01:00
registration . conference_id = conference . id
registration . save!
else
registration . update_attributes! ( params [ :registration ] )
end
rescue Exception = > e
2013-02-06 07:23:35 +01:00
Rails . logger . debug e . backtrace . join ( " \n " )
2013-01-07 09:04:09 +01:00
redirect_to ( register_conference_path ( :id = > conference . short_title ) , :alert = > 'Registration failed:' + e . message )
return
end
redirect_message = " You are now registered. "
if update_registration
redirect_message = " Registration updated. "
2013-01-14 08:49:49 +01:00
else
2013-01-17 13:56:05 +01:00
Mailbot . registration_mail ( conference , current_user . person ) . deliver
2013-01-07 09:04:09 +01:00
end
redirect_to ( register_conference_path ( :id = > conference . short_title ) , :notice = > redirect_message )
end
def unregister
conference = Conference . find_all_by_short_title ( params [ :id ] ) . first
person = current_user . person
registration = person . registrations . where ( :conference_id = > conference . id ) . first
registration . destroy
redirect_to :root
end
end