Skip to content

Commit d4d8720

Browse files
authored
Merge pull request #87 from Davnit/patch-1
Better/fixed missing user handling
2 parents 6d01887 + aac4649 commit d4d8720

File tree

10 files changed

+83
-134
lines changed

10 files changed

+83
-134
lines changed

src/libraries/Comment.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,7 @@ public function getParentType() {
219219
}
220220

221221
public function getUser() {
222-
if (is_null($this->user_id)) return null;
223-
return new User($this->user_id);
222+
return User::findUserById($this->user_id);
224223
}
225224

226225
public function getUserId() {

src/libraries/Document.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,7 @@ public function getURI() {
265265
}
266266

267267
public function getUser() {
268-
if (is_null($this->user_id)) return null;
269-
return new User($this->user_id);
268+
return User::findUserById($this->user_id);
270269
}
271270

272271
public function getUserId() {

src/libraries/NewsPost.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,7 @@ public function getURI() {
302302
}
303303

304304
public function getUser() {
305-
if (is_null($this->user_id)) return null;
306-
return new User($this->user_id);
305+
return User::findUserById($this->user_id);
307306
}
308307

309308
public function getUserId() {

src/libraries/Packet.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -523,11 +523,7 @@ public function getUsedBy() {
523523
}
524524

525525
public function getUser() {
526-
if ( is_null( $this->user_id )) {
527-
return null;
528-
}
529-
530-
return new User( $this->user_id );
526+
return User::findUserById($this->user_id);
531527
}
532528

533529
public function getUserId() {

src/libraries/Server.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,7 @@ public function getUpdatedDateTime() {
177177
}
178178

179179
public function getUser() {
180-
if (is_null($this->user_id)) return null;
181-
return new User($this->user_id);
180+
return User::findUserById($this->user_id);
182181
}
183182

184183
public function getUserId() {

src/libraries/User.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,16 @@ public function getAcl($acl) {
341341
return ($this->options_bitmask & $acl);
342342
}
343343

344+
public static function findUserById($user_id) {
345+
if (is_null($user_id)) return null;
346+
347+
try {
348+
return new User($user_id);
349+
} catch (UserNotFoundException $e) {
350+
return null;
351+
}
352+
}
353+
344354
public static function &getAllUsers(
345355
$order = null, $limit = null, $index = null
346356
) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace BNETDocs\Templates\Comment;
4+
5+
use \BNETDocs\Libraries\Comment;
6+
use \BNETDocs\Libraries\User;
7+
8+
use \CarlBennett\MVC\Libraries\Common;
9+
10+
11+
?>
12+
<header><a name="comments">Comments</a></header>
13+
<section>
14+
<?php if (!$comments) { ?>
15+
<p class="center"><em>no one has commented yet.</em></p>
16+
<?php } else {
17+
$c_edit_visible_master = ($logged_in && ($logged_in->getOptionsBitmask() & User::OPTION_ACL_COMMENT_MODIFY));
18+
$c_delete_visible_master = ($logged_in && ($logged_in->getOptionsBitmask() & User::OPTION_ACL_COMMENT_DELETE));
19+
?>
20+
<table class="comments"><tbody>
21+
<?php foreach ($comments as $c) {
22+
$c_id = $c->getId();
23+
$c_user = $c->getUser();
24+
$c_user_id = $c->getUserId();
25+
26+
$c_edit_visible = ($c_user_id == $logged_in_id || $c_edit_visible_master);
27+
$c_delete_visible = ($c_user_id == $logged_in_id || $c_delete_visible_master);
28+
?>
29+
<tr><td><?php if ($c_user) { ?><a href="<?php echo $c_user->getURI(); ?>"><img class="avatar" src="<?php echo $c_user->getAvatarURI(22); ?>"/> <?php echo filter_var($c_user->getName(), FILTER_SANITIZE_STRING); ?></a><?php } else { ?>Anonymous<?php } ?><br/><time class="comment_timestamp" datetime="<?php echo $c->getCreatedDateTime()->format("c"); ?>"><?php echo $c->getCreatedDateTime()->format("D M j, Y g:ia T"); ?></time><?php if ($c_delete_visible) { ?><a class="button comment_button" href="<?php echo Common::relativeUrlToAbsolute("/comment/delete?id=" . urlencode($c_id)); ?>">Delete</a><?php } if ($c_edit_visible) { ?><a class="button comment_button" href="<?php echo Common::relativeUrlToAbsolute("/comment/edit?id=" . urlencode($c_id)); ?>">Edit</a><?php } ?></td><td><?php echo $c->getContent(true); ?></td></tr>
30+
<?php } ?>
31+
</tbody></table>
32+
<?php } ?>
33+
</section>
34+
<?php if ($logged_in) { ?>
35+
<section>
36+
<hr/>
37+
<form method="POST" action="<?php echo Common::relativeUrlToAbsolute("/comment/create"); ?>">
38+
<input type="hidden" name="parent_type" value="<?php echo $comment_parent_type; ?>"/>
39+
<input type="hidden" name="parent_id" value="<?php echo $object_id; ?>"/>
40+
<p class="center"><label for="comment-content">Comment on this post:</label></p>
41+
<p class="center"><textarea id="comment-content" name="content" cols="80" rows="5"></textarea></p>
42+
<p class="center"><input type="submit" value="Comment"/></p>
43+
</form>
44+
</section>
45+
<?php } ?>

src/templates/Document/View.phtml

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ if ($object) {
3232
$object->getContent(true), FILTER_SANITIZE_STRING
3333
)), "\n", 300);
3434

35-
$user_name = $object->getUser()->getName();
36-
$user_id = $object->getUserId();
37-
$user_url = $object->getUser()->getURI();
38-
$user_avatar = $object->getUser()->getAvatarURI(22);
35+
$user = $object->getUser();
3936

4037
}
4138

@@ -72,52 +69,21 @@ require("./header.inc.phtml");
7269
<?php } else { ?>
7370
<span class="float-right"><time datetime="<?php echo $object->getCreatedDateTime()->format('c'); ?>"><?php echo $object->getCreatedDateTime()->format("l, F j, Y"); ?></time></span>
7471
<?php } ?>
75-
<?php if ($user_id !== null) { ?>
76-
<span><a href="<?php echo $user_url; ?>"><img class="avatar" src="<?php echo $user_avatar; ?>"/> <?php echo filter_var($user_name, FILTER_SANITIZE_STRING); ?></a></span>
72+
<?php if ($user !== null) { ?>
73+
<span><a href="<?php echo $user->getURI(); ?>"><img class="avatar" src="<?php echo $user->getAvatarURI(22); ?>"/> <?php echo filter_var($user->getName(), FILTER_SANITIZE_STRING); ?></a></span>
7774
<?php } ?>
7875
</footer>
7976
</article>
8077
<article>
81-
<header><a name="comments">Comments</a></header>
82-
<section>
83-
<?php if (!$comments) { ?>
84-
<p class="center"><em>no one has commented yet.</em></p>
85-
<?php } else {
86-
$c_edit_visible_master = ($logged_in && ($logged_in->getOptionsBitmask() & User::OPTION_ACL_COMMENT_MODIFY));
87-
$c_delete_visible_master = ($logged_in && ($logged_in->getOptionsBitmask() & User::OPTION_ACL_COMMENT_DELETE));
88-
?>
89-
<table class="comments"><tbody>
90-
<?php foreach ($comments as $c) {
91-
$c_id = $c->getId();
92-
$c_user = $c->getUser();
93-
$c_user_name = $c_user->getName();
94-
$c_user_id = $c->getUserId();
95-
$c_user_url = $c_user->getURI();
96-
$c_user_avatar = $c_user->getAvatarURI(22);
97-
98-
$c_edit_visible = ($c_user_id == $logged_in_id || $c_edit_visible_master);
99-
$c_delete_visible = ($c_user_id == $logged_in_id || $c_delete_visible_master);
100-
?>
101-
<tr><td><a href="<?php echo $c_user_url; ?>"><img class="avatar" src="<?php echo $c_user_avatar; ?>"/> <?php echo filter_var($c_user_name, FILTER_SANITIZE_STRING); ?></a><br/><time class="comment_timestamp" datetime="<?php echo $c->getCreatedDateTime()->format("c"); ?>"><?php echo $c->getCreatedDateTime()->format("D M j, Y g:ia T"); ?></time><?php if ($c_delete_visible) { ?><a class="button comment_button" href="<?php echo Common::relativeUrlToAbsolute("/comment/delete?id=" . urlencode($c_id)); ?>">Delete</a><?php } if ($c_edit_visible) { ?><a class="button comment_button" href="<?php echo Common::relativeUrlToAbsolute("/comment/edit?id=" . urlencode($c_id)); ?>">Edit</a><?php } ?></td><td><?php echo $c->getContent(true); ?></td></tr>
102-
<?php } ?>
103-
</tbody></table>
104-
<?php } ?>
105-
</section>
106-
<?php if ($logged_in) { ?>
107-
<section>
108-
<hr/>
109-
<form method="POST" action="<?php echo Common::relativeUrlToAbsolute("/comment/create"); ?>">
110-
<input type="hidden" name="parent_type" value="<?php echo Comment::PARENT_TYPE_DOCUMENT; ?>"/>
111-
<input type="hidden" name="parent_id" value="<?php echo $object_id; ?>"/>
112-
<p class="center"><label for="comment-content">Comment on this post:</label></p>
113-
<p class="center"><textarea id="comment-content" name="content" cols="80" rows="5"></textarea></p>
114-
<p class="center"><input type="submit" value="Comment"/></p>
115-
</form>
116-
</section>
117-
<?php } ?>
118-
<?php } else { ?>
78+
<?php
79+
80+
$comment_parent_type = Comment::PARENT_TYPE_DOCUMENT;
81+
require("./Comment/Section.inc.phtml");
82+
83+
} else { ?>
11984
<header class="red"><?php echo filter_var($title, FILTER_SANITIZE_STRING); ?></header>
12085
<section class="red"><?php echo filter_var($description, FILTER_SANITIZE_STRING); ?></section>
12186
<?php } ?>
12287
</article>
123-
<?php require("./footer.inc.phtml"); ?>
88+
89+
require("./footer.inc.phtml"); ?>

src/templates/News/View.phtml

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -85,44 +85,12 @@ require("./header.inc.phtml");
8585
</footer>
8686
</article>
8787
<article>
88-
<header><a name="comments">Comments</a></header>
89-
<section>
90-
<?php if (!$comments) { ?>
91-
<p class="center"><em>no one has commented yet.</em></p>
92-
<?php } else {
93-
$c_edit_visible_master = ($logged_in && ($logged_in->getOptionsBitmask() & User::OPTION_ACL_COMMENT_MODIFY));
94-
$c_delete_visible_master = ($logged_in && ($logged_in->getOptionsBitmask() & User::OPTION_ACL_COMMENT_DELETE));
95-
?>
96-
<table class="comments"><tbody>
97-
<?php foreach ($comments as $c) {
98-
$c_id = $c->getId();
99-
$c_user = $c->getUser();
100-
$c_user_name = $c_user->getName();
101-
$c_user_id = $c->getUserId();
102-
$c_user_url = $c_user->getURI();
103-
$c_user_avatar = $c_user->getAvatarURI(22);
104-
105-
$c_edit_visible = ($c_user_id == $logged_in_id || $c_edit_visible_master);
106-
$c_delete_visible = ($c_user_id == $logged_in_id || $c_delete_visible_master);
107-
?>
108-
<tr><td><a href="<?php echo $c_user_url; ?>"><img class="avatar" src="<?php echo $c_user_avatar; ?>"/> <?php echo filter_var($c_user_name, FILTER_SANITIZE_STRING); ?></a><br/><time class="comment_timestamp" datetime="<?php echo $c->getCreatedDateTime()->format("c"); ?>"><?php echo $c->getCreatedDateTime()->format("D M j, Y g:ia T"); ?></time><?php if ($c_delete_visible) { ?><a class="button comment_button" href="<?php echo Common::relativeUrlToAbsolute("/comment/delete?id=" . urlencode($c_id)); ?>">Delete</a><?php } if ($c_edit_visible) { ?><a class="button comment_button" href="<?php echo Common::relativeUrlToAbsolute("/comment/edit?id=" . urlencode($c_id)); ?>">Edit</a><?php } ?></td><td><?php echo $c->getContent(true); ?></td></tr>
109-
<?php } ?>
110-
</tbody></table>
111-
<?php } ?>
112-
</section>
113-
<?php if ($logged_in) { ?>
114-
<section>
115-
<hr/>
116-
<form method="POST" action="<?php echo Common::relativeUrlToAbsolute("/comment/create"); ?>">
117-
<input type="hidden" name="parent_type" value="<?php echo Comment::PARENT_TYPE_NEWS_POST; ?>"/>
118-
<input type="hidden" name="parent_id" value="<?php echo $object_id; ?>"/>
119-
<p class="center"><label for="comment-content">Comment on this post:</label></p>
120-
<p class="center"><textarea id="comment-content" name="content" cols="80" rows="5"></textarea></p>
121-
<p class="center"><input type="submit" value="Comment"/></p>
122-
</form>
123-
</section>
124-
<?php } ?>
125-
<?php } else { ?>
88+
<?php
89+
90+
$comment_parent_type = Comment::PARENT_TYPE_NEWS_POST;
91+
require("./Comment/Section.inc.phtml");
92+
93+
} else { ?>
12694
<header class="red"><?php echo filter_var($title, FILTER_SANITIZE_STRING); ?></header>
12795
<section class="red"><?php echo filter_var($description, FILTER_SANITIZE_STRING); ?></section>
12896
<?php } ?>

