Skip to content

Commit 75d53e7

Browse files
committed
Implement not found page for documents
1 parent 3780dfe commit 75d53e7

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

controllers/Document/View.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use \BNETDocs\Libraries\Common;
66
use \BNETDocs\Libraries\Controller;
77
use \BNETDocs\Libraries\Document;
8+
use \BNETDocs\Libraries\Exceptions\DocumentNotFoundException;
89
use \BNETDocs\Libraries\Exceptions\UnspecifiedViewException;
910
use \BNETDocs\Libraries\Router;
1011
use \BNETDocs\Libraries\UserSession;
@@ -35,11 +36,16 @@ public function run(Router &$router) {
3536
throw new UnspecifiedViewException();
3637
}
3738
$model = new DocumentViewModel();
38-
$model->document = new Document($this->document_id);
39+
$model->document_id = $this->document_id;
40+
try {
41+
$model->document = new Document($this->document_id);
42+
} catch (DocumentNotFoundException $e) {
43+
$model->document = null;
44+
}
3945
$model->user_session = UserSession::load($router);
4046
ob_start();
4147
$view->render($model);
42-
$router->setResponseCode(200);
48+
$router->setResponseCode(($model->document ? 200 : 404));
4349
$router->setResponseTTL(0);
4450
$router->setResponseHeader("Content-Type", $view->getMimeType());
4551
$router->setResponseContent(ob_get_contents());

templates/Document/View.phtml

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,32 @@ namespace BNETDocs\Templates;
44
use \BNETDocs\Libraries\Common;
55
use \BNETDocs\Libraries\Pair;
66

7-
$title = $this->getContext()->document->getTitle();
8-
$description = Common::stripUpTo(trim(filter_var($this->getContext()->document->getContent(true), FILTER_SANITIZE_STRING)), "\n", 300);
9-
$this->opengraph->attach(new Pair("url", "/document/" . $this->getContext()->document->getId()));
7+
$object_id = $this->getContext()->document_id;
8+
$object = $this->getContext()->document;
9+
10+
$title = ($object ? $object->getTitle() : "Document Not Found");
11+
$description = Common::stripUpTo(trim(filter_var(
12+
($object ? $object->getContent(true) : "The requested document does not exist or could not be found."),
13+
FILTER_SANITIZE_STRING
14+
)), "\n", 300);
15+
$this->opengraph->attach(new Pair("url", "/document/" . urlencode($object_id)));
1016
$this->opengraph->attach(new Pair("type", "article"));
1117

12-
$url = Common::relativeUrlToAbsolute("/document/" . urlencode($this->getContext()->document->getId()));
13-
$url .= "/" . Common::sanitizeForUrl($this->getContext()->document->getTitle(), true);
18+
$url = Common::relativeUrlToAbsolute("/document/" . urlencode($object_id));
19+
if ($object)
20+
$url .= "/" . Common::sanitizeForUrl($object->getTitle(), true);
1421

1522
#$this->additional_css[] = "/a/document.css";
1623
require("./header.inc.phtml");
1724
?>
1825
<article>
26+
<?php if ($object) { ?>
1927
<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>
20-
<a href="https://twitter.com/share?text=<?php echo urlencode($this->getContext()->document->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>
28+
<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>
2129
<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>
22-
<header><a href="<?php echo $url; ?>"><?php echo htmlspecialchars($this->getContext()->document->getTitle(), ENT_HTML5, "UTF-8"); ?></a></header>
30+
<header><a href="<?php echo $url; ?>"><?php echo htmlspecialchars($object->getTitle(), ENT_HTML5, "UTF-8"); ?></a></header>
2331
<section>
24-
<?php echo $this->getContext()->document->getContent(true); ?>
32+
<?php echo $object->getContent(true); ?>
2533
</section>
2634
</article>
2735
<article>
@@ -30,5 +38,9 @@ require("./header.inc.phtml");
3038
<?php require("./NYI.inc.phtml"); ?>
3139
<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>
3240
</section>
41+
<?php } else { ?>
42+
<header class="red"><?php echo htmlspecialchars($title, ENT_HTML5, "UTF-8"); ?></header>
43+
<section class="red"><?php echo htmlspecialchars($description, ENT_HTML5, "UTF-8"); ?></section>
44+
<?php } ?>
3345
</article>
3446
<?php require("./footer.inc.phtml"); ?>

0 commit comments

Comments
 (0)