From 29af85dbfa6518bd1ccdc9e5d511ac6fe5c15cc3 Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Wed, 27 Oct 2021 16:21:41 +0200
Subject: [PATCH] import categories

---
 .../communication/websites_controller.rb      |  1 +
 app/models/communication/website/category.rb  |  3 +-
 .../communication/websites/import.html.erb    | 28 +++++++++++++++++++
 config/locales/communication/en.yml           |  6 ++--
 config/locales/communication/fr.yml           |  4 ++-
 5 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/app/controllers/admin/communication/websites_controller.rb b/app/controllers/admin/communication/websites_controller.rb
index 66cff8d06..48bf0cbcd 100644
--- a/app/controllers/admin/communication/websites_controller.rb
+++ b/app/controllers/admin/communication/websites_controller.rb
@@ -22,6 +22,7 @@ class Admin::Communication::WebsitesController < Admin::Communication::Applicati
     @imported_website = @website.imported_website
     @imported_pages = @imported_website.pages.page params[:pages_page]
     @imported_posts = @imported_website.posts.page params[:posts_page]
+    @imported_categories = @imported_website.categories
     @imported_media = @imported_website.media.includes(file_attachment: :blob ).page params[:media_page]
     @imported_media_total_size = @imported_website.media.joins(file_attachment: :blob).sum(:byte_size)
     breadcrumb
diff --git a/app/models/communication/website/category.rb b/app/models/communication/website/category.rb
index 23b4e519b..108c7e5fc 100644
--- a/app/models/communication/website/category.rb
+++ b/app/models/communication/website/category.rb
@@ -36,7 +36,8 @@ class Communication::Website::Category < ApplicationRecord
             optional: true
   has_many   :children,
             class_name: 'Communication::Website::Category',
-            foreign_key: :parent_id
+            foreign_key: :parent_id,
+            dependent: :destroy
   has_and_belongs_to_many :posts,
                           class_name: 'Communication::Website::Post',
                           join_table: 'communication_website_categories_posts',
diff --git a/app/views/admin/communication/websites/import.html.erb b/app/views/admin/communication/websites/import.html.erb
index 0274d1e7a..0510ac65e 100644
--- a/app/views/admin/communication/websites/import.html.erb
+++ b/app/views/admin/communication/websites/import.html.erb
@@ -103,3 +103,31 @@
     <%= paginate @imported_media, param_name: :media_page, theme: 'bootstrap-5' %>
   </div>
 </div>
+
+<div class="card mt-5">
+  <div class="card-header">
+    <h2><%= @imported_categories.count %> categories</h2>
+  </div>
+  <table class="<%= table_classes %>">
+    <thead>
+      <tr>
+        <th><%= Communication::Website::Category.human_attribute_name('name') %></th>
+        <th><%= Communication::Website::Imported::Category.human_attribute_name('original') %></th>
+        <th class="text-end" width="150">&nbsp;</th>
+      </tr>
+    </thead>
+    <tbody>
+      <% @imported_categories.each do |category| %>
+        <tr>
+          <td><%= category.name %></td>
+          <td class="small"><%= link_to category.url, category.url, target: :_blank %></td>
+          <td class="text-end">
+            <%= link_to t('show'),
+                          admin_communication_website_category_path(website_id: category.category.website.id, id: category.category.id),
+                          class: button_classes %>
+          </td>
+        </tr>
+      <% end %>
+    </tbody>
+  </table>
+</div>
diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml
index 6d084a518..0805e93a7 100644
--- a/config/locales/communication/en.yml
+++ b/config/locales/communication/en.yml
@@ -52,13 +52,15 @@ en:
       communication/website/category:
         children: Children categories
         description: Description
+        name: Name
         parent: Parent category
         slug: slug
-        title: Title
+      communication/website/imported/category:
+        original: Original
       communication/website/imported/medium:
         filename: Filename
       communication/website/page:
-        children: Children pages 
+        children: Children pages
         title: Title
         description: Description (SEO)
         featured_image: Featured image
diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml
index 05b42a0f2..341b92716 100644
--- a/config/locales/communication/fr.yml
+++ b/config/locales/communication/fr.yml
@@ -51,9 +51,11 @@ fr:
       communication/website/category:
         children: Catégories enfantes
         description: Description
+        name: Nom
         parent: Catégorie parente
         slug: slug
-        title: Titre
+      communication/website/imported/category:
+        original: Original
       communication/website/imported/medium:
         filename: Nom du fichier
       communication/website/page:
-- 
GitLab