2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-07-31 16:20:49 +02:00
|
|
|
class ContactController < ApplicationController
|
2021-12-29 14:00:47 +01:00
|
|
|
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)
|
2024-07-30 19:14:04 +02:00
|
|
|
@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)
|
2024-07-30 19:14:04 +02:00
|
|
|
@form.user = current_user
|
2018-11-28 15:19:12 +01:00
|
|
|
end
|
|
|
|
|
2018-08-29 11:44:12 +02:00
|
|
|
def create
|
2024-07-30 19:14:04 +02:00
|
|
|
if direct_message?
|
|
|
|
create_commentaire!
|
2024-07-31 11:59:50 +02:00
|
|
|
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)
|
2024-06-13 13:27:32 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2024-07-31 16:29:45 +02:00
|
|
|
@form = ContactForm.new(contact_form_params)
|
2024-07-30 19:14:04 +02:00
|
|
|
@form.user = current_user
|
2024-06-13 13:27:32 +02:00
|
|
|
|
2024-07-30 19:14:04 +02:00
|
|
|
if @form.save
|
|
|
|
@form.create_conversation_later
|
2024-07-31 11:59:50 +02:00
|
|
|
flash.notice = t('.message_sent')
|
2024-07-24 17:18:02 +02:00
|
|
|
|
|
|
|
redirect_to root_path
|
2018-08-29 11:44:12 +02:00
|
|
|
else
|
2024-07-24 17:18:02 +02:00
|
|
|
flash.alert = @form.errors.full_messages
|
|
|
|
render @form.for_admin ? :admin : :index
|
2018-08-29 11:44:12 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2024-07-30 19:14:04 +02:00
|
|
|
def create_commentaire!
|
2019-01-21 17:36:36 +01:00
|
|
|
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]}"
|
2018-11-29 15:00:26 +01:00
|
|
|
}
|
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
|
|
|
|
|
2024-07-24 17:18:02 +02:00
|
|
|
def direct_message?
|
2024-07-30 19:14:04 +02:00
|
|
|
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
|
2024-07-30 19:14:04 +02:00
|
|
|
|
|
|
|
dossier&.messagerie_available?
|
2018-08-29 11:44:12 +02:00
|
|
|
end
|
|
|
|
|
2024-07-24 17:18:02 +02:00
|
|
|
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
|
2021-12-29 14:00:47 +01:00
|
|
|
|
|
|
|
def redirect_to_root
|
2022-07-28 12:31:55 +02:00
|
|
|
redirect_to root_path, alert: t('invisible_captcha.sentence_for_humans')
|
2021-12-29 14:00:47 +01:00
|
|
|
end
|
2024-07-24 17:18:02 +02:00
|
|
|
|
2024-07-31 16:20:49 +02:00
|
|
|
def contact_form_params
|
2024-07-30 19:14:04 +02:00
|
|
|
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)
|
2024-07-24 17:18:02 +02:00
|
|
|
else
|
|
|
|
params.permit(:dossier_id, tags: []) # prefilling form
|
|
|
|
end
|
|
|
|
end
|
2018-08-29 11:44:12 +02:00
|
|
|
end
|