2021-03-25 00:59:28 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use Mni\FrontYAML\Parser;
|
2021-04-02 02:37:12 +02:00
|
|
|
use function _\orderBy;
|
2021-04-07 23:09:40 +02:00
|
|
|
use function _\filter;
|
2021-04-10 22:19:05 +02:00
|
|
|
use function _\find;
|
2021-03-25 00:59:28 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2021-04-02 02:37:12 +02:00
|
|
|
|
2021-04-10 22:19:05 +02:00
|
|
|
public function findPageById(string $uuid)
|
|
|
|
{
|
|
|
|
$content = $this->getContents();
|
|
|
|
$page = find($content, ["uuid" => $uuid]);
|
|
|
|
return $page;
|
|
|
|
}
|
|
|
|
|
2021-04-07 23:09:40 +02:00
|
|
|
public function getPages(int $page, int $limit, string $sort = null)
|
2021-04-02 02:37:12 +02:00
|
|
|
{
|
|
|
|
$content = $this->getContents();
|
2021-04-07 23:09:40 +02:00
|
|
|
|
|
|
|
$published = filter($content, function ($item) {
|
|
|
|
return $item["published"] == "true";
|
|
|
|
});
|
|
|
|
$deleted = filter($content, function ($item) {
|
|
|
|
return $item["deleted"];
|
|
|
|
});
|
|
|
|
|
|
|
|
$all = $content;
|
|
|
|
|
|
|
|
$filter = isset($sort) ? $sort : "all";
|
|
|
|
|
|
|
|
//echo $filter;
|
|
|
|
$filtered = [];
|
|
|
|
switch ($filter) {
|
|
|
|
case "published":
|
|
|
|
$filtered = $published;
|
|
|
|
break;
|
|
|
|
case "deleted":
|
|
|
|
$filtered = $deleted;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$filtered = $content;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$numOfPages = ceil(count($filtered) / $limit);
|
2021-04-02 02:37:12 +02:00
|
|
|
|
|
|
|
$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++) {
|
2021-04-09 00:27:05 +02:00
|
|
|
if (isset($filtered[$i + $range])) {
|
2021-04-07 23:09:40 +02:00
|
|
|
array_push($folder, $filtered[$i + $range]);
|
2021-04-09 00:27:05 +02:00
|
|
|
} else {
|
|
|
|
//chill out
|
2021-04-07 23:09:40 +02:00
|
|
|
}
|
2021-04-02 02:37:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 00:27:05 +02:00
|
|
|
$prev = $page - 1;
|
|
|
|
if ($prev <= 0) {
|
|
|
|
$prev = $numOfPages;
|
|
|
|
}
|
|
|
|
|
|
|
|
$next = $page + 1;
|
|
|
|
if ($next > $numOfPages) {
|
|
|
|
$next = 1;
|
|
|
|
}
|
|
|
|
|
2021-04-02 02:37:12 +02:00
|
|
|
return [
|
|
|
|
"pages" => $folder,
|
2021-04-07 23:09:40 +02:00
|
|
|
"numOfPages" => $numOfPages,
|
|
|
|
"entryCount" => count($filtered),
|
2021-04-09 00:27:05 +02:00
|
|
|
"paginate" => [
|
|
|
|
"sort" => $filter,
|
|
|
|
"nextPage" => $next,
|
|
|
|
"prevPage" => $prev,
|
|
|
|
],
|
2021-04-07 23:09:40 +02:00
|
|
|
"stats" => [
|
|
|
|
"all" => count($all),
|
|
|
|
"published" => count($published),
|
|
|
|
"deleted" => count($deleted),
|
|
|
|
],
|
2021-04-02 02:37:12 +02:00
|
|
|
];
|
|
|
|
}
|
2021-03-25 00:59:28 +01:00
|
|
|
public function getContents()
|
|
|
|
{
|
|
|
|
$parser = new Parser();
|
|
|
|
$contents = [];
|
|
|
|
foreach ($this->files as $file) {
|
|
|
|
$doc = $parser->parse(file_get_contents($file), false);
|
2021-04-02 02:37:12 +02:00
|
|
|
|
2021-03-25 00:59:28 +01:00
|
|
|
$meta = $doc->getYAML();
|
2021-04-02 02:37:12 +02:00
|
|
|
//$date = getdate($meta["created"]);
|
|
|
|
$newDate = date("Y M D d", $meta["created"]);
|
2021-03-25 00:59:28 +01:00
|
|
|
$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"],
|
2021-04-02 02:37:12 +02:00
|
|
|
"prettyDate" => $newDate,
|
2021-03-25 00:59:28 +01:00
|
|
|
"created" => $meta["created"],
|
|
|
|
"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);
|
|
|
|
}
|
|
|
|
}
|
2021-04-02 02:37:12 +02:00
|
|
|
$contents = orderBy($contents, ["id"], ["desc"]);
|
2021-03-25 00:59:28 +01:00
|
|
|
return $contents;
|
|
|
|
}
|
|
|
|
}
|