2021-03-25 00:59:28 +01:00
|
|
|
<?php
|
|
|
|
|
2022-03-19 00:00:51 +01:00
|
|
|
namespace brain\data;
|
|
|
|
|
2022-09-18 22:07:52 +02:00
|
|
|
use Carbon\Carbon;
|
2022-03-20 03:36:15 +01:00
|
|
|
use brain\utility\DocTools;
|
|
|
|
use brain\utility\StringTools;
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-05-17 02:41:15 +02:00
|
|
|
use function _\find;
|
|
|
|
use function _\filter;
|
2021-03-25 00:59:28 +01:00
|
|
|
|
|
|
|
class Book
|
|
|
|
{
|
2022-03-14 01:46:42 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
2021-11-03 00:19:03 +01:00
|
|
|
}
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-14 01:46:42 +01:00
|
|
|
public function findPageById(string $uuid)
|
|
|
|
{
|
|
|
|
$content = $this->getContents();
|
2022-05-17 02:41:15 +02:00
|
|
|
$page = find($content, ['uuid' => $uuid]);
|
2022-03-20 03:36:15 +01:00
|
|
|
|
2022-03-14 01:46:42 +01:00
|
|
|
return $page;
|
2021-04-14 00:20:57 +02:00
|
|
|
}
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-14 01:46:42 +01:00
|
|
|
public function findPageBySlug(string $slug = null)
|
|
|
|
{
|
|
|
|
$content = $this->getContents();
|
|
|
|
if (isset($slug)) {
|
2022-03-20 03:36:15 +01:00
|
|
|
$page = find($content, ['slug' => $slug]);
|
2022-03-14 01:46:42 +01:00
|
|
|
} else {
|
2022-03-20 03:36:15 +01:00
|
|
|
$page = find($content, ['layout' => 'index']);
|
2022-03-14 01:46:42 +01:00
|
|
|
}
|
2021-04-14 00:20:57 +02:00
|
|
|
|
2022-03-14 01:46:42 +01:00
|
|
|
return $page;
|
2021-04-14 00:20:57 +02:00
|
|
|
}
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-14 01:46:42 +01:00
|
|
|
public function editPage($task, $request)
|
|
|
|
{
|
|
|
|
$content = $this->getContents();
|
2023-03-23 21:55:34 +01:00
|
|
|
$body = json_decode(file_get_contents("php://input"), true);
|
|
|
|
//$body = find($content, ["uuid" => $parsed["id"]]);
|
2021-04-14 00:20:57 +02:00
|
|
|
|
2022-05-17 02:41:15 +02:00
|
|
|
$page = find($content, ['uuid' => $body['uuid']]);
|
2022-03-14 01:46:42 +01:00
|
|
|
$files = $request->getUploadedFiles();
|
|
|
|
|
2022-03-20 03:36:15 +01:00
|
|
|
$member = Session::get('member');
|
2022-03-14 01:46:42 +01:00
|
|
|
|
2022-03-20 03:36:15 +01:00
|
|
|
if ($task != 'create') {
|
2022-05-17 02:41:15 +02:00
|
|
|
$path = date('Y', date($page['rawCreated'])) .
|
|
|
|
'/' .
|
2022-03-20 03:36:15 +01:00
|
|
|
date('m', date($page['rawCreated']));
|
2022-03-14 01:46:42 +01:00
|
|
|
} else {
|
2022-05-17 02:41:15 +02:00
|
|
|
$path = date('Y') . '/' . date('m');
|
2022-03-14 01:46:42 +01:00
|
|
|
}
|
2021-04-14 00:20:57 +02:00
|
|
|
|
2022-03-16 00:56:37 +01:00
|
|
|
$page_feature = '';
|
2022-05-17 02:41:15 +02:00
|
|
|
$page_files = '';
|
2022-03-16 00:56:37 +01:00
|
|
|
|
2022-03-20 03:36:15 +01:00
|
|
|
if ($task == 'delete') {
|
2022-05-17 02:41:15 +02:00
|
|
|
$deleted = 'true';
|
|
|
|
$body['menu'] = 'false';
|
2022-03-20 03:36:15 +01:00
|
|
|
$body['published'] = 'false';
|
2022-05-17 02:41:15 +02:00
|
|
|
$body['featured'] = 'false';
|
2022-03-14 01:46:42 +01:00
|
|
|
} else {
|
2022-03-20 03:36:15 +01:00
|
|
|
$deleted = isset($page['deleted']) ? $page['deleted'] : 'false';
|
2022-03-14 01:46:42 +01:00
|
|
|
}
|
|
|
|
|
2022-09-18 22:07:52 +02:00
|
|
|
$created = $task != 'create' ? new Carbon($page['rawCreated']) : Carbon::now();
|
|
|
|
$updated = Carbon::now();
|
2021-04-14 00:20:57 +02:00
|
|
|
|
2022-03-20 03:36:15 +01:00
|
|
|
// grab current index from settings and update
|
2022-05-17 02:41:15 +02:00
|
|
|
$id = $task != 'create' ? $body['id'] : Settings::getCurrentIndex();
|
2022-03-20 03:36:15 +01:00
|
|
|
$uuid = $task != 'create' ? $body['uuid'] : StringTools::createUUID();
|
2022-03-19 00:00:51 +01:00
|
|
|
// now that variables are done, set to body object and then convert to markdown to save
|
2021-04-14 00:20:57 +02:00
|
|
|
|
2023-03-23 21:55:34 +01:00
|
|
|
$body['id'] = $id;
|
|
|
|
$body['uuid'] = $uuid;
|
|
|
|
//$body['feature'] = $page_feature;
|
|
|
|
//$body['files'] = $page_files;
|
2022-05-17 02:41:15 +02:00
|
|
|
$body['path'] = $path;
|
|
|
|
$body['author'] = $member['handle'];
|
2022-03-20 03:36:15 +01:00
|
|
|
$body['created'] = $created->format("Y-m-d\TH:i:sP");
|
|
|
|
$body['updated'] = $updated->format("Y-m-d\TH:i:sP");
|
|
|
|
$body['deleted'] = $deleted;
|
2022-03-14 01:46:42 +01:00
|
|
|
|
|
|
|
$write = DocTools::objectToMD($body);
|
|
|
|
|
2022-03-19 00:00:51 +01:00
|
|
|
// if layout is index, change path to file
|
2022-03-14 01:46:42 +01:00
|
|
|
|
2022-03-20 03:36:15 +01:00
|
|
|
if ($body['layout'] == 'index') {
|
|
|
|
$writePath = '../content/pages/start/index.md';
|
2021-04-09 00:27:05 +02:00
|
|
|
} else {
|
2022-05-17 02:41:15 +02:00
|
|
|
$writePath = '../content/pages/' . $path . '/' . $body['slug'] . '.md';
|
2021-04-07 23:09:40 +02:00
|
|
|
}
|
2021-04-02 02:37:12 +02:00
|
|
|
|
2022-03-14 01:46:42 +01:00
|
|
|
$status = DocTools::writePages($task, $path, $writePath, $write);
|
|
|
|
|
|
|
|
if ($status) {
|
2022-05-17 02:41:15 +02:00
|
|
|
$config = new Settings();
|
2022-03-14 01:46:42 +01:00
|
|
|
$settings = $config->getSettings();
|
2022-05-17 02:41:15 +02:00
|
|
|
$message = '';
|
2022-03-14 01:46:42 +01:00
|
|
|
|
2022-03-19 00:00:51 +01:00
|
|
|
if (
|
2022-03-20 03:36:15 +01:00
|
|
|
$settings['global']['renderOnSave'] == 'true' &&
|
|
|
|
$settings['global']['dynamicRender'] == 'false'
|
2022-03-14 01:46:42 +01:00
|
|
|
) {
|
|
|
|
$render = new Render();
|
|
|
|
$render->renderTags();
|
|
|
|
$render->renderArchive();
|
|
|
|
$render->renderPages();
|
2022-03-20 03:36:15 +01:00
|
|
|
$message = 'Filed edited and rendered. NOICE.';
|
2022-03-14 01:46:42 +01:00
|
|
|
} else {
|
2022-03-20 03:36:15 +01:00
|
|
|
$message = 'File edited. Nice work';
|
2022-03-14 01:46:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$response = [
|
2022-05-17 02:41:15 +02:00
|
|
|
'message' => $message,
|
|
|
|
'type' => $task == 'write' ? 'postUpdated' : 'postAdded',
|
|
|
|
'id' => $uuid,
|
2022-03-14 01:46:42 +01:00
|
|
|
];
|
|
|
|
|
2022-03-20 03:36:15 +01:00
|
|
|
// 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);
|
2022-03-14 01:46:42 +01:00
|
|
|
|
2022-03-20 03:36:15 +01:00
|
|
|
// once saved, update menu
|
|
|
|
$body['path'] = $path;
|
2022-03-14 01:46:42 +01:00
|
|
|
Settings::updateMenu($body);
|
|
|
|
Settings::updateTags();
|
2022-03-20 03:36:15 +01:00
|
|
|
// if new page added, update current index in Settings file
|
|
|
|
if ($task == 'create') {
|
2022-03-14 01:46:42 +01:00
|
|
|
Settings::updateIndex();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$response = [
|
2022-05-17 02:41:15 +02:00
|
|
|
'message' => "Uh oh. File save problem. Don't panic",
|
|
|
|
'type' => 'postError',
|
|
|
|
'id' => $uuid,
|
2022-03-14 01:46:42 +01:00
|
|
|
];
|
|
|
|
}
|
2021-04-09 00:27:05 +02:00
|
|
|
|
2022-03-14 01:46:42 +01:00
|
|
|
return $response;
|
2021-04-09 00:27:05 +02:00
|
|
|
}
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-14 01:46:42 +01:00
|
|
|
public function getPages(int $page, int $limit, string $sort = null)
|
|
|
|
{
|
|
|
|
$content = $this->getContents();
|
|
|
|
|
|
|
|
$published = filter($content, function ($item) {
|
2022-03-20 03:36:15 +01:00
|
|
|
return $item['published'] == true && $item['deleted'] == false;
|
2022-03-14 01:46:42 +01:00
|
|
|
});
|
|
|
|
$deleted = filter($content, function ($item) {
|
2022-03-20 03:36:15 +01:00
|
|
|
return $item['deleted'] == true;
|
2022-03-14 01:46:42 +01:00
|
|
|
});
|
|
|
|
|
2022-03-20 03:36:15 +01:00
|
|
|
// $all = $content;
|
2022-03-14 01:46:42 +01:00
|
|
|
$all = filter($content, function ($item) {
|
2022-03-20 03:36:15 +01:00
|
|
|
return $item['deleted'] == false;
|
2022-03-14 01:46:42 +01:00
|
|
|
});
|
2022-03-20 03:36:15 +01:00
|
|
|
$filter = isset($sort) ? $sort : 'all';
|
2022-03-14 01:46:42 +01:00
|
|
|
switch ($filter) {
|
2022-03-20 03:36:15 +01:00
|
|
|
case 'published':
|
2022-03-14 01:46:42 +01:00
|
|
|
$filtered = $published;
|
|
|
|
break;
|
2022-03-20 03:36:15 +01:00
|
|
|
case 'deleted':
|
2022-03-14 01:46:42 +01:00
|
|
|
$filtered = $deleted;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$filtered = $all;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$numOfPages = ceil(count($filtered) / ($limit + 1));
|
2022-05-17 02:41:15 +02:00
|
|
|
$folder = [];
|
2022-03-14 01:46:42 +01:00
|
|
|
|
|
|
|
if (count($filtered) != 0) {
|
|
|
|
if (count($filtered) < $limit) {
|
|
|
|
$limit = count($filtered) - 1;
|
|
|
|
}
|
|
|
|
$range = $page * $limit - $limit;
|
|
|
|
|
|
|
|
if ($range != 0) {
|
|
|
|
$range = $range + 1;
|
|
|
|
}
|
2022-03-20 03:36:15 +01:00
|
|
|
for ($i = 0; $i <= $limit; ++$i) {
|
2022-03-14 01:46:42 +01:00
|
|
|
if (isset($filtered[$i + $range])) {
|
|
|
|
array_push($folder, $filtered[$i + $range]);
|
|
|
|
} else {
|
2022-03-20 03:36:15 +01:00
|
|
|
// chill out
|
2022-03-14 01:46:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$prev = $page - 1;
|
|
|
|
if ($prev <= 0) {
|
|
|
|
$prev = $numOfPages;
|
|
|
|
}
|
|
|
|
|
|
|
|
$next = $page + 1;
|
|
|
|
if ($next > $numOfPages) {
|
|
|
|
$next = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2022-05-17 02:41:15 +02:00
|
|
|
'pages' => $folder,
|
|
|
|
'numOfPages' => $numOfPages,
|
|
|
|
'entryCount' => count($filtered),
|
|
|
|
'paginate' => [
|
|
|
|
'sort' => $sort,
|
|
|
|
'nextPage' => $next,
|
|
|
|
'prevPage' => $prev,
|
|
|
|
],
|
|
|
|
'stats' => [
|
|
|
|
'all' => count($all),
|
|
|
|
'published' => count($published),
|
|
|
|
'deleted' => count($deleted),
|
|
|
|
],
|
2022-03-14 01:46:42 +01:00
|
|
|
];
|
|
|
|
}
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-14 01:46:42 +01:00
|
|
|
public function getContents()
|
|
|
|
{
|
2022-03-20 03:36:15 +01:00
|
|
|
// test new contents data class
|
|
|
|
// $new = (new Contents("../content/pages"))->getAll();
|
|
|
|
$contents = (new Contents('../content/pages'))->getAll();
|
|
|
|
|
2022-03-14 01:46:42 +01:00
|
|
|
return $contents;
|
|
|
|
}
|
2021-03-25 00:59:28 +01:00
|
|
|
}
|