2015-11-03 15:27:49 +01:00
|
|
|
class Champ < ActiveRecord::Base
|
2017-10-24 18:12:25 +02:00
|
|
|
belongs_to :dossier, touch: true
|
2015-11-05 11:21:44 +01:00
|
|
|
belongs_to :type_de_champ
|
2016-11-14 18:00:26 +01:00
|
|
|
has_many :commentaires
|
2016-03-15 17:17:56 +01:00
|
|
|
|
2016-08-09 16:21:39 +02:00
|
|
|
delegate :libelle, :type_champ, :order_place, :mandatory, :description, :drop_down_list, to: :type_de_champ
|
2016-03-15 17:17:56 +01:00
|
|
|
|
2017-06-09 12:04:16 +02:00
|
|
|
before_save :format_date_to_iso, if: Proc.new { type_champ == 'date' }
|
2017-07-27 20:54:02 +02:00
|
|
|
before_save :serialize_datetime_if_needed, if: Proc.new { type_champ == 'datetime' }
|
|
|
|
before_save :multiple_select_to_string, if: Proc.new { type_champ == 'multiple_drop_down_list' }
|
|
|
|
|
2018-01-11 19:08:04 +01:00
|
|
|
after_save :internal_notification, if: Proc.new { dossier.present? }
|
2016-12-26 11:47:51 +01:00
|
|
|
|
2017-10-05 16:10:00 +02:00
|
|
|
scope :updated_since?, -> (date) { where('champs.updated_at > ?', date) }
|
|
|
|
|
2016-03-15 17:17:56 +01:00
|
|
|
def mandatory?
|
|
|
|
mandatory
|
|
|
|
end
|
2016-06-09 12:08:18 +02:00
|
|
|
|
|
|
|
def data_provide
|
2017-06-09 11:45:50 +02:00
|
|
|
return 'datepicker' if (type_champ == 'datetime') && !(BROWSER.value.chrome? || BROWSER.value.edge?)
|
2016-06-09 12:08:18 +02:00
|
|
|
return 'typeahead' if type_champ == 'address'
|
|
|
|
end
|
2016-06-22 11:25:41 +02:00
|
|
|
|
|
|
|
def data_date_format
|
2017-06-09 11:45:50 +02:00
|
|
|
('dd/mm/yyyy' if type_champ == 'datetime')
|
2016-06-22 11:25:41 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def same_hour? num
|
|
|
|
same_date? num, '%H'
|
|
|
|
end
|
|
|
|
|
|
|
|
def same_minute? num
|
|
|
|
same_date? num, '%M'
|
|
|
|
end
|
|
|
|
|
2017-03-29 13:37:07 +02:00
|
|
|
def mandatory_and_blank?
|
|
|
|
mandatory? && value.blank?
|
|
|
|
end
|
|
|
|
|
2016-06-22 11:25:41 +02:00
|
|
|
def same_date? num, compare
|
2018-01-11 19:08:04 +01:00
|
|
|
if type_champ == 'datetime' && value.present?
|
2016-06-22 11:25:41 +02:00
|
|
|
if value.to_datetime.strftime(compare) == num
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
2016-10-12 17:22:56 +02:00
|
|
|
|
|
|
|
def self.regions
|
2016-12-26 11:47:51 +01:00
|
|
|
JSON.parse(Carto::GeoAPI::Driver.regions).sort_by { |e| e['nom'] }.inject([]) { |acc, liste| acc.push(liste['nom']) }
|
2016-10-12 17:22:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.departements
|
2016-12-26 11:47:51 +01:00
|
|
|
JSON.parse(Carto::GeoAPI::Driver.departements).inject([]) { |acc, liste| acc.push(liste['code'] + ' - ' + liste['nom']) }.push('99 - Étranger')
|
2016-10-12 17:22:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.pays
|
2016-12-26 11:47:51 +01:00
|
|
|
JSON.parse(Carto::GeoAPI::Driver.pays).inject([]) { |acc, liste| acc.push(liste['nom']) }
|
|
|
|
end
|
|
|
|
|
2017-10-16 17:39:34 +02:00
|
|
|
def to_s
|
|
|
|
if value.present?
|
|
|
|
case type_champ
|
|
|
|
when 'date'
|
|
|
|
Date.parse(value).strftime('%d/%m/%Y')
|
2017-11-23 17:38:10 +01:00
|
|
|
when 'multiple_drop_down_list'
|
|
|
|
drop_down_list.selected_options_without_decorator(self).join(', ')
|
2017-10-16 17:39:34 +02:00
|
|
|
else
|
|
|
|
value.to_s
|
|
|
|
end
|
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-26 16:54:10 +02:00
|
|
|
def for_export
|
|
|
|
if value.present?
|
|
|
|
case type_champ
|
|
|
|
when 'textarea'
|
|
|
|
ActionView::Base.full_sanitizer.sanitize(value)
|
2017-10-26 17:45:35 +02:00
|
|
|
when 'yes_no'
|
2017-11-21 10:28:21 +01:00
|
|
|
value == 'true' ? 'oui' : 'non'
|
2017-10-27 10:01:53 +02:00
|
|
|
when 'multiple_drop_down_list'
|
|
|
|
drop_down_list.selected_options_without_decorator(self).join(', ')
|
2017-10-26 16:54:10 +02:00
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-26 11:47:51 +01:00
|
|
|
private
|
|
|
|
|
2017-06-09 12:04:16 +02:00
|
|
|
def format_date_to_iso
|
|
|
|
date = begin
|
2017-08-01 16:40:43 +02:00
|
|
|
Date.parse(value).iso8601
|
2017-06-09 12:04:16 +02:00
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
self.value = date
|
|
|
|
end
|
|
|
|
|
2017-07-27 20:54:02 +02:00
|
|
|
def serialize_datetime_if_needed
|
|
|
|
if (value =~ /=>/).present?
|
|
|
|
date = begin
|
2017-08-29 10:38:48 +02:00
|
|
|
hash_date = YAML.safe_load(value.gsub('=>', ': '))
|
|
|
|
year, month, day, hour, minute = hash_date.values_at(1,2,3,4,5)
|
|
|
|
DateTime.new(year, month, day, hour, minute).strftime("%d/%m/%Y %H:%M")
|
2017-07-27 20:54:02 +02:00
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
self.value = date
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-26 11:47:51 +01:00
|
|
|
def internal_notification
|
2017-12-04 16:17:15 +01:00
|
|
|
if dossier.state != 'brouillon'
|
2017-09-27 15:08:34 +02:00
|
|
|
if type == 'ChampPublic'
|
|
|
|
NotificationService.new('champs', self.dossier.id, self.libelle).notify
|
|
|
|
else
|
|
|
|
NotificationService.new('annotations_privees', self.dossier.id, self.libelle).notify
|
|
|
|
end
|
2016-12-26 14:38:00 +01:00
|
|
|
end
|
2016-10-12 17:22:56 +02:00
|
|
|
end
|
2017-07-27 20:54:02 +02:00
|
|
|
|
|
|
|
def multiple_select_to_string
|
|
|
|
if value.present?
|
|
|
|
json = JSON.parse(value)
|
|
|
|
if json == ['']
|
|
|
|
self.value = nil
|
|
|
|
else
|
|
|
|
json = json - ['']
|
|
|
|
self.value = json.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-11-03 10:48:40 +01:00
|
|
|
end
|