reorder selenium tests
[sgn.git] / cgi-bin / forum / posts.pl
blob231941f48f34209ebadfd9b365a3a4d756ca70b6
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("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="container-fluid"><div class="row">$forum_toolbar<br/><br/> };
88 print qq { <div class="panel panel-primary"><div class="panel-body"><b>$topic_name</b><br /><br />$topic_description<br />$topic_started_by</div></div> };
89 #print blue_section_html("Topic Description", $topic_description." ".$topic_started_by);
90 print $s;
92 print "<br/>$forum_toolbar</div></div>\n";
94 $page->footer();
97 sub posts_list {
98 my ($dbh,$sp_person_id,$topic_id,$page) = @_;
99 my $topic = CXGN::People::Forum::Topic -> new($dbh, $topic_id);
100 my $user = CXGN::People::Person->new($dbh, $sp_person_id);
102 my @posts = $topic -> get_all_posts($topic_id);
104 my $s = "";
105 $s .= qq { };
107 if (!@posts) {
108 $s .= "<br/>No user comments.<br/>";
111 foreach my $p (@posts) {
112 my $post_subject = $p->get_subject();
113 my $post_person_id = $p -> get_person_id();
114 my $post_person = CXGN::People::Person -> new($dbh, $post_person_id);
115 my $post_sp_person_id = $post_person -> get_sp_person_id();
116 my $post_name = $post_person->get_first_name()." ".$post_person->get_last_name();
117 my $user_type = $post_person->get_user_type();
118 if($user_type and $user_type ne 'user')
120 $user_type=" ($user_type)";
122 else
124 $user_type='';
126 my $post_text = $p -> get_post_text();
127 my $post_date = $p -> get_formatted_post_time();
128 my $remove_link = "&nbsp;";
130 my $refering_url = $page->{request}->uri()."?".$page->{request}->args();
131 my $encoded_url = URI::Escape::uri_escape($refering_url);
132 if (($sp_person_id && ($post_person_id == $sp_person_id)) || $user->get_user_type() eq "curator") {
133 $remove_link = "<a href=\"forum_post_delete.pl?post_id=".($p->get_forum_post_id())."&amp;refering_page=$encoded_url\">Delete</a>\n";
136 $s .= qq { <div class="panel panel-default"><div class="panel-heading"><div class="row"><div class="col-sm-10">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 </div><div class="col-sm-2">$remove_link</div></div></div> };
137 $s .= qq { <div class="panel-body">$post_text</div></div> };
139 $s .= "";
142 sub old_posts_list {
144 my $topic_id = shift;
145 my $topic = Topic -> new($topic_id);
147 my @posts = $topic -> get_all_posts($topic_id);
149 my $previous_parent_id = -1;
150 my $previous_id = -1;
151 my $indent = 0;
152 my $indent_size = 20;
154 my @parent_ids = ();
156 unless( @posts ) {
157 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 />";
160 my $s = "<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\">";
162 foreach my $p (@posts) {
163 my $subject = $p->get_subject();
164 if (!$subject) { $subject = "[No subject]"; }
165 my $post_text = $p->get_post_text();
167 my $person_id = $p->get_person_id();
168 my $post_time = $p->get_post_time();
169 my $parent_id = $p->get_parent_post_id();
170 my $forum_post_id = $p -> get_forum_post_id();
172 my $person = CXGN::People::Person->new($person_id);
173 my $name = $person->get_first_name()." ".$person->get_last_name();
175 #$indent = $p->get_post_level() * $indent_size;
176 my $has_parent = 0;
177 for ( my $i=0; $i<@parent_ids; $i++) {
178 if ($parent_id == $parent_ids[$i]) {
179 $indent=$i;
180 $parent_ids[$i+1]=$forum_post_id;
181 $has_parent=1;
185 if (!$has_parent) {
186 push @parent_ids, $parent_id;
187 $indent = @parent_ids;
190 $post_time =~ s/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/$2\-$3\-$1 $4\:$5\:$6/;
191 my $indent_pixels = $indent * $indent_size;
192 my $indent_chars = "";
193 for (my $i=0; $i<$indent; $i++) {
194 $indent_chars .="&rarr;";
197 my $respond_link = "Respond";
198 if ($person_id) {
199 $respond_link = "<a href=\"add_post.pl?parent_post_id=$forum_post_id&amp;topic_id=$topic_id\">Respond</a>";
201 #my $post_level = $p -> get_post_level();
202 $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";
203 $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>";
204 $s .="<tr><td colspan=\"7\" align=\"right\">$respond_link</td></tr>";
205 $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>";
207 $s.="</table>\n<br /><br />";
209 return $s;
212 sub forum_toolbar {
213 my $topic_id=shift;
214 my $person_id = shift; # not used... always display link, will go to login page if not logged in
215 my $s = "<a href=\"topics.pl\">View topics list</a> | \n";
217 $s .= "<a href=\"add_post.pl?topic_id=$topic_id\">Add post</a>";
218 $s .= "\n";
219 return $s;