From eab78f2fca13adf56cbf05fc9b52c33973e4a320 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Fri, 29 Dec 2023 16:54:33 +0100
Subject: [PATCH] default time zone in websites

---
 .../communication/websites_controller.rb      |  1 +
 app/models/communication/website.rb           |  1 +
 .../communication/website/configs/base.rb     |  1 +
 .../default_content_security_policy.rb        |  1 +
 .../website/configs/default_languages.rb      |  1 +
 .../website/configs/default_permalinks.rb     |  1 +
 .../website/configs/deuxfleurs_workflow.rb    |  1 +
 .../website/configs/development_config.rb     |  1 +
 .../website/configs/production_config.rb      |  1 +
 .../communication/websites/_form.html.erb     | 36 ++++++++++++-------
 .../websites/agenda/events/_form.html.erb     |  2 +-
 config/locales/communication/en.yml           |  1 +
 config/locales/communication/fr.yml           |  1 +
 ...ult_time_zone_to_communication_websites.rb |  7 ++++
 db/schema.rb                                  |  3 +-
 test/fixtures/communication/websites.yml      |  3 ++
 16 files changed, 47 insertions(+), 15 deletions(-)
 create mode 100644 db/migrate/20231229154550_add_default_time_zone_to_communication_websites.rb

diff --git a/app/controllers/admin/communication/websites_controller.rb b/app/controllers/admin/communication/websites_controller.rb
index f8b20c0c1..6d64a22e3 100644
--- a/app/controllers/admin/communication/websites_controller.rb
+++ b/app/controllers/admin/communication/websites_controller.rb
@@ -79,6 +79,7 @@ class Admin::Communication::WebsitesController < Admin::Communication::Websites:
       :name, :url, :repository, :about_type, :about_id, :in_production,
       :git_provider, :git_endpoint, :git_branch, :plausible_url,
       :feature_posts, :feature_agenda,
+      :default_time_zone,
       :deuxfleurs_hosting, :default_image, :default_image_delete,
       :social_mastodon, :social_x, :social_linkedin, :social_youtube, :social_vimeo, :social_peertube, :social_instagram, :social_facebook, :social_tiktok, :social_email, :social_github,
       :deployment_status_badge, :autoupdate_theme, language_ids: []
diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb
index 6ddbe5fa8..aa6b81230 100644
--- a/app/models/communication/website.rb
+++ b/app/models/communication/website.rb
@@ -6,6 +6,7 @@
 #  about_type              :string           indexed => [about_id]
 #  access_token            :string
 #  autoupdate_theme        :boolean          default(TRUE)
+#  default_time_zone       :string
 #  deployment_status_badge :text
 #  deuxfleurs_hosting      :boolean          default(TRUE)
 #  deuxfleurs_identifier   :string
diff --git a/app/models/communication/website/configs/base.rb b/app/models/communication/website/configs/base.rb
index 20c4f4383..77eb400bd 100644
--- a/app/models/communication/website/configs/base.rb
+++ b/app/models/communication/website/configs/base.rb
@@ -6,6 +6,7 @@
 #  about_type              :string           indexed => [about_id]
 #  access_token            :string
 #  autoupdate_theme        :boolean          default(TRUE)
+#  default_time_zone       :string
 #  deployment_status_badge :text
 #  deuxfleurs_hosting      :boolean          default(TRUE)
 #  deuxfleurs_identifier   :string
diff --git a/app/models/communication/website/configs/default_content_security_policy.rb b/app/models/communication/website/configs/default_content_security_policy.rb
index e85e4ec02..d318702fd 100644
--- a/app/models/communication/website/configs/default_content_security_policy.rb
+++ b/app/models/communication/website/configs/default_content_security_policy.rb
@@ -6,6 +6,7 @@
 #  about_type              :string           indexed => [about_id]
 #  access_token            :string
 #  autoupdate_theme        :boolean          default(TRUE)
+#  default_time_zone       :string
 #  deployment_status_badge :text
 #  deuxfleurs_hosting      :boolean          default(TRUE)
 #  deuxfleurs_identifier   :string
diff --git a/app/models/communication/website/configs/default_languages.rb b/app/models/communication/website/configs/default_languages.rb
index c189c7b52..3a8af76e2 100644
--- a/app/models/communication/website/configs/default_languages.rb
+++ b/app/models/communication/website/configs/default_languages.rb
@@ -6,6 +6,7 @@
 #  about_type              :string           indexed => [about_id]
 #  access_token            :string
 #  autoupdate_theme        :boolean          default(TRUE)
+#  default_time_zone       :string
 #  deployment_status_badge :text
 #  deuxfleurs_hosting      :boolean          default(TRUE)
 #  deuxfleurs_identifier   :string
