From a00b7f40823f2ed80e35def0e8c0bef465cca49e Mon Sep 17 00:00:00 2001
From: Maximilian Giller <m.giller@tu-bs.de>
Date: Wed, 26 Jan 2022 15:56:31 +0100
Subject: [PATCH] Implemented some more database functions

---
 datasets/datasets.js              |  2 +-
 datasets/datasets.php             | 37 +++++++++++----
 datasets/ks-datasets-database.php | 75 ++++++++++++++++++++++++-------
 knowledge-space-database.php      |  1 +
 4 files changed, 90 insertions(+), 25 deletions(-)

diff --git a/datasets/datasets.js b/datasets/datasets.js
index 1e8c589..c1bc4b7 100644
--- a/datasets/datasets.js
+++ b/datasets/datasets.js
@@ -12,7 +12,7 @@ import jQuery from "jquery";
 export function loadGraphJson(spaceId) {
     let payload = {
         action: "get_space",
-        space: spaceId,
+        space_id: spaceId,
     };
 
     return jQuery
diff --git a/datasets/datasets.php b/datasets/datasets.php
index 110067c..7041473 100644
--- a/datasets/datasets.php
+++ b/datasets/datasets.php
@@ -7,22 +7,24 @@ $SPACES_DIR = __DIR__."/spaces/";
 add_action("wp_ajax_get_space", "kg_get_space"); // Fires only for logged-in-users
 add_action("wp_ajax_nopriv_get_space", 'kg_get_space' ); // Fires for everyone
 function kg_get_space() {
-    $file_path = kg_get_space_file_path($_POST["space"]);
+    $space_id = kg_get_space_file_path($_POST["space_id"]);
 
-    // If it doesn't exist, create new empty space
-    if (!file_exists($file_path)) {
-        kg_create_empty_space($file_path);
+    $space = ks_select_all_spaces($space_id);
+    if ($space == null) {
+        // No valid space found
+        wp_die();
     }
+    
+    $space_id = $space->space_id;
 
-    $content = file_get_contents($file_path);
-    echo $content;
-
-    wp_die();
+    // Collect nodes
+    $nodes = ks_select_space_nodes($space_id);
 }
 
 add_action("wp_ajax_list_spaces", "kg_list_spaces"); // Fires only for logged-in-users
 function kg_list_spaces() {
     $spaces = ks_select_spaces();
+    $spaces = ks_spaces_to_array($spaces);
     $payload = array("spaces" => $spaces);
 
     echo json_encode($payload);
@@ -86,3 +88,22 @@ function endsWith( $haystack, $needle ) {
     }
     return substr( $haystack, -$length ) === $needle;
 }
+
+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/datasets/ks-datasets-database.php b/datasets/ks-datasets-database.php
index 7ccacf1..e83dd77 100644
--- a/datasets/ks-datasets-database.php
+++ b/datasets/ks-datasets-database.php
@@ -1,4 +1,5 @@
 <?php
+//! TODO: REMOVE!
 ini_set('display_errors', 1);
 ini_set('display_startup_errors', 1);
 error_reporting(E_ALL);
@@ -6,34 +7,76 @@ require_once(__DIR__ . "/../knowledge-space-database.php");
 
 function ks_insert_or_update_graph($graph)
 {
+    // Does graph exist already?
+
+}
+
+function ks_select_all_spaces()
+{
+    global $SPACES_TABLE;
+    return ks_select("SELECT * FROM $SPACES_TABLE");
 }
 
-function ks_select_spaces()
+function ks_select_space($space_id)
 {
     global $SPACES_TABLE;
-    $sql = "SELECT * FROM $SPACES_TABLE";
+    return ks_select("SELECT * FROM $SPACES_TABLE WHERE space_id = %i", $space_id);
+}
 
+function ks_insert_space($name = null, $description = "")
+{
+    // If no name given, set default name
+    if ($name == null) {
+        // Adding current time to make it somewhat unique
+        $now = new DateTime();
+        $name = "New space - " . $now->format('Y-m-d H:i:s');
+    }
+
+    $data = array(
+        "name" => $name,
+        "description" => $description
+    );
+    $format = array("%s", "%s");
+
+    global $SPACES_TABLE;
     global $wpdb;
-    $results = $wpdb->get_results($sql); // or die(mysql_error());
+    $wpdb->insert($SPACES_TABLE, $data, $format);
 
-    return ks_spaces_to_array($results);
+    return $wpdb->insert_id;
 }
 
-function ks_spaces_to_array($spaces)
+function ks_select_space_nodes($space_id)
 {
-    $array = array();
+    global $NODES_TABLE;
+    return ks_select("SELECT * FROM $NODES_TABLE WHERE space_id = %i", $space_id);
+}
 
-    foreach ($spaces as $space) {
-        $space_id = $space->space_id;
+function ks_select_space_links($space_id)
+{
+    global $LINKS_TABLE;
+    global $NODES_TABLE;
+    return ks_select("SELECT link_id, source_node_id, target_node_id FROM $LINKS_TABLE JOIN $NODES_TABLE ON $NODES_TABLE.node_id = $LINKS_TABLE.source_node_id WHERE $NODES_TABLE.space_id = %i", $space_id);
+}
 
-        $array_space = array(
-            "space_id" => $space_id,
-            "name" => $space->name,
-            "description" => $space->description
-        );
+function ks_select_space_nodetypes($space_id)
+{
+    global $NODETYPES_TABLE;
+    return ks_select("SELECT * FROM $NODETYPES_TABLE WHERE space_id = %i", $space_id);
+}
 
-        $array[$space_id] = $array_space;
-    }
+function ks_select_space_references($space_id)
+{
+    global $REFERENCES_TABLE;
+    global $NODES_TABLE;
+    return ks_select("SELECT reference_id, url, node_id FROM $REFERENCES_TABLE JOIN $NODES_TABLE ON $NODES_TABLE.node_id = $REFERENCES_TABLE.node_id WHERE $NODES_TABLE.space_id = %i", $space_id);
+}
+
+function ks_select($query, $parameters = array())
+{
+    global $wpdb;
+
+    $sql = $wpdb->prepare($query, $parameters);
+    $results = $wpdb->get_results($sql) or die(mysql_error());
 
-    return $array;
+    return $results;
 }
diff --git a/knowledge-space-database.php b/knowledge-space-database.php
index 07d5e13..6202c34 100644
--- a/knowledge-space-database.php
+++ b/knowledge-space-database.php
@@ -44,6 +44,7 @@ function ks_install()
 	global $NODETYPES_TABLE;
 	$sql = "CREATE TABLE $NODETYPES_TABLE (
 		nodetype_id int(11) NOT NULL AUTO_INCREMENT,
+		space_id int(11) NOT NULL,
 		name text NOT NULL,
 		color bit(24) NOT NULL,
 		PRIMARY KEY  (nodetype_id)
-- 
GitLab