From db604aac56ba7fa01dd8c4a044077cca943e03aa Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Mon, 13 Mar 2023 11:47:38 +0100
Subject: [PATCH] export

---
 Gemfile                                       |  1 +
 Gemfile.lock                                  |  9 +++
 .../extranets/contacts_controller.rb          |  4 +-
 app/models/concerns/with_country.rb           | 20 ++++++
 app/models/concerns/with_geolocation.rb       |  4 +-
 app/models/education/school.rb                |  1 +
 app/models/research/laboratory.rb             |  1 +
 app/models/university.rb                      |  1 +
 app/models/university/organization.rb         |  1 +
 app/models/university/person.rb               |  1 +
 .../extranets/contacts/index.html.erb         |  6 +-
 .../contacts/organizations.xlsx.axlsx         | 60 ++++++++++++++++++
 .../extranets/contacts/people.xlsx.axlsx      | 62 +++++++++++++++++++
 .../extranets/contacts/persons.xlsx.axlsx     | 14 -----
 .../admin/education/schools/static.html.erb   |  2 +-
 .../research/laboratories/static.html.erb     |  2 +-
 .../university/organizations/show.html.erb    |  2 +-
 .../university/people/_main_infos.html.erb    |  8 ++-
 .../extranet/organizations/show.html.erb      |  2 +-
 config/initializers/mime_types.rb             |  5 +-
 20 files changed, 176 insertions(+), 30 deletions(-)
 create mode 100644 app/models/concerns/with_country.rb
 create mode 100644 app/views/admin/communication/extranets/contacts/organizations.xlsx.axlsx
 create mode 100644 app/views/admin/communication/extranets/contacts/people.xlsx.axlsx
 delete mode 100644 app/views/admin/communication/extranets/contacts/persons.xlsx.axlsx

diff --git a/Gemfile b/Gemfile
index 63370895b..2ba0ccf69 100644
--- a/Gemfile
+++ b/Gemfile
@@ -14,6 +14,7 @@ gem "bootstrap5-kaminari-views"
 gem "breadcrumbs_on_rails"
 gem "bugsnag"
 gem "cancancan", "3.3.0"
+gem "caxlsx_rails", "~> 0.6.3"
 gem "cocoon", "~> 1.2"
 gem "country_select"
 gem "curation"#, path: "../../arnaudlevy/curation"
diff --git a/Gemfile.lock b/Gemfile.lock
index b067b1495..5ace75a66 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -145,6 +145,14 @@ GEM
       rack-test (>= 0.6.3)
       regexp_parser (>= 1.5, < 3.0)
       xpath (~> 3.2)
+    caxlsx (3.3.0)
+      htmlentities (~> 4.3, >= 4.3.4)
+      marcel (~> 1.0)
+      nokogiri (~> 1.10, >= 1.10.4)
+      rubyzip (>= 1.3.0, < 3)
+    caxlsx_rails (0.6.3)
+      actionpack (>= 3.1)
+      caxlsx (>= 3.0)
     cocoon (1.2.15)
     concurrent-ruby (1.2.0)
     countries (5.3.1)
@@ -568,6 +576,7 @@ DEPENDENCIES
   byebug
   cancancan (= 3.3.0)
   capybara (>= 3.26)
+  caxlsx_rails (~> 0.6.3)
   cocoon (~> 1.2)
   country_select
   curation
diff --git a/app/controllers/admin/communication/extranets/contacts_controller.rb b/app/controllers/admin/communication/extranets/contacts_controller.rb
index 874d4cf96..7ef7bd148 100644
--- a/app/controllers/admin/communication/extranets/contacts_controller.rb
+++ b/app/controllers/admin/communication/extranets/contacts_controller.rb
@@ -1,10 +1,10 @@
 class Admin::Communication::Extranets::ContactsController < Admin::Communication::Extranets::ApplicationController
   def index
