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 <?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 public function messages(): string
{ {
$html = ''; $html = '';

View file

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

View file

@ -36,13 +36,12 @@ class Wishlist
WHERE `' . $column . '` = ' . $id_or_hash . ';') WHERE `' . $column . '` = ' . $id_or_hash . ';')
->fetch(); ->fetch();
foreach ($columns as $key => $value) { if ($columns) {
$this->$key = $value;
}
/** Exists */
if (isset($this->id)) {
$this->exists = true; $this->exists = true;
foreach ($columns as $key => $value) {
$this->$key = $value;
}
} else { } else {
return; return;
} }
@ -109,4 +108,15 @@ class Wishlist
return $html; 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')); $page->messages[] = Page::success(__('Wish successfully updated.'), __('Success'));
} }
if (!$userIsAuthenticated || !$wish->exists) {
$page->errorDocument(404, $wish);
}
$wishlists = $user->getWishlists($wish->wishlist); $wishlists = $user->getWishlists($wish->wishlist);
foreach ($wishlists as $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->header();
$page->bodyStart(); $page->bodyStart();
$page->navigation(); $page->navigation();

View file

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