From 931174a1ca8b69d6facf17037abe036c38f1dbe4 Mon Sep 17 00:00:00 2001
From: Maximilian Giller <m.giller@tu-bs.de>
Date: Wed, 26 Jan 2022 12:10:30 +0100
Subject: [PATCH] Implemented listing spaces

---
 datasets/datasets.php             | 22 ++---------------
 datasets/ks-datasets-database.php | 39 +++++++++++++++++++++++++++++++
 editor/js/interactions.js         |  7 +++---
 3 files changed, 45 insertions(+), 23 deletions(-)
 create mode 100644 datasets/ks-datasets-database.php

diff --git a/datasets/datasets.php b/datasets/datasets.php
index 653951b..110067c 100644
--- a/datasets/datasets.php
+++ b/datasets/datasets.php
@@ -1,4 +1,5 @@
 <?php
+require_once(__DIR__."/ks-datasets-database.php");
 
 $EMPTY_SPACE = '{"links":[],"nodes":[]}';
 $SPACES_DIR = __DIR__."/spaces/";
@@ -21,7 +22,7 @@ function kg_get_space() {
 
 add_action("wp_ajax_list_spaces", "kg_list_spaces"); // Fires only for logged-in-users
 function kg_list_spaces() {
-    $spaces = kg_get_list_of_spaces();
+    $spaces = ks_select_spaces();
     $payload = array("spaces" => $spaces);
 
     echo json_encode($payload);
@@ -66,25 +67,6 @@ function kg_get_space_file_path($space_id) {
     return $SPACES_DIR.$space_id.".json";
 }
 
-function kg_get_list_of_spaces() {
-    global $SPACES_DIR;
-    $all_files = scandir($SPACES_DIR);
-
-    if ($all_files == false) {
-        return [];
-    }
-
-    $spaces = [];
-
-    foreach ($all_files as $file) {
-        if (endsWith($file, ".json")) {
-            $spaces[] = substr($file, 0, -strlen(".json"));
-        }
-    }
-
-    return $spaces;
-}
-
 function kg_create_empty_space($file_path) {
     // Don't do anything, if it exists
     if (file_exists($file_path)) {
diff --git a/datasets/ks-datasets-database.php b/datasets/ks-datasets-database.php
new file mode 100644
index 0000000..7ccacf1
--- /dev/null
+++ b/datasets/ks-datasets-database.php
@@ -0,0 +1,39 @@
+<?php
+ini_set('display_errors', 1);
+ini_set('display_startup_errors', 1);
+error_reporting(E_ALL);
+require_once(__DIR__ . "/../knowledge-space-database.php");
+
+function ks_insert_or_update_graph($graph)
+{
+}
+
+function ks_select_spaces()
+{
+    global $SPACES_TABLE;
+    $sql = "SELECT * FROM $SPACES_TABLE";
+
+    global $wpdb;
+    $results = $wpdb->get_results($sql); // or die(mysql_error());
+
+    return ks_spaces_to_array($results);
+}
+
+function ks_spaces_to_array($spaces)
+{
+    $array = array();
+
+    foreach ($spaces as $space) {
+        $space_id = $space->space_id;
+
+        $array_space = array(
+            "space_id" => $space_id,
+            "name" => $space->name,
+            "description" => $space->description
+        );
+
+        $array[$space_id] = $array_space;
+    }
+
+    return $array;
+}
diff --git a/editor/js/interactions.js b/editor/js/interactions.js
index d24fec2..60917d1 100644
--- a/editor/js/interactions.js
+++ b/editor/js/interactions.js
@@ -14,13 +14,14 @@ export function initInteractions() {
     // Fill space dropdown
     var selectContainer = jQuery("#space-id-select");
     listAllSpaces().then((spaces) => {
-        spaces.forEach(space => {
+        Object.keys(spaces).forEach(space_id => {
             var selectedTxt = "";
-            if (space === SPACE) {
+            var space = spaces[space_id];
+            if (space.name === SPACE) {
                 selectedTxt = "selected ";
             }
 
-            var child = "<option " + selectedTxt + "value=\"" + space + "\">" + space + "</option>"
+            var child = "<option " + selectedTxt + "value=\"" + space_id + "\" title=\"" + space.description + "\">" + space.name  + "</option>"
             selectContainer.append(child);
         });
     });
-- 
GitLab