diff --git a/app/models/communication/website/configs/default_permalinks.rb b/app/models/communication/website/configs/default_permalinks.rb
index 4db1202cb..939d94ff8 100644
--- a/app/models/communication/website/configs/default_permalinks.rb
+++ b/app/models/communication/website/configs/default_permalinks.rb
@@ -6,6 +6,7 @@
 #  about_type              :string           indexed => [about_id]
 #  access_token            :string
 #  autoupdate_theme        :boolean          default(TRUE)
+#  default_time_zone       :string
 #  deployment_status_badge :text
 #  deuxfleurs_hosting      :boolean          default(TRUE)
 #  deuxfleurs_identifier   :string
diff --git a/app/models/communication/website/configs/deuxfleurs_workflow.rb b/app/models/communication/website/configs/deuxfleurs_workflow.rb
index 2c393b962..fd7205dfe 100644
--- a/app/models/communication/website/configs/deuxfleurs_workflow.rb
+++ b/app/models/communication/website/configs/deuxfleurs_workflow.rb
@@ -6,6 +6,7 @@
 #  about_type              :string           indexed => [about_id]
 #  access_token            :string
 #  autoupdate_theme        :boolean          default(TRUE)
+#  default_time_zone       :string
 #  deployment_status_badge :text
 #  deuxfleurs_hosting      :boolean          default(TRUE)
 #  deuxfleurs_identifier   :string
diff --git a/app/models/communication/website/configs/development_config.rb b/app/models/communication/website/configs/development_config.rb
index d6ca505f0..dcbd53b0b 100644
--- a/app/models/communication/website/configs/development_config.rb
+++ b/app/models/communication/website/configs/development_config.rb
@@ -6,6 +6,7 @@
 #  about_type              :string           indexed => [about_id]
 #  access_token            :string
 #  autoupdate_theme        :boolean          default(TRUE)
+#  default_time_zone       :string
 #  deployment_status_badge :text
 #  deuxfleurs_hosting      :boolean          default(TRUE)
 #  deuxfleurs_identifier   :string
diff --git a/app/models/communication/website/configs/production_config.rb b/app/models/communication/website/configs/production_config.rb
index da58f4e05..ce4add152 100644
--- a/app/models/communication/website/configs/production_config.rb
+++ b/app/models/communication/website/configs/production_config.rb
@@ -6,6 +6,7 @@
 #  about_type              :string           indexed => [about_id]
 #  access_token            :string
 #  autoupdate_theme        :boolean          default(TRUE)
+#  default_time_zone       :string
 #  deployment_status_badge :text
 #  deuxfleurs_hosting      :boolean          default(TRUE)
 #  deuxfleurs_identifier   :string
diff --git a/app/views/admin/communication/websites/_form.html.erb b/app/views/admin/communication/websites/_form.html.erb
index 285079b23..d447306c2 100644
--- a/app/views/admin/communication/websites/_form.html.erb
+++ b/app/views/admin/communication/websites/_form.html.erb
@@ -24,16 +24,16 @@
         <%= osuny_panel Language.model_name.human(count: 2) do %>
           <div class="row">
             <div class="col-lg-6">
-              <%= f.association :languages, 
-                                as: :check_boxes, 
-                                required: true, 
-                                wrapper_html: { class: "js-languages" }, 
+              <%= f.association :languages,
+                                as: :check_boxes,
+                                required: true,
+                                wrapper_html: { class: "js-languages" },
                                 label_method: lambda { |l| language_name(l.iso_code) } %>
             </div>
             <div class="col-lg-6">
-              <%= f.association :default_language, 
-                                include_blank: t('simple_form.include_blanks.defaults.language'), 
-                                label_method: lambda { |l| language_name(l.iso_code) }, 
+              <%= f.association :default_language,
+                                include_blank: t('simple_form.include_blanks.defaults.language'),
+                                label_method: lambda { |l| language_name(l.iso_code) },
                                 input_html: (@website.persisted? ? { disabled: true } : { class: "js-default-language" }) %>
             </div>
           </div>
@@ -55,6 +55,16 @@
           </div>
         <% end %>
 
+        <%= osuny_panel Communication::Website.human_attribute_name('feature_agenda') do %>
+          <div class="row">
+            <div class="col-lg-6">
+              <%= f.input :default_time_zone,
+                          collection: time_zones_for_select,
+                          selected: f.object.default_time_zone || Time.zone.name %>
+            </div>
+          </div>
+        <% end %>
+
         <%= osuny_panel Communication::Website.human_attribute_name('social') do %>
           <div class="row">
             <div class="col-lg-6">
@@ -77,7 +87,7 @@
           </div>
         <% end %>
       </section>
-      
+
       <section>
         <p class="float-end blocks__category__description"><%= t "admin.communication.website.technical.description" %></p>
         <h2 class="h3 category blocks__category__title"><%= t "admin.communication.website.technical.label" %></h2>
@@ -97,9 +107,9 @@
         <%= osuny_panel t('communication.website.git') do %>
           <%= f.input :git_provider, include_blank: false %>
           <%= f.input :git_endpoint %>
