From 146377e67ad392583c94a6c6b46e34930a13891d Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Tue, 21 Nov 2023 16:53:27 +0100
Subject: [PATCH] init

---
 app/models/concerns/as_backlinkable.rb        | 43 +++++++++++++++++++
 app/models/university/organization.rb         |  1 +
 .../connections/_backlinks.html.erb           | 19 ++++++++
 .../university/organizations/show.html.erb    |  1 +
 .../university/organizations/static.html.erb  |  1 +
 5 files changed, 65 insertions(+)
 create mode 100644 app/models/concerns/as_backlinkable.rb
 create mode 100644 app/views/admin/application/connections/_backlinks.html.erb

diff --git a/app/models/concerns/as_backlinkable.rb b/app/models/concerns/as_backlinkable.rb
new file mode 100644
index 000000000..bf82169c3
--- /dev/null
+++ b/app/models/concerns/as_backlinkable.rb
@@ -0,0 +1,43 @@
+module AsBacklinkable
+  extend ActiveSupport::Concern
+
+  def backlinks_pages(website)
+    backlinks(
+      Communication::Website::Page,
+      website
+    )
+    .collect(&:direct_source)
+    .compact
+    .reject { |page| page.is_special_page? }
+    .reject { |page| !page.published? }
+  end
+
+  def backlinks_posts(website)
+    backlinks(
+      Communication::Website::Post,
+      website
+    )
+    .collect(&:direct_source)
+    .compact
+    .reject { |page| !page.published? }
+  end
+
+  def backlinks_agenda_events(website)
+    backlinks(
+      Communication::Website::Agenda::Event,
+      website
+    )
+    .collect(&:direct_source)
+    .compact
+    .reject { |event| !event.published? }
+  end
+
+  protected
+
+  def backlinks(kind, website)
+    connections.where(
+      direct_source_type: kind.to_s,
+      website_id: website.id
+    )
+  end
+end
\ No newline at end of file
diff --git a/app/models/university/organization.rb b/app/models/university/organization.rb
index 567c053e2..f70d61e3a 100644
--- a/app/models/university/organization.rb
+++ b/app/models/university/organization.rb
@@ -47,6 +47,7 @@
 #  fk_rails_5af11ea0cc  (original_id => university_organizations.id)
 #
 class University::Organization < ApplicationRecord
+  include AsBacklinkable
   include AsIndirectObject
   include Sanitizable
   include WithBlobs
diff --git a/app/views/admin/application/connections/_backlinks.html.erb b/app/views/admin/application/connections/_backlinks.html.erb
new file mode 100644
index 000000000..ecc543a66
--- /dev/null
+++ b/app/views/admin/application/connections/_backlinks.html.erb
@@ -0,0 +1,19 @@
+backlinks:
+  current_website:
+    id: <%= @website.id %>
+    name: "<%= @website %>"
+    pages:
+<% @about.backlinks_pages(@website).each do |element| %>
+      - path: "<%= element.path %>"
+        file: "<%= element.git_path(@website) %>"
+<% end %>
+    posts:
+<% @about.backlinks_posts(@website).each do |element| %>
+      - path: "<%= element.static_path %>"
+        file: "<%= element.git_path(@website) %>"
+<% end %>
+    events:
+<% @about.backlinks_agenda_events(@website).each do |element| %>
+      - path: "<%= element.current_permalink_in_website(@website)&.path %>"
+        file: "<%= element.git_path(@website) %>"
+<% end %>
diff --git a/app/views/admin/university/organizations/show.html.erb b/app/views/admin/university/organizations/show.html.erb
index 032e2489d..0b70bb3bc 100644
--- a/app/views/admin/university/organizations/show.html.erb
+++ b/app/views/admin/university/organizations/show.html.erb
@@ -170,6 +170,7 @@
   </div>
 <% end if @organization.experiences.any? %>
 
+<%= render 'admin/application/connections/list', about: @organization %>
 
 <% content_for :action_bar_left do %>
   <%= destroy_link @organization %>
diff --git a/app/views/admin/university/organizations/static.html.erb b/app/views/admin/university/organizations/static.html.erb
index 9a05c35d0..110b6586c 100644
--- a/app/views/admin/university/organizations/static.html.erb
+++ b/app/views/admin/university/organizations/static.html.erb
@@ -55,6 +55,7 @@ logo_on_dark_background: "<%= @about.logo_on_dark_background.blob.id %>"
 <% end %>
 <%= render 'admin/application/meta_description/static' %>
 <%= render 'admin/application/summary/static' %>
+<%= render 'admin/application/connections/backlinks' %>
 <%= render 'admin/communication/blocks/content/static', about: @about %>
 ---
 <%= prepare_html_for_static @about.text, @about.university %>
-- 
GitLab