From 30710fe4af56393be895de21a09cc8a94a74b0f4 Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Mon, 29 Jan 2024 17:20:50 +0100
Subject: [PATCH] change helpers for files

---
 app/helpers/application_helper.rb             | 20 +++++++++++++++++--
 .../application/featured_image/_edit.html.erb |  2 +-
 .../blocks/components/file/_edit.html.erb     | 11 +++++++---
 .../blocks/components/image/_edit.html.erb    |  2 +-
 .../blocks/templates/gallery/_edit.html.erb   |  8 ++++++--
 .../blocks/templates/sound/_edit.html.erb     |  4 +++-
 .../communication/extranets/_form.html.erb    |  3 ++-
 .../communication/websites/_form.html.erb     |  2 +-
 config/application.rb                         |  1 +
 config/locales/en.yml                         |  3 ++-
 config/locales/fr.yml                         |  3 ++-
 db/schema.rb                                  |  4 ++--
 12 files changed, 47 insertions(+), 16 deletions(-)

diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 6ce281b92..41c52a366 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -64,9 +64,25 @@ module ApplicationHelper
   def default_images_formats_accepted
     Rails.application.config.default_images_formats.join(', ')
   end
+  
+  def default_sounds_formats_accepted
+    Rails.application.config.default_sounds_formats.join(', ')
+  end
+
+  def default_file_hint(filesize: number_to_human_size(Communication::Block::FILE_MAX_SIZE), formats: [])
+    if formats.empty?
+      t('file_hint_without_formats', filesize: filesize)
+    else
+      t('file_hint_with_formats', filesize: filesize, formats: formats)
+    end
+  end
 
-  def default_images_formats_accepted_hint
-    t('default_images_hint', formats: default_images_formats_accepted)
+  def images_formats_accepted_hint(formats: default_images_formats_accepted)
+    default_file_hint(filesize: number_to_human_size(Communication::Block::IMAGE_MAX_SIZE), formats: formats)
+  end
+  
+  def sounds_formats_accepted_hint(formats: default_sounds_formats_accepted)
+    default_file_hint(formats: formats)
   end
 
 end
diff --git a/app/views/admin/application/featured_image/_edit.html.erb b/app/views/admin/application/featured_image/_edit.html.erb
index 2de1e713a..6b741fcdd 100644
--- a/app/views/admin/application/featured_image/_edit.html.erb
+++ b/app/views/admin/application/featured_image/_edit.html.erb
@@ -3,7 +3,7 @@
               as: :single_deletable_file,
               direct_upload: true,
               label: false,
-              hint: default_images_formats_accepted_hint,
+              hint: images_formats_accepted_hint,
               input_html: { accept: default_images_formats_accepted },
               preview: true,
               resize: true %>
diff --git a/app/views/admin/communication/blocks/components/file/_edit.html.erb b/app/views/admin/communication/blocks/components/file/_edit.html.erb
index 592c1f391..3af0c7dc2 100644
--- a/app/views/admin/communication/blocks/components/file/_edit.html.erb
+++ b/app/views/admin/communication/blocks/components/file/_edit.html.erb
@@ -1,6 +1,8 @@
 <%
-label = t 'admin.communication.blocks.components.file.input.label'
-remove = t 'admin.communication.blocks.components.file.input.remove'
+label = t('admin.communication.blocks.components.file.input.label')
+remove = t('admin.communication.blocks.components.file.input.remove')
+hint = default_file_hint if hint.blank?
+accept ||= '*'
 %>
 <div class="mb-3">
   <div v-if="!<%= model %>.<%= property %>.id">
@@ -10,7 +12,7 @@ remove = t 'admin.communication.blocks.components.file.input.remove'
     </label>
     <input  class="form-control"
             type="file"
-            accept="*"
+            accept="<%= accept %>"
             @change="onFileImageChange( $event, <%= model %>, '<%= property %>' )"
             data-size-limit="<%= Communication::Block::FILE_MAX_SIZE %>"
             :id="<%= dom_id.html_safe %>">
@@ -23,4 +25,7 @@ remove = t 'admin.communication.blocks.components.file.input.remove'
       <%= remove %>
     </a>
   </div>
+  <div v-if="<%= hint.present? %> && !<%= model %>.<%= property %>.id" class="form-text">
+    <%= hint %>
+  </div>
 </div>
diff --git a/app/views/admin/communication/blocks/components/image/_edit.html.erb b/app/views/admin/communication/blocks/components/image/_edit.html.erb
index 3c8b741b7..a16a19052 100644
--- a/app/views/admin/communication/blocks/components/image/_edit.html.erb
+++ b/app/views/admin/communication/blocks/components/image/_edit.html.erb
@@ -1,7 +1,7 @@
 <%
 label = t('admin.communication.blocks.components.image.input.label')
 remove = t('admin.communication.blocks.components.image.input.remove')
-hint = default_images_formats_accepted_hint
+hint = images_formats_accepted_hint
 %>
 <div class="mb-3">
   <div v-if="!<%= model %>.<%= property %>.id">
diff --git a/app/views/admin/communication/blocks/templates/gallery/_edit.html.erb b/app/views/admin/communication/blocks/templates/gallery/_edit.html.erb
index 50965764c..e86595717 100644
--- a/app/views/admin/communication/blocks/templates/gallery/_edit.html.erb
+++ b/app/views/admin/communication/blocks/templates/gallery/_edit.html.erb
@@ -8,13 +8,17 @@
 
 <p><%= t('.send_multiple_images') %></p>
 
-<input  class="form-control mb-2"
+<input  class="form-control"
         type="file"
-        accept="image/*"
+        accept="<%= default_images_formats_accepted %>"
         @change="onMultipleFileImageChange( $event, 'image' )"
         data-size-limit="<%= Communication::Block::IMAGE_MAX_SIZE %>"
         multiple>
 
+<div class="form-text mb-2">
+    <%= images_formats_accepted_hint %>
+</div>
+
 <hr class="my-5">
 
 <p><%= t('.move_images') %></p>
diff --git a/app/views/admin/communication/blocks/templates/sound/_edit.html.erb b/app/views/admin/communication/blocks/templates/sound/_edit.html.erb
index 68c966a68..b32fa4da4 100644
--- a/app/views/admin/communication/blocks/templates/sound/_edit.html.erb
+++ b/app/views/admin/communication/blocks/templates/sound/_edit.html.erb
@@ -1,6 +1,8 @@
 <div class="row pure__row--small">
   <div class="col-md-6">
-    <%= block_component_edit block, :file %>
+    <%= block_component_edit block, :file,
+            hint: sounds_formats_accepted_hint,
+            accept: default_sounds_formats_accepted %>
     <%= block_component_edit block, :title %>
   </div>
   <div class="col-md-6">
diff --git a/app/views/admin/communication/extranets/_form.html.erb b/app/views/admin/communication/extranets/_form.html.erb
index 1a7ef3849..378f3b290 100644
--- a/app/views/admin/communication/extranets/_form.html.erb
+++ b/app/views/admin/communication/extranets/_form.html.erb
@@ -15,12 +15,13 @@
       <%= osuny_panel t('look_feel') do %>
         <%= f.input :logo,
                     as: :single_deletable_file,
-                    hint: default_images_formats_accepted_hint,
+                    hint: images_formats_accepted_hint,
                     input_html: { accept: default_images_formats_accepted },
                     preview: 200,
                     direct_upload: true %>
         <%= f.input :favicon,
                     as: :single_deletable_file,
+                    hint: images_formats_accepted_hint('.png')
                     input_html: { accept: '.png' },
                     preview: 100,
                     direct_upload: true %>
diff --git a/app/views/admin/communication/websites/_form.html.erb b/app/views/admin/communication/websites/_form.html.erb
index 269247bb7..9fcf63a8e 100644
--- a/app/views/admin/communication/websites/_form.html.erb
+++ b/app/views/admin/communication/websites/_form.html.erb
@@ -48,7 +48,7 @@
               <%= f.input :default_image,
                           as: :single_deletable_file,
                           direct_upload: true,
-                          hint: default_images_formats_accepted_hint,
+                          hint: images_formats_accepted_hint,
                           input_html: { accept: default_images_formats_accepted },
                           preview: 300 %>
             </div>
diff --git a/config/application.rb b/config/application.rb
index 8ff90a501..830a5510f 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -71,6 +71,7 @@ module Osuny
 
     config.allowed_special_chars = '#?!,_@$%^&*+:;£µ-'
     config.default_images_formats = ['.jpg', '.jpeg', '.png', '.svg']
+    config.default_sounds_formats = ['.mp3']
 
     config.generators do |g|
       g.orm :active_record, primary_key_type: :uuid
diff --git a/config/locales/en.yml b/config/locales/en.yml
index db4b1d125..27d5ac2c3 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -168,7 +168,6 @@ en:
       birthday: "%B %d"
       full: "%A %-d %B %Y"
   delete: Delete
-  default_images_hint: "5 MB max, %{formats}"
   devise:
     failure:
       invalid: "Invalid email or password."
@@ -225,6 +224,8 @@ en:
       label: Credit
       hint: The credit must be present according to copyright or copyleft licenses.
     remove: Remove image
+  file_hint_with_formats: "%{formats} files only. %{filesize} max."
+  file_hint_without_formats: "%{filesize} max"
   filters:
     attributes:
       category: Filter by category
diff --git a/config/locales/fr.yml b/config/locales/fr.yml
index e0eb27589..47404bef1 100644
--- a/config/locales/fr.yml
+++ b/config/locales/fr.yml
@@ -167,7 +167,6 @@ fr:
     formats:
       birthday: "%d %B"
       full: "%A %-d %B %Y"
-  default_images_hint: "5 Mo max, %{formats}"
   delete: Supprimer
   devise:
     failure:
@@ -225,6 +224,8 @@ fr:
       label: Crédit
       hint: Le crédit photo doit être indiqué, dans le respect du droit d'auteur.
     remove: Supprimer l'image
+  file_hint_with_formats: "Fichiers %{formats} uniquement. %{filesize} max."
+  file_hint_without_formats: "%{filesize} max."
   filters:
     attributes:
       category: Filtrer par catégorie
diff --git a/db/schema.rb b/db/schema.rb
index 13fed44a6..5f838fbda 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -106,8 +106,8 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_29_100647) do
     t.datetime "updated_at", null: false
     t.string "title"
     t.boolean "published", default: true
-    t.uuid "heading_id"
     t.uuid "communication_website_id"
+    t.uuid "heading_id"
     t.string "migration_identifier"
     t.index ["about_type", "about_id"], name: "index_communication_website_blocks_on_about"
     t.index ["communication_website_id"], name: "index_communication_blocks_on_communication_website_id"
@@ -419,7 +419,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_29_100647) do
     t.index ["university_id"], name: "index_communication_website_pages_on_university_id"
   end
 
-  create_table "communication_website_permalinks", id: :uuid, default: -> { "public.gen_random_uuid()" }, force: :cascade do |t|
+  create_table "communication_website_permalinks", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
     t.uuid "university_id", null: false
     t.uuid "website_id", null: false
     t.string "about_type", null: false
-- 
GitLab