Merge pull request #5230 from solgenomics/topic/open_pollinated
[sgn.git] / lib / CXGN / People / PageComment.pm
blobe8c8cd4d99c0cdd21e0e3cf969436d48146232f5
2 =head1 NAME
4 CXGN::People::PageComment - a package for adding user comments to database detail pages.
6 =head1 SYNOPSYS
8 my $page_comment_obj = CXGN::People::PageComment->new($dbh, "map", $map_id, $referer);
10 print $page_comment_obj->get_html();
12 =head1 DESCRIPTION
14 Handles the addition and deletion of user comments to SGN pages. Users have to be logged in using SGN's login system to post/delete messages.
16 =head1 DEPENDENCIES
18 The current implementation depends on CXGN::People::Forum.
20 =head1 USAGE ON DETAIL PAGES
22 On detail pages, the page comment feature should be added using a mason module, /page/comments.mas.
24 =head1 FUNCTIONS
26 This class implements the following methods:
28 =cut
30 use strict;
32 package CXGN::People::PageComment;
34 use CXGN::Login;
35 use CXGN::People;
36 use CXGN::People::Forum;
38 use base qw | CXGN::DB::Object |;
40 =head2 constructor new()
42 Synopsis: my $pc = CXGN::People::PageComment -> new($dbh, "map", 9, $referer);
43 Arguments:
44 (1) A database handle
45 (2) A type, one of "BAC", "EST", "unigene", "marker", "map", "bac_end"
46 (3) the object\'s ID (an integer value that specifies the page).
47 (4) the refering page, including the page arguments
48 Returns: a handle to a Page_comment object
49 Side effects: Accesses the sgn_people database through the Forum.pm interface to search for comments for the specified type/ID and caches them.
50 Description:
52 =cut
54 sub new {
55 my $class = shift;
56 my $dbh = shift;
57 my $type = shift;
58 my $id = shift;
59 my $referer = shift;
61 my $args = {};
62 my $self = $class->SUPER::new($dbh);
64 $self->set_type($type);
65 $self->set_id($id);
67 # get a page object to decide if a user is logged in.
68 # my $page= CXGN::Page->new("", "Lukas");
69 $self->set_user_id(CXGN::Login->new($self->get_dbh())->has_session());
70 $self->set_refering_page($referer);
71 #print STDERR "Referer: ".($self->get_refering_page())."\n";
73 @{$self->{posts}}= ();
74 $self->fetch_page_comments();
76 return $self;
79 =head2 function fetch_page_comments()
81 Usage: $pcobj -> fetch_page_comments();
82 Desc: populates the page comment object from the
83 database. Called by the constructor.
84 Side Effects:
85 Example:
87 =cut
89 sub fetch_page_comments {
91 # fetch the page comments using a function in the Topic class
93 my $self = shift;
94 #print STDERR "TYPE: ".$self->get_type.", ID: ".$self->get_id()."\n";
95 my $topic = CXGN::People::Forum::Topic->new_page_comment($self->get_dbh(), $self->get_type(), $self->get_id());
96 #print STDERR "TOPIC ID: ".$topic->get_forum_topic_id()."\n";
98 $self->set_topic($topic);
99 if ($self->get_topic()->get_forum_topic_id()) {
100 #print STDERR "Topic_id: ".$self->get_topic()->get_forum_topic_id()."\n";
101 my @posts = $self->get_topic()->get_all_posts();
102 # foreach my $p (@posts) { print STDERR "POSTS: ".($p->get_subject())."\n"; }
103 $self->set_posts(@posts);
106 else {
107 #print STDERR "No topic could be found corresponding to type=".$self->get_type()." and id=".$self->get_id()."\n";
112 =head2 accessors set_refering_page(), get_refering_page()
114 Usage: $pc->set_refering_page('/detail?object_id=$id');
115 Property: the url of the page that the post is stored
116 for.
117 Side Effects: the url will be embedded in links to get back to
118 the relevant detail page.
119 Example:
121 =cut
123 sub set_refering_page {
124 my $self = shift;
125 $self->{refering_page} = shift;
128 sub get_refering_page {
129 my $self = shift;
130 return $self->{refering_page};
133 =head2 accessors set_topic(), get_topic()
135 Usage: $pc->set_topic($topic_id)
136 Property: each post has an associated topic_id
137 Side Effects:
138 Example:
140 =cut
142 sub set_topic {
143 my $self = shift;
144 $self->{topic}=shift;
147 sub get_topic {
148 my $self = shift;
149 return $self->{topic};
152 =head2 accessors set_posts(), get_posts()
154 Usage: $pc->set_posts(@posts)
155 Property: The posts for this type and id combination
156 Side Effects:
157 Example:
159 =cut
161 sub set_posts {
162 my $self = shift;
163 @{$self->{posts}} = @_;
166 sub get_posts {
167 my $self = shift;
168 return @{$self->{posts}};
171 =head2 accessors set_type(), get_type()
173 Usage: $pc->set_type('marker');
174 Property: the type of page this page comment
175 is stored for. Supported are things like
176 bac, marker, pub, locus, etc.
177 Side Effects:
178 Example:
180 =cut
182 sub set_type {
183 my $self = shift;
184 $self->{type}=shift;
187 sub get_type {
188 my $self = shift;
189 return $self->{type};
192 =head2 accessors set_id(), get_id()
194 Usage: $pc->set_id($id)
195 Property: the id of this post
196 Side Effects:
197 Example:
199 =cut
201 sub set_id {
202 my $self = shift;
203 $self->{id}=shift;
206 sub get_id {
207 my $self = shift;
208 return $self->{id};
211 =head2 accessors set_user_id(), get_user_id()
213 Usage: $pc->set_user_id($sp_person_id)
214 Property: the id of the user owning the post
215 Side Effects:
216 Example:
218 =cut
220 sub set_user_id {
221 my $self = shift;
222 $self->{user_id}=shift;
225 sub get_user_id {
226 my $self = shift;
227 return $self->{user_id};
230 =head2 function get_html()
232 THIS FUNCTION IS DEPRECATED. USE /page/comments.mas IN NEW CODE.
233 Synopsis: print $cp -> get_html();
234 Arguments: none
235 Returns: a string containing html code containing the user
236 comments
237 Side effects:
238 Description:
241 =cut
243 sub get_html {
245 my ($self, $passed_referer) = @_;
247 my @posts = $self->get_posts();
249 # We want to eventually return the user to the page they came from
250 #my $encoded_url = url_encode($self->get_refering_page());
252 # ...but if a referer is provided, we'll use that instead (helpful
253 # with ajax)
254 my $encoded_url = url_encode($self->get_refering_page()) || url_encode($passed_referer);
255 # warn "UNENCODED URL IS $passed_referer";
256 # warn "ENCODED URL IS $encoded_url";
258 my $s;
259 my $subtitle;
260 if (!@posts) {
261 #$subtitle ="<span class=\"ghosted\">No user comments.</span>";
263 else
265 $s .= "<div class=\"indentedcontent\"><table width=\"700\" summary=\"\" cellpadding=\"3\" cellspacing=\"0\" border=\"0\" >";
266 foreach my $p (@posts) {
267 my $subject = $p->get_subject();
268 my $person_id = $p -> get_person_id();
269 my $person = CXGN::People::Person -> new($self->get_dbh(), $person_id);
270 my $sp_person_id = $person -> get_sp_person_id();
271 my $name = $person->get_first_name()." ".$person->get_last_name();
273 my $user_type = $person->get_user_type();
275 my $remove_link = "&nbsp;";
276 if ($sp_person_id && ($self->get_user_id() == $sp_person_id || $user_type eq 'curator' )) {
277 $remove_link = "<a href=\"/forum/forum_post_delete.pl?post_id=".($p->get_forum_post_id())."&amp;refering_page=$encoded_url\">Delete</a>\n";
280 if($user_type and $user_type ne 'user'){
281 $user_type=" ($user_type)";
282 } else {
283 $user_type='';
286 my $text = $p -> get_post_text();
287 my $date = $p -> get_formatted_post_time();
289 my $poster_html = $sp_person_id
290 ? "<a href=\"/solpeople/personal-info.pl?action=view&amp;sp_person_id=$sp_person_id\">$name</a>$user_type"
291 : "anonymous";
293 $s .= "<tr><td><div class=\"boxbgcolor2\">
295 <table summary=\"\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\"><tr><td>Posted by <b>$poster_html</b> on $date </td><td class=\"right\">$remove_link&nbsp;</td></tr></table>
297 </div>";
298 $s .= "<div class=\"boxbgcolor5\"><div class=\"indentedcontent\">$text</div></div></td></tr>";
300 $s .= "</table></div>";
303 $subtitle .= "<a href=\"/forum/add_post.pl?page_type=".$self->get_type()."&amp;page_object_id=".$self->get_id()."&amp;refering_page=$encoded_url\">[Add comment]</a>";
306 return CXGN::Page::FormattingHelpers::info_section_html(title => 'User comments',
307 collapsible => 1,
308 subtitle=>$subtitle,
309 contents =>$s ,
313 sub url_encode {
314 my $theURL = $_[0];
315 $theURL =~ s/([\W])/"%" . uc(sprintf("%2.2x",ord($1)))/eg;
316 return $theURL;