diff --git a/datasets/datasets.php b/datasets/datasets.php index 653951b1cd248662dc96b68d3674f91de1721337..110067c1a165ef1d098c3d903ab0048bdd1219d6 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 0000000000000000000000000000000000000000..7ccacf17c00a5f56ffeefdaeabec389305945988 --- /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 d24fec2652d2f9940210fa5f13021cf0fe201f6e..60917d1a409a777c3ad97aed4fb9191165032aed 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); }); });