From a1b5d202ab9e0a37c34234d726edcd842bff06cf Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Wed, 8 Mar 2023 14:42:58 +0100
Subject: [PATCH] map

---
 Gemfile                                       |  2 ++
 Gemfile.lock                                  | 23 +++++++++++++++++++
 app/assets/javascripts/extranet.js            |  1 +
 app/assets/stylesheets/extranet.sass          |  1 +
 .../stylesheets/extranet/layout/_leaflet.sass |  2 ++
 .../extranet/layout/_typography.sass          |  9 +-------
 app/models/concerns/with_geolocation.rb       | 22 ++++++++++++++++++
 .../university/organizations/show.html.erb    |  4 ++++
 .../contacts/organizations/show.html.erb      | 16 +++++++++++++
 config/initializers/leaflet.rb                |  3 +++
 config/locales/university/en.yml              |  2 ++
 config/locales/university/fr.yml              |  2 ++
 12 files changed, 79 insertions(+), 8 deletions(-)
 create mode 100644 app/assets/stylesheets/extranet/layout/_leaflet.sass
 create mode 100644 config/initializers/leaflet.rb

diff --git a/Gemfile b/Gemfile
index 612a5bc28..63370895b 100644
--- a/Gemfile
+++ b/Gemfile
@@ -27,6 +27,7 @@ gem "font-awesome-sass"
 gem "front_matter_parser"
 gem "gdpr"
 gem "geocoder", "~> 1.8"
+gem "geo_point"
 gem "gitlab"
 gem "hal_openscience", "~> 0.1"
 # gem "hal_openscience", path: "../hal_openscience"
@@ -38,6 +39,7 @@ gem "jquery-rails"
 gem "jquery-ui-rails", "~> 6.0.1"
 gem "kamifusen"#, path: "../kamifusen"
 gem "kaminari"
+gem "leaflet-rails"
 gem "mini_magick"
 gem "octokit"
 gem "omniauth-rails_csrf_protection", "~> 1.0"
diff --git a/Gemfile.lock b/Gemfile.lock
index c6747c137..b067b1495 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -221,6 +221,20 @@ GEM
       js_cookie_rails
       rails
       sassc-rails
+    geo_calc (0.7.8)
+      activesupport (>= 3.0.1)
+      geo_units (~> 0.3.2)
+      i18n (>= 0.5)
+      require_all (>= 1.2.0)
+      sugar-high (~> 0.7.2)
+      sweetloader
+    geo_point (0.2.6)
+      geo_calc (>= 0.7.7.1)
+    geo_units (0.3.4)
+      activesupport (>= 4)
+      i18n (>= 0.8)
+      sugar-high (~> 0.7.2)
+      sweetloader (~> 0.1.6)
     geocoder (1.8.1)
     gitlab (4.19.0)
       httparty (~> 0.20)
@@ -277,6 +291,8 @@ GEM
       activerecord
       kaminari-core (= 1.2.2)
     kaminari-core (1.2.2)
+    leaflet-rails (1.9.3)
+      rails (>= 4.2.0)
     listen (3.8.0)
       rb-fsevent (~> 0.10, >= 0.10.3)
       rb-inotify (~> 0.9, >= 0.9.10)
@@ -408,6 +424,7 @@ GEM
       ffi (~> 1.0)
     regexp_parser (2.7.0)
     requests (1.0.2)
+    require_all (3.0.0)
     responders (3.1.0)
       actionpack (>= 5.2)
       railties (>= 5.2)
@@ -479,6 +496,10 @@ GEM
       activesupport (>= 5.2)
       sprockets (>= 3.0.0)
     stringio (3.0.5)
+    sugar-high (0.7.3)
+    sweetloader (0.1.6)
+      activesupport (>= 3.0.1)
+      i18n
     terminal-table (3.0.2)
       unicode-display_width (>= 1.1.1, < 3)
     thor (1.2.1)
