cleaned up page creation

This commit is contained in:
Ro 2021-04-13 20:49:12 -07:00
parent 1b9252fef0
commit ed2105edcc
8 changed files with 67 additions and 20 deletions

View file

@ -36,6 +36,7 @@ class APIControl
} }
//there's only one verion of the api for now //there's only one verion of the api for now
//$result = [];
switch (isset($args["third"]) ? $args["third"] : "none") { switch (isset($args["third"]) ? $args["third"] : "none") {
case "login": case "login":
$result = Auth::login($body); $result = Auth::login($body);
@ -50,17 +51,22 @@ class APIControl
if (Session::verifyToken($token[0])) { if (Session::verifyToken($token[0])) {
switch ($task) { switch ($task) {
case "delete": case "delete":
case "add": case "create":
case "edit": case "write":
$result = (new Book("../content/pages"))->editPage( $result = (new Book("../content/pages"))->editPage(
$task, $task,
$request $request
); );
break; break;
case "add-entry-image": case "add-entry-image":
$result = ImagesAPI::uploadImage($request); $result = ImagesAPI::uploadImage($request);
break; break;
default:
$result = [
"message" => "Hm, no task. That's unfortunate",
"type" => "TASK_NONE",
];
break;
} }
} else { } else {
$result = [ $result = [

View file

@ -47,17 +47,26 @@ class Book
$image = $request->getUploadedFiles(); $image = $request->getUploadedFiles();
$member = Session::get("member"); $member = Session::get("member");
if ($task != "add") { if ($task != "create") {
$path = $path =
date("Y", date($page["rawCreated"])) . date("Y", date($page["rawCreated"])) .
"/" . "/" .
date("m", date($page["rawCreated"])); date("m", date($page["rawCreated"]));
} else { } else {
$path = date("Y", date()) . "/" . date("m"); $path = date("Y") . "/" . date("m");
} }
if (isset($image["feature_image"])) { 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( FileUploader::uploadFile(
"../public/assets/images/blog/" . $path . "/", "../public/assets/images/blog/" . $path . "/",
$image["feature_image"] $image["feature_image"]
@ -93,22 +102,25 @@ class Book
? ($body["featured"] = "true") ? ($body["featured"] = "true")
: ($body["featured"] = "false"); : ($body["featured"] = "false");
} else { } else {
$deleted = !$page["deleted"] ? "false" : $page["deleted"]; $deleted = isset($page["deleted"]) ? $page["deleted"] : "false";
} }
$created = $created =
$task != "add" $task != "create"
? new \Moment\Moment($page["rawCreated"]) ? new \Moment\Moment($page["rawCreated"])
: new \Moment\Moment(); : new \Moment\Moment();
$updated = 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 = $write =
"---\n" . "---\n" .
"id: " . "id: " .
$body["id"] . $id .
"\n" . "\n" .
"uuid: " . "uuid: " .
$body["uuid"] . $uuid .
"\n" . "\n" .
"title: " . "title: " .
$body["title"] . $body["title"] .
@ -154,18 +166,27 @@ class Book
// if layout is index, change path to file // if layout is index, change path to file
if ($body["layout"] == "index") { if ($body["layout"] == "index") {
$writePath = "../content/start/index.md"; $writePath = "../content/pages/start/index.md";
} else { } else {
$writePath = "../content/pages/" . $path . "/" . $body["slug"] . ".md"; $writePath = "../content/pages/" . $path . "/" . $body["slug"] . ".md";
} }
($new = fopen($writePath, "w")) or die("Unable to open file!"); if ($task == "create") {
fwrite($new, $write); if (!is_dir("../content/pages/" . $path)) {
fclose($new); //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 = [ $response = [
"message" => "File edited. Nice work", "message" => "File edited. Nice work",
"type" => $task == "write" ? "postUpdated" : "postAdded", "type" => $task == "write" ? "postUpdated" : "postAdded",
"id" => $uuid,
]; ];
return $response; return $response;

View file

@ -4,6 +4,25 @@
class StringTools 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) public static function randomString(int $length)
{ {
$alphanum = $alphanum =

View file

@ -23,6 +23,9 @@
<a class="page-link" href="/dashboard/page/edit/{{ page.uuid }}"> <a class="page-link" href="/dashboard/page/edit/{{ page.uuid }}">
<div class="page-bg" style="background: url({{ page.feature }}) no-repeat center center / cover"> <div class="page-bg" style="background: url({{ page.feature }}) no-repeat center center / cover">
<div id="meta"> <div id="meta">
<span>
{{ page.updated }}
</span>
<label> <label>
{{ page.title }} {{ page.title }}
</label> </label>

View file

@ -39,9 +39,7 @@
<div id="post-feature"> <div id="post-feature">
{% if page['feature'] == null %} {% if page['feature'] == null %}
<div id="featured-image-drop"> <div id="featured-image-drop">
DRAG AND DROP IMAGE OR DRAG AND DROP IMAGE OR <label for="featured-image-upload">CLICK TO CHOSE</label>
<label for="featured-image-upload"></label>
CLICK TO CHOSE
</div> </div>
{% else %} {% else %}
<div id="featured-new-image-btn"> <div id="featured-new-image-btn">

View file

@ -34,7 +34,7 @@
</button> </button>
{% else %} {% else %}
<button id="edit-save" class="post-sumbit-btn submit-start editor-button" data-action='blog-add' type='submit'> <button id="edit-save" class="post-sumbit-btn submit-start editor-button" data-action='blog-add' type='submit'>
<svg id="submit-save" class="icon-hide" viewbox="0 0 20 20" class="icons"> <svg id="submit-save" viewbox="0 0 20 20" class="icons">
<use id="submit-save" xlink:href="/assets/images/global/sprite.svg#entypo-plus"/> <use id="submit-save" xlink:href="/assets/images/global/sprite.svg#entypo-plus"/>
</svg> </svg>
</button> </button>

View file

@ -162,7 +162,7 @@ export default class PostEditor {
notify.alert(r.message, true); notify.alert(r.message, true);
} else { } else {
notify.alert(r.message, true); notify.alert(r.message, true);
window.location = "/@/dashboard/page/edit/" + r.id; window.location = "/dashboard/page/edit/" + r.id;
} }
} }
}) })

View file

@ -280,7 +280,7 @@ export default class APIUtils {
request.open(requestType, requestURL, true); request.open(requestType, requestURL, true);
request.onload = () => { request.onload = () => {
if (request.status == 200) { if (request.status == 200) {
//console.log("RESPONSE", request); console.log("RESPONSE", request);
let response = JSON.parse(request["response"]); let response = JSON.parse(request["response"]);
resolve(response); resolve(response);
} else { } else {