You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Fipamo/brain/controller/DashControl.php

220 lines
9.5 KiB
PHTML

<?php
namespace brain\controller;
use brain\data\Book;
use brain\data\Session;
use brain\data\Settings;
use brain\data\Themes;
use brain\utility\Setup;
3 years ago
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Views\Twig;
class DashControl
{
public static function start(
ServerRequestInterface $request,
ResponseInterface $response,
array $args
): ResponseInterface {
$view = Twig::fromRequest($request);
$pageOptions = [];
3 years ago
$template = '';
if (Setup::status()) {
3 years ago
switch (isset($args['second']) ? $args['second'] : 'index') {
case 'settings':
if (Session::active()) {
$config = new Settings();
$settings = $config->getSettings();
3 years ago
$themes = (new Themes())->getThemes(); // $config->getThemes();
$template = 'dash/settings.twig';
$member = Session::get('member');
$form_token = Session::get('form_token');
$updated = new \Moment\Moment($settings['global']['last_backup']);
$pageOptions = [
3 years ago
'title' => 'Dash Settings',
'private' => $settings['global']['private'],
'renderOnSave' => $settings['global']['renderOnSave'],
'background' => $settings['global']['background'],
'member' => $member,
'ftoken' => $form_token,
'siteTitle' => $settings['global']['title'],
'baseUrl' => $settings['global']['base_url'],
'desc' => $settings['global']['descriptions'],
'lastBackup' => $updated->format('Y M D d'),
'currentTheme' => $settings['global']['theme'],
'themes' => $themes,
'apiStatus' => isset($settings['global']['externalAPI'])
? $settings['global']['externalAPI']
: 'false',
'dynamicRenderStatus' => isset(
$settings['global']['dynamicRender']
)
3 years ago
? $settings['global']['dynamicRender']
: 'false',
'mailOption' => $settings['email']['active'],
'mailConfig' => $settings['email'],
'status' => Session::active(),
];
} else {
3 years ago
header('Location: /dashboard');
exit();
}
break;
3 years ago
case 'navigation':
if (Session::active()) {
$config = new Settings();
$settings = $config->getSettings();
3 years ago
$template = 'dash/navigation.twig';
$pageOptions = [
3 years ago
'title' => 'Edit Dash Navigation',
'status' => Session::active(),
'menu' => $settings['menu'],
];
} else {
3 years ago
header('Location: /dashboard');
exit();
}
break;
3 years ago
case 'pages':
if (Session::active()) {
3 years ago
$currentPage = isset($args['fourth']) ? $args['fourth'] : 1;
$filter = isset($args['third']) ? $args['third'] : 'all';
$data = (new Book())->getPages($currentPage, 4, $filter);
3 years ago
$template = 'dash/book.twig';
$pageOptions = [
3 years ago
'title' => 'Contents',
'entryCount' => $data['entryCount'],
'numOfPages' => $data['numOfPages'],
'currentPage' => $currentPage,
'filter' => $data['paginate']['sort'],
'stats' => $data['stats'],
'pages' => $data['pages'],
'paginate' => $data['paginate'],
'status' => Session::active(),
];
} else {
3 years ago
header('Location: /dashboard');
exit();
}
break;
3 years ago
case 'page':
if (Session::active()) {
3 years ago
$template = 'dash/page-edit.twig';
$mode = $args['third'];
$uuid = $args['fourth'];
switch ($mode) {
3 years ago
case 'edit':
$page = (new Book())->findPageById($uuid);
$views = [];
3 years ago
if (str_contains($page['layout'], 'index')) {
$views = (new Themes())->getCustomIndex();
} else {
$views = (new Themes())->getCustomViews();
}
3 years ago
$imageList = explode(',', $page['feature']);
$fileList = explode(',', $page['files']);
$images = [];
$files = [];
foreach ($imageList as $item) {
$image = trim($item);
if (!empty($image)) {
array_push($images, $image);
}
}
foreach ($fileList as $item) {
$file = trim($item);
if (!empty($file)) {
array_push($files, $file);
}
}
$pageOptions = [
3 years ago
'title' => 'Fipamo | Edit Page',
'page' => $page,
'mode' => $mode,
'token' => Session::get('form_token'),
'status' => Session::active(),
'images' => $images,
'files' => $files,
'views' => $views,
];
break;
3 years ago
case 'preview':
$config = new Settings();
$settings = $config->getSettings();
$loader = new \Twig\Loader\FilesystemLoader(
3 years ago
'../content/themes'
);
$display = new \Twig\Environment($loader, []);
$book = new Book();
$page = $book->findPageById($uuid);
$pageOptions = Sorting::page($page);
$preview =
3 years ago
$settings['global']['theme'] .
'/' .
$page['layout'] .
'.twig';
$html = $display->render($preview, $pageOptions);
$response->getBody()->write($html);
3 years ago
return $response;
break;
default:
$pageOptions = [
3 years ago
'title' => 'Fipamo | Create Page',
'token' => Session::get('form_token'),
'mode' => $mode,
'status' => Session::active(),
];
break;
}
} else {
3 years ago
header('Location: /dashboard');
exit();
}
break;
3 years ago
case 'logout':
Session::kill();
3 years ago
header('Location: /dashboard');
exit();
break;
3 years ago
case 'reset-password':
$template = 'dash/reset-password.twig';
$pageOptions = [
3 years ago
'title' => 'Reset Password',
];
break;
default:
3 years ago
$template = 'dash/start.twig';
if (Session::active()) {
$pageOptions = [
3 years ago
'title' => 'Welcome Back',
'status' => Session::active(),
'data' => (new Book())->getPages(1, 4),
];
} else {
$pageOptions = [
3 years ago
'title' => 'Welcome to Fipamo',
'status' => Session::active(),
];
}
break;
}
} else {
3 years ago
$template = 'dash/init.twig';
$pageOptions = ['title' => 'Fipamo Setup'];
}
return $view->render($response, $template, $pageOptions);
}
}