@@ -560,6 +581,7 @@ DEPENDENCIES
   font-awesome-sass
   front_matter_parser
   gdpr
+  geo_point
   geocoder (~> 1.8)
   gitlab
   hal_openscience (~> 0.1)
@@ -571,6 +593,7 @@ DEPENDENCIES
   jquery-ui-rails (~> 6.0.1)
   kamifusen
   kaminari
+  leaflet-rails
   listen (~> 3.3)
   mini_magick
   octokit
diff --git a/app/assets/javascripts/extranet.js b/app/assets/javascripts/extranet.js
index ca7451c78..1bb3ee541 100644
--- a/app/assets/javascripts/extranet.js
+++ b/app/assets/javascripts/extranet.js
@@ -10,6 +10,7 @@
 //= require simple_form_bs5_file_input
 //= require summernote/summernote-bs5
 //= require gdpr/cookie_consent
+//= require leaflet
 //= require autocomplete-rails
 //= require_tree ./application/plugins
 //= require_tree ./extranet
diff --git a/app/assets/stylesheets/extranet.sass b/app/assets/stylesheets/extranet.sass
index b0734c2c9..f502e046a 100644
--- a/app/assets/stylesheets/extranet.sass
+++ b/app/assets/stylesheets/extranet.sass
@@ -10,6 +10,7 @@
 @import 'cropperjs/dist/cropper'
 @import 'commons/summernote'
 @import 'commons/bootstrap-icons'
+@import 'leaflet'
 
 // Default
 @import 'extranet/layout/*'
diff --git a/app/assets/stylesheets/extranet/layout/_leaflet.sass b/app/assets/stylesheets/extranet/layout/_leaflet.sass
new file mode 100644
index 000000000..715e7ceff
--- /dev/null
+++ b/app/assets/stylesheets/extranet/layout/_leaflet.sass
@@ -0,0 +1,2 @@
+#map
+    height: 300px
\ No newline at end of file
diff --git a/app/assets/stylesheets/extranet/layout/_typography.sass b/app/assets/stylesheets/extranet/layout/_typography.sass
index 1da69d9fe..023898c34 100644
--- a/app/assets/stylesheets/extranet/layout/_typography.sass
+++ b/app/assets/stylesheets/extranet/layout/_typography.sass
@@ -5,13 +5,6 @@
     font-weight: 400
     src: asset-url("Basier-Square/basiersquare-regular-webfont.woff2") format("woff2"), url("Basier-Square/basiersquare-regular-webfont.woff") format("woff")
 
-@font-face
-    font-display: swap
-    font-family: 'Basier Square'
-    font-style: normal
-    font-weight: 500
-    src: asset-url("Basier-Square/basiersquare-medium-webfont.woff2") format("woff2"), url("Basier-Square/basiersquare-medium-webfont.woff") format("woff")
-
 @font-face
     font-display: swap
     font-family: 'Basier Square'
@@ -21,7 +14,7 @@
 
 h1
     margin-top: 20px
-    font-weight: 500
+    font-weight: 700
     line-height: 125%
     @include media-breakpoint-up(md)
         max-width: 70vw
diff --git a/app/models/concerns/with_geolocation.rb b/app/models/concerns/with_geolocation.rb
index d233db29e..df92252bd 100644
--- a/app/models/concerns/with_geolocation.rb
+++ b/app/models/concerns/with_geolocation.rb
@@ -11,6 +11,28 @@ module WithGeolocation
     "#{address}, #{zipcode} #{city} #{country}"
   end
 
+  def full_address
+    string = ""
+    string += "#{address_name}<br>" if address_name.present?
+    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
+  end
+
+  def geolocated?
+    latitude.present? && longitude.present?
+  end
+
+  def latlong
+    @latlong ||= [latitude, longitude]
+  end
+
+  def geo_point
+    @geo_point ||= GeoPoint.new latitude, longitude
+  end
+
   protected
 
   def full_street_address_present?
