Skip to content
Snippets Groups Projects
Commit a00b7f40 authored by Maximilian Giller's avatar Maximilian Giller :squid:
Browse files

Implemented some more database functions

parent 2ade668c
No related branches found
No related tags found
No related merge requests found
Pipeline #53948 passed
......@@ -12,7 +12,7 @@ import jQuery from "jquery";
export function loadGraphJson(spaceId) {
let payload = {
action: "get_space",
space: spaceId,
space_id: spaceId,
};
return jQuery
......
......@@ -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;
}
<?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;
}
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment