From ed2105edccffb54b15ea432ed53ed1e7ef4eeb0b Mon Sep 17 00:00:00 2001 From: Ro Date: Tue, 13 Apr 2021 20:49:12 -0700 Subject: [PATCH] cleaned up page creation --- brain/controller/APIControl.inc.php | 12 ++++++-- brain/data/Book.inc.php | 43 ++++++++++++++++++++------- brain/utility/StringTools.inc.php | 19 ++++++++++++ brain/views/dash/book.twig | 3 ++ brain/views/dash/page-edit.twig | 4 +-- brain/views/dash/partials/editor.twig | 2 +- src/com/controllers/PageEditor.js | 2 +- src/libraries/FipamoAdminAPI.js | 2 +- 8 files changed, 67 insertions(+), 20 deletions(-) diff --git a/brain/controller/APIControl.inc.php b/brain/controller/APIControl.inc.php index ceae441..282b8fa 100644 --- a/brain/controller/APIControl.inc.php +++ b/brain/controller/APIControl.inc.php @@ -36,6 +36,7 @@ class APIControl } //there's only one verion of the api for now + //$result = []; switch (isset($args["third"]) ? $args["third"] : "none") { case "login": $result = Auth::login($body); @@ -50,17 +51,22 @@ class APIControl if (Session::verifyToken($token[0])) { switch ($task) { case "delete": - case "add": - case "edit": + case "create": + case "write": $result = (new Book("../content/pages"))->editPage( $task, $request ); break; - case "add-entry-image": $result = ImagesAPI::uploadImage($request); break; + default: + $result = [ + "message" => "Hm, no task. That's unfortunate", + "type" => "TASK_NONE", + ]; + break; } } else { $result = [ diff --git a/brain/data/Book.inc.php b/brain/data/Book.inc.php index f9d25aa..c68ed44 100644 --- a/brain/data/Book.inc.php +++ b/brain/data/Book.inc.php @@ -47,17 +47,26 @@ class Book $image = $request->getUploadedFiles(); $member = Session::get("member"); - if ($task != "add") { + if ($task != "create") { $path = date("Y", date($page["rawCreated"])) . "/" . date("m", date($page["rawCreated"])); } else { - $path = date("Y", date()) . "/" . date("m"); + $path = date("Y") . "/" . date("m"); } if (isset($image["feature_image"])) { - $feature = $image["feature_image"]->getClientFileName(); + if ($task != "create") { + $feature = $image["feature_image"]->getClientFileName(); + } else { + $feature = + "/assets/images/blog/" . + $path . + "/" . + $image["feature_image"]->getClientFileName(); + } + FileUploader::uploadFile( "../public/assets/images/blog/" . $path . "/", $image["feature_image"] @@ -93,22 +102,25 @@ class Book ? ($body["featured"] = "true") : ($body["featured"] = "false"); } else { - $deleted = !$page["deleted"] ? "false" : $page["deleted"]; + $deleted = isset($page["deleted"]) ? $page["deleted"] : "false"; } $created = - $task != "add" + $task != "create" ? new \Moment\Moment($page["rawCreated"]) : new \Moment\Moment(); $updated = new \Moment\Moment(); + //grab current index from settings and update + $id = $task != "create" ? $body["id"] : 6; + $uuid = $task != "create" ? $body["uuid"] : StringTools::createUUID(); $write = "---\n" . "id: " . - $body["id"] . + $id . "\n" . "uuid: " . - $body["uuid"] . + $uuid . "\n" . "title: " . $body["title"] . @@ -154,18 +166,27 @@ class Book // if layout is index, change path to file if ($body["layout"] == "index") { - $writePath = "../content/start/index.md"; + $writePath = "../content/pages/start/index.md"; } else { $writePath = "../content/pages/" . $path . "/" . $body["slug"] . ".md"; } - ($new = fopen($writePath, "w")) or die("Unable to open file!"); - fwrite($new, $write); - fclose($new); + if ($task == "create") { + if (!is_dir("../content/pages/" . $path)) { + //Directory does not exist, so lets create it. + mkdir("../content/pages/" . $path, 0755, true); + } + file_put_contents($writePath, $write); + } else { + ($new = fopen($writePath, "w")) or die("Unable to open file!"); + fwrite($new, $write); + fclose($new); + } $response = [ "message" => "File edited. Nice work", "type" => $task == "write" ? "postUpdated" : "postAdded", + "id" => $uuid, ]; return $response; diff --git a/brain/utility/StringTools.inc.php b/brain/utility/StringTools.inc.php index 2f1fb17..0888a7e 100644 --- a/brain/utility/StringTools.inc.php +++ b/brain/utility/StringTools.inc.php @@ -4,6 +4,25 @@ class StringTools { + public static function createUUID() + { + if (function_exists("com_create_guid") === true) { + return trim(com_create_guid(), "{}"); + } + + return sprintf( + "%04X%04X-%04X-%04X-%04X-%04X%04X%04X", + mt_rand(0, 65535), + mt_rand(0, 65535), + mt_rand(0, 65535), + mt_rand(16384, 20479), + mt_rand(32768, 49151), + mt_rand(0, 65535), + mt_rand(0, 65535), + mt_rand(0, 65535) + ); + } + public static function randomString(int $length) { $alphanum = diff --git a/brain/views/dash/book.twig b/brain/views/dash/book.twig index 09eb558..78458be 100644 --- a/brain/views/dash/book.twig +++ b/brain/views/dash/book.twig @@ -23,6 +23,9 @@
+ + {{ page.updated }} + diff --git a/brain/views/dash/page-edit.twig b/brain/views/dash/page-edit.twig index 5e1278c..29d93d0 100644 --- a/brain/views/dash/page-edit.twig +++ b/brain/views/dash/page-edit.twig @@ -39,9 +39,7 @@
{% if page['feature'] == null %} {% else %}