From beb9b8a23f9ee4ac99b9f650e74eb5c6aeecd5e0 Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Thu, 7 Apr 2022 12:29:46 +0200
Subject: [PATCH] special pages

---
 app/models/communication/website.rb           |  1 +
 app/models/communication/website/page.rb      |  2 +-
 .../communication/website/page/with_kind.rb   | 14 ++++-
 .../website/with_dependencies.rb              |  1 +
 .../website/with_special_pages.rb             | 51 +++++++++++++++
 .../admin/communication/blocks/_list.html.erb |  2 +-
 .../website/pages/_form.html.erb              | 10 ++-
 .../communication/website/pages/show.html.erb |  9 +--
 config/locales/communication/fr.yml           | 62 ++++++++++++++++++-
 config/locales/en.yml                         |  1 +
 config/locales/fr.yml                         |  1 +
 db/schema.rb                                  |  3 +
 12 files changed, 146 insertions(+), 11 deletions(-)
 create mode 100644 app/models/communication/website/with_special_pages.rb

diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb
index a3ec423b6..8b91fbde2 100644
--- a/app/models/communication/website.rb
+++ b/app/models/communication/website.rb
@@ -35,6 +35,7 @@ class Communication::Website < ApplicationRecord
   include WithIndexPages
   include WithMenuItems
   include WithProgramCategories
+  include WithSpecialPages
 
   scope :ordered, -> { order(:name) }
 
diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb
index 33d813f4e..2d3d66e79 100644
--- a/app/models/communication/website/page.rb
+++ b/app/models/communication/website/page.rb
@@ -155,7 +155,7 @@ class Communication::Website::Page < ApplicationRecord
   end
 
   def make_path
-    self.path = generated_path
+    self.path = kind_home? ? '/' : generated_path
   end
 
   def slug_must_be_unique
diff --git a/app/models/communication/website/page/with_kind.rb b/app/models/communication/website/page/with_kind.rb
index 14cf5e983..f464cf6a4 100644
--- a/app/models/communication/website/page/with_kind.rb
+++ b/app/models/communication/website/page/with_kind.rb
@@ -9,13 +9,18 @@ module Communication::Website::Page::WithKind
       education_programs: 20,
       research_articles: 30,
       research_volumes: 32,
-      persons: 100,
+      legal_terms: 80,
+        sitemap: 81,
+        privacy_policy: 82,
+      people: 100,
         administrators: 110,
         authors: 120,
         researchers: 130,
         teachers: 140
     }, _prefix: 'kind'
 
+    after_create :move_legacy_root_pages, if: :kind_home?
+
     def is_special_page?
       kind != nil
     end
@@ -26,4 +31,11 @@ module Communication::Website::Page::WithKind
 
   end
 
+  private
+
+  def move_legacy_root_pages
+    root_pages = website.pages.where.not(id: id).root
+    root_pages.update_all(parent_id: id)
+  end
+
 end
diff --git a/app/models/communication/website/with_dependencies.rb b/app/models/communication/website/with_dependencies.rb
index 193fb1dd6..2d7f31e6b 100644
--- a/app/models/communication/website/with_dependencies.rb
+++ b/app/models/communication/website/with_dependencies.rb
@@ -75,6 +75,7 @@ module Communication::Website::WithDependencies
     end
   end
 
+  # those tests has_xxx? should match the special page kind
   def has_communication_posts?
     posts.published.any?
   end
