fix organism controller tests for wording changes
[sgn.git] / cgi-bin / forum / posts.pl
blobb1a8f0ac60639456bb42b3d34e63dbeb5aea7609
1 use strict;
2 use warnings;
4 use CXGN::DB::Connection;
5 use CXGN::Page;
6 use CXGN::People;
7 use CXGN::Login;
8 use CXGN::People::Forum;
9 use CXGN::Page::FormattingHelpers qw(page_title_html blue_section_html);
11 use URI::Escape;
13 =head1 NAME
15 posts.pl - a web script that displays posts for a given topic.
17 =head1 DESCRIPTION
19 Parameters:
20 topic_id - the id of the topic to be displayed. Required. Omitting it will display an error message.
21 post_text - if a post text is being supplied, the post_text is added to the topic.
22 post_parent_id - the id of the post that is the parent of this post. Currently not used in this interface.
24 Note: people have to be logged in to add posts. Users can browse posts without being logged in.
26 =head1 AUTHOR
28 Lukas Mueller (lam87@cornell.edu)
30 =cut
34 my $page = CXGN::Page->new( "SGN Forum Main", "Lukas");
36 my $dbh = CXGN::DB::Connection->new();
37 my $sp_person_id = CXGN::Login->new($dbh)->has_session();
39 my ($topic_id, $post_text, $post_parent_id) = $page -> get_encoded_arguments("topic_id", "post_text", "post_parent_id");
41 my $person;
42 if ($sp_person_id) {
43 $person = CXGN::People::Person->new($dbh, $sp_person_id);
44 $sp_person_id = $person -> get_sp_person_id();
47 # do some sanity checking
49 if (!$topic_id) {
50 $page->header();
51 print "<p>No topic was specified. Please go back and try another topic.\n</p>";
52 $page->footer();
53 exit(0);
56 # get some information about the topic
58 my $topic = CXGN::People::Forum::Topic->new($dbh, $topic_id);
59 if ($topic->get_topic_name() eq "") {
60 $page->header();
61 print "<p>The topic you are refering to does not exist or no longer exists. Please <a href=\"topics.pl\">go back</a> and try again!<br /><br />\n";
62 $page->footer();
63 exit(0);
65 my $topic_start_person_id = $topic -> get_person_id();
66 my $topic_start_person = CXGN::People::Person->new($dbh, $topic_start_person_id);
68 # generate the post list
70 my $s = posts_list($dbh, $sp_person_id, $topic_id, $page);
72 # render the page
74 $page->header();
76 print page_title_html("SOL Forum Topic: ".$topic->get_topic_name()."\n");
77 my $topic_started_by = "<br />This topic was started by <b><a href=\"/solpeople/personal-info.pl?sp_person_id=".
78 ($topic_start_person->get_sp_person_id)."\">".
79 ($topic_start_person->get_first_name()." ".$topic_start_person->get_last_name())."</a></b>.\n";
81 my $topic_name = $topic->get_topic_name();
82 my $topic_description = $topic->get_topic_description();
83 my $forum_toolbar = forum_toolbar($topic_id, $sp_person_id);
84 if (!$topic_description) { $topic_description = "(topic description not available)"; }
87 print qq { <div class="indentedcontent">$forum_toolbar</div>\n };
88 print qq { <center><table width="710" cellpadding="5" class="topicbox"><tr><td><b>$topic_name</b><br /><br />$topic_description<br />$topic_started_by</td></tr></table> };
89 #print blue_section_html("Topic Description", $topic_description." ".$topic_started_by);
90 print $s;
92 print qq { </center>\n };
93 print "<div class=\"indentedcontent\">$forum_toolbar</div>\n";
95 $page->footer();
98 sub posts_list {
99 my ($dbh,$sp_person_id,$topic_id,$page) = @_;
100 my $topic = CXGN::People::Forum::Topic -> new($dbh, $topic_id);
101 my $user = CXGN::People::Person->new($dbh, $sp_person_id);
103 my @posts = $topic -> get_all_posts($topic_id);
105 my $s = "";
106 $s .= qq { <table cellpadding="5" cellspacing="0" width="720" border="0" > };
108 if (!@posts) {
109 $s .= "<tr><td>No user comments. <br /></td></tr>\n";
112 foreach my $p (@posts) {
113 my $post_subject = $p->get_subject();
114 my $post_person_id = $p -> get_person_id();
115 my $post_person = CXGN::People::Person -> new($dbh, $post_person_id);
116 my $post_sp_person_id = $post_person -> get_sp_person_id();
117 my $post_name = $post_person->get_first_name()." ".$post_person->get_last_name();
118 my $user_type = $post_person->get_user_type();
119 if($user_type and $user_type ne 'user')
121 $user_type=" ($user_type)";
123 else
125 $user_type='';
127 my $post_text = $p -> get_post_text();
128 my $post_date = $p -> get_formatted_post_time();
129 my $remove_link = "&nbsp;";
131 my $refering_url = $page->{request}->uri()."?".$page->{request}->args();
132 my $encoded_url = URI::Escape::uri_escape($refering_url);
133 if (($sp_person_id && ($post_person_id == $sp_person_id)) || $user->get_user_type() eq "curator") {
134 $remove_link = "<a href=\"forum_post_delete.pl?post_id=".($p->get_forum_post_id())."&amp;refering_page=$encoded_url\">Delete</a>\n";
137 $s .= qq { <tr><td><div class="topicbox"><table cellspacing="0" cellpadding="5" border="0" width="100%"><tr><td>Posted by <b><a href="/solpeople/personal-info.pl?sp_person_id=$post_sp_person_id">$post_name</a>$user_type</b> on $post_date </td><td class="right">$remove_link&nbsp;</td></tr></table></div> };
138 $s .= qq { <table cellpadding="5" width="100%" class="topicdescbox"><tr><td>$post_text</td></tr></table> };
140 $s .= "</table>";
143 sub old_posts_list {
145 my $topic_id = shift;
146 my $topic = Topic -> new($topic_id);
148 my @posts = $topic -> get_all_posts($topic_id);
150 my $previous_parent_id = -1;
151 my $previous_id = -1;
152 my $indent = 0;
153 my $indent_size = 20;
155 my @parent_ids = ();
157 unless( @posts ) {
158 return "Currently there are no posts under this topic. Please use the Add Posting link to add a post. Please note that you have to be logged in to post. [<a href=\"login.pl\">Login</a>]<br /><br />";
161 my $s = "<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\">";
163 foreach my $p (@posts) {
164 my $subject = $p->get_subject();
165 if (!$subject) { $subject = "[No subject]"; }
166 my $post_text = $p->get_post_text();
168 my $person_id = $p->get_person_id();
169 my $post_time = $p->get_post_time();
170 my $parent_id = $p->get_parent_post_id();
171 my $forum_post_id = $p -> get_forum_post_id();
173 my $person = CXGN::People::Person->new($person_id);
174 my $name = $person->get_first_name()." ".$person->get_last_name();
176 #$indent = $p->get_post_level() * $indent_size;
177 my $has_parent = 0;
178 for ( my $i=0; $i<@parent_ids; $i++) {
179 if ($parent_id == $parent_ids[$i]) {
180 $indent=$i;
181 $parent_ids[$i+1]=$forum_post_id;
182 $has_parent=1;
186 if (!$has_parent) {
187 push @parent_ids, $parent_id;
188 $indent = @parent_ids;
191 $post_time =~ s/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/$2\-$3\-$1 $4\:$5\:$6/;
192 my $indent_pixels = $indent * $indent_size;
193 my $indent_chars = "";
194 for (my $i=0; $i<$indent; $i++) {
195 $indent_chars .="&rarr;";
198 my $respond_link = "Respond";
199 if ($person_id) {
200 $respond_link = "<a href=\"add_post.pl?parent_post_id=$forum_post_id&amp;topic_id=$topic_id\">Respond</a>";
202 #my $post_level = $p -> get_post_level();
203 $s .= "<tr><td colspan=\"4\" class=\"bgcolorselected\">From: <b>$name</b> </td><td colspan=3 class=\"bgcolorselected\" align=\"right\">Posted: $post_time</td></tr>\n";
204 $s .= "<tr><td><table cellpadding=\"5\"><tr><td width=\"$indent_pixels\">$indent_chars</td><td><b>$subject</b></td></tr></table></td><td colspan=\"4\" wrap=\"wrap\">$post_text</td></tr>";
205 $s .="<tr><td colspan=\"7\" align=\"right\">$respond_link</td></tr>";
206 $s .= "<tr><td><img src=\"/img/dot_clear.png\" height=\"2\" /></td><td colspan=\"5\"><img src=\"/img/dot_clear.png\" height=\"2\" /></td></tr>";
208 $s.="</table>\n<br /><br />";
210 return $s;
213 sub forum_toolbar {
214 my $topic_id=shift;
215 my $person_id = shift; # not used... always display link, will go to login page if not logged in
216 my $s = "<a href=\"topics.pl\">View topics list</a> | \n";
218 $s .= "<a href=\"add_post.pl?topic_id=$topic_id\">Add post</a>";
219 $s .= "\n";
220 return $s;