Browse Source

draft page form progress

main
ghost 1 year ago
parent
commit
1dba3a4126
  1. 189
      src/app/controller/page.php

189
src/app/controller/page.php

@ -7,13 +7,15 @@ class AppControllerPage
private $_locale; private $_locale;
private $_website; private $_website;
private $_session; private $_session;
private $_request;
public function __construct( public function __construct(
AppModelDatabase $database, AppModelDatabase $database,
AppModelValidator $validator, AppModelValidator $validator,
AppModelLocale $locale, AppModelLocale $locale,
AppModelWebsite $website, AppModelWebsite $website,
AppModelSession $session AppModelSession $session,
AppModelRequest $request
) )
{ {
$this->_database = $database; $this->_database = $database;
@ -21,6 +23,7 @@ class AppControllerPage
$this->_locale = $locale; $this->_locale = $locale;
$this->_website = $website; $this->_website = $website;
$this->_session = $session; $this->_session = $session;
$this->_request = $request;
} }
private function _response(string $title, string $h1, mixed $data, int $code = 200) private function _response(string $title, string $h1, mixed $data, int $code = 200)
@ -117,45 +120,154 @@ class AppControllerPage
} }
} }
public function get(int $pageId) private function _initLocale(string $value)
{ {
return $this->_database->getPage($pageId); if (!$locale = $this->_database->findLocale($value))
{
$locale = $this->_database->getLocale(
$this->_database->addLocale(
$value
)
);
} }
public function add(int $timeAdded) return $locale;
}
private function _initPage(int $pageId = 0)
{
if (!$page = $this->_database->getPage($pageId))
{ {
return $this->_database->addPage($timeAdded); $page = $this->_database->getPage(
$this->_database->addPage(
time()
)
);
} }
public function commitTitle(int $localeId, string $value) return $page;
}
private function _initText(string $value, string $mime = 'text/plain')
{
if (!$text = $this->_database->findText($mime, md5($value)))
{ {
$text = $this->_database->getText(
$this->_database->addText(
$mime,
md5($value),
$value,
time()
)
);
}
return $text;
} }
private function _commitPageTitle(int $pageId, int $userId, int $localeId, string $text, string $mime = 'text/plain')
{
$textId = $this->_initText(
$text,
$mime
)->textId;
if (!$this->_database->findPageTitleLatest($pageId,
$userId,
$localeId,
$textId))
{
$this->_database->addPageTitle(
$pageId,
$userId,
$localeId,
$textId,
time()
);
}
}
public function renderFormSubmit() public function renderFormSubmit()
{ {
// Init user
$user = $this->_initUser( $user = $this->_initUser(
$this->_session->getAddress() $this->_session->getAddress()
); );
// Init page
if ($this->_request->get('pageId'))
{
$page = $this->_initPage(
(int) $this->_request->get('pageId')
);
}
else if ($this->_request->post('pageId'))
{
$page = $this->_initPage(
(int) $this->_request->post('pageId')
);
}
else
{
$page = $this->_initPage();
}
// Init locale
if ($this->_locale->codeExists($this->_request->get('locale')))
{
$localeCode = (int) $this->_request->get('locale');
}
else if ($this->_locale->codeExists($this->_request->post('locale')))
{
$localeCode = (int) $this->_request->post('locale');
}
else
{
$localeCode = $this->_website->getDefaultLocale();
if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) // @TODO environment
{
foreach ($this->_locale->getList() as $value)
{
if (false !== stripos($_SERVER['HTTP_ACCEPT_LANGUAGE'], $value->code))
{
$localeCode = $value->code;
break;
}
}
}
}
$locale = $this->_initLocale($localeCode);
// Init form // Init form
$form = (object) $form = (object)
[ [
'locale' => (object) 'pageId' => (object)
[ [
'error' => [], 'error' => [],
'values' => $this->_locale->getLocales(), 'type' => 'hidden',
'attribute' => (object) 'attribute' => (object)
[ [
'value' => null, 'value' => $page->pageId,
'placeholder' => _('Page content language'),
] ]
], ],
'locale' => (object)
[
'error' => [],
'type' => 'select',
'options' => $this->_locale->getList(),
'value' => $locale->value,
'placeholder' => _('Page content language'),
],
'title' => (object) 'title' => (object)
[ [
'error' => [], 'error' => [],
'type' => 'text',
'attribute' => (object) 'attribute' => (object)
[ [
'value' => null, 'value' => null,
@ -172,6 +284,7 @@ class AppControllerPage
'description' => (object) 'description' => (object)
[ [
'error' => [], 'error' => [],
'type' => 'textarea',
'attribute' => (object) 'attribute' => (object)
[ [
'value' => null, 'value' => null,
@ -188,6 +301,7 @@ class AppControllerPage
'keywords' => (object) 'keywords' => (object)
[ [
'error' => [], 'error' => [],
'type' => 'textarea',
'attribute' => (object) 'attribute' => (object)
[ [
'value' => null, 'value' => null,
@ -204,6 +318,7 @@ class AppControllerPage
'sensitive' => (object) 'sensitive' => (object)
[ [
'error' => [], 'error' => [],
'type' => 'checkbox',
'attribute' => (object) 'attribute' => (object)
[ [
'value' => null, 'value' => null,
@ -213,37 +328,29 @@ class AppControllerPage
]; ];
// Submit request // Submit request
if (isset($_POST)) if ($this->_request->hasPost())
{ {
if (isset($_POST['locale'])) /// Title
{ if ($title = $this->_request->post('title'))
if (!$this->_locale->localeKeyExists($_POST['locale']))
{
$form->locale->error[] = [
_('Locale not supported')
];
}
$form->locale->attribute->value = htmlentities($_POST['locale']);
}
if (isset($_POST['title']))
{ {
$error = []; $error = [];
if (!$this->_validator->pageTitle($_POST['title'], $error)) if (!$this->_validator->pageTitle($title, $error))
{ {
$form->title->error[] = $error; $form->title->error[] = $error;
$form->title->attribute->value = htmlentities($_POST['title']);
} }
else else
{ {
$this->commitTitle($_POST['locale'], $_POST['title']); $this->_commitPageTitle(
$page->pageId,
$form->title->attribute->value = $this->getTitle(); $user->userId,
$locale->localeId,
$title
);
} }
$form->title->attribute->value = htmlentities($title);
} }
if (isset($_POST['description'])) if (isset($_POST['description']))
@ -278,26 +385,6 @@ 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
} }
} }

Loading…
Cancel
Save