forked from projects/fipamo
94 lines
2.3 KiB
PHP
94 lines
2.3 KiB
PHP
<?php
|
|
|
|
use Mni\FrontYAML\Parser;
|
|
use function _\orderBy;
|
|
|
|
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 getPages(int $page, int $limit)
|
|
{
|
|
$content = $this->getContents();
|
|
$count = ceil(count($content) / $limit);
|
|
|
|
$folder = [];
|
|
$range = $page * $limit - $limit;
|
|
for ($i = 0; $i <= $limit; $i++) {
|
|
try {
|
|
array_push($folder, $content[$i + $range]);
|
|
} catch (Exception $error) {
|
|
//echo $error;
|
|
}
|
|
}
|
|
|
|
return [
|
|
"pages" => $folder,
|
|
"total" => $count,
|
|
];
|
|
}
|
|
public function getContents()
|
|
{
|
|
$parser = new Parser();
|
|
$contents = [];
|
|
foreach ($this->files as $file) {
|
|
$doc = $parser->parse(file_get_contents($file), false);
|
|
|
|
$meta = $doc->getYAML();
|
|
//$date = getdate($meta["created"]);
|
|
$newDate = date("Y M D d", $meta["created"]);
|
|
$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"],
|
|
"prettyDate" => $newDate,
|
|
"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);
|
|
}
|
|
}
|
|
$contents = orderBy($contents, ["id"], ["desc"]);
|
|
return $contents;
|
|
}
|
|
}
|