commit 72a438f39663a59abc93bb77198563e21439de22
Author: vastja <vastja@gmail.com>
Date:   Wed May 20 15:57:52 2020 +0200

    + location persistence
    + last available collections
            + available collections code rework

diff --git a/website/public/js/zcu-heatmap.js b/website/public/js/zcu-heatmap.js
index a75ab10..f7bbdd0 100644
--- a/website/public/js/zcu-heatmap.js
+++ b/website/public/js/zcu-heatmap.js
@@ -52,6 +52,11 @@ const genPopUp = (place, number, sum, currentPos, maxPos) => {
 }
 // eslint-disable-next-line no-unused-vars
 function initMap () {
+
+  startX = localStorage.getItem('lat') || startX;
+  startY = localStorage.getItem('lng') || startY;
+  startZoom = localStorage.getItem('zoom') || startZoom;
+
   mymap = L.map('heatmap').setView([startX, startY], startZoom)
 
   L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
@@ -125,7 +130,10 @@ function displayInfoText () {
 }
 
 // eslint-disable-next-line no-unused-vars
-function setMapView (latitude = startX, longitude = startY, zoom = startZoom) {
+function setMapView (latitude, longitude, zoom) {
+  localStorage.setItem('lat', latitude);
+  localStorage.setItem('lng', longitude);
+  localStorage.setItem('zoom', zoom);
   mymap.setView([latitude, longitude], zoom)
 }
 
diff --git a/website/src/Controller/HeatmapController.php b/website/src/Controller/HeatmapController.php
index 35b6f0b..de207cc 100644
--- a/website/src/Controller/HeatmapController.php
+++ b/website/src/Controller/HeatmapController.php
@@ -68,4 +68,11 @@ class HeatmapController extends AbstractController {
     public function dataSourcePoistions(IOpenDataManager $manager, $name = 'NONE') {
         return $this->json($manager->getDataSourcePositions($name));
     }
+
+    /**
+     * @Route("heatmap/last", name="last")
+     */
+    public function lastAvailableCollections(IOpenDataManager $manager) {
+        return $this->json($manager->getLastAvailableCollections());
+    }
 }
diff --git a/website/src/IOpenDataManager.php b/website/src/IOpenDataManager.php
index a46d6e8..a9d89e9 100644
--- a/website/src/IOpenDataManager.php
+++ b/website/src/IOpenDataManager.php
@@ -16,4 +16,6 @@ interface IOpenDataManager {
     public function getMaxCollectionNumberAtDay($name, $date);
 
     public function getDataSourcePositions($name);
+
+    public function getLastAvailableCollections();
 }
diff --git a/website/src/OpenDataManager.php b/website/src/OpenDataManager.php
index 640fc8d..ef33d98 100644
--- a/website/src/OpenDataManager.php
+++ b/website/src/OpenDataManager.php
@@ -3,7 +3,6 @@
 namespace App\OpenData;
 
 use MongoDB\Driver\Query;
-use MongoDB\Driver\Command;
 use MongoDB\Driver\Manager;
 
 class OpenDataManager implements IOpenDataManager {
@@ -54,24 +53,39 @@ class OpenDataManager implements IOpenDataManager {
     }
 
     public function isCollectionAvailable($name, $date) {
-        $command = new Command(['listCollections' => 1, 'filter' => ['name' => $name.$date]]);
-        $result = $this->manager->executeCommand('open-data-db', $command)->toArray();
+        $result = $this->manager->executeQuery('open-data-db.'.$name, new Query(['date' => $date], []));
 
-        return !empty($result);
+        return !empty($result->toArray());
     }
 
     public function getDatesWithAvailableCollection() {
-        $command = new Command(['listCollections' => 1]);
-        $result = $this->manager->executeCommand('open-data-db', $command)->toArray();
+        $available = $this->getAvailableCollections();
+        $result = [];
+
+        foreach ($available as $key => $value) {
+            $dates = $this->manager->executeQuery('open-data-db.'.$value['key-name'], new Query([], ['projection' => ['date' => true, '_id' => false]]));
+            $dates->setTypeMap(['root' => 'array']);
+            $result = array_merge($result, array_map(function ($item) {return $item['date']; }, $dates->toArray()));
+        }
+
+        return array_values(array_unique($result));
+    }
+
+    public function getLastAvailableCollections() {
+        $available = $this->getAvailableCollections();
+        $result = [];
+
+        foreach ($available as $key => $value) {
+            $date = $this->manager->executeQuery('open-data-db.'.$value['key-name'], new Query([], ['sort' => ['date' => -1], 'limit' => 1, 'projection' => ['date' => true, '_id' => false]]));
+            $date->setTypeMap(['root' => 'array']);
 
-        $dates = [];
-        foreach ($result as $value) {
-            if (preg_match('/\\d{4}-\\d{2}-\\d{2}/', $value->name)) {
-                array_push($dates, substr($value->name, -10));
+            $date_array = $date->toArray();
+            if (!empty($date_array)) {
+                $result[$value['key-name']] = $date_array[0]['date'];
             }
         }
 
-        return array_values(array_unique($dates));
+        return $result;
     }
 
     public function getMaxCollectionNumberAtDay($name, $date) {
diff --git a/website/templates/heatmap.html.twig b/website/templates/heatmap.html.twig
index 8c7c5d1..252d628 100644
--- a/website/templates/heatmap.html.twig
+++ b/website/templates/heatmap.html.twig
@@ -95,7 +95,7 @@
     </a>
     <div class="locations collapse show" id="mapLocations">
       <ul>
-        <li onclick="setMapView()">Kampus ZČU</li>
+        <li onclick="setMapView(49.7248, 13.3521, 17)">Kampus ZČU</li>
         <li onclick="setMapView(49.7367263, 13.3709177, 18)">FPE &ndash; Pedagogická fakulta</li>
         <li onclick="setMapView(49.7474950, 13.3748308, 18)">FPR &ndash; Právnická fakulta</li>
         <li onclick="setMapView(49.7450169, 13.3702668, 18)">FZS &ndash; Fakulta zdravotnických studií</li>