diff --git a/app/models/communication/website/with_special_pages.rb b/app/models/communication/website/with_special_pages.rb
new file mode 100644
index 000000000..e0dbb6021
--- /dev/null
+++ b/app/models/communication/website/with_special_pages.rb
@@ -0,0 +1,51 @@
+module Communication::Website::WithSpecialPages
+  extend ActiveSupport::Concern
+
+  included do
+
+    def create_missing_special_pages
+      homepage = create_special_page('home')
+      # first level generic pages
+      ['legal_terms', 'sitemap', 'privacy_policy'].each do |kind|
+        create_special_page(kind, homepage.id)
+      end
+      # first level pages with test
+      ['communication_posts', 'education_programs', 'research_articles', 'research_volumes'].each do |kind|
+        create_special_page(kind, homepage.id) if public_send("has_#{kind}?")
+      end
+      # team pages
+      if has_people?
+        people = create_special_page('people', homepage.id)
+        ['administrators', 'authors', 'researchers', 'teachers'].each do |kind|
+          create_special_page(kind, people.id) if public_send("has_#{kind}?")
+        end
+      end
+    end
+    # handle_async
+
+    def manage_special_pages_publication
+      current_special_pages_keys = pages.where.not(kind: nil).pluck(:kind).uniq
+      current_special_pages_keys.each do |kind|
+        state = !(respond_to?("has_#{kind}?") && !public_send("has_#{kind}?"))
+        pages.where(kind: kind).update_all(published: state)
+      end
+    end
+
+  end
+
+  private
+
+  def create_special_page(kind, parent_id = nil)
+    i18n_key = "communication.website.pages.defaults.#{kind}"
+    pages.where(kind: kind).first_or_create(
+      title: I18n.t("#{i18n_key}.title"),
+      slug: I18n.t("#{i18n_key}.slug"),
+      description_short: I18n.t("#{i18n_key}.description_short"),
+      parent_id: parent_id,
+      published: true,
+      university_id: university_id
+    )
+  end
+
+
+end
diff --git a/app/views/admin/communication/blocks/_list.html.erb b/app/views/admin/communication/blocks/_list.html.erb
index 5600bb047..ad44cee9f 100644
--- a/app/views/admin/communication/blocks/_list.html.erb
+++ b/app/views/admin/communication/blocks/_list.html.erb
@@ -13,7 +13,7 @@
       <thead>
         <tr>
           <th width="20" class="ps-0">&nbsp;</th>
-          <th><%= Communication::Block.human_attribute_name('name') %></th>
+          <th><%= Communication::Block.human_attribute_name('title') %></th>
           <th><%= Communication::Block.human_attribute_name('template') %></th>
           <th></th>
         </tr>
diff --git a/app/views/admin/communication/website/pages/_form.html.erb b/app/views/admin/communication/website/pages/_form.html.erb
index 4f8d6ee58..dd6c9e88e 100644
--- a/app/views/admin/communication/website/pages/_form.html.erb
+++ b/app/views/admin/communication/website/pages/_form.html.erb
@@ -8,7 +8,6 @@
         <div class="card-body">
           <%= f.input :title %>
           <%= f.input :breadcrumb_title %>
-          <%= f.input :description %>
           <%= f.input :description_short %>
           <%= f.input :header_text,
                       as: :summernote,
@@ -61,6 +60,15 @@
            <%= f.input :featured_image_alt %>
         </div>
       </div>
+      <div class="card flex-fill w-100">
+        <div class="card-header">
+          <h5 class="card-title mb-0"><%= t('seo') %></h5>
+        </div>
+        <div class="card-body">
+          <%= f.input :description %>
+        </div>
+      </div>
+    </div>
     </div>
   </div>
   <% content_for :action_bar_right do %>
diff --git a/app/views/admin/communication/website/pages/show.html.erb b/app/views/admin/communication/website/pages/show.html.erb
index 84b5af932..27fe594df 100644
--- a/app/views/admin/communication/website/pages/show.html.erb
+++ b/app/views/admin/communication/website/pages/show.html.erb
@@ -9,7 +9,6 @@
           <h2 class="card-title mb-0 h5"><%= t('content') %></h2>
         </div>
         <div class="card-body">
-          <%= render 'admin/application/property/text', object: @page, property: :description %>
           <%= render 'admin/application/property/text', object: @page, property: :description_short %>
           <%= render 'admin/application/property/text', object: @page, property: :text %>
         </div>
@@ -30,8 +29,10 @@
           <h2 class="card-title mb-0 h5"><%= t('metadata') %></h2>
         </div>
         <div class="card-body">
-          <h3 class="h5"><%= Communication::Website::Page.human_attribute_name('slug') %></h3>
-          <p><%= @page.slug %></p>
+          <% unless @page.slug.blank? %>
+            <h3 class="h5"><%= Communication::Website::Page.human_attribute_name('slug') %></h3>
+            <p><%= @page.slug %></p>
+          <% end %>
           <h3 class="h5"><%= Communication::Website::Page.human_attribute_name('path') %></h3>
           <p><%= @page.path %></p>
           <h3 class="h5"><%= Communication::Website::Page.human_attribute_name('published') %></h3>
@@ -58,7 +59,7 @@
           <% if @page.children.any? %>
             <h3 class="h5"><%= Communication::Website::Page.human_attribute_name('children') %></h3>
             <ul class="list-unstyled mb-0">