-          <%= f.input :access_token, 
-                      as: :string, 
-                      placeholder: masked_string(f.object.access_token), 
+          <%= f.input :access_token,
+                      as: :string,
+                      placeholder: masked_string(f.object.access_token),
                       hint: t("simple_form.hints.communication_website.access_token_#{f.object.access_token.blank? ? 'without' : 'with'}_existing").html_safe,
                       input_html: {
                         autocomplete: 'access_token',
@@ -109,8 +119,8 @@
                       %>
           <%= f.input :repository %>
           <%= f.input :git_branch %>
-          <%= f.input :deployment_status_badge, 
-                      as: :string, 
+          <%= f.input :deployment_status_badge,
+                      as: :string,
                       input_html: {
                         autocomplete: 'deployment_status_badge',
                         role: 'presentation'
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 23cc7e845..d751222a7 100644
--- a/app/views/admin/communication/websites/agenda/events/_form.html.erb
+++ b/app/views/admin/communication/websites/agenda/events/_form.html.erb
@@ -28,7 +28,7 @@
         </div>
         <%= f.input :time_zone,
                     collection: time_zones_for_select,
-                    selected: f.object.time_zone || Time.zone.name %>
+                    selected: f.object.time_zone || @website.default_time_zone %>
       <% end %>
       <% if @categories.any? %>
         <%= osuny_panel t('activerecord.attributes.communication/website/agenda/event.categories') do %>
diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml
index 13284109d..7b2d2e049 100644
--- a/config/locales/communication/en.yml
+++ b/config/locales/communication/en.yml
@@ -139,6 +139,7 @@ en:
         created_at: Creation
         default_image: Default image
         default_language: Default language
+        default_time_zone: Default time zone
         deployment_status_badge: Deployment status badge
         deuxfleurs_hosting: Hébergement avec Deuxfleurs
         deuxfleurs_identifier: Identifiant Deuxfleurs
diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml
index a26cf846b..c54f405af 100644
--- a/config/locales/communication/fr.yml
+++ b/config/locales/communication/fr.yml
@@ -139,6 +139,7 @@ fr:
         created_at: Création
         default_image: Image par défaut
         default_language: Langue par défaut
+        default_time_zone: Fuseau horaire par défaut
         deployment_status_badge: Badge de statut du déploiement
         deuxfleurs_hosting: Hébergement avec Deuxfleurs
         deuxfleurs_identifier: Identifiant Deuxfleurs
diff --git a/db/migrate/20231229154550_add_default_time_zone_to_communication_websites.rb b/db/migrate/20231229154550_add_default_time_zone_to_communication_websites.rb
new file mode 100644
index 000000000..241e8cc5f
--- /dev/null
+++ b/db/migrate/20231229154550_add_default_time_zone_to_communication_websites.rb
@@ -0,0 +1,7 @@
+class AddDefaultTimeZoneToCommunicationWebsites < ActiveRecord::Migration[7.1]
+  def change
+    add_column :communication_websites, :default_time_zone, :string
+    Communication::Website.reset_column_information
+    Communication::Website.update_all(default_time_zone: "Europe/Paris")
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 3e73cf11f..183724d90 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_29_120641) do
+ActiveRecord::Schema[7.1].define(version: 2023_12_29_154550) do
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
   enable_extension "plpgsql"
@@ -498,6 +498,7 @@ ActiveRecord::Schema[7.1].define(version: 2023_12_29_120641) do
     t.string "deuxfleurs_identifier"
     t.string "social_email"
     t.string "social_github"
+    t.string "default_time_zone"
     t.index ["about_type", "about_id"], name: "index_communication_websites_on_about"
     t.index ["default_language_id"], name: "index_communication_websites_on_default_language_id"
     t.index ["university_id"], name: "index_communication_websites_on_university_id"
diff --git a/test/fixtures/communication/websites.yml b/test/fixtures/communication/websites.yml
index e4849bdc0..4c1e830c5 100644
--- a/test/fixtures/communication/websites.yml
+++ b/test/fixtures/communication/websites.yml
@@ -6,6 +6,7 @@
 #  about_type              :string           indexed => [about_id]
 #  access_token            :string
 #  autoupdate_theme        :boolean          default(TRUE)
+#  default_time_zone       :string
 #  deployment_status_badge :text
 #  deuxfleurs_hosting      :boolean          default(TRUE)
 #  deuxfleurs_identifier   :string
@@ -60,6 +61,7 @@ website_with_github:
   repository: <%= ENV['TEST_GITHUB_REPOSITORY'] %>
   languages: [fr]
   default_language: fr
+  default_time_zone: Europe/Paris
 
 website_with_gitlab:
   university: default_university
@@ -71,3 +73,4 @@ website_with_gitlab:
   repository: <%= ENV['TEST_GITLAB_REPOSITORY'] %>
   languages: [fr, en]
   default_language: fr
+  default_time_zone: Europe/Paris
-- 
GitLab