|
|
|
<?php
|
|
|
|
|
|
|
|
namespace brain\controller;
|
|
|
|
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Slim\Views\Twig;
|
|
|
|
use brain\data\Settings;
|
|
|
|
use brain\utility\Sorting;
|
|
|
|
use function _\find;
|
|
|
|
|
|
|
|
class IndexControl
|
|
|
|
{
|
|
|
|
public static function start(
|
|
|
|
ServerRequestInterface $request,
|
|
|
|
ResponseInterface $response,
|
|
|
|
array $args
|
|
|
|
): ResponseInterface {
|
|
|
|
//unset($_SESSION);
|
|
|
|
$config = new Settings();
|
|
|
|
$settings = $config->getSettings();
|
|
|
|
$view = Twig::fromRequest($request);
|
|
|
|
//checks dynamic render flag for site render status
|
|
|
|
if ($settings['global']['dynamicRender']) {
|
|
|
|
if ($settings['global']['dynamicRender'] == 'true') {
|
|
|
|
$loader = new \Twig\Loader\FilesystemLoader('../content/themes');
|
|
|
|
$display = new \Twig\Environment($loader, []);
|
|
|
|
$template = '';
|
|
|
|
$pageOptions = [];
|
|
|
|
|
|
|
|
$pageInfo = [
|
|
|
|
'keywords' => isset($settings['global']['keywords'])
|
|
|
|
? $settings['global']['keywords']
|
|
|
|
: 'fipamo, blog, jamstack, php, markdown, js',
|
|
|
|
'description' => $settings['global']['descriptions'],
|
|
|
|
'image' => $settings['global']['base_url'] . $settings['global']['background'],
|
|
|
|
'baseURL' => $settings['global']['base_url'],
|
|
|
|
];
|
|
|
|
|
|
|
|
if (isset($args['first'])) {
|
|
|
|
switch ($args['first']) {
|
|
|
|
case 'tags':
|
|
|
|
$template = $settings['global']['theme'] . '/tags.twig';
|
|
|
|
$tag = trim($args['second']);
|
|
|
|
$taglist = Sorting::tags();
|
|
|
|
$item = find($taglist, ['tag_name' => $tag]);
|
|
|
|
$pageOptions = [
|
|
|
|
'title' => 'Pages Tagged as ' . $item['tag_name'],
|
|
|
|
'background' => $pageInfo['image'],
|
|
|
|
'tag_list' => $item['pages'],
|
|
|
|
'info' => $pageInfo,
|
|
|
|
'menu' => $settings['menu'],
|
|
|
|
'dynamicRender' => $settings['global']['dynamicRender'],
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 'archives':
|
|
|
|
$archive = Sorting::archive();
|
|
|
|
$template = $settings['global']['theme'] . '/archive.twig';
|
|
|
|
$pageOptions = [
|
|
|
|
'title' => 'Archive',
|
|
|
|
'background' => $pageInfo['image'],
|
|
|
|
'archives' => $archive,
|
|
|
|
'info' => $pageInfo,
|
|
|
|
'menu' => $settings['menu'],
|
|
|
|
'dynamicRender' => $settings['global']['dynamicRender'],
|
|
|
|
];
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
//check if page is a menu item, if not render along path as usual
|
|
|
|
$page = [];
|
|
|
|
$book = new Book();
|
|
|
|
if (is_numeric($args['first'])) {
|
|
|
|
$page = $book->findPageBySlug($args['third']);
|
|
|
|
} else {
|
|
|
|
$page = $book->findPageBySlug($args['first']);
|
|
|
|
}
|
|
|
|
$template = $settings['global']['theme'] . '/' . $page['layout'] . '.twig';
|
|
|
|
$pageOptions = Sorting::page($page);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//index
|
|
|
|
$template = $settings['global']['theme'] . '/' . $page['layout'] . '.twig';
|
|
|
|
$book = new Book('');
|
|
|
|
$page = $book->findPageBySlug();
|
|
|
|
$pageOptions = Sorting::page($page);
|
|
|
|
}
|
|
|
|
|
|
|
|
$html = $display->render($template, $pageOptions);
|
|
|
|
$response->getBody()->write($html);
|
|
|
|
return $response;
|
|
|
|
} else {
|
|
|
|
//if dynamic flag is false, load up html
|
|
|
|
$view = Twig::fromRequest($request);
|
|
|
|
$html = file_get_contents('../public/index.html');
|
|
|
|
$response->getBody()->write($html);
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//if flag is not present, default to static html
|
|
|
|
$view = Twig::fromRequest($request);
|
|
|
|
$html = file_get_contents('../public/index.html');
|
|
|
|
$response->getBody()->write($html);
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|