fipamo/brain/data/Book.inc.php

266 lines
6.7 KiB
PHP
Raw Normal View History

<?php
2021-04-07 23:09:40 +02:00
use function _\filter;
use function _\find;
class Book
{
public function __construct()
{
}
public function findPageById(string $uuid)
{
$content = $this->getContents();
$page = find($content, ["uuid" => $uuid]);
return $page;
}
public function findPageBySlug(string $slug = null)
{
$content = $this->getContents();
if (isset($slug)) {
$page = find($content, ["slug" => $slug]);
} else {
$page = find($content, ["layout" => "index"]);
}
return $page;
}
public function editPage($task, $request)
{
$content = $this->getContents();
if ($task == "delete") {
//$parsed = json_decode(file_get_contents("php://input"), true);
//$body = find($content, ["uuid" => $parsed["id"]]);
$body = $request->getParsedBody();
} else {
$body = $request->getParsedBody();
}
$page = find($content, ["uuid" => $body["uuid"]]);
$image = $request->getUploadedFiles();
$member = Session::get("member");
2021-04-14 05:49:12 +02:00
if ($task != "create") {
$path =
date("Y", date($page["rawCreated"])) .
"/" .
date("m", date($page["rawCreated"]));
} else {
2021-04-14 05:49:12 +02:00
$path = date("Y") . "/" . date("m");
}
if (isset($image["feature_image"])) {
2021-04-14 05:49:12 +02:00
if ($task != "create") {
$feature =
"/assets/images/blog/" .
$path .
"/" .
$image["feature_image"]->getClientFileName();
2021-04-14 05:49:12 +02:00
} else {
$feature =
"/assets/images/blog/" .
$path .
"/" .
$image["feature_image"]->getClientFileName();
}
FileUploader::uploadFile(
"../public/assets/images/blog/" . $path . "/",
$image["feature_image"]
);
} else {
if (isset($body["feature_image"])) {
$url = explode("/", $body["feature_image"]);
$feature =
"/" .
$url[3] .
"/" .
$url[4] .
"/" .
$url[5] .
"/" .
$url[6] .
"/" .
$url[7] .
"/" .
$url[8];
} else {
$task == "create" ? ($feature = "") : ($feature = $body["feature"]);
}
}
if ($task == "delete") {
$deleted = "true";
$body["menu"] = "false";
$body["published"] = "false";
$body["featured"] = "false";
} else {
2021-04-14 05:49:12 +02:00
$deleted = isset($page["deleted"]) ? $page["deleted"] : "false";
}
$created =
2021-04-14 05:49:12 +02:00
$task != "create"
? new \Moment\Moment($page["rawCreated"])
: new \Moment\Moment();
$updated = new \Moment\Moment();
2021-04-14 05:49:12 +02:00
//grab current index from settings and update
$id = $task != "create" ? $body["id"] : Settings::getCurrentIndex();
2021-04-14 05:49:12 +02:00
$uuid = $task != "create" ? $body["uuid"] : StringTools::createUUID();
// now that variables are done, set to body object and then convert to markdown to save
$body["id"] = $id;
$body["uuid"] = $uuid;
$body["feature"] = $feature;
$body["path"] = $path;
$body["author"] = $member["handle"];
$body["created"] = $created->format("Y-m-d\TH:i:sP");
$body["updated"] = $updated->format("Y-m-d\TH:i:sP");
$body["deleted"] = $deleted;
$write = DocTools::objectToMD($body);
// if layout is index, change path to file
if ($body["layout"] == "index") {
2021-04-14 05:49:12 +02:00
$writePath = "../content/pages/start/index.md";
} else {
$writePath = "../content/pages/" . $path . "/" . $body["slug"] . ".md";
}
$status = DocTools::writePages($task, $path, $writePath, $write);
2021-04-14 21:48:57 +02:00
if ($status) {
$config = new Settings();
$settings = $config->getSettings();
$message = "";
if (
$settings["global"]["renderOnSave"] == "true" &&
$settings["global"]["dynamicRender"] == "false"
) {
$render = new Render();
$render->renderTags();
$render->renderArchive();
$render->renderPages();
$message = "Filed edited and rendered. NOICE.";
} else {
$message = "File edited. Nice work";
}
2021-04-14 21:48:57 +02:00
$response = [
"message" => $message,
2021-04-14 21:48:57 +02:00
"type" => $task == "write" ? "postUpdated" : "postAdded",
"id" => $uuid,
];
//TODO: When form submission is successful, make new form token
//Session token doesn't reset on the front end, so turning this off for now
//$form_token = md5(uniqid(microtime(), true));
//Session::set("form_token", $form_token);
//once saved, update menu
$body["path"] = $path;
Settings::updateMenu($body);
Settings::updateTags();
//if new page added, update current index in Settings file
if ($task == "create") {
Settings::updateIndex();
}
2021-04-14 05:49:12 +02:00
} else {
2021-04-14 21:48:57 +02:00
$response = [
"message" => "Uh oh. File save problem. Don't panic",
"type" => "postError",
"id" => $uuid,
];
2021-04-14 05:49:12 +02:00
}
return $response;
}
2021-04-07 23:09:40 +02:00
public function getPages(int $page, int $limit, string $sort = null)
{
$content = $this->getContents();
2021-04-07 23:09:40 +02:00
$published = filter($content, function ($item) {
return $item["published"] == true && $item["deleted"] == false;
2021-04-07 23:09:40 +02:00
});
$deleted = filter($content, function ($item) {
return $item["deleted"] == true;
2021-04-07 23:09:40 +02:00
});
//$all = $content;
$all = filter($content, function ($item) {
return $item["deleted"] == false;
});
2021-04-07 23:09:40 +02:00
$filter = isset($sort) ? $sort : "all";
switch ($filter) {
case "published":
$filtered = $published;
break;
case "deleted":
$filtered = $deleted;
break;
default:
$filtered = $all;
2021-04-07 23:09:40 +02:00
break;
}
2021-04-15 00:08:09 +02:00
$numOfPages = ceil(count($filtered) / ($limit + 1));
$folder = [];
2021-04-07 23:09:40 +02:00
if (count($filtered) != 0) {
if (count($filtered) < $limit) {
$limit = count($filtered) - 1;
}
$range = $page * $limit - $limit;
2021-04-09 00:38:31 +02:00
if ($range != 0) {
$range = $range + 1;
}
2021-04-07 23:09:40 +02:00
for ($i = 0; $i <= $limit; $i++) {
if (isset($filtered[$i + $range])) {
2021-04-07 23:09:40 +02:00
array_push($folder, $filtered[$i + $range]);
} else {
//chill out
2021-04-07 23:09:40 +02:00
}
}
}
$prev = $page - 1;
if ($prev <= 0) {
$prev = $numOfPages;
}
$next = $page + 1;
if ($next > $numOfPages) {
$next = 1;
}
return [
"pages" => $folder,
2021-04-07 23:09:40 +02:00
"numOfPages" => $numOfPages,
"entryCount" => count($filtered),
"paginate" => [
"sort" => $sort,
"nextPage" => $next,
"prevPage" => $prev,
],
2021-04-07 23:09:40 +02:00
"stats" => [
"all" => count($all),
"published" => count($published),
"deleted" => count($deleted),
],
];
}
public function getContents()
{
//test new contents data class
//$new = (new Contents("../content/pages"))->getAll();
$contents = (new Contents("../content/pages"))->getAll();
return $contents;
}
}