Skip to content

Commit 3780dfe

Browse files
committed
Implement not found page for news posts
1 parent b0b5251 commit 3780dfe

File tree

2 files changed

+41
-21
lines changed

2 files changed

+41
-21
lines changed

controllers/News/View.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use \BNETDocs\Libraries\Common;
66
use \BNETDocs\Libraries\Controller;
7+
use \BNETDocs\Libraries\Exceptions\NewsPostNotFoundException;
78
use \BNETDocs\Libraries\Exceptions\UnspecifiedViewException;
89
use \BNETDocs\Libraries\NewsPost;
910
use \BNETDocs\Libraries\Router;
@@ -37,7 +38,7 @@ public function run(Router &$router) {
3738
$this->getNewsPost($model);
3839
ob_start();
3940
$view->render($model);
40-
$router->setResponseCode(200);
41+
$router->setResponseCode(($model->news_post ? 200 : 404));
4142
$router->setResponseTTL(0);
4243
$router->setResponseHeader("Content-Type", $view->getMimeType());
4344
$router->setResponseContent(ob_get_contents());
@@ -46,7 +47,11 @@ public function run(Router &$router) {
4647

4748
protected function getNewsPost(NewsViewModel &$model) {
4849
$model->news_post_id = $this->news_post_id;
49-
$model->news_post = new NewsPost($this->news_post_id);
50+
try {
51+
$model->news_post = new NewsPost($this->news_post_id);
52+
} catch (NewsPostNotFoundException $e) {
53+
$model->news_post = null;
54+
}
5055
}
5156

5257
}

templates/News/View.phtml

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,50 @@ use \BNETDocs\Libraries\Common;
55
use \BNETDocs\Libraries\Gravatar;
66
use \BNETDocs\Libraries\Pair;
77

8-
$title = $this->context->news_post->getTitle();
9-
$description = Common::stripUpTo(trim(filter_var($this->context->news_post->getContent(true), FILTER_SANITIZE_STRING)), "\n", 300);
8+
$object_id = $this->getContext()->news_post_id;
9+
$object = $this->getContext()->news_post;
1010

11-
$this->opengraph->attach(new Pair("url", "/news/" . $this->context->news_post_id));
11+
$title = ($object ? $object->getTitle() : "News Post Not Found");
12+
$description = Common::stripUpTo(trim(filter_var(
13+
($object ? $object->getContent(true) : "The requested news post does not exist or could not be found."),
14+
FILTER_SANITIZE_STRING
15+
)), "\n", 300);
16+
17+
$this->opengraph->attach(new Pair("url", "/news/" . urlencode($object_id)));
1218
$this->opengraph->attach(new Pair("type", "article"));
13-
$this->opengraph->attach(new Pair("image", "/a/news_categories/" . $this->context->news_post->getCategory()->getFilename()));
14-
$this->opengraph->attach(new Pair("article:published_time", $this->context->news_post->getCreatedDateTime()->format("c")));
15-
if (!is_null($this->context->news_post->getEditedDateTime())) {
16-
$this->opengraph->attach(new Pair("article:modified_time", $this->context->news_post->getEditedDateTime()->format("c")));
19+
if ($object) {
20+
$this->opengraph->attach(new Pair("image", "/a/news_categories/" . $object->getCategory()->getFilename()));
21+
$this->opengraph->attach(new Pair("article:published_time", $object->getCreatedDateTime()->format("c")));
22+
if (!is_null($object->getEditedDateTime())) {
23+
$this->opengraph->attach(new Pair("article:modified_time", $object->getEditedDateTime()->format("c")));
24+
}
25+
$this->opengraph->attach(new Pair("article:author:username", $object->getUser()->getName()));
26+
$this->opengraph->attach(new Pair("article:section", $object->getCategory()->getLabel()));
1727
}
18-
$this->opengraph->attach(new Pair("article:author:username", $this->context->news_post->getUser()->getName()));
19-
$this->opengraph->attach(new Pair("article:section", $this->context->news_post->getCategory()->getLabel()));
2028

21-
$url = Common::relativeUrlToAbsolute("/news/" . urlencode($this->context->news_post->getId()));
22-
$url .= "/" . Common::sanitizeForUrl($this->context->news_post->getTitle(), true);
29+
$url = Common::relativeUrlToAbsolute("/news/" . urlencode($object_id));
2330

24-
$user_name = $this->context->news_post->getUser()->getName();
25-
$user_id = $this->context->news_post->getUserId();
26-
$user_url = Common::relativeUrlToAbsolute("/user/" . $user_id . "/" . Common::sanitizeForUrl($user_name, true));
27-
$user_avatar = "https:" . (new Gravatar($this->context->news_post->getUser()->getEmail()))->getUrl(22, "identicon");
31+
if ($object) {
32+
$url .= "/" . Common::sanitizeForUrl($object->getTitle(), true);
33+
$user_name = $object->getUser()->getName();
34+
$user_id = $object->getUserId();
35+
$user_url = Common::relativeUrlToAbsolute("/user/" . $user_id . "/" . Common::sanitizeForUrl($user_name, true));
36+
$user_avatar = "https:" . (new Gravatar($object->getUser()->getEmail()))->getUrl(22, "identicon");
37+
}
2838

2939
$this->additional_css[] = "/a/news.css";
3040
require("./header.inc.phtml");
3141
?>
3242
<article>
43+
<?php if ($object) { ?>
3344
<a href="https://plus.google.com/share?url=<?php echo urlencode($url); ?>" rel="external" data-popup="1"><img class="social-btn float-right" src="<?php echo Common::relativeUrlToAbsolute("/a/social-gplus-24px.png"); ?>"/></a>
34-
<a href="https://twitter.com/share?text=<?php echo urlencode($this->context->news_post->getTitle()); ?>&amp;url=<?php echo urlencode($url); ?>" rel="external" data-popup="1"><img class="social-btn float-right" src="<?php echo Common::relativeUrlToAbsolute("/a/social-twitter-24px.png"); ?>"/></a>
45+
<a href="https://twitter.com/share?text=<?php echo urlencode($object->getTitle()); ?>&amp;url=<?php echo urlencode($url); ?>" rel="external" data-popup="1"><img class="social-btn float-right" src="<?php echo Common::relativeUrlToAbsolute("/a/social-twitter-24px.png"); ?>"/></a>
3546
<a href="https://facebook.com/sharer/sharer.php?u=<?php echo urlencode($url); ?>" rel="external" data-popup="1"><img class="social-btn float-right" src="<?php echo Common::relativeUrlToAbsolute("/a/social-facebook-24px.png"); ?>"/></a>
36-
<header><a href="<?php echo $url; ?>"><?php echo $this->context->news_post->getTitle(); ?></a></header>
37-
<section class="news"><img class="category" alt="<?php echo $this->context->news_post->getCategory()->getLabel(); ?>" title="<?php echo $this->context->news_post->getCategory()->getLabel(); ?>" src="<?php echo Common::relativeUrlToAbsolute("/a/news_categories/" . $this->context->news_post->getCategory()->getFilename()); ?>"/><?php echo $this->context->news_post->getContent(true); ?></section>
47+
<header><a href="<?php echo $url; ?>"><?php echo $object->getTitle(); ?></a></header>
48+
<section class="news"><img class="category" alt="<?php echo $object->getCategory()->getLabel(); ?>" title="<?php echo $object->getCategory()->getLabel(); ?>" src="<?php echo Common::relativeUrlToAbsolute("/a/news_categories/" . $object->getCategory()->getFilename()); ?>"/><?php echo $object->getContent(true); ?></section>
3849
<footer>
3950
<span class="float-left"><a href="<?php echo $user_url; ?>"><img class="avatar" src="<?php echo $user_avatar; ?>"/> <?php echo htmlspecialchars($user_name, ENT_HTML5, "UTF-8"); ?></a></span>
40-
<span class="float-right"><?php echo $this->context->news_post->getPublishedDateTime()->format("l, F j, Y"); ?></span>
51+
<span class="float-right"><?php echo $object->getPublishedDateTime()->format("l, F j, Y"); ?></span>
4152
</footer>
4253
</article>
4354
<article>
@@ -46,5 +57,9 @@ require("./header.inc.phtml");
4657
<?php require("./NYI.inc.phtml"); ?>
4758
<p class="center">If you'd like to leave a suggestion or concern, you can do so <a href="https://github.com/BNETDocs/bnetdocs-web/issues/new?labels[]=bnetdocs-phoenix&labels[]=question&body=<?php echo rawurlencode("Hi,\n\n<fill in your question here>\n\nThanks!\n\nReference: " . $url); ?>" rel="external">over on GitHub</a>. Sorry for the trouble!</p>
4859
</section>
60+
<?php } else { ?>
61+
<header class="red"><?php echo htmlspecialchars($title, ENT_HTML5, "UTF-8"); ?></header>
62+
<section class="red"><?php echo htmlspecialchars($description, ENT_HTML5, "UTF-8"); ?></section>
63+
<?php } ?>
4964
</article>
5065
<?php require("./footer.inc.phtml"); ?>

0 commit comments

Comments
 (0)