diff --git a/app/views/admin/university/organizations/show.html.erb b/app/views/admin/university/organizations/show.html.erb
index 5454b724f..760bf3e64 100644
--- a/app/views/admin/university/organizations/show.html.erb
+++ b/app/views/admin/university/organizations/show.html.erb
@@ -34,6 +34,10 @@
           <% end %>
         </div>
       </div>
+      <% if @organization.geolocated? %>
+        <%= osuny_label University::Organization.human_attribute_name('geolocation') %>
+        <p><%= @organization.geo_point.to_s %></p>
+      <% end %>
     <% end %>
     <%= osuny_panel University::Organization.human_attribute_name('digital') do %>
       <div class="row pure__row--small">
diff --git a/app/views/extranet/contacts/organizations/show.html.erb b/app/views/extranet/contacts/organizations/show.html.erb
index 0a7a6118a..7e294913f 100644
--- a/app/views/extranet/contacts/organizations/show.html.erb
+++ b/app/views/extranet/contacts/organizations/show.html.erb
@@ -13,6 +13,8 @@
   <div class="offset-md-1 col-md-3 order-1 order-md-2">
     <%= kamifusen_tag @organization.logo, width: 400, class: 'img-fluid organization__logo' if @organization.logo.attached? %>
     <dl>
+      <dt><%= University::Organization.human_attribute_name(:address) %></dt>
+      <dd><%= sanitize @organization.full_address %></dd>
       <% if @organization.phone.present? %>
         <dt><%= University::Organization.human_attribute_name(:phone) %></dt>
         <dd><a href="tel:<%= @organization.phone %>" target="_blank" rel="noreferrer"><%= @organization.phone %></a></dd>
@@ -29,6 +31,20 @@
           </a>
         </dd>
       <% end %>
+      <% if @organization.geolocated? %>
+        <dt><%= University::Organization.human_attribute_name(:map) %></dt>
+        <dl><%= map(
+              center: {
+                latlng: @organization.latlong,
+                zoom: 15
+              },
+              markers: [
+                {
+                  latlng: @organization.latlong
+                }
+              ]
+          ) %></dl>
+      <% end %>
     </dl>
   </div>
 </div>
diff --git a/config/initializers/leaflet.rb b/config/initializers/leaflet.rb
new file mode 100644
index 000000000..d0bf69b81
--- /dev/null
+++ b/config/initializers/leaflet.rb
@@ -0,0 +1,3 @@
+Leaflet.tile_layer = "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
+Leaflet.attribution = "Osuny"
+Leaflet.max_zoom = 18
diff --git a/config/locales/university/en.yml b/config/locales/university/en.yml
index fe0f1837a..b71021709 100644
--- a/config/locales/university/en.yml
+++ b/config/locales/university/en.yml
@@ -92,12 +92,14 @@ en:
         description: Personne morale liée à une université, une composante, un laboratoire, etc.
         digital: Digital contact
         email: Email
+        geolocation: Geographical coordinates
         kind: Kind
         legal: Legal information
         linkedin: LinkedIn
         logo: Logo for light backgrounds (default)
         logo_on_dark_background: Logo for dark backgrounds (optional)
         long_name: Long name
+        map: Map
         name: Name
         phone: Telephone
         physical: Contact information
diff --git a/config/locales/university/fr.yml b/config/locales/university/fr.yml
index 00e6aa77c..8214f25cd 100644
--- a/config/locales/university/fr.yml
+++ b/config/locales/university/fr.yml
@@ -93,11 +93,13 @@ fr:
         digital: Coordonnées numériques
         email: Email
         kind: Type
+        geolocation: Coordonnées géographiques
         legal: Informations légales
         linkedin: LinkedIn
         logo: Logo sur fond clair (par défaut)
         logo_on_dark_background: Logo sur fond sombre (optionnel)
         long_name: Nom complet
+        map: Carte
         name: Nom
         phone: Téléphone
         physical: Coordonnées physiques
-- 
GitLab