2020-08-06 16:35:45 +02:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: champs
|
|
|
|
#
|
2021-02-04 19:23:40 +01:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# data :jsonb
|
|
|
|
# fetch_external_data_exceptions :string is an Array
|
2022-12-01 12:00:21 +01:00
|
|
|
# prefilled :boolean default(FALSE)
|
2021-02-04 19:23:40 +01:00
|
|
|
# private :boolean default(FALSE), not null
|
2021-10-20 17:26:09 +02:00
|
|
|
# rebased_at :datetime
|
2021-02-04 19:23:40 +01:00
|
|
|
# row :integer
|
|
|
|
# type :string
|
|
|
|
# value :string
|
2021-10-05 15:37:13 +02:00
|
|
|
# value_json :jsonb
|
2021-02-04 19:23:40 +01:00
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
2022-09-21 15:20:42 +02:00
|
|
|
# dossier_id :integer
|
2021-02-04 19:23:40 +01:00
|
|
|
# etablissement_id :integer
|
|
|
|
# external_id :string
|
|
|
|
# parent_id :bigint
|
2022-12-16 12:39:51 +01:00
|
|
|
# row_id :string
|
2022-09-21 15:20:42 +02:00
|
|
|
# type_de_champ_id :integer
|
2020-08-06 16:35:45 +02:00
|
|
|
#
|
2018-02-13 18:18:20 +01:00
|
|
|
class Champs::PhoneChamp < Champs::TextChamp
|
2022-03-01 16:42:46 +01:00
|
|
|
# We want to allow:
|
|
|
|
# * international (e164) phone numbers
|
|
|
|
# * “french format” (ten digits with a leading 0)
|
|
|
|
# * DROM numbers
|
|
|
|
#
|
|
|
|
# However, we need to special-case some ten-digit numbers,
|
|
|
|
# because the ARCEP assigns some blocks of "O6 XX XX XX XX" numbers to DROM operators.
|
|
|
|
# Guadeloupe | GP | +590 | 0690XXXXXX, 0691XXXXXX
|
|
|
|
# Guyane | GF | +594 | 0694XXXXXX
|
|
|
|
# Martinique | MQ | +596 | 0696XXXXXX, 0697XXXXXX
|
|
|
|
# Réunion | RE | +262 | 0692XXXXXX, 0693XXXXXX
|
|
|
|
# Mayotte | YT | +262 | 0692XXXXXX, 0693XXXXXX
|
|
|
|
# Nouvelle-Calédonie | NC | +687 |
|
|
|
|
# Polynésie française | PF | +689 | 40XXXXXX, 45XXXXXX, 87XXXXXX, 88XXXXXX, 89XXXXXX
|
|
|
|
#
|
|
|
|
# Cf: Plan national de numérotation téléphonique,
|
|
|
|
# https://www.arcep.fr/uploads/tx_gsavis/05-1085.pdf “Numéros mobiles à 10 chiffres”, page 6
|
|
|
|
#
|
|
|
|
# See issue #6996.
|
|
|
|
DEFAULT_COUNTRY_CODES = [:FR, :GP, :GF, :MQ, :RE, :YT, :NC, :PF].freeze
|
|
|
|
|
2020-07-29 15:45:25 +02:00
|
|
|
validates :value,
|
|
|
|
phone: {
|
|
|
|
possible: true,
|
|
|
|
allow_blank: true,
|
|
|
|
message: I18n.t(:not_a_phone, scope: 'activerecord.errors.messages')
|
2022-03-01 16:42:46 +01:00
|
|
|
}, unless: -> { Phonelib.valid_for_countries?(value, DEFAULT_COUNTRY_CODES) }
|
2021-06-10 15:43:45 +02:00
|
|
|
|
|
|
|
def to_s
|
2022-03-01 16:44:16 +01:00
|
|
|
return '' if value.blank?
|
2022-03-01 16:45:44 +01:00
|
|
|
|
|
|
|
if Phonelib.valid_for_countries?(value, DEFAULT_COUNTRY_CODES)
|
|
|
|
Phonelib.parse_for_countries(value, DEFAULT_COUNTRY_CODES).full_national
|
|
|
|
else
|
|
|
|
# When he phone number is possible for the default countries, but not strictly valid,
|
|
|
|
# `full_national` could mess up the formatting. In this case just return the original.
|
|
|
|
value
|
|
|
|
end
|
2021-06-10 15:43:45 +02:00
|
|
|
end
|
2018-02-13 18:18:20 +01:00
|
|
|
end
|