Skip to content

Commit aac4649

Browse files
committed
Unify comment section display
Instead of having code to show the same comment section in multiple files, centralize it and have the other files include the central file. The file is located in templates\Comment\Section.inc.phtml, which seemed to fit the general pattern. Before including you should have $comments, $logged_in, $object_id, and $comment_parent_type defined. $comment_parent_type is the constant value from libraries\Comment "Comment::PARENT_TYPE_DOCUMENT" etc. The new comment section display also handles missing users.
1 parent 67de1b2 commit aac4649

File tree

4 files changed

+65
-112
lines changed

4 files changed

+65
-112
lines changed
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: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -76,43 +76,15 @@ require("./header.inc.phtml");
7676
</footer>
7777
</article>
7878
<article>
79-
<header><a name="comments">Comments</a></header>
80-
<section>
81-
<?php if (!$comments) { ?>
82-
<p class="center"><em>no one has commented yet.</em></p>
83-
<?php } else {
84-
$c_edit_visible_master = ($logged_in && ($logged_in->getOptionsBitmask() & User::OPTION_ACL_COMMENT_MODIFY));
85-
$c_delete_visible_master = ($logged_in && ($logged_in->getOptionsBitmask() & User::OPTION_ACL_COMMENT_DELETE));
86-
?>
87-
<table class="comments"><tbody>
88-
<?php foreach ($comments as $c) {
89-
$c_id = $c->getId();
90-
$c_user = $c->getUser();
91-
$c_user_id = $c->getUserId();
92-
93-
$c_edit_visible = ($c_user_id == $logged_in_id || $c_edit_visible_master);
94-
$c_delete_visible = ($c_user_id == $logged_in_id || $c_delete_visible_master);
95-
?>
96-
<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>
97-
<?php } ?>
98-
</tbody></table>
99-
<?php } ?>
100-
</section>
101-
<?php if ($logged_in) { ?>
102-
<section>
103-
<hr/>
104-
<form method="POST" action="<?php echo Common::relativeUrlToAbsolute("/comment/create"); ?>">
105-
<input type="hidden" name="parent_type" value="<?php echo Comment::PARENT_TYPE_DOCUMENT; ?>"/>
106-
<input type="hidden" name="parent_id" value="<?php echo $object_id; ?>"/>
107-
<p class="center"><label for="comment-content">Comment on this post:</label></p>
108-
<p class="center"><textarea id="comment-content" name="content" cols="80" rows="5"></textarea></p>
109-
<p class="center"><input type="submit" value="Comment"/></p>
110-
</form>
111-
</section>
112-
<?php } ?>
113-
<?php } else { ?>
79+
<?php
80+
81+
$comment_parent_type = Comment::PARENT_TYPE_DOCUMENT;
82+
require("./Comment/Section.inc.phtml");
83+
84+
} else { ?>
11485
<header class="red"><?php echo filter_var($title, FILTER_SANITIZE_STRING); ?></header>
11586
<section class="red"><?php echo filter_var($description, FILTER_SANITIZE_STRING); ?></section>
11687
<?php } ?>
11788
</article>
118-
<?php require("./footer.inc.phtml"); ?>
89+
90+
require("./footer.inc.phtml"); ?>

src/templates/News/View.phtml

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

src/templates/Packet/View.phtml

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

0 commit comments

Comments
 (0)