src/templates/Packet/View.phtml

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -119,44 +119,12 @@ require("./header.inc.phtml");
119119
</footer>
120120
</article>
121121
<article>
122-
<header><a name="comments">Comments</a></header>
123-
<section>
124-
<?php if (!$comments) { ?>
125-
<p class="center"><em>no one has commented yet.</em></p>
126-
<?php } else {
127-
$c_edit_visible_master = ($logged_in && ($logged_in->getOptionsBitmask() & User::OPTION_ACL_COMMENT_MODIFY));
128-
$c_delete_visible_master = ($logged_in && ($logged_in->getOptionsBitmask() & User::OPTION_ACL_COMMENT_DELETE));
129-
?>
130-
<table class="comments"><tbody>
131-
<?php foreach ($comments as $c) {
132-
$c_id = $c->getId();
133-
$c_user = $c->getUser();
134-
$c_user_name = $c_user->getName();
135-
$c_user_id = $c->getUserId();
136-
$c_user_url = $c_user->getURI();
137-
$c_user_avatar = $c_user->getAvatarURI(22);
138-
139-
$c_edit_visible = ($c_user_id == $logged_in_id || $c_edit_visible_master);
140-
$c_delete_visible = ($c_user_id == $logged_in_id || $c_delete_visible_master);
141-
?>
142-
<tr><td><a href="<?php echo $c_user_url; ?>"><img class="avatar" src="<?php echo $c_user_avatar; ?>"/> <?php echo filter_var($c_user_name, FILTER_SANITIZE_STRING); ?></a><br/><time class="comment_timestamp" datetime="<?php echo $c->getCreatedDateTime()->format("c"); ?>"><?php echo $c->getCreatedDateTime()->format("D M j, Y g:ia T"); ?></time><?php if ($c_delete_visible) { ?><a class="button comment_button" href="<?php echo Common::relativeUrlToAbsolute("/comment/delete?id=" . urlencode($c_id)); ?>">Delete</a><?php } if ($c_edit_visible) { ?><a class="button comment_button" href="<?php echo Common::relativeUrlToAbsolute("/comment/edit?id=" . urlencode($c_id)); ?>">Edit</a><?php } ?></td><td><?php echo $c->getContent(true); ?></td></tr>
143-
<?php } ?>
144-
</tbody></table>
145-
<?php } ?>
146-
</section>
147-
<?php if ($logged_in) { ?>
148-
<section>
149-
<hr/>
150-
<form method="POST" action="<?php echo Common::relativeUrlToAbsolute("/comment/create"); ?>">
151-
<input type="hidden" name="parent_type" value="<?php echo Comment::PARENT_TYPE_PACKET; ?>"/>
152-
<input type="hidden" name="parent_id" value="<?php echo $object_id; ?>"/>
153-
<p class="center"><label for="comment-content">Comment on this post:</label></p>
154-
<p class="center"><textarea id="comment-content" name="content" cols="80" rows="5"></textarea></p>
155-
<p class="center"><input type="submit" value="Comment"/></p>
156-
</form>
157-
</section>
158-
<?php } ?>
159-
<?php } else { ?>
122+
<?php
123+
124+
$comment_parent_type = Comment::PARENT_TYPE_PACKET;
125+
require("./Comment/Section.inc.phtml");
126+
127+
} else { ?>
160128
<header class="red"><?php echo filter_var($title, FILTER_SANITIZE_STRING); ?></header>
161129
<section class="red"><?php echo filter_var($description, FILTER_SANITIZE_STRING); ?></section>
162130
<?php } ?>

0 commit comments

Comments
 (0)