-    @persons = current_university.people.ordered
+    @people = current_university.people.ordered
     @organizations = current_university.organizations.ordered
     respond_to do |format|
       format.html {
-        @persons = @persons.page params[:persons_page]
+        @people = @people.page params[:persons_page]
         @organizations = @organizations.page params[:organizations_page]
       }
       format.xlsx {
diff --git a/app/models/concerns/with_country.rb b/app/models/concerns/with_country.rb
new file mode 100644
index 000000000..0dcc66b76
--- /dev/null
+++ b/app/models/concerns/with_country.rb
@@ -0,0 +1,20 @@
+module WithCountry
+  extend ActiveSupport::Concern
+
+  def country_name
+    return if country_iso.blank?
+    country_iso.translations[I18n.locale.to_s] || country_common_name
+  end
+
+  def country_common_name
+    return if country_iso.blank?
+    country_iso.common_name || country_iso.iso_short_name
+  end
+
+  private
+
+  def country_iso
+    return @country_iso if defined?(@country_iso)
+    @country_iso ||= country.blank? ? nil : ISO3166::Country[country]
+  end
+end
diff --git a/app/models/concerns/with_geolocation.rb b/app/models/concerns/with_geolocation.rb
index df92252bd..2e975c0c7 100644
--- a/app/models/concerns/with_geolocation.rb
+++ b/app/models/concerns/with_geolocation.rb
@@ -17,7 +17,7 @@ module WithGeolocation
     string += "#{address}<br>" if address.present?
     string += "#{address_additional}<br>" if address_additional.present?
     string += "#{zipcode} #{city}"
-    string += "<br>#{ISO3166::Country[country]}" if country
+    string += "<br>#{country_common_name}" if country
     string
   end
 
@@ -42,4 +42,4 @@ module WithGeolocation
   def full_street_address_changed?
     address_changed? || zipcode_changed? || city_changed?
   end
-end
\ No newline at end of file
+end
diff --git a/app/models/education/school.rb b/app/models/education/school.rb
index 78ab3e1fc..b98be91e9 100644
--- a/app/models/education/school.rb
+++ b/app/models/education/school.rb
@@ -25,6 +25,7 @@
 #
 class Education::School < ApplicationRecord
   include Sanitizable
+  include WithCountry
   include WithGit
   include Aboutable
   include WithPrograms # must come before WithAlumni and WithTeam
diff --git a/app/models/research/laboratory.rb b/app/models/research/laboratory.rb
index a70817fe9..78a882d25 100644
--- a/app/models/research/laboratory.rb
+++ b/app/models/research/laboratory.rb
@@ -23,6 +23,7 @@
 class Research::Laboratory < ApplicationRecord
   include Aboutable
   include Sanitizable
+  include WithCountry
   include WithGit
 
   belongs_to  :university
diff --git a/app/models/university.rb b/app/models/university.rb
index 321f21333..832286fc4 100644
--- a/app/models/university.rb
+++ b/app/models/university.rb
@@ -41,6 +41,7 @@ class University < ApplicationRecord
   # We don't include Sanitizable because too many complex attributes. We handle it below.
   include WithPeopleAndOrganizations
   include WithCommunication
+  include WithCountry
   include WithEducation
   include WithIdentifier
   include WithInvoice
diff --git a/app/models/university/organization.rb b/app/models/university/organization.rb
index 26ec7059d..54f1e9ba8 100644
--- a/app/models/university/organization.rb
+++ b/app/models/university/organization.rb
@@ -43,6 +43,7 @@ class University::Organization < ApplicationRecord
   include Sanitizable
   include WithBlobs
   include WithBlocks
+  include WithCountry
   include WithGeolocation
   include WithGit
   include WithPermalink
diff --git a/app/models/university/person.rb b/app/models/university/person.rb
index 59a9e253f..1475b5bee 100644
--- a/app/models/university/person.rb
+++ b/app/models/university/person.rb
@@ -57,6 +57,7 @@ class University::Person < ApplicationRecord
   include WithUniversity
   include WithGit
   include WithBlobs
+  include WithCountry
   include WithEducation
   include WithExperiences
   include WithSlug
diff --git a/app/views/admin/communication/extranets/contacts/index.html.erb b/app/views/admin/communication/extranets/contacts/index.html.erb
index d79fc8d55..d737b2393 100644
--- a/app/views/admin/communication/extranets/contacts/index.html.erb
+++ b/app/views/admin/communication/extranets/contacts/index.html.erb
@@ -2,13 +2,13 @@
 
 <%= render 'admin/communication/extranets/sidebar' do %>
   <% action = link_to t('export'),
-                    admin_communication_extranet_contacts_path(extranet_id: @extranet.id, export: 'persons', format: :xlsx),
+                    admin_communication_extranet_contacts_path(extranet_id: @extranet.id, export: 'people', format: :xlsx),
                     class: button_classes('ms-1') if can?(:show, University::Person) %>
   <%= osuny_panel University::Person.model_name.human(count: 2), action: action do %>
     <div class="table-responsive">
       <table class="<%= table_classes%>">
         <tbody>
-          <% @persons.each do |person| %>
+          <% @people.each do |person| %>
             <tr>
               <td><%= link_to person, [:admin, person] %></td>
               <td><%= person.email %></td>
@@ -38,7 +38,7 @@
         </tbody>
       </table>
     </div>
-    <%= paginate @persons, theme: 'bootstrap-5', param_name: :persons_page %>
+    <%= paginate @people, theme: 'bootstrap-5', param_name: :persons_page %>
   <% end %>
   <% action = link_to t('export'),
                     admin_communication_extranet_contacts_path(extranet_id: @extranet.id, export: 'organizations', format: :xlsx),
diff --git a/app/views/admin/communication/extranets/contacts/organizations.xlsx.axlsx b/app/views/admin/communication/extranets/contacts/organizations.xlsx.axlsx
new file mode 100644
index 000000000..a0e20d194
--- /dev/null
+++ b/app/views/admin/communication/extranets/contacts/organizations.xlsx.axlsx
@@ -0,0 +1,60 @@
+wb = xlsx_package.workbook
+wb.add_worksheet(name: "index") do |sheet|
+  options = [
+    University::Organization.human_attribute_name('name'),
+    University::Organization.human_attribute_name('address_name'),
+    University::Organization.human_attribute_name('address'),
+    University::Organization.human_attribute_name('address_additional'),
+    University::Organization.human_attribute_name('zipcode'),
+    University::Organization.human_attribute_name('city'),
+    University::Organization.human_attribute_name('country'),
+    University::Organization.human_attribute_name('kind'),
+    University::Organization.human_attribute_name('siren'),
+    University::Organization.human_attribute_name('phone'),
+    University::Organization.human_attribute_name('url'),
+    University::Organization.human_attribute_name('email'),
+    University::Organization.human_attribute_name('linkedin'),
+    University::Organization.human_attribute_name('twitter')
+    ]
+
+  types = [
+    :string,
+    :string,
+    :string,
+    :string,
+    :string,
+    :string,
+    :string,
+    :string,
+    :string,
+    :string,
+    :string,
+    :string,
+    :string,
+    :string,
+    :string
+  ]
+
+  sheet.add_row options, types: Array.new(types.length) { |_| :string }
+
+  @organizations.each do |organization|
+    infos = [
+      organization.name,
+      organization.address_name,
+      organization.address,
+      organization.address_additional,
+      organization.zipcode,
+      organization.city,
+      organization.country_name,
+      organization.kind_i18n,
+      organization.siren,
+      organization.phone,
+      organization.url,
+      organization.email,
+      organization.linkedin,
+      organization.twitter.present? ? "@#{organization.twitter}" : ''
+    ]
+
+    sheet.add_row infos, types: types
+  end
+end
diff --git a/app/views/admin/communication/extranets/contacts/people.xlsx.axlsx b/app/views/admin/communication/extranets/contacts/people.xlsx.axlsx
new file mode 100644
index 000000000..c140b04f0
--- /dev/null
+++ b/app/views/admin/communication/extranets/contacts/people.xlsx.axlsx
@@ -0,0 +1,62 @@
+wb = xlsx_package.workbook
+wb.add_worksheet(name: "index") do |sheet|
+  options = [
+    University::Person.human_attribute_name('last_name'),
+    University::Person.human_attribute_name('first_name'),
+    University::Person.human_attribute_name('email'),
+    University::Person.human_attribute_name('gender'),
+    University::Person.human_attribute_name('address'),
+    University::Person.human_attribute_name('zipcode'),
+    University::Person.human_attribute_name('city'),
+    University::Person.human_attribute_name('country'),
+    University::Person.human_attribute_name('birthdate'),
+    University::Person.human_attribute_name('phone_personal'),
+    University::Person.human_attribute_name('phone_professional'),
+    University::Person.human_attribute_name('phone_mobile'),
+    University::Person.human_attribute_name('url'),
+    University::Person.human_attribute_name('linkedin'),
+    University::Person.human_attribute_name('twitter')
+    ]
+
+  types = [
+    :string,
+    :string,
+    :string,
+    :string,
+    :string,
+    :string,
+    :string,
+    :string,
+    :string,
+    :string,
+    :string,
+    :string,
+    :string,
+    :string,
+    :string
+  ]
+
+  sheet.add_row options, types: Array.new(types.length) { |_| :string }
+
+  @people.each do |person|
+    infos = [
+      person.last_name,
+      person.first_name,
+      person.email,
+      person.gender.present? ? t("activerecord.attributes.university/person.genders.#{person.gender}") : '',
+      person.address,
+      person.zipcode,
+      person.city,
+      person.country_name,
+      person.birthdate.present? ? l(person.birthdate, format: :birthday) : '',
+      person.phone_personal,
+      person.phone_professional,
+      person.phone_mobile,
+      person.url,
+      person.linkedin,
+      person.twitter.present? ? "@#{person.twitter}" : ''
+    ]
+
+    sheet.add_row infos, types: types
+  end
+end
diff --git a/app/views/admin/communication/extranets/contacts/persons.xlsx.axlsx b/app/views/admin/communication/extranets/contacts/persons.xlsx.axlsx
deleted file mode 100644
index 47e62236f..000000000
--- a/app/views/admin/communication/extranets/contacts/persons.xlsx.axlsx
+++ /dev/null
@@ -1,14 +0,0 @@
-@users ||= users
-
-wb = xlsx_package.workbook
-wb.add_worksheet(name: "index") do |sheet|
-
-  headers = User::xls_header_for(current_brand)
-
-  # for headers types are always :string (because those are labels)
-  sheet.add_row headers[:labels], types: Array.new(headers[:types].length) { |_| :string }
-
-  @users.each do |user|
-    sheet.add_row user.xls_basic_row, types: headers[:types]
-  end
-end
diff --git a/app/views/admin/education/schools/static.html.erb b/app/views/admin/education/schools/static.html.erb
index bb1396b0e..01bafc821 100644
--- a/app/views/admin/education/schools/static.html.erb
+++ b/app/views/admin/education/schools/static.html.erb
@@ -9,7 +9,7 @@ zipcode: >
 city: >
   <%= @about.city %>
 country: >
-  <%= ISO3166::Country[@about.country].translations[@about.country.downcase] %>
+  <%= @about.country_common_name %>
 phone: >
   <%= @about.phone %>
 <% if administrator_involvements.any? %>
diff --git a/app/views/admin/research/laboratories/static.html.erb b/app/views/admin/research/laboratories/static.html.erb
index 01b70fa3d..e294a824d 100644
--- a/app/views/admin/research/laboratories/static.html.erb
+++ b/app/views/admin/research/laboratories/static.html.erb
@@ -8,5 +8,5 @@ zipcode: >
 city: >
   <%= @about.city %>
 country: >
-  <%= ISO3166::Country[@about.country].translations[@about.country.downcase] %>
+  <%= @about.country_common_name %>
 ---
diff --git a/app/views/admin/university/organizations/show.html.erb b/app/views/admin/university/organizations/show.html.erb
index 46dd34a5f..fdf545477 100644
--- a/app/views/admin/university/organizations/show.html.erb
+++ b/app/views/admin/university/organizations/show.html.erb
@@ -26,7 +26,7 @@
         <% if @organization.country.present? %>
           <div class="col-xxl-6">
             <%= osuny_label University::Organization.human_attribute_name('country') %>
-            <p><%= ISO3166::Country[@organization.country].common_name %></p>
+            <p><%= @organization.country_name %></p>
           </div>
         <% end %>
         <% if @organization.geolocated? %>
diff --git a/app/views/admin/university/people/_main_infos.html.erb b/app/views/admin/university/people/_main_infos.html.erb
index caf29a011..9211f6b14 100644
--- a/app/views/admin/university/people/_main_infos.html.erb
+++ b/app/views/admin/university/people/_main_infos.html.erb
@@ -29,7 +29,7 @@
             <p><%= l(person.birthdate, format: :birthday) %></p>
            </div>
         <% end %>
-        <% [:address, :zipcode, :city, :country].each do |property| %>
+        <% [:address, :zipcode, :city].each do |property| %>
           <% value = person.send property %>
           <% next if value.blank? %>
           <div class="col-md-6">
@@ -37,6 +37,12 @@
             <p><%= value %></p>
           </div>
         <% end %>
+        <% if person.country.present? %>
+          <div class="col-md-6">
+            <%= osuny_label University::Person.human_attribute_name(:country) %>
+            <p><%= person.country_name %></p>
+          </div>
+        <% end %>
       </div>
       <% unless person.biography.blank? %>
         <%= osuny_label University::Person.human_attribute_name('biography') %>
diff --git a/app/views/extranet/organizations/show.html.erb b/app/views/extranet/organizations/show.html.erb
index 0c6394a26..0042d2c29 100644
--- a/app/views/extranet/organizations/show.html.erb
+++ b/app/views/extranet/organizations/show.html.erb
@@ -56,7 +56,7 @@
         <% if @organization.country.present? %>
           <div class="col-xxl-6">
             <%= osuny_label University::Organization.human_attribute_name('country') %>
-            <p><%= ISO3166::Country[@organization.country].common_name %></p>
+            <p><%= @organization.country_common_name %></p>
           </div>
         <% end %>
         <% if @organization.geolocated? %>
diff --git a/config/initializers/mime_types.rb b/config/initializers/mime_types.rb
index e7f0e9b10..07ca293a6 100644
--- a/config/initializers/mime_types.rb
+++ b/config/initializers/mime_types.rb
@@ -1,6 +1,3 @@
 # Be sure to restart your server when you modify this file.
-
-# Add new mime types for use in respond_to blocks:
-# Mime::Type.register "text/richtext", :rtf
-# Mime::Type.register "text/css", :css
+Mime::Type.register "application/xls", :xlsx
 Mime::Type.register "application/xml", :xml, %w( text/xml application/x-xml )
-- 
GitLab