From 3778b5774c5c6c98c3049f31ba24d7e737e90133 Mon Sep 17 00:00:00 2001
From: Leila Gonzales <lmg@agiweb.org>
Date: Mon, 8 Aug 2016 10:20:53 -0400
Subject: [PATCH] Fixed broken ResultGoogleMapAjax module.

---
 config/vufind/config.ini                      |  5 ++++
 module/VuFind/config/module.config.php        |  2 +-
 .../VuFind/src/VuFind/Recommend/Factory.php   | 15 +++++++++++
 .../VuFind/Recommend/ResultGoogleMapAjax.php  | 27 +++++++++++++++++++
 .../Recommend/ResultGoogleMapAjax.phtml       | 24 ++++++++++-------
 5 files changed, 63 insertions(+), 10 deletions(-)

diff --git a/config/vufind/config.ini b/config/vufind/config.ini
index bf19240ef0d..83b02699af2 100644
--- a/config/vufind/config.ini
+++ b/config/vufind/config.ini
@@ -723,6 +723,11 @@ authors         = Wikipedia
 ; "google"
 ;recordMap = google
 
+; If you set recordMap = google, then Google requires that you obtain an API key.
+; For more information on obtaining an API key, see:
+; https://developers.google.com/maps/documentation/javascript/get-api-key#key
+;googleMapApiKey = "your-key-here"
+
 ; This section controls the behavior of the cover generator when makeDynamicCovers
 ; above is non-false.
 [DynamicCovers]
diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php
index 9fc55eff7fb..c59aaf79216 100644
--- a/module/VuFind/config/module.config.php
+++ b/module/VuFind/config/module.config.php
@@ -421,6 +421,7 @@ $config = [
                     'europeanaresults' => 'VuFind\Recommend\Factory::getEuropeanaResults',
                     'expandfacets' => 'VuFind\Recommend\Factory::getExpandFacets',
                     'favoritefacets' => 'VuFind\Recommend\Factory::getFavoriteFacets',
+                    'resultgooglemapajax' => 'VuFind\Recommend\Factory::getResultGoogleMapAjax',
                     'sidefacets' => 'VuFind\Recommend\Factory::getSideFacets',
                     'randomrecommend' => 'VuFind\Recommend\Factory::getRandomRecommend',
                     'summonbestbets' => 'VuFind\Recommend\Factory::getSummonBestBets',
@@ -442,7 +443,6 @@ $config = [
                     'openlibrarysubjectsdeferred' => 'VuFind\Recommend\OpenLibrarySubjectsDeferred',
                     'pubdatevisajax' => 'VuFind\Recommend\PubDateVisAjax',
                     'removefilters' => 'VuFind\Recommend\RemoveFilters',
-                    'resultgooglemapajax' => 'VuFind\Recommend\ResultGoogleMapAjax',
                     'spellingsuggestions' => 'VuFind\Recommend\SpellingSuggestions',
                     'summonbestbetsdeferred' => 'VuFind\Recommend\SummonBestBetsDeferred',
                     'summondatabasesdeferred' => 'VuFind\Recommend\SummonDatabasesDeferred',
diff --git a/module/VuFind/src/VuFind/Recommend/Factory.php b/module/VuFind/src/VuFind/Recommend/Factory.php
index e031afb54d1..6b8bd25320c 100644
--- a/module/VuFind/src/VuFind/Recommend/Factory.php
+++ b/module/VuFind/src/VuFind/Recommend/Factory.php
@@ -197,6 +197,21 @@ class Factory
         );
     }
 
+    /**
+     * Factory for ResultGoogleMapAjax Recommendations.
+     *
+     * @param ServiceManager $sm Service manager.
+     *
+     * @return ResultGoogleMapAjax
+     */
+    public static function getResultGoogleMapAjax(ServiceManager $sm)
+    {
+        $config = $sm->getServiceLocator()->get('VuFind\Config')->get('config');
+        $key = isset($config->Content->googleMapApiKey)
+            ? $config->Content->googleMapApiKey : null;
+        return new ResultGoogleMapAjax($key);
+    }
+
     /**
      * Factory for SideFacets module.
      *
diff --git a/module/VuFind/src/VuFind/Recommend/ResultGoogleMapAjax.php b/module/VuFind/src/VuFind/Recommend/ResultGoogleMapAjax.php
index f68d068d31a..8fa8e71c6ad 100644
--- a/module/VuFind/src/VuFind/Recommend/ResultGoogleMapAjax.php
+++ b/module/VuFind/src/VuFind/Recommend/ResultGoogleMapAjax.php
@@ -50,6 +50,23 @@ class ResultGoogleMapAjax implements RecommendInterface
      */
     protected $searchObject;
 
