diff --git a/datasets/datasets.js b/datasets/datasets.js index 1e8c58909c335ede6064dedf78383c394710ae98..c1bc4b7161061e5896bbe0ac2e30c02580c55fc4 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 110067c1a165ef1d098c3d903ab0048bdd1219d6..704147310a730e86d1819dc7cdb4327c33cb6cd3 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 7ccacf17c00a5f56ffeefdaeabec389305945988..e83dd77bc4a3789fec63dff522f96ff2fc574ff5 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 07d5e1379b92921226257ef521626de63678c079..6202c34933a1ec54797e4ecbf017ed75f1475c06 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)