From 043bd25cf780877015e6695099ccf9a8b7de1bb7 Mon Sep 17 00:00:00 2001 From: Ro Date: Wed, 14 Apr 2021 14:51:06 -0700 Subject: [PATCH] current index updates when new page added --- brain/data/Book.inc.php | 8 ++++++-- brain/data/Settings.inc.php | 19 +++++++++++++++++-- brain/utility/DocTools.inc.php | 9 ++++++++- src/libraries/FipamoAdminAPI.js | 2 +- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/brain/data/Book.inc.php b/brain/data/Book.inc.php index cca3470..d9dbf72 100644 --- a/brain/data/Book.inc.php +++ b/brain/data/Book.inc.php @@ -112,7 +112,7 @@ class Book $updated = new \Moment\Moment(); //grab current index from settings and update - $id = $task != "create" ? $body["id"] : 6; + $id = $task != "create" ? $body["id"] : Settings::getCurrentIndex(); $uuid = $task != "create" ? $body["uuid"] : StringTools::createUUID(); $write = "---\n" . @@ -171,7 +171,7 @@ class Book $writePath = "../content/pages/" . $path . "/" . $body["slug"] . ".md"; } - $status = DocTools::write($task, $path, $writePath, $write); + $status = DocTools::writePages($task, $path, $writePath, $write); if ($status) { $response = [ @@ -179,6 +179,10 @@ class Book "type" => $task == "write" ? "postUpdated" : "postAdded", "id" => $uuid, ]; + //if new page added, update current index in Settings file + if ($task == "create") { + Settings::updateIndex(); + } } else { $response = [ "message" => "Uh oh. File save problem. Don't panic", diff --git a/brain/data/Settings.inc.php b/brain/data/Settings.inc.php index 0ff27c1..8ce817a 100644 --- a/brain/data/Settings.inc.php +++ b/brain/data/Settings.inc.php @@ -4,14 +4,14 @@ class Settings { private $folks; private $tags; - private $settings; + private static $settings; public function __construct() { //gets all settings files and converts to php objects $this->folks = json_decode(file_get_contents("../config/folks.json"), true); $this->tags = json_decode(file_get_contents("../config/tags.json"), true); - $this->settings = json_decode( + self::$settings = json_decode( file_get_contents("../config/settings.json"), true ); @@ -27,4 +27,19 @@ class Settings return $this->folks; } } + + public static function getCurrentIndex() + { + $settings = self::$settings; + return $settings["library_stats"]["current_index"]; + } + + public static function updateIndex() + { + $settings = self::$settings; + $index = $settings["library_stats"]["current_index"]; + $settings["library_stats"]["current_index"] = $index + 1; + + DocTools::writeSettings("../config/settings.json", $settings); + } } diff --git a/brain/utility/DocTools.inc.php b/brain/utility/DocTools.inc.php index e6fbd47..08876be 100644 --- a/brain/utility/DocTools.inc.php +++ b/brain/utility/DocTools.inc.php @@ -6,7 +6,7 @@ class DocTools { } - public static function write($task, $path, $fileLocation, $fileContents) + public static function writePages($task, $path, $fileLocation, $fileContents) { try { if ($task == "create") { @@ -26,4 +26,11 @@ class DocTools return false; } } + + public static function writeSettings($fileLocation, $fileContents) + { + ($new = fopen($fileLocation, "w")) or die("Unable to open file!"); + fwrite($new, json_encode($fileContents)); + fclose($new); + } } diff --git a/src/libraries/FipamoAdminAPI.js b/src/libraries/FipamoAdminAPI.js index c0ed695..56fd737 100644 --- a/src/libraries/FipamoAdminAPI.js +++ b/src/libraries/FipamoAdminAPI.js @@ -280,7 +280,7 @@ export default class APIUtils { request.open(requestType, requestURL, true); request.onload = () => { if (request.status == 200) { - console.log("RESPONSE", request); + //console.log("RESPONSE", request); let response = JSON.parse(request["response"]); resolve(response); } else {