diff --git a/app/assets/stylesheets/admin/commons/offcanvas.sass b/app/assets/stylesheets/admin/commons/offcanvas.sass new file mode 100644 index 0000000000000000000000000000000000000000..728e70af6bcdfdd7a335247b16318dd2d1bfcded --- /dev/null +++ b/app/assets/stylesheets/admin/commons/offcanvas.sass @@ -0,0 +1,4 @@ +.offcanvas + &__redirections + @include media-breakpoint-up(md) + width: 50vw !important \ No newline at end of file diff --git a/app/controllers/admin/communication/websites/permalinks_controller.rb b/app/controllers/admin/communication/websites/permalinks_controller.rb new file mode 100644 index 0000000000000000000000000000000000000000..49a39f0c4d723c22af3f1e60a082819609f8b9ce --- /dev/null +++ b/app/controllers/admin/communication/websites/permalinks_controller.rb @@ -0,0 +1,8 @@ +class Admin::Communication::Websites::PermalinksController < Admin::Communication::Websites::ApplicationController + + def create + @path = params['communication_website_permalink']['path'] + @about = PolymorphicObjectFinder.find(params, :about) + @permalink = @about.add_redirection(@path) + end +end \ No newline at end of file diff --git a/app/models/communication/website/permalink.rb b/app/models/communication/website/permalink.rb index f0ad95d3bb04b34534913a03848e509c009197fa..9d3493a2a55252388d939c4eaddd5a6a2ff90811 100644 --- a/app/models/communication/website/permalink.rb +++ b/app/models/communication/website/permalink.rb @@ -92,6 +92,17 @@ class Communication::Website::Permalink < ApplicationRecord raise NotImplementedError end + def self.clean_path(path) + clean_path = path.dup + # Remove eventual host + clean_path = URI(clean_path).path + # Leading slash for absolute path + clean_path = "/#{clean_path}" unless clean_path.start_with?('/') + # Trailing slash for coherence + clean_path = "#{clean_path}/" unless clean_path.end_with?('/') + clean_path + end + def pattern language = about.respond_to?(:language) ? about.language : website.default_language self.class.pattern_in_website(website, language) @@ -116,6 +127,10 @@ class Communication::Website::Permalink < ApplicationRecord end end + def to_s + "#{path}" + end + protected def self.required_kinds_in_website(website) @@ -152,5 +167,4 @@ class Communication::Website::Permalink < ApplicationRecord def set_university self.university_id = website.university_id end - end diff --git a/app/models/concerns/with_permalink.rb b/app/models/concerns/with_permalink.rb index 1246b2464b12450cfe25959e1462fd1bb79decc2..68cd521b0b1127e963319c1c5e581105bf4fcf12 100644 --- a/app/models/concerns/with_permalink.rb +++ b/app/models/concerns/with_permalink.rb @@ -29,4 +29,13 @@ module WithPermalink new_permalink_in_website(website).save_if_needed end + def add_redirection(path) + clean_path = Communication::Website::Permalink.clean_path(path) + Communication::Website::Permalink.create( + website: website, + about: self, + is_current: false, + path: clean_path + ) + end end diff --git a/app/views/admin/application/permalinks/_redirects.html.erb b/app/views/admin/application/permalinks/_redirects.html.erb new file mode 100644 index 0000000000000000000000000000000000000000..91dbab67ca8eadcbcdb304cc053b9998579da653 --- /dev/null +++ b/app/views/admin/application/permalinks/_redirects.html.erb @@ -0,0 +1,38 @@ +<p> + <a class="action" data-bs-toggle="offcanvas" href="#offcanvasRedirects" role="button" aria-controls="offcanvasRedirects"> + <%= t('admin.communication.website.redirects.button') %> + </a> +</p> +<div class="offcanvas offcanvas-end offcanvas__redirections" tabindex="-1" id="offcanvasRedirects" aria-labelledby="Redirections"> + <div class="offcanvas-header"> + <h2 class="offcanvas-title"><%= t('admin.communication.website.redirects.title') %></h2> + <button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button> + </div> + <div class="offcanvas-body"> + <%= osuny_panel t('admin.communication.website.redirects.current_path'), small: true do %> + <p class="lead"> + <%= @page.current_permalink_in_website(@website) %> + </p> + <% end %> + <%= osuny_panel t('admin.communication.website.redirects.previous_paths'), small: true do %> + <table id="previous-permalinks" class="table border-top"> + <tbody> + <% @page.previous_permalinks_in_website(@website).each do |link| %> + <tr> + <td><%= link.to_s %></td> + </tr> + <% end %> + </tbody> + </table> + <%= simple_form_for Communication::Website::Permalink.new, + url: admin_communication_website_permalinks_path( + about_type: about.class, + about_id: about.id + ), + remote: true do |f| %> + <%= f.input :path, label: t('admin.communication.website.redirects.add_path') %> + <%= f.button :submit, t('add'), class: button_classes %> + <% end %> + <% end %> + </div> +</div> \ No newline at end of file diff --git a/app/views/admin/communication/websites/pages/show/_metadata.html.erb b/app/views/admin/communication/websites/pages/show/_metadata.html.erb index 11f23bc1deabecc8cac8a5ed0344d8844598d576..6d7e56fb26163c162d8b786f7c698996eb6bb266 100644 --- a/app/views/admin/communication/websites/pages/show/_metadata.html.erb +++ b/app/views/admin/communication/websites/pages/show/_metadata.html.erb @@ -3,6 +3,7 @@ # @pa t'affole pas, c'est pas encore le cas %> <%= osuny_panel t('metadata'), small: true do %> + <p> <% if @page.is_special_page? %> <% page_type = t("communication.website.pages.defaults.#{@page.type_key}.title") %> <% if page_type != @page.to_s %> @@ -16,12 +17,12 @@ <% if @page.parent && !@page.parent.is_home? %> dans - <%= link_to_if can?(:read, @page.parent), + “<%= link_to_if can?(:read, @page.parent), @page.parent, admin_communication_website_page_path( website_id: @website.id, id: @page.parent.id - ) %>. + ) %>†<% end %> <% if @page.children.any? %> @@ -32,12 +33,13 @@ admin_communication_website_page_path( website_id: @website.id, id: child.id), class: "#{'draft' unless child.published?}" - }.join(', ') %>. + }.join(', ') %> <% end %> <% if @page.full_width %> - <br> - Pleine largeur + en pleine largeur <% end %> + </p> + + <%= render 'admin/application/permalinks/redirects', about: @page %> <% end %> - diff --git a/app/views/admin/communication/websites/permalinks/create.js.erb b/app/views/admin/communication/websites/permalinks/create.js.erb new file mode 100644 index 0000000000000000000000000000000000000000..f16602c9a719d62e97847ce6fd89610c6ed14841 --- /dev/null +++ b/app/views/admin/communication/websites/permalinks/create.js.erb @@ -0,0 +1,7 @@ +document.getElementById('previous-permalinks') + .getElementsByTagName('tbody')[0] + .insertRow() + .insertCell() + .appendChild( + document.createTextNode('<%= @permalink.path %>') + ) diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml index dc7c0dde660666620fa193106fb45a4e2e78ec43..7a4aad58855f571ab6a33e525abc5cbf3785c120 100644 --- a/config/locales/communication/en.yml +++ b/config/locales/communication/en.yml @@ -218,6 +218,8 @@ en: text: Main page text title: Title website: Website + communication/website/permalink: + path: Path communication/website/post: author: Author category: Category @@ -268,6 +270,12 @@ en: publish: button: Publish notice: Publication in progress, it should take a few minutes to be online. + redirects: + button: Manage redirects + title: Manage redirects + current_path: Current path + previous_paths: Redirects (old paths) + add_path: Add a redirect communication: authors: one: Author diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml index 373308841eeaced2e3e1fe8d641ef085f3a1ab44..c84e9a75931acc5c4ef6aace2b46bf596b49bfc9 100644 --- a/config/locales/communication/fr.yml +++ b/config/locales/communication/fr.yml @@ -218,6 +218,8 @@ fr: text: Texte principal de la page title: Titre website: Site Web + communication/website/permalink: + path: Chemin communication/website/post: author: Auteur·rice category: Catégorie @@ -268,6 +270,12 @@ fr: publish: button: Publier notice: Publication en cours, cela devrait prendre quelques minutes pour arriver en ligne. + redirects: + button: Gérer les redirections + title: Gérer les redirections + current_path: Chemin actuel + previous_paths: Redirections (anciens chemins) + add_path: Ajouter une redirection communication: authors: one: Auteur·rice diff --git a/config/routes/admin/communication.rb b/config/routes/admin/communication.rb index 44454fe9951d2dd4fd1e3eee32607d32fa5b1342..e739fb561ec0823ee29d5b16c8bd9d5e7a30aa8d 100644 --- a/config/routes/admin/communication.rb +++ b/config/routes/admin/communication.rb @@ -14,6 +14,7 @@ namespace :communication do get 'assets/*path' => 'websites/preview#assets' resources :dependencies, controller: 'websites/dependencies', only: :index resources :connections, controller: 'websites/connections', only: [:index, :show] + resources :permalinks, controller: 'websites/permalinks', only: :create resources :pages, controller: 'websites/pages', path: '/:lang/pages' do collection do post :reorder