You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Fipamo/brain/data/Book.inc.php

290 lines
6.9 KiB
PHP

<?php
use Mni\FrontYAML\Parser;
use function _\orderBy;
use function _\filter;
use function _\find;
class Book
{
public $files = [];
public function __construct($folder)
{
$this->read($folder);
}
public function read($folder)
{
$folders = glob("$folder/*", GLOB_ONLYDIR);
foreach ($folders as $folder) {
//$this->files[] = $folder . "/";
$this->read($folder);
}
$files = array_filter(glob("$folder/*md"), "is_file");
foreach ($files as $file) {
$this->files[] = $file;
}
}
public function findPageById(string $uuid)
{
$content = $this->getContents();
$page = find($content, ["uuid" => $uuid]);
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"]]);
} else {
$body = $request->getParsedBody();
}
$page = find($content, ["uuid" => $body["uuid"]]);
$image = $request->getUploadedFiles();
$member = Session::get("member");
if ($task != "add") {
$path =
date("Y", date($page["rawCreated"])) .
"/" .
date("m", date($page["rawCreated"]));
} else {
$path = date("Y", date()) . "/" . date("m");
}
if (isset($image["feature_image"])) {
$feature = $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 {
$feature = $body["feature"];
}
}
if ($task == "delete") {
$deleted = "true";
$body["menu"] ? ($body["menu"] = "true") : ($body["menu"] = "false");
$body["published"]
? ($body["published"] = "true")
: ($body["published"] = "false");
$body["featured"]
? ($body["featured"] = "true")
: ($body["featured"] = "false");
} else {
$deleted = !$page["deleted"] ? "false" : $page["deleted"];
}
$created =
$task != "add"
? new \Moment\Moment($page["rawCreated"])
: new \Moment\Moment();
$updated = new \Moment\Moment();
$write =
"---\n" .
"id: " .
$body["id"] .
"\n" .
"uuid: " .
$body["uuid"] .
"\n" .
"title: " .
$body["title"] .
"\n" .
"feature: " .
$feature .
"\n" .
"path: " .
$path .
"\n" .
"layout: " .
$body["layout"] .
"\n" .
"tags: " .
$body["tags"] .
"\n" .
"author: " .
$member["handle"] .
"\n" .
"created: " .
$created->format("Y-m-d\TH:i:sP") .
"\n" .
"updated: " .
$updated->format("Y-m-d\TH:i:sP") .
"\n" .
"deleted: " .
$deleted .
"\n" .
"slug: " .
$body["slug"] .
"\n" .
"menu: " .
$body["menu"] .
"\n" .
"published: " .
$body["published"] .
"\n" .
"featured: " .
$body["featured"] .
"\n---\n" .
$body["content"];
// if layout is index, change path to file
if ($body["layout"] == "index") {
$writePath = "../content/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);
$response = [
"message" => "File edited. Nice work",
"type" => $task == "write" ? "postUpdated" : "postAdded",
];
return $response;
}
public function getPages(int $page, int $limit, string $sort = null)
{
$content = $this->getContents();
$published = filter($content, function ($item) {
return $item["published"] == true && $item["deleted"] == false;
});
$deleted = filter($content, function ($item) {
return $item["deleted"] == true;
});
$all = $content;
$filter = isset($sort) ? $sort : "all";
switch ($filter) {
case "published":
$filtered = $published;
break;
case "deleted":
$filtered = $deleted;
break;
default:
$filtered = $content;
break;
}
$numOfPages = ceil(count($filtered) / $limit);
$folder = [];
if (count($filtered) != 0) {
if (count($filtered) < $limit) {
$limit = count($filtered) - 1;
}
$range = $page * $limit - $limit;
if ($range != 0) {
$range = $range + 1;
}
for ($i = 0; $i <= $limit; $i++) {
if (isset($filtered[$i + $range])) {
array_push($folder, $filtered[$i + $range]);
} else {
//chill out
}
}
}
$prev = $page - 1;
if ($prev <= 0) {
$prev = $numOfPages;
}
$next = $page + 1;
if ($next > $numOfPages) {
$next = 1;
}
return [
"pages" => $folder,
"numOfPages" => $numOfPages,
"entryCount" => count($filtered),
"paginate" => [
"sort" => $sort,
"nextPage" => $next,
"prevPage" => $prev,
],
"stats" => [
"all" => count($all),
"published" => count($published),
"deleted" => count($deleted),
],
];
}
public function getContents()
{
$parser = new Parser();
$contents = [];
foreach ($this->files as $file) {
$doc = $parser->parse(file_get_contents($file), false);
$meta = $doc->getYAML();
$page = [
"id" => $meta["id"],
"uuid" => $meta["uuid"],
"title" => $meta["title"],
"feature" => $meta["feature"],
"path" => $meta["path"],
"layout" => $meta["layout"],
"tags" => $meta["tags"],
"author" => $meta["author"],
"created" => date("Y M D d", $meta["created"]),
"updated" => date("Y M D d", $meta["updated"]),
"rawCreated" => $meta["created"],
"rawUpdated" => $meta["updated"],
"deleted" => $meta["deleted"],
"menu" => $meta["menu"],
"featured" => $meta["featured"],
"published" => $meta["published"],
"slug" => $meta["slug"],
"filePath" => $file,
"content" => $doc->getContent(),
];
//checks for duplicates
$uuid = $meta["uuid"];
$found = current(
array_filter($contents, function ($item) use ($uuid) {
return isset($item["uuid"]) && $uuid == $item["uuid"];
})
);
// if uuid is not present, add it
if (!$found) {
array_push($contents, $page);
}
}
$contents = orderBy($contents, ["id"], ["desc"]);
return $contents;
}
}