demarches-normaliennes/app/controllers/contact_controller.rb

77 lines
2 KiB
Ruby
Raw Normal View History

2024-07-31 16:20:49 +02:00
class ContactController < ApplicationController
invisible_captcha only: [:create], on_spam: :redirect_to_root
2018-08-29 11:44:12 +02:00
def index
2024-07-31 16:20:49 +02:00
@form = ContactForm.new(tags: contact_form_params.fetch(:tags, []), dossier_id: dossier&.id)
@form.user = current_user
2018-08-29 11:44:12 +02:00
end
2018-11-28 15:19:12 +01:00
def admin
2024-07-31 16:20:49 +02:00
@form = ContactForm.new(tags: contact_form_params.fetch(:tags, []), for_admin: true)
@form.user = current_user
2018-11-28 15:19:12 +01:00
end
2018-08-29 11:44:12 +02:00
def create
if direct_message?
create_commentaire!
flash.notice = t('.direct_message_sent')
2018-08-29 11:44:12 +02:00
2018-09-27 17:14:00 +02:00
redirect_to messagerie_dossier_path(dossier)
return
end
@form = ContactForm.new(contact_form_params)
@form.user = current_user
if @form.save
@form.create_conversation_later
flash.notice = t('.message_sent')
redirect_to root_path
2018-08-29 11:44:12 +02:00
else
flash.alert = @form.errors.full_messages
render @form.for_admin ? :admin : :index
2018-08-29 11:44:12 +02:00
end
end
private
def create_commentaire!
attributes = {
2024-07-31 16:20:49 +02:00
piece_jointe: contact_form_params[:piece_jointe],
body: "[#{contact_form_params[:subject]}]<br><br>#{contact_form_params[:text]}"
}
2021-11-30 18:56:12 +01:00
CommentaireService.create!(current_user, dossier, attributes)
2018-08-29 11:44:12 +02:00
end
def browser_name
if browser.known?
"#{browser.name} #{browser.version} (#{browser.platform.name})"
end
end
def direct_message?
return false unless user_signed_in?
2024-07-31 16:20:49 +02:00
return false unless contact_form_params[:question_type] == ContactForm::TYPE_INSTRUCTION
dossier&.messagerie_available?
2018-08-29 11:44:12 +02:00
end
def dossier
2024-07-31 16:20:49 +02:00
@dossier ||= current_user&.dossiers&.find_by(id: contact_form_params[:dossier_id])
2018-08-29 11:44:12 +02:00
end
def redirect_to_root
redirect_to root_path, alert: t('invisible_captcha.sentence_for_humans')
end
2024-07-31 16:20:49 +02:00
def contact_form_params
keys = [:email, :subject, :text, :question_type, :dossier_id, :piece_jointe, :phone, :for_admin, tags: []]
if params.key?(:contact_form) # submitting form
params.require(:contact_form).permit(*keys)
else
params.permit(:dossier_id, tags: []) # prefilling form
end
end
2018-08-29 11:44:12 +02:00
end