fipamo/brain/data/Book.php

299 lines
9.3 KiB
PHP
Raw Permalink Normal View History

<?php
namespace brain\data;
2021-04-07 23:09:40 +02:00
use function _\filter;
use function _\find;
2022-03-20 03:36:15 +01:00
use brain\utility\DocTools;
use brain\utility\FileUploader;
use brain\utility\StringTools;
class Book
{
public function __construct()
{
}
public function findPageById(string $uuid)
{
$content = $this->getContents();
2022-03-20 03:36:15 +01:00
$page = find($content, ['uuid' => $uuid]);
return $page;
}
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]);
} else {
2022-03-20 03:36:15 +01:00
$page = find($content, ['layout' => 'index']);
}
return $page;
}
public function editPage($task, $request)
{
$content = $this->getContents();
2022-03-20 03:36:15 +01:00
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();
}
2022-03-20 03:36:15 +01:00
$page = find($content, ['uuid' => $body['uuid']]);
$files = $request->getUploadedFiles();
2022-03-20 03:36:15 +01:00
$member = Session::get('member');
2022-03-20 03:36:15 +01:00
if ($task != 'create') {
$path =
2022-03-20 03:36:15 +01:00
date('Y', date($page['rawCreated'])).
'/'.
date('m', date($page['rawCreated']));
} else {
2022-03-20 03:36:15 +01:00
$path = date('Y').'/'.date('m');
}
$page_feature = '';
$page_files = '';
2022-03-20 03:36:15 +01:00
if (isset($files['page_files'])) {
$imageList = '';
$fileList = '';
// var_dump($files["page_files"] );
foreach ($files['page_files'] as $file) {
$type = $file->getClientMediaType();
switch ($type) {
2022-03-20 03:36:15 +01:00
case 'image/jpeg':
case 'image/png':
case 'image/gif':
case 'image/svg':
$imagesPath = '/assets/images/blog/'.$path.'/';
$imageList =
2022-03-20 03:36:15 +01:00
$imageList.$imagesPath.urlencode($file->getClientFileName()).', ';
FileUploader::uploadFile(
2022-03-20 03:36:15 +01:00
'../public/assets/images/blog/'.$path.'/',
$file
);
break;
2022-03-20 03:36:15 +01:00
case 'video/mp4':
$videosPath = '/assets/video/blog/'.$path.'/';
$imageList =
2022-03-20 03:36:15 +01:00
$imageList.$videosPath.urlencode($file->getClientFileName()).', ';
FileUploader::uploadFile(
2022-03-20 03:36:15 +01:00
'../public/assets/video/blog/'.$path.'/',
$file
);
break;
2022-03-20 03:36:15 +01:00
case 'audio/mpeg':
$soundPath = '/assets/sound/blog/'.$path.'/';
$fileList = $fileList.$soundPath.urlencode($file->getClientFileName()).', ';
FileUploader::uploadFile(
2022-03-20 03:36:15 +01:00
'../public/assets/sound/blog/'.$path.'/',
$file
);
break;
case 'application/pdf':
case 'text/plain':
case 'text/rtf':
2022-03-20 03:36:15 +01:00
$docPath = '/assets/docs/blog/'.$path.'/';
$fileList = $fileList.$docPath.urlencode($file->getClientFileName()).', ';
FileUploader::uploadFile(
2022-03-20 03:36:15 +01:00
'../public/assets/docs/blog/'.$path.'/',
$file
);
break;
}
}
$page_feature = $imageList;
$page_files = $fileList;
} else {
2022-03-20 03:36:15 +01:00
// if no files, just reset string from page object
$page_feature = $page['feature'];
$page_files = $page['files'];
}
2022-03-20 03:36:15 +01:00
if ($task == 'delete') {
$deleted = 'true';
$body['menu'] = 'false';
$body['published'] = 'false';
$body['featured'] = 'false';
} else {
2022-03-20 03:36:15 +01:00
$deleted = isset($page['deleted']) ? $page['deleted'] : 'false';
}
$created =
2022-03-20 03:36:15 +01:00
$task != 'create'
? new \Moment\Moment($page['rawCreated'])
: new \Moment\Moment();
$updated = new \Moment\Moment();
2022-03-20 03:36:15 +01:00
// grab current index from settings and update
$id = $task != 'create' ? $body['id'] : Settings::getCurrentIndex();
$uuid = $task != 'create' ? $body['uuid'] : StringTools::createUUID();
// now that variables are done, set to body object and then convert to markdown to save
2022-03-20 03:36:15 +01:00
$body['id'] = $id;
$body['uuid'] = $uuid;
$body['feature'] = $page_feature;
$body['files'] = $page_files;
$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
2022-03-20 03:36:15 +01:00
if ($body['layout'] == 'index') {
$writePath = '../content/pages/start/index.md';
} else {
2022-03-20 03:36:15 +01:00
$writePath = '../content/pages/'.$path.'/'.$body['slug'].'.md';
2021-04-07 23:09:40 +02:00
}
$status = DocTools::writePages($task, $path, $writePath, $write);
if ($status) {
$config = new Settings();
$settings = $config->getSettings();
2022-03-20 03:36:15 +01:00
$message = '';
if (
2022-03-20 03:36:15 +01:00
$settings['global']['renderOnSave'] == 'true' &&
$settings['global']['dynamicRender'] == 'false'
) {
$render = new Render();
$render->renderTags();
$render->renderArchive();
$render->renderPages();
2022-03-20 03:36:15 +01:00
$message = 'Filed edited and rendered. NOICE.';
} else {
2022-03-20 03:36:15 +01:00
$message = 'File edited. Nice work';
}
$response = [
2022-03-20 03:36:15 +01:00
'message' => $message,
'type' => $task == 'write' ? 'postUpdated' : 'postAdded',
'id' => $uuid,
];
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-20 03:36:15 +01:00
// once saved, update menu
$body['path'] = $path;
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') {
Settings::updateIndex();
}
} else {
$response = [
2022-03-20 03:36:15 +01:00
'message' => "Uh oh. File save problem. Don't panic",
'type' => 'postError',
'id' => $uuid,
];
}
return $response;
}
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;
});
$deleted = filter($content, function ($item) {
2022-03-20 03:36:15 +01:00
return $item['deleted'] == true;
});
2022-03-20 03:36:15 +01:00
// $all = $content;
$all = filter($content, function ($item) {
2022-03-20 03:36:15 +01:00
return $item['deleted'] == false;
});
2022-03-20 03:36:15 +01:00
$filter = isset($sort) ? $sort : 'all';
switch ($filter) {
2022-03-20 03:36:15 +01:00
case 'published':
$filtered = $published;
break;
2022-03-20 03:36:15 +01:00
case 'deleted':
$filtered = $deleted;
break;
default:
$filtered = $all;
break;
}
$numOfPages = ceil(count($filtered) / ($limit + 1));
$folder = [];
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) {
if (isset($filtered[$i + $range])) {
array_push($folder, $filtered[$i + $range]);
} else {
2022-03-20 03:36:15 +01:00
// chill out
}
}
}
$prev = $page - 1;
if ($prev <= 0) {
$prev = $numOfPages;
}
$next = $page + 1;
if ($next > $numOfPages) {
$next = 1;
}
return [
2022-03-20 03:36:15 +01:00
'pages' => $folder,
'numOfPages' => $numOfPages,
'entryCount' => count($filtered),
'paginate' => [
'sort' => $sort,
'nextPage' => $next,
'prevPage' => $prev,
],
2022-03-20 03:36:15 +01:00
'stats' => [
'all' => count($all),
'published' => count($published),
'deleted' => count($deleted),
],
];
}
2022-03-20 03:36:15 +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();
return $contents;
}
}