Skip to content

Commit e30ba5a

Browse files
committed
Implement not found page for users
1 parent 75d53e7 commit e30ba5a

File tree

3 files changed

+84
-48
lines changed

3 files changed

+84
-48
lines changed

controllers/User/View.php

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use \BNETDocs\Libraries\Credits;
88
use \BNETDocs\Libraries\Document;
99
use \BNETDocs\Libraries\Exceptions\UnspecifiedViewException;
10+
use \BNETDocs\Libraries\Exceptions\UserNotFoundException;
1011
use \BNETDocs\Libraries\Exceptions\UserProfileNotFoundException;
1112
use \BNETDocs\Libraries\Router;
1213
use \BNETDocs\Libraries\User as UserLib;
@@ -39,7 +40,7 @@ public function run(Router &$router) {
3940
$this->getUserInfo($model);
4041
ob_start();
4142
$view->render($model);
42-
$router->setResponseCode(200);
43+
$router->setResponseCode(($model->user ? 200 : 404));
4344
$router->setResponseTTL(0);
4445
$router->setResponseHeader("Content-Type", $view->getMimeType());
4546
$router->setResponseContent(ob_get_contents());
@@ -49,6 +50,46 @@ public function run(Router &$router) {
4950
protected function getUserInfo(UserViewModel &$model) {
5051
$model->user_id = $this->user_id;
5152

53+
// Try to get the user
54+
try {
55+
$model->user = new UserLib($this->user_id);
56+
} catch (UserNotFoundException $e) {
57+
$model->user = null;
58+
return;
59+
}
60+
61+
// Try to get their user profile
62+
try {
63+
$model->user_profile = new UserProfile($this->user_id);
64+
$model->biography = $model->user_profile->getBiography();
65+
$model->github = $model->user_profile->getGitHubUsername();
66+
$model->facebook = $model->user_profile->getFacebookUsername();
67+
$model->twitter = $model->user_profile->getTwitterUsername();
68+
$model->instagram = $model->user_profile->getInstagramUsername();
69+
$model->skype = $model->user_profile->getSkypeUsername();
70+
$model->website = $model->user_profile->getWebsite();
71+
} catch (UserProfileNotFoundException $e) {
72+
// Not a problem
73+
}
74+
75+
// Should we display profile data at all?
76+
$model->profiledata = (
77+
$model->github || $model->facebook ||
78+
$model->twitter || $model->instagram ||
79+
$model->skype || $model->website
80+
);
81+
82+
// How long have they been a member?
83+
$model->user_est = Common::intervalToString(
84+
$model->user->getCreatedDateTime()->diff(
85+
new DateTime("now", new DateTimeZone("UTC"))
86+
)
87+
);
88+
$user_est_comma = strpos($model->user_est, ",");
89+
if ($user_est_comma !== false)
90+
$model->user_est = substr($model->user_est, 0, $user_est_comma);
91+
92+
// Summary of contributions
5293
$model->sum_documents = Credits::getTotalDocumentsByUserId(
5394
$this->user_id
5495
);
@@ -62,19 +103,22 @@ protected function getUserInfo(UserViewModel &$model) {
62103
$this->user_id
63104
);
64105

106+
// Total number of contributions
65107
$model->contributions = 0;
66108
$model->contributions += $model->sum_documents;
67109
$model->contributions += $model->sum_news_posts;
68110
$model->contributions += $model->sum_packets;
69111
$model->contributions += $model->sum_servers;
70112

113+
// References to the contributions
71114
$model->documents = ($model->sum_documents ?
72115
Document::getDocumentsByUserId($this->user_id) : null
73116
);
74117
$model->news_posts = ($model->sum_news_posts ? true : null);
75118
$model->packets = ($model->sum_packets ? true : null);
76119
$model->servers = ($model->sum_servers ? true : null);
77120

121+
// Process documents
78122
if ($model->documents) {
79123
// Alphabetically sort the documents
80124
usort($model->documents, function($a, $b){
@@ -94,43 +138,6 @@ protected function getUserInfo(UserViewModel &$model) {
94138
--$i;
95139
}
96140
}
97-
98-
$model->user = new UserLib($this->user_id);
99-
100-
$model->user_est = Common::intervalToString(
101-
$model->user->getCreatedDateTime()->diff(
102-
new DateTime("now", new DateTimeZone("UTC"))
103-
)
104-
);
105-
$user_est_comma = strpos($model->user_est, ",");
106-
if ($user_est_comma !== false)
107-
$model->user_est = substr($model->user_est, 0, $user_est_comma);
108-
109-
try {
110-
$model->user_profile = new UserProfile($this->user_id);
111-
$model->biography = $model->user_profile->getBiography();
112-
$model->github = $model->user_profile->getGitHubUsername();
113-
$model->facebook = $model->user_profile->getFacebookUsername();
114-
$model->twitter = $model->user_profile->getTwitterUsername();
115-
$model->instagram = $model->user_profile->getInstagramUsername();
116-
$model->skype = $model->user_profile->getSkypeUsername();
117-
$model->website = $model->user_profile->getWebsite();
118-
} catch (UserProfileNotFoundException $e) {
119-
$model->user_profile = null;
120-
$model->biography = null;
121-
$model->github = null;
122-
$model->facebook = null;
123-
$model->twitter = null;
124-
$model->instagram = null;
125-
$model->skype = null;
126-
$model->website = null;
127-
}
128-
129-
$model->profiledata = (
130-
$model->github || $model->facebook ||
131-
$model->twitter || $model->instagram ||
132-
$model->skype || $model->website
133-
);
134141
}
135142

136143
}

models/User/View.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,53 @@
66

77
class View extends Model {
88

9+
public $biography;
10+
public $contributions;
911
public $documents;
12+
public $facebook;
13+
public $github;
14+
public $instagram;
1015
public $news_posts;
1116
public $packets;
17+
public $profiledata;
1218
public $servers;
19+
public $skype;
1320
public $sum_documents;
1421
public $sum_news_posts;
1522
public $sum_packets;
1623
public $sum_servers;
24+
public $twitter;
1725
public $user;
1826
public $user_est;
1927
public $user_id;
2028
public $user_profile;
2129
public $user_session;
30+
public $website;
2231

2332
public function __construct() {
2433
parent::__construct();
34+
$this->biography = null;
35+
$this->contributions = null;
2536
$this->documents = null;
37+
$this->facebook = null;
38+
$this->github = null;
39+
$this->instagram = null;
2640
$this->news_posts = null;
2741
$this->packets = null;
42+
$this->profiledata = null;
2843
$this->servers = null;
44+
$this->skype = null;
2945
$this->sum_documents = null;
3046
$this->sum_news_posts = null;
3147
$this->sum_packets = null;
3248
$this->sum_servers = null;
49+
$this->twitter = null;
3350
$this->user = null;
3451
$this->user_est = null;
3552
$this->user_id = null;
3653
$this->user_profile = null;
3754
$this->user_session = null;
55+
$this->website = null;
3856
}
3957

4058
}

templates/User/View.phtml

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,31 @@ use \BNETDocs\Libraries\Document;
66
use \BNETDocs\Libraries\Gravatar;
77
use \BNETDocs\Libraries\Pair;
88

9-
$title = $this->getContext()->user->getName();
10-
$description = $this->getContext()->user->getName() . "'s user profile on BNETDocs";
11-
$this->opengraph->attach(new Pair("url", "/user/" . $this->getContext()->user_id));
9+
$object_id = $this->getContext()->user_id;
10+
$object = $this->getContext()->user;
11+
12+
$title = ($object ? $object->getName() : "User Not Found");
13+
$description = ($object ? $object->getName() . "'s user profile on BNETDocs" : "The requested user does not exist or could not be found.");
14+
$this->opengraph->attach(new Pair("url", "/user/" . urlencode($object_id)));
1215
$this->opengraph->attach(new Pair("type", "profile"));
13-
$this->opengraph->attach(new Pair("profile:username", $this->getContext()->user->getName()));
1416

15-
$gravatar = new Gravatar($this->getContext()->user->getEmail());
16-
$this->opengraph->attach(new Pair("image", "https:" . $gravatar->getUrl(null, "identicon")));
17+
if ($object) {
18+
$this->opengraph->attach(new Pair("profile:username", $object->getName()));
19+
20+
$gravatar = new Gravatar($object->getEmail());
21+
$this->opengraph->attach(new Pair("image", "https:" . $gravatar->getUrl(null, "identicon")));
1722

18-
$safe_name = htmlspecialchars($this->getContext()->user->getName(), ENT_HTML5, "UTF-8");
23+
$safe_name = htmlspecialchars($object->getName(), ENT_HTML5, "UTF-8");
1924

20-
if ($this->getContext()->biography) $description = $biography;
21-
$safe_biography = htmlspecialchars($this->getContext()->biography, ENT_HTML5, "UTF-8");
25+
if ($this->getContext()->biography) $description = $biography;
26+
$safe_biography = htmlspecialchars($this->getContext()->biography, ENT_HTML5, "UTF-8");
27+
}
2228

2329
$this->additional_css[] = "/a/userprofile.css";
2430
require("./header.inc.phtml");
2531
?>
2632
<article>
33+
<?php if ($object) { ?>
2734
<header>User Profile</header>
2835
<section class="accountinfo">
2936
<img class="avatar" src="https:<?php echo $gravatar->getUrl(75, "identicon"); ?>" />
@@ -59,7 +66,7 @@ require("./header.inc.phtml");
5966
<header>User Statistics</header>
6067
<section>
6168
<ul>
62-
<li><strong><?php echo $safe_name; ?></strong> has been a member for <?php echo $this->getContext()->user_est; ?> (since <time datetime="<?php echo $this->getContext()->user->getCreatedDateTime()->format("c"); ?>"><?php echo $this->getContext()->user->getCreatedDateTime()->format("l, F j, Y"); ?></time>).</li>
69+
<li><strong><?php echo $safe_name; ?></strong> has been a member for <?php echo $object_est; ?> (since <time datetime="<?php echo $object->getCreatedDateTime()->format("c"); ?>"><?php echo $object->getCreatedDateTime()->format("l, F j, Y"); ?></time>).</li>
6370
<?php if ($this->getContext()->contributions == 0) { ?>
6471
<li><strong><?php echo $safe_name; ?></strong> has not made any contributions to BNETDocs.</li>
6572
<?php } else { ?>
@@ -82,6 +89,10 @@ require("./header.inc.phtml");
8289
</ul>
8390
<?php } ?>
8491
</section>
92+
<?php } else { ?>
93+
<header class="red"><?php echo htmlspecialchars($title, ENT_HTML5, "UTF-8"); ?></header>
94+
<section class="red"><?php echo htmlspecialchars($description, ENT_HTML5, "UTF-8"); ?></section>
95+
<?php } ?>
8596
</article>
8697
<?php if (isset($this->getContext()->documents)) { ?>
8798
<article>

0 commit comments

Comments
 (0)