diff --git a/app/controllers/admin/communication/websites/agenda/events_controller.rb b/app/controllers/admin/communication/websites/agenda/events_controller.rb index c26e6d88adaeb30c8ac9fdacb53d4e92db4a3f8a..030d725190074a52a4dc2abb2207aa52defc4d10 100644 --- a/app/controllers/admin/communication/websites/agenda/events_controller.rb +++ b/app/controllers/admin/communication/websites/agenda/events_controller.rb @@ -86,7 +86,7 @@ class Admin::Communication::Websites::Agenda::EventsController < Admin::Communic .permit( :title, :subtitle, :meta_description, :summary, :published, :slug, :featured_image, :featured_image_delete, :featured_image_infos, :featured_image_alt, :featured_image_credit, - :from_day, :from_hour, :to_day, :to_hour, + :from_day, :from_hour, :to_day, :to_hour, :time_zone, category_ids: [] ) .merge( diff --git a/app/helpers/admin/application_helper.rb b/app/helpers/admin/application_helper.rb index 87171b052b8e88789729523f1c3b825a7408a56c..036dfebf5ea17e00fe6ab28cc2e516cf06c65335 100644 --- a/app/helpers/admin/application_helper.rb +++ b/app/helpers/admin/application_helper.rb @@ -205,6 +205,19 @@ module Admin::ApplicationHelper } end + def time_zones_for_select + # Collection items are like ["(GMT+01:00) Paris", "Europe/Paris"] + # Label specifies the UTC offset + # Value is in tz database format + time_zones = ActiveSupport::TimeZone.all.sort + time_zones.map { |time_zone| + [ + time_zone.to_s, + time_zone.tzinfo.name + ] + } + end + private def polymorphic_url_param(object_or_class, **options) diff --git a/app/models/communication/website/agenda/event.rb b/app/models/communication/website/agenda/event.rb index 30977fc2b6791e20cb6f142406f2f7fbbd5bf189..e24d795ca9cd75a3cfffcce1dbbeaee5eef238c3 100644 --- a/app/models/communication/website/agenda/event.rb +++ b/app/models/communication/website/agenda/event.rb @@ -12,6 +12,7 @@ # slug :string indexed # subtitle :string # summary :text +# time_zone :string # title :string # to_day :date # to_hour :time diff --git a/app/models/communication/website/agenda/event/with_cal.rb b/app/models/communication/website/agenda/event/with_cal.rb index 6fd24c245bef82e75cdb2824c77473b72e7d1c06..b03c58c3e980285a41e58e55ae56116a8516fded 100644 --- a/app/models/communication/website/agenda/event/with_cal.rb +++ b/app/models/communication/website/agenda/event/with_cal.rb @@ -5,7 +5,7 @@ module Communication::Website::Agenda::Event::WithCal @cal ||= AddToCalendar::URLs.new( start_datetime: cal_from_time, end_datetime: cal_to_time, - timezone: timezone.name, + timezone: time_zone, all_day: cal_all_day, title: "#{title} #{subtitle}", url: url, @@ -48,11 +48,6 @@ module Communication::Website::Agenda::Event::WithCal : date_and_time(to_day, cal_end_time) # Il y a bien une heure de fin end - def timezone - # FIXME la timezone est Europe/Paris pour tout - Time.zone - end - def date_and_time(date, time) Time.new date.year, date.month, @@ -60,7 +55,7 @@ module Communication::Website::Agenda::Event::WithCal time.hour, time.min, time.sec, - timezone + ActiveSupport::TimeZone[time_zone] end end diff --git a/app/models/communication/website/agenda/event/with_time.rb b/app/models/communication/website/agenda/event/with_time.rb index 301428455c8fb1b64a5d18e845ae806b2ed16d17..4f2078fd5b21324c44e2d47360fe7deadb85b7f6 100644 --- a/app/models/communication/website/agenda/event/with_time.rb +++ b/app/models/communication/website/agenda/event/with_time.rb @@ -5,13 +5,14 @@ module Communication::Website::Agenda::Event::WithTime STATUS_FUTURE = 'future' STATUS_CURRENT = 'current' STATUS_ARCHIVE = 'archive' - + scope :future, -> { where('from_day > :today', today: Date.today).ordered_asc } scope :future_or_current, -> { where('from_day >= :today', today: Date.today).ordered_asc } scope :current, -> { where('(from_day <= :today AND to_day IS NULL) OR (from_day <= :today AND to_day >= :today)', today: Date.today).ordered_asc } scope :archive, -> { where('to_day < :today', today: Date.today).ordered_desc } scope :past, -> { archive } + before_validation :set_time_zone before_validation :set_to_day validates_presence_of :from_day, :title @@ -51,9 +52,13 @@ module Communication::Website::Agenda::Event::WithTime def distance_in_days (Date.today - from_day).to_i.abs end - + protected + def set_time_zone + self.time_zone = Time.zone.name if self.time_zone.nil? + end + def set_to_day self.to_day = self.from_day if self.to_day.nil? end diff --git a/app/views/admin/communication/websites/agenda/events/_dates.html.erb b/app/views/admin/communication/websites/agenda/events/_dates.html.erb index 5df299ee030a068ece7e25fbfa992e47793dac7e..7528bc5be65c40946c8b50059cabc20564d44fa2 100644 --- a/app/views/admin/communication/websites/agenda/events/_dates.html.erb +++ b/app/views/admin/communication/websites/agenda/events/_dates.html.erb @@ -20,4 +20,8 @@ day_format = detailed ? :full <%= l(event.to_day, format: day_format).upcase_first %> <%= l(event.to_hour, format: :time_only) if event.to_hour %> <% end %> +<% end %> +<% unless event.from_hour.nil? && event.to_hour.nil? %> + <br> + <%= t('admin.communication.website.agenda.events.formatted_time_zone', time_zone: event.time_zone) %> <% end %> \ No newline at end of file diff --git a/app/views/admin/communication/websites/agenda/events/_form.html.erb b/app/views/admin/communication/websites/agenda/events/_form.html.erb index 04be0f69533e0cd3e2f40a6826222413f42bb5a3..23cc7e845db328da79ee99752608a2b461b22fab 100644 --- a/app/views/admin/communication/websites/agenda/events/_form.html.erb +++ b/app/views/admin/communication/websites/agenda/events/_form.html.erb @@ -10,22 +10,25 @@ <%= render 'admin/application/summary/form', f: f, about: event %> <% end %> <%= osuny_panel Communication::Website::Agenda::Event.human_attribute_name('dates') do %> - <div class="row pure__row--small"> - <div class="col-md-6"> - <%= f.input :from_day, html5: true %> + <div class="row pure__row--small"> + <div class="col-md-6"> + <%= f.input :from_day, html5: true %> + </div> + <div class="col-md-6"> + <%= f.input :from_hour, html5: true %> + </div> </div> - <div class="col-md-6"> - <%= f.input :from_hour, html5: true %> + <div class="row pure__row--small"> + <div class="col-md-6"> + <%= f.input :to_day, html5: true %> + </div> + <div class="col-md-6"> + <%= f.input :to_hour, html5: true %> + </div> </div> - </div> - <div class="row pure__row--small"> - <div class="col-md-6"> - <%= f.input :to_day, html5: true %> - </div> - <div class="col-md-6"> - <%= f.input :to_hour, html5: true %> - </div> - </div> + <%= f.input :time_zone, + collection: time_zones_for_select, + selected: f.object.time_zone || Time.zone.name %> <% end %> <% if @categories.any? %> <%= osuny_panel t('activerecord.attributes.communication/website/agenda/event.categories') do %> @@ -37,7 +40,7 @@ <% end %> <%= render 'admin/application/meta_description/form', f: f, about: event %> - + </div> <div class="col-md-4"> <%= osuny_panel t('metadata') do %> diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml index 04b27db534c041e55da4ce001be9f51b27b58acd..13284109d89390e582aeeadcb678b44193f7ebeb 100644 --- a/config/locales/communication/en.yml +++ b/config/locales/communication/en.yml @@ -183,6 +183,7 @@ en: status_future: In the future status_archive: Archive subtitle: Subtitle + time_zone: Time zone title: Title to_day: To day to_hour: To hour @@ -273,6 +274,7 @@ en: events: draft: Draft none: No event yet + formatted_time_zone: "Time zone: %{time_zone}" default_image: "The default image will be used on pages and posts that do not have an image, if the theme is set to display images in all cases. It is also the default sharing image." editorial: label: Editorial diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml index 478fe40a9d7e58b6698542050afcb3c51d6bc116..a26cf846b19c3eeefabbd05181be6fd370c7bc24 100644 --- a/config/locales/communication/fr.yml +++ b/config/locales/communication/fr.yml @@ -183,6 +183,7 @@ fr: status_future: À venir status_archive: Archive subtitle: Sous-titre + time_zone: Fuseau horaire title: Titre to_day: Jour de fin to_hour: Heure de fin @@ -273,6 +274,7 @@ fr: events: draft: Brouillon none: Pas encore d'événements + formatted_time_zone: "Fuseau horaire : %{time_zone}" default_image: "L'image par défaut sera utilisée dans les pages et actualités qui n'ont pas d'image, si le thème est paramétré pour afficher des images dans tous les cas. C'est aussi l'image de partage par défaut." editorial: label: Éditorial diff --git a/db/migrate/20231229120641_add_time_zone_to_communication_website_agenda_events.rb b/db/migrate/20231229120641_add_time_zone_to_communication_website_agenda_events.rb new file mode 100644 index 0000000000000000000000000000000000000000..3d0231656fdcd86e431c96265991c1279bd548d5 --- /dev/null +++ b/db/migrate/20231229120641_add_time_zone_to_communication_website_agenda_events.rb @@ -0,0 +1,7 @@ +class AddTimeZoneToCommunicationWebsiteAgendaEvents < ActiveRecord::Migration[7.1] + def change + add_column :communication_website_agenda_events, :time_zone, :string + Communication::Website::Agenda::Event.reset_column_information + Communication::Website::Agenda::Event.update_all(time_zone: "Europe/Paris") + end +end diff --git a/db/schema.rb b/db/schema.rb index 77d655784813bc3adf224f12a612103b10003619..3e73cf11f5e79791a2915c687186ed9fe6e35f1d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2023_12_26_094553) do +ActiveRecord::Schema[7.1].define(version: 2023_12_29_120641) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -274,6 +274,7 @@ ActiveRecord::Schema[7.1].define(version: 2023_12_26_094553) do t.datetime "updated_at", null: false t.string "slug" t.string "subtitle" + t.string "time_zone" t.index ["communication_website_id"], name: "index_agenda_events_on_communication_website_id" t.index ["language_id"], name: "index_communication_website_agenda_events_on_language_id" t.index ["original_id"], name: "index_communication_website_agenda_events_on_original_id" diff --git a/test/fixtures/communication/website/agenda/events.yml b/test/fixtures/communication/website/agenda/events.yml index 01f5d18dabd095b22d80aed382f3a2d3f5b8f2e9..b66f2bcbf0499257836e583f5165c7c918e4a862 100644 --- a/test/fixtures/communication/website/agenda/events.yml +++ b/test/fixtures/communication/website/agenda/events.yml @@ -12,6 +12,7 @@ # slug :string indexed # subtitle :string # summary :text +# time_zone :string # title :string # to_day :date # to_hour :time @@ -52,6 +53,7 @@ one: from_hour: 2023-09-05 13:13:30 to_day: 2023-09-05 to_hour: 2023-09-05 13:13:30 + time_zone: Europe/Paris featured_image_alt: MyText featured_image_credit: MyText meta_description: MyText @@ -67,6 +69,7 @@ two: from_hour: 2023-09-05 13:13:30 to_day: 2023-09-05 to_hour: 2023-09-05 13:13:30 + time_zone: Europe/Paris featured_image_alt: MyText featured_image_credit: MyText meta_description: MyText diff --git a/test/models/communication/website/agenda/event_test.rb b/test/models/communication/website/agenda/event_test.rb index 7c2d08a12f0c46c07383699a708c6245987af70c..40ba6c4cf4c8610ec986d311b69dccfbd84f9293 100644 --- a/test/models/communication/website/agenda/event_test.rb +++ b/test/models/communication/website/agenda/event_test.rb @@ -12,6 +12,7 @@ # slug :string indexed # subtitle :string # summary :text +# time_zone :string # title :string # to_day :date # to_hour :time