-              <% @page.children.each do |child| %>
+              <% @page.children.ordered.each do |child| %>
                 <li>
                   <%= link_to_if can?(:read, child),
                                 child,
diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml
index de87b6357..1be1ea70f 100644
--- a/config/locales/communication/fr.yml
+++ b/config/locales/communication/fr.yml
@@ -89,16 +89,16 @@ fr:
       communication/website/page:
         breadcrumb_title: Nom affiché dans le fil d'ariane
         children: Pages enfants
-        description: Description (SEO)
+        description: Meta Description
         description_short: Description courte
         featured_image: Image à la une
         featured_image_alt: Texte alternatif
-        header_text: Texte affiché dans le header
+        header_text: Titre affiché dans le header
         parent: Page parente
         published: Publié ?
         related_category: Catégorie liée
         slug: Slug
-        text: Texte de la page
+        text: Texte principal de la page
         title: Titre
         website: Site Web
       communication/website/post:
@@ -231,6 +231,61 @@ fr:
             title: Équipe pédagogique
       last_pages: Dernières pages
       last_posts: Dernières actualités
+      pages:
+        defaults:
+          administrators:
+            description_short: Liste des membres de l'équipe administrative
+            slug: equipe-administrative
+            title: Équipe administrative
+          authors:
+            description_short: Liste des membres de l'équipe éditoriale
+            slug: equipe-editoriale
+            title: Équipe éditoriale
+          communication_posts:
+            description_short: Liste des actualités
+            slug: actualites
+            title: Actualités
+          education_programs:
+            description_short: Liste des formations proposées
+            slug: offre-de-formation
+            title: "L'offre de formation"
+          home:
+            description_short: ''
+            slug: ''
+            title: Accueil
+          legal_terms:
+            description_short: ''
+            slug: mentions-legales
+            title: Mentions légales
+          people:
+            description_short: Liste des membres de l'équipe
+            slug: equipe
+            title: Équipe
+          privacy_policy:
+            description_short: ''
+            slug: politique-de-confidentialite
+            title: Politique de confidentialité
+          research_articles:
+            description_short: Liste des articles
+            slug: articles
+            title: Articles
+          research_volumes:
+            description_short: Liste des volumes
+            slug: volumes
+            title: Volumes
+          researchers:
+            description_short: Liste des membres de l'équipe de recherche
+            slug: equipe-de-recherche
+            title: Équipe de recherche
+          sitemap:
+            description_short: ''
+            slug: plan-du-site
+            title: Plan du site
+          teachers:
+            description_short: Liste des membres de l'équipe pédagogique
+            slug: equipe-pedagogique
+            title: Équipe pédagogique
+
       posts:
         new_curation: Nouvelle curation
       see_all: Voir la liste complète (%{number} éléments)
@@ -268,3 +323,4 @@ fr:
     hints:
       communication_website_page:
         breadcrumb_title: Si ce champ est vide le titre de la page sera utilisé dans le fil d'Ariane.
+        description: Si ce champ est vide la "description courte" sera utilisée. Si elle est également vide le début du texte principal sera utilisé.
diff --git a/config/locales/en.yml b/config/locales/en.yml
index e15d7f974..d8c2241b8 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -134,6 +134,7 @@ en:
   save: Save
   search: Search
   select_language: Select language
+  seo: SEO
   simple_form:
     error_notification:
       default_message: "Please review the problems below:"
diff --git a/config/locales/fr.yml b/config/locales/fr.yml
index cfa6f2e2a..9c50eb42a 100644
--- a/config/locales/fr.yml
+++ b/config/locales/fr.yml
@@ -134,6 +134,7 @@ fr:
   save: Enregistrer
   search: Rechercher
   select_language: Sélectionnez une langue
+  seo: SEO
   simple_form:
     error_notification:
       default_message: "Les erreurs ci-dessous empêchent la validation :"
diff --git a/db/schema.rb b/db/schema.rb
index e0da33f45..4039e1e37 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -320,6 +320,9 @@ ActiveRecord::Schema.define(version: 2022_04_04_140813) do
     t.string "featured_image_alt"
     t.text "text"
     t.text "description_short"
+    t.string "breadcrumb_title"
+    t.text "header_text"
+    t.integer "kind"
     t.index ["about_type", "about_id"], name: "index_communication_website_pages_on_about"
     t.index ["communication_website_id"], name: "index_communication_website_pages_on_communication_website_id"
     t.index ["parent_id"], name: "index_communication_website_pages_on_parent_id"
-- 
GitLab