+    /**
+     * GoogleMapAPI key from config.ini.
+     *
+     * @var string
+     */
+    protected $googleMapApiKey;
+
+    /**
+     * Constructor
+     *
+     * @param string $key API key
+     */
+    public function __construct($key)
+    {
+        $this->googleMapApiKey = $key;
+    }
+
     /**
      * Store the configuration of the recommendation module.
      *
@@ -93,6 +110,16 @@ class ResultGoogleMapAjax implements RecommendInterface
         $this->searchObject = $results;
     }
 
+    /**
+     * Get the Google Maps API key.
+     *
+     * @return string
+     */
+    public function getGoogleMapApiKey()
+    {
+        return $this->googleMapApiKey;
+    }
+
     /**
      * Get search parameters
      *
diff --git a/themes/bootstrap3/templates/Recommend/ResultGoogleMapAjax.phtml b/themes/bootstrap3/templates/Recommend/ResultGoogleMapAjax.phtml
index f2c9cdfb6a3..ba651a91b78 100644
--- a/themes/bootstrap3/templates/Recommend/ResultGoogleMapAjax.phtml
+++ b/themes/bootstrap3/templates/Recommend/ResultGoogleMapAjax.phtml
@@ -1,7 +1,7 @@
 <?
   $searchParams = $this->recommend->getSearchParams();
 
-  $this->headScript()->appendFile('https://maps.googleapis.com/maps/api/js?v=3.8&sensor=false&language='.$this->layout()->userLang);
+  $this->headScript()->appendFile('https://maps.googleapis.com/maps/api/js?key=' . urlencode($this->recommend->getGoogleMapApiKey()) . '&sensor=false&language='.$this->layout()->userLang);
   $this->headScript()->appendFile('vendor/markerclusterer.min.js');
 ?>
 <script type="text/javascript">
@@ -44,7 +44,7 @@ ClusterIcon.prototype.onAdd = function () {
   this.div_ = document.createElement("div");
   this.div_.className = "clusterDiv";
   if (this.visible_) {
-    this.removeClass('hidden');
+    $(this).removeClass('hidden');
   }
 
   this.getPanes().overlayMouseTarget.appendChild(this.div_);
@@ -86,7 +86,7 @@ ClusterIcon.prototype.onAdd = function () {
  * @type {string}
  * @constant
  */
-MarkerClusterer.IMAGE_PATH = "https://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclustererplus/images/m";
+MarkerClusterer.IMAGE_PATH = "https://raw.githubusercontent.com/googlemaps/js-marker-clusterer/gh-pages/images/m";
 
 var markers;
 var mc;
@@ -116,20 +116,25 @@ var infowindow = new google.maps.InfoWindow({maxWidth: 480, minWidth: 480});
       };
       map = new google.maps.Map(document.getElementById("map_canvas"),
           myOptions);
-      //mc = new MarkerClusterer(map);
-      showMarkers();
+
+      var useGmm = document.getElementById('usegmm');
+      google.maps.event.addDomListener(useGmm, 'click', refreshMap);
+
       var checkbx = document.getElementById("useCluster");
       var wrap = document.getElementById("mapWrap");
       wrap.style.display = "block";
       checkbx.style.display = "block";
+
+      showMarkers();
     });
   }
   function showMarkers(){
+    markers = [];
+
     deleteOverlays();
-    if(mc != null) {
+    if (mc) {
       mc.clearMarkers();
     }
-    markers = [];
 
     for (var i = 0; i<markersData.length; i++){
       var disTitle = markersData[i].title;
@@ -159,7 +164,7 @@ var infowindow = new google.maps.InfoWindow({maxWidth: 480, minWidth: 480});
       mc = new MarkerClusterer(map, markers);
     } else {
       for (var i = 0; i < markers.length; i++) {
-        map.addOverlay(markers[i]);
+        markers[i].setMap(map);
       }
     }
   }
@@ -187,6 +192,7 @@ var infowindow = new google.maps.InfoWindow({maxWidth: 480, minWidth: 480});
       }
   }
   function refreshMap() {
+    deleteOverlays();
     showMarkers();
   }
 
@@ -197,7 +203,7 @@ var infowindow = new google.maps.InfoWindow({maxWidth: 480, minWidth: 480});
   <div class="mapClusterToggle" id="useCluster">
     <div class="checkbox">
       <label for="usegmm">
-        <input type="checkbox" id="usegmm" checked="true" onclick="refreshMap();"></input>
+        <input type="checkbox" id="usegmm" checked="true"></input>
         <?=$this->transEsc('google_map_cluster_points') ?>
       </label>
     </div>
-- 
GitLab