Browse Source

draft page controller

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

212
src/app/controller/page.php

@ -4,38 +4,38 @@ class AppControllerPage
{ {
private $_database; private $_database;
private $_validator; private $_validator;
private $_locale;
private $_user; private $_website;
private $_session;
public function __construct()
public function __construct(
AppModelDatabase $database,
AppModelValidator $validator,
AppModelLocale $locale,
AppModelWebsite $website,
AppModelSession $session,
)
{ {
require_once __DIR__ . '/../model/database.php'; $this->_database = $database;
$this->_validator = $validator;
$this->_database = new AppModelDatabase( $this->_locale = $locale;
Environment::config('database') $this->_website = $website;
); $this->_session = $session;
require_once __DIR__ . '/../model/validator.php';
$this->_validator = new AppModelValidator(
Environment::config('validator')
);
require_once __DIR__ . '/user.php';
$this->_user = new AppControllerUser(
$_SERVER['REMOTE_ADDR']
);
} }
private function _response(string $title, string $h1, string $text, int $code = 200) private function _response(string $title, string $h1, mixed $data, int $code = 200)
{ {
require_once __DIR__ . '/response.php'; require_once __DIR__ . '/response.php';
if (is_array($data))
{
$data = implode('<br />', $data);
}
$appControllerResponse = new AppControllerResponse( $appControllerResponse = new AppControllerResponse(
$title, $title,
$h1, $h1,
$text, $data,
$code $code
); );
@ -44,28 +44,123 @@ class AppControllerPage
exit; exit;
} }
public function renderFormDescription() private function _initUser(string $address)
{ {
// Prepare locales if (empty($address))
$locales = []; {
$this->_response(
sprintf(
_('Error - %s'),
$this->_website->getName()
),
_('500'),
_('Could not init session'),
500
);
}
foreach (Environment::config('locales') as $key => $value) $error = [];
if (!$this->_validator->host($address, $error))
{ {
$locales[$key] = (object) $this->_response(
[ sprintf(
'key' => $key, _('Error - %s'),
'value' => $value[0], $this->_website->getName()
'active' => false !== stripos($_SERVER['HTTP_ACCEPT_LANGUAGE'], $key) ? true : false, ),
]; _('406'),
$error,
406
);
} }
try
{
$this->_database->beginTransaction();
$user = $this->_database->getUser(
$this->_database->initUserId(
$address,
$this->_website->getDefaultUserStatus(),
$this->_website->getDefaultUserApproved(),
time()
)
);
$this->_database->commit();
}
catch (Exception $error)
{
$this->_database->rollback();
$this->_response(
sprintf(
_('Error - %s'),
$this->_website->getName()
),
_('500'),
$error,
500
);
}
// Access denied
if (!$user->status)
{
$this->_response(
sprintf(
_('Error - %s'),
$this->_website->getName()
),
_('403'),
_('Access denied'),
403
);
}
// Require account type selection
if (is_null($user->public))
{
header(
sprintf(
'Location: %s/welcome',
trim($this->_website->getUrl(), '/')
)
);
}
}
public function get(int $pageId)
{
return $this->_database->getPage($pageId);
}
public function add(int $timeAdded)
{
return $this->_database->addPage($timeAdded);
}
public function commitTitle(int $localeId, string $value)
{
}
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