Browse Source

draft page controller

main
ghost 1 year ago
parent
commit
246944e74e
  1. 206
      src/app/controller/page.php

206
src/app/controller/page.php

@ -4,68 +4,163 @@ class AppControllerPage
{ {
private $_database; private $_database;
private $_validator; private $_validator;
private $_locale;
private $_website;
private $_session;
public function __construct(
AppModelDatabase $database,
AppModelValidator $validator,
AppModelLocale $locale,
AppModelWebsite $website,
AppModelSession $session,
)
{
$this->_database = $database;
$this->_validator = $validator;
$this->_locale = $locale;
$this->_website = $website;
$this->_session = $session;
}
private $_user; private function _response(string $title, string $h1, mixed $data, int $code = 200)
{
require_once __DIR__ . '/response.php';
public function __construct() if (is_array($data))
{ {
require_once __DIR__ . '/../model/database.php'; $data = implode('<br />', $data);
}
$this->_database = new AppModelDatabase( $appControllerResponse = new AppControllerResponse(
Environment::config('database') $title,
$h1,
$data,
$code
); );
require_once __DIR__ . '/../model/validator.php'; $appControllerResponse->render();
exit;
}
$this->_validator = new AppModelValidator( private function _initUser(string $address)
Environment::config('validator') {
if (empty($address))
{
$this->_response(
sprintf(
_('Error - %s'),
$this->_website->getName()
),
_('500'),
_('Could not init session'),
500
); );
}
require_once __DIR__ . '/user.php'; $error = [];
if (!$this->_validator->host($address, $error))
{
$this->_response(
sprintf(
_('Error - %s'),
$this->_website->getName()
),
_('406'),
$error,
406
);
}
$this->_user = new AppControllerUser( try
$_SERVER['REMOTE_ADDR'] {
$this->_database->beginTransaction();
$user = $this->_database->getUser(
$this->_database->initUserId(
$address,
$this->_website->getDefaultUserStatus(),
$this->_website->getDefaultUserApproved(),
time()
)
); );
$this->_database->commit();
} }
private function _response(string $title, string $h1, string $text, int $code = 200) catch (Exception $error)
{ {
require_once __DIR__ . '/response.php'; $this->_database->rollback();
$appControllerResponse = new AppControllerResponse( $this->_response(
$title, sprintf(
$h1, _('Error - %s'),
$text, $this->_website->getName()
$code ),
_('500'),
$error,
500
); );
}
$appControllerResponse->render(); // Access denied
if (!$user->status)
{
$this->_response(
sprintf(
_('Error - %s'),
$this->_website->getName()
),
_('403'),
_('Access denied'),
403
);
}
exit; // Require account type selection
if (is_null($user->public))
{
header(
sprintf(
'Location: %s/welcome',
trim($this->_website->getUrl(), '/')
)
);
}
} }
public function renderFormDescription()
public function get(int $pageId)
{ {
// Prepare locales return $this->_database->getPage($pageId);
$locales = []; }
foreach (Environment::config('locales') as $key => $value) public function add(int $timeAdded)
{ {
$locales[$key] = (object) return $this->_database->addPage($timeAdded);
[ }
'key' => $key,
'value' => $value[0], public function commitTitle(int $localeId, string $value)
'active' => false !== stripos($_SERVER['HTTP_ACCEPT_LANGUAGE'], $key) ? true : false, {
];
} }
public function renderFormSubmit()
{
$user = $this->_initUser(
$this->_session->getAddress()
);
// Init form // Init form
$form = (object) $form = (object)
[ [
'locale' => (object) 'locale' => (object)
[ [
'error' => [], 'error' => [],
'values' => $locales, 'values' => $this->_locale->getLocales(),
'attribute' => (object) 'attribute' => (object)
[ [
'value' => null, 'value' => null,
@ -134,6 +229,18 @@ class AppControllerPage
// Submit request // Submit request
if (isset($_POST)) if (isset($_POST))
{ {
if (isset($_POST['locale']))
{
if (!$this->_locale->localeKeyExists($_POST['locale']))
{
$form->locale->error[] = [
_('Locale not supported')
];
}
$form->locale->attribute->value = htmlentities($_POST['locale']);
}
if (isset($_POST['title'])) if (isset($_POST['title']))
{ {
$error = []; $error = [];
@ -141,11 +248,16 @@ class AppControllerPage
if (!$this->_validator->pageTitle($_POST['title'], $error)) if (!$this->_validator->pageTitle($_POST['title'], $error))
{ {
$form->title->error[] = $error; $form->title->error[] = $error;
$form->title->attribute->value = htmlentities($_POST['title']);
} }
// @TODO check for page duplicates else
{
$this->commitTitle($_POST['locale'], $_POST['title']);
$form->title->attribute->value = htmlentities($_POST['title']); $form->title->attribute->value = $this->getTitle();
}
} }
if (isset($_POST['description'])) if (isset($_POST['description']))
@ -180,6 +292,26 @@ class AppControllerPage
// Request valid // Request valid
if (empty($error)) if (empty($error))
{ {
// Init page
if (isset($_GET['pageId']))
{
$page = $this->_database->getPage((int) $_GET['pageId']);
}
else if (isset($_POST['pageId']))
{
$page = $this->_database->getPage((int) $_POST['pageId']);
}
else
{
$page = $this->_database->getPage(
$this->_database->addPage(
time()
)
);
}
// @TODO redirect // @TODO redirect
} }
} }
@ -188,10 +320,10 @@ class AppControllerPage
require_once __DIR__ . '/module/head.php'; require_once __DIR__ . '/module/head.php';
$appControllerModuleHead = new AppControllerModuleHead( $appControllerModuleHead = new AppControllerModuleHead(
Environment::config('website')->url, $this->_website->getUrl(),
sprintf( sprintf(
_('Submit - %s'), _('Submit - %s'),
Environment::config('website')->name $this->_website->getName()
), ),
[ [
[ [
@ -216,7 +348,7 @@ class AppControllerPage
require_once __DIR__ . '/module/profile.php'; require_once __DIR__ . '/module/profile.php';
$appControllerModuleProfile = new AppControllerModuleProfile( $appControllerModuleProfile = new AppControllerModuleProfile(
$this->_user $user
); );
require_once __DIR__ . '/module/header.php'; require_once __DIR__ . '/module/header.php';
@ -227,6 +359,6 @@ class AppControllerPage
$appControllerModuleFooter = new AppControllerModuleFooter(); $appControllerModuleFooter = new AppControllerModuleFooter();
include __DIR__ . '../../view/theme/default/page/form/description.phtml'; include __DIR__ . '../../view/theme/default/page/form/submit.phtml';
} }
} }
Loading…
Cancel
Save