Improve 404 pages

This commit is contained in:
Jay Trees 2022-04-07 15:28:58 +02:00
parent c6e6121f06
commit 3ef2272400
5 changed files with 91 additions and 39 deletions

View file

@ -657,6 +657,50 @@ class Page
<?php
}
public function errorDocument(int $statusCode, object $objectNotFound): void
{
http_response_code($statusCode);
$this->header();
$this->bodyStart();
$this->navigation();
$class = new \ReflectionClass($objectNotFound);
$className = $class->getShortName();
?>
<main>
<div class="ui container">
<h1 class="ui header">
<?= $statusCode ?>
<div class="sub header"><?= sprintf(__('%s not found'), $className) ?></div>
</h1>
<?= $this->messages() ?>
<?php
switch ($statusCode) {
case 404:
switch ($className) {
case 'Wishlist':
echo '<p>' . sprintf(__('The requested %s was not found and likely deleted by its creator.'), $className) . '</p>';
break;
default:
echo '<p>' . sprintf(__('The requested %s was not found.'), $className) . '</p>';
break;
}
break;
}
?>
</div>
</main>
<?php
$this->footer();
$this->bodyEnd();
die();
}
public function messages(): string
{
$html = '';

View file

@ -49,6 +49,8 @@ class Wish
public \stdClass $info;
public bool $exists = false;
public function __construct(int|array $wish, bool $generateCache = false)
{
global $database;
@ -68,26 +70,28 @@ class Wish
}
if ($columns) {
$this->exists = true;
foreach ($columns as $key => $value) {
$this->$key = $value;
}
}
$this->info = new \stdClass();
$this->info = new \stdClass();
if ($this->url) {
$this->cache = new EmbedCache($this->url);
$this->info = $this->cache->get($generateCache);
}
foreach ($columns as $key => $value) {
if (empty($value) && isset($this->info->$key)) {
$this->$key = $this->info->$key;
if ($this->url) {
$this->cache = new EmbedCache($this->url);
$this->info = $this->cache->get($generateCache);
}
}
if (empty($this->image)) {
$this->image = '/src/assets/img/no-image.svg';
foreach ($columns as $key => $value) {
if (empty($value) && isset($this->info->$key)) {
$this->$key = $this->info->$key;
}
}
if (empty($this->image)) {
$this->image = '/src/assets/img/no-image.svg';
}
}
}
@ -202,10 +206,14 @@ class Wish
public function getTitle(): string
{
$title = $this->title
?: $this->description
?: $this->url
?: $this->id;
$title = __('Wish not found');
if ($this->exists) {
$title = $this->title
?: $this->description
?: $this->url
?: $this->id;
}
return $title;
}

View file

@ -36,13 +36,12 @@ class Wishlist
WHERE `' . $column . '` = ' . $id_or_hash . ';')
->fetch();
foreach ($columns as $key => $value) {
$this->$key = $value;
}
/** Exists */
if (isset($this->id)) {
if ($columns) {
$this->exists = true;
foreach ($columns as $key => $value) {
$this->$key = $value;
}
} else {
return;
}
@ -109,4 +108,15 @@ class Wishlist
return $html;
}
public function getTitle(): string
{
$title = __('Wishlist not found');
if ($this->exists) {
$title = $this->name;
}
return $title;
}
}

View file

@ -35,6 +35,10 @@ if ('POST' === $_SERVER['REQUEST_METHOD'] && count($_POST) >= 0) {
$page->messages[] = Page::success(__('Wish successfully updated.'), __('Success'));
}
if (!$userIsAuthenticated || !$wish->exists) {
$page->errorDocument(404, $wish);
}
$wishlists = $user->getWishlists($wish->wishlist);
foreach ($wishlists as $wishlist) {
@ -44,15 +48,6 @@ foreach ($wishlists as $wishlist) {
}
}
if (!$userIsAuthenticated) {
http_response_code(404);
?>
<h1><?= __('Not found') ?></h1>
<p><?= __('The requested Wish was not found.') ?></p>
<?php
die();
}
$page->header();
$page->bodyStart();
$page->navigation();

View file

@ -9,17 +9,12 @@
use wishthis\{Page, User, Wishlist};
$wishlist = new Wishlist($_GET['wishlist']);
$page = new Page(__FILE__, $wishlist->getTitle());
if (!$wishlist->exists) {
http_response_code(404);
?>
<h1><?= __('Not found') ?></h1>
<p><?= __('The requested Wishlist was not found and likely deleted by its creator.') ?></p>
<?php
die();
$page->errorDocument(404, $wishlist);
}
$page = new Page(__FILE__, $wishlist->name);
$page->header();
$page->bodyStart();
$page->navigation();