2017-06-21 16:14:41 +02:00
|
|
|
|
module ApplicationHelper
|
2024-02-23 10:42:23 +01:00
|
|
|
|
APP_HOST = ENV['APP_HOST']
|
|
|
|
|
APP_HOST_LEGACY = ENV['APP_HOST_LEGACY']
|
2024-06-03 09:20:56 +02:00
|
|
|
|
REGEXP_REPLACE_TRAILING_EXTENSION = /(\.\w+)+$/.freeze
|
|
|
|
|
REGEXP_REPLACE_WORD_SEPARATOR = /[\s_-]+/.freeze
|
2024-02-23 10:42:23 +01:00
|
|
|
|
|
|
|
|
|
def app_host_legacy?(request)
|
|
|
|
|
return false if APP_HOST_LEGACY.blank?
|
|
|
|
|
|
|
|
|
|
Regexp.new(APP_HOST_LEGACY).match?(request.base_url)
|
|
|
|
|
end
|
|
|
|
|
|
2024-02-26 05:41:01 +01:00
|
|
|
|
def auto_switch_domain?(request, user_signed_in)
|
|
|
|
|
switch_domain_enabled?(request) && !user_signed_in && app_host_legacy?(request)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def switch_domain_enabled?(request)
|
2024-05-15 10:10:37 +02:00
|
|
|
|
request.params.key?(:switch_domain) || Flipper.enabled?(:switch_domain, Current.user)
|
2024-02-26 05:41:01 +01:00
|
|
|
|
end
|
|
|
|
|
|
2022-04-21 10:28:23 +02:00
|
|
|
|
def html_lang
|
|
|
|
|
I18n.locale.to_s
|
|
|
|
|
end
|
|
|
|
|
|
2022-11-29 18:11:59 +01:00
|
|
|
|
def active_locale_link(locale)
|
|
|
|
|
link_to save_locale_path(locale:), {
|
|
|
|
|
method: :post,
|
|
|
|
|
class: "fr-translate__language fr-nav__link",
|
|
|
|
|
hreflang: locale,
|
|
|
|
|
lang: locale,
|
|
|
|
|
"aria-current": I18n.locale == locale ? "true" : nil
|
|
|
|
|
}.compact do
|
|
|
|
|
yield
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-11-14 16:28:02 +01:00
|
|
|
|
def flash_class(level, sticky: false, fixed: false)
|
2022-01-06 12:43:02 +01:00
|
|
|
|
class_names = []
|
|
|
|
|
|
|
|
|
|
case level
|
2018-11-14 16:28:02 +01:00
|
|
|
|
when 'notice'
|
2022-01-06 12:43:02 +01:00
|
|
|
|
class_names << 'alert-success'
|
2022-01-06 13:21:47 +01:00
|
|
|
|
when 'alert', 'error'
|
2022-01-06 12:43:02 +01:00
|
|
|
|
class_names << 'alert-danger'
|
2017-06-21 16:14:41 +02:00
|
|
|
|
end
|
2022-01-06 12:43:02 +01:00
|
|
|
|
|
2018-11-14 16:28:02 +01:00
|
|
|
|
if sticky
|
|
|
|
|
class_names << 'sticky'
|
|
|
|
|
end
|
|
|
|
|
if fixed
|
|
|
|
|
class_names << 'alert-fixed'
|
|
|
|
|
end
|
|
|
|
|
class_names.join(' ')
|
2017-06-21 16:14:41 +02:00
|
|
|
|
end
|
2017-06-22 16:45:57 +02:00
|
|
|
|
|
2022-08-30 16:02:59 +02:00
|
|
|
|
def flash_role(level)
|
|
|
|
|
return "status" if level == "notice"
|
|
|
|
|
|
|
|
|
|
'alert'
|
|
|
|
|
end
|
|
|
|
|
|
2017-06-22 16:45:57 +02:00
|
|
|
|
def current_email
|
2018-05-30 18:45:46 +02:00
|
|
|
|
current_user&.email ||
|
2019-08-06 11:02:54 +02:00
|
|
|
|
current_instructeur&.email ||
|
2023-08-25 22:53:30 +02:00
|
|
|
|
current_administrateur&.email ||
|
2023-09-04 12:07:04 +02:00
|
|
|
|
current_gestionnaire&.email
|
2017-06-22 16:45:57 +02:00
|
|
|
|
end
|
2018-03-08 13:54:26 +01:00
|
|
|
|
|
2018-08-21 15:43:06 +02:00
|
|
|
|
def staging?
|
2021-03-24 15:05:24 +01:00
|
|
|
|
Rails.application.config.ds_env == 'staging'
|
2018-08-21 15:43:06 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 15:00:35 +02:00
|
|
|
|
def contact_link(title, options = {})
|
2018-08-30 15:10:18 +02:00
|
|
|
|
tags, type, dossier_id = options.values_at(:tags, :type, :dossier_id)
|
|
|
|
|
options.except!(:tags, :type, :dossier_id)
|
|
|
|
|
|
2019-01-17 11:52:38 +01:00
|
|
|
|
params = { tags: tags, type: type, dossier_id: dossier_id }.compact
|
|
|
|
|
link_to title, contact_url(params), options
|
2018-08-29 15:00:35 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-03-08 13:54:26 +01:00
|
|
|
|
def root_path_for_profile(nav_bar_profile)
|
|
|
|
|
case nav_bar_profile
|
2019-08-06 11:02:54 +02:00
|
|
|
|
when :instructeur
|
|
|
|
|
instructeur_procedures_path
|
2018-03-08 13:54:26 +01:00
|
|
|
|
when :user
|
2018-06-27 14:47:02 +02:00
|
|
|
|
dossiers_path
|
2018-03-08 13:54:26 +01:00
|
|
|
|
else
|
|
|
|
|
root_path
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-04-10 15:49:13 +02:00
|
|
|
|
|
2020-06-03 14:55:36 +02:00
|
|
|
|
def root_path_info_for_profile(nav_bar_profile)
|
|
|
|
|
case nav_bar_profile
|
|
|
|
|
when :administrateur
|
2022-11-28 14:50:41 +01:00
|
|
|
|
[admin_procedures_path, t("admin", scope: "layouts.root_path_link_title")]
|
2023-09-04 12:07:04 +02:00
|
|
|
|
when :gestionnaire
|
|
|
|
|
[gestionnaire_groupe_gestionnaires_path, t("gestionnaire", scope: "layouts.root_path_link_title")]
|
2020-06-03 14:55:36 +02:00
|
|
|
|
when :instructeur
|
2022-11-28 14:50:41 +01:00
|
|
|
|
[instructeur_procedures_path, t("instructeur", scope: "layouts.root_path_link_title")]
|
2020-06-03 14:55:36 +02:00
|
|
|
|
when :user
|
2022-11-28 14:50:41 +01:00
|
|
|
|
[dossiers_path, t("user", scope: "layouts.root_path_link_title")]
|
2020-06-03 14:55:36 +02:00
|
|
|
|
else
|
2022-11-28 14:50:41 +01:00
|
|
|
|
[root_path, t("default", scope: "layouts.root_path_link_title")]
|
2020-06-03 14:55:36 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-04-10 15:49:13 +02:00
|
|
|
|
def try_format_date(date)
|
2020-08-31 13:47:27 +02:00
|
|
|
|
date.present? ? I18n.l(date, format: :long) : ''
|
2019-04-10 15:49:13 +02:00
|
|
|
|
end
|
|
|
|
|
|
2023-07-03 16:50:07 +02:00
|
|
|
|
def try_format_datetime(datetime, format: nil)
|
|
|
|
|
datetime.present? ? I18n.l(datetime, format:) : ''
|
2019-04-10 15:49:13 +02:00
|
|
|
|
end
|
2019-12-03 16:02:08 +01:00
|
|
|
|
|
2020-04-07 19:29:14 +02:00
|
|
|
|
def try_format_mois_effectif(etablissement)
|
2020-04-21 20:26:07 +02:00
|
|
|
|
if etablissement.entreprise_effectif_mois.present? && etablissement.entreprise_effectif_annee.present?
|
|
|
|
|
[etablissement.entreprise_effectif_mois, etablissement.entreprise_effectif_annee].join('/')
|
2020-04-07 19:29:14 +02:00
|
|
|
|
else
|
|
|
|
|
''
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-03-04 16:23:54 +01:00
|
|
|
|
def show_outdated_browser_banner?
|
2024-04-02 21:30:38 +02:00
|
|
|
|
!BrowserSupport.supported?(browser)
|
2020-03-04 16:23:54 +01:00
|
|
|
|
end
|
2022-06-23 11:47:59 +02:00
|
|
|
|
|
2022-09-07 14:52:51 +02:00
|
|
|
|
def external_link_attributes
|
|
|
|
|
{ target: "_blank", rel: "noopener noreferrer" }
|
|
|
|
|
end
|
2022-10-04 13:17:25 +02:00
|
|
|
|
|
2022-11-28 15:48:40 +01:00
|
|
|
|
def new_tab_suffix(title)
|
2023-03-22 12:48:53 +01:00
|
|
|
|
[title, I18n.t('utils.new_tab')].compact.join(' — ')
|
2022-11-28 15:48:40 +01:00
|
|
|
|
end
|
|
|
|
|
|
2022-10-04 13:17:25 +02:00
|
|
|
|
def download_details(attachment)
|
|
|
|
|
"#{attachment.filename.extension.upcase} – #{number_to_human_size(attachment.byte_size)}"
|
|
|
|
|
end
|
2023-11-27 13:56:13 +01:00
|
|
|
|
|
|
|
|
|
def dsfr_icon(classes, *options)
|
|
|
|
|
sm = options.include?(:sm)
|
|
|
|
|
mr = options.include?(:mr)
|
|
|
|
|
|
|
|
|
|
tag.span(class: class_names(classes, 'fr-icon--sm': sm, 'fr-mr-1v': mr),
|
|
|
|
|
"aria-hidden" => true)
|
|
|
|
|
end
|
2024-06-03 09:20:56 +02:00
|
|
|
|
|
|
|
|
|
def acronymize(str)
|
|
|
|
|
str.gsub(REGEXP_REPLACE_TRAILING_EXTENSION, '')
|
|
|
|
|
.split(REGEXP_REPLACE_WORD_SEPARATOR)
|
|
|
|
|
.map { |word| word[0].upcase }
|
|
|
|
|
.join
|
|
|
|
|
end
|
2017-06-21 16:14:41 +02:00
|
|
|
|
end
|