2022-12-09 16:16:26 +01:00
|
|
|
module Merged
|
|
|
|
class FormController < MergedController
|
|
|
|
|
|
|
|
def sendit
|
|
|
|
@section = Section.find( params[:section_id] )
|
|
|
|
@errors = { }
|
|
|
|
return if bot_alert
|
|
|
|
check_maths
|
|
|
|
verify_data
|
|
|
|
if( @errors.length > 0)
|
|
|
|
render :form
|
|
|
|
else
|
|
|
|
dispatch_form_data( )
|
|
|
|
redirect_to main_app.root_url , notice: @section.option("ok_message")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def dispatch_form_data
|
|
|
|
puts "Dispatch data"
|
|
|
|
data = {}
|
|
|
|
@section.cards.each do |card|
|
|
|
|
data[card.header] = params[card.header]
|
|
|
|
end
|
|
|
|
if(@section.has_option?("handler") )
|
|
|
|
puts "Sending data"
|
|
|
|
@section.option("handler").constantize.new.handle_form(@section, data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def verify_data
|
|
|
|
@section.cards.each do |card|
|
|
|
|
check_option( card , params[card.header])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def check_option(card , value)
|
|
|
|
return unless value.blank?
|
|
|
|
puts "Checking #{card.header} #{value}"
|
2022-12-09 17:03:09 +01:00
|
|
|
compulsory = card.option("compulsory") != "no"
|
2022-12-09 16:16:26 +01:00
|
|
|
return unless compulsory
|
|
|
|
# check different types
|
2022-12-09 17:03:09 +01:00
|
|
|
message = card.text
|
|
|
|
message = "May not be empty" if message.blank?
|
|
|
|
@errors[card.header] = message
|
2022-12-09 16:16:26 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_maths
|
|
|
|
key = params[:bot_fudder].to_i / 2
|
|
|
|
if( (2*key + 1).to_s != params[:challenge])
|
|
|
|
@errors[:challenge] = "Check the maths #{key.to_i} #{params[:challenge]}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def bot_alert
|
|
|
|
if @section.nil?
|
|
|
|
head :ok
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
key = params[:bot_fudder]
|
|
|
|
if key.to_i.to_s != key
|
|
|
|
head :ok
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|