minor fixes
[sgn.git] / lib / CXGN / People / Forum / Topic.pm
blobb7085293fda54145b234c076e04c31d0097f29c5
2 =head1 PACKAGE CXGN::People::Forum::Topic
5 =cut
7 package CXGN::People::Forum::Topic;
9 use base qw | CXGN::People::Forum |;
10 use strict;
12 =head2 function new()
14 Synopsis: constructor:
15 Example: my $t = CXGN::People::Forum::Topic->new($dbh, $id)
16 Arguments: a database handle and a topic id
17 Returns: an object filled in with the db row of id provided,
18 or an empty object if $id is omitted.
19 Side effects:
20 Description:
22 =cut
24 sub new {
25 my $class = shift;
26 my $dbh = shift;
27 my $id = shift;
29 my $self = $class->SUPER::new($dbh);
31 $self->set_sql();
33 if ($id and $id=~/^\d+$/) {
34 $self->set_forum_topic_id($id);
35 $self->fetch_topic();
41 return $self;
44 =head2 function new_page_comment()
46 Synopsis: my $t = CXGN::People::Forum::Topic
47 ->new_page_comment($dbh, "BAC", $id)
48 Arguments:
49 Returns:
50 Side effects:
51 Description: alternate constructor that takes a page type
52 and id as parameters. Page types describe different
53 detail pages such as BAC, EST, unigene, marker,
54 map, etc. The id is the id of the object in the
55 database that the topic is assigned to.
57 =cut
59 sub new_page_comment {
60 my $class = shift;
61 my $dbh = shift;
62 my $page_type = shift;
63 my $page_id = shift;
64 my $self = $class->new($dbh);
65 my $sth = $self->get_sql('page_comment');
66 $sth->execute($page_type, $page_id);
67 if ($sth->rows()>0) {
68 my ($topic_id) = $sth->fetchrow_array();
69 $self->set_forum_topic_id($topic_id);
70 $self->fetch_topic($topic_id);
72 else {
73 #print STDERR "Couldn't find topic...\n";
76 return $self;
80 =head2 function all_topics()
82 Synopsis:
83 Arguments:
84 Returns:
85 Side effects:
86 Description: returns a list of all topics, as
87 CXGN::People::Forum::Topic objects
89 =cut
91 sub all_topics {
93 # static function that return all topics as a list of topic objects.
94 # only get topics that are not page topics (page_type IS NULL)
96 my $dbh = shift;
97 my $forum = CXGN::People::Forum::Topic->new($dbh);
98 my $forum_handle = $forum->get_sql('all_topics');
99 $forum_handle -> execute();
100 my @topics=();
101 while (my ($id) = $forum_handle->fetchrow_array()) {
102 push @topics, CXGN::People::Forum::Topic->new($dbh, $id);
105 return @topics;
110 sub all_topics_by_class {
111 # static function
112 my $class = shift;
116 sub all_topic_classes {
117 my $self = shift;
118 my $forum = CXGN::People::Forum->new($self->get_dbh());
119 my $forum_handle = $forum->get_sql('all_topic_classes');
120 $forum_handle->execute();
121 my @classes;
122 while (my ($class) = $forum_handle->fetchrow_array()) {
123 push @classes, $class;
125 return @classes;
128 sub fetch_topic {
129 my $self = shift;
130 my $sth = $self->get_sql('fetch');
131 $sth->execute($self->get_forum_topic_id());
132 my ($forum_topic_id, $person_id, $topic_name, $topic_description,
133 $parent_topic, $topic_class, $page_type, $page_object_id,
134 $sort_order) = $sth->fetchrow_array();
136 $self->set_person_id($person_id);
137 $self->set_topic_name($topic_name);
138 $self->set_topic_description($topic_description);
139 $self->set_parent_topic($parent_topic);
140 $self->set_topic_class($topic_class);
141 $self->set_page_type($page_type);
142 $self->set_page_object_id($page_object_id);
143 $self->set_topic_sort_order($sort_order);
146 sub store {
147 my $self = shift;
148 #print STDERR "Storing Topic...".$self->get_topic_name()."\n";
149 if ($self->get_forum_topic_id()) {
151 # update db
153 my $uh = $self->get_sql('update');
155 $uh->execute(
156 $self->get_person_id(), $self->get_topic_name(),
157 $self->get_topic_description(), $self->get_topic_class(),
158 $self->get_page_type(), $self->get_page_object_id(),
159 $self->get_topic_sort_order(),
160 ($self->get_forum_topic_id() + 0)
163 else {
165 # insert into db
167 my $ih = $self->get_sql('insert');
168 $ih->execute(
169 $self->get_person_id, $self->get_topic_name(),
170 $self->get_topic_description(), $self->get_parent_topic(),
171 $self->get_topic_class(), $self->get_page_type(),
172 $self->get_page_object_id(), $self->get_topic_sort_order()
174 my $lh = $self->get_sql('currval');
175 $lh->execute();
176 my ($last_id) = $lh->fetchrow_array();
177 $self->set_forum_topic_id($last_id);
179 my $subject="[Forum.pm] Topic stored: ".$self->get_topic_name();
180 my $body="Submitted by person ID: ".$self->get_person_id()."\n\n";
181 $body.="Topic description: ".$self->get_topic_description()."\n\n";
182 eval { CXGN::Contact::send_email($subject,$body,'cxgn-devel\@sgn.cornell.edu'); };
183 return $self->get_forum_topic_id();
186 =head2 function get_post_count()
188 Synopsis:
189 Arguments:
190 Returns: the number of posts associated with the
191 topic_id associated with this object.
192 Side effects:
193 Description:
195 =cut
197 sub get_post_count {
198 my $self = shift;
199 my $h = $self->get_sql('post_count');
200 $h->execute($self->get_forum_topic_id());
201 my ($count) = $h->fetchrow_array();
202 return $count;
205 =head2 function get_most_recent_post_date()
207 Synopsis:
208 Arguments: none
209 Returns: a formatted string representing the date
210 and time of the most recent posting to this
211 topic.
212 Side effects:
213 Description:
215 =cut
217 sub get_most_recent_post_date {
218 my $self = shift;
219 my $h = $self->get_sql('latest_post');
220 $h->execute($self->get_forum_topic_id());
221 my $post = CXGN::People::Forum::Post->new($self->get_dbh(), ($h->fetchrow_array())[0]);
222 return $post->get_formatted_post_time();
225 =head2 accessors get_person_id() and set_person_id()
227 Synopsis: accessors for the person property
228 Arguments: set: the person id
229 Returns: get: the id of the person who created the topic
230 Side effects:
231 Description: the person_id refers to the sgn_people.sp_person.person_id
233 =cut
235 sub get_person_id {
236 my $self = shift;
237 return $self->{person_id};
240 sub set_person_id {
241 my $self = shift;
242 $self->{person_id} = shift;
245 =head2 accessors get_forum_topic_id() and set_forum_topic_id()
247 Synopsis:
248 Arguments:
249 Returns:
250 Side effects:
251 Description:
253 =cut
255 sub get_forum_topic_id {
256 my $self = shift;
257 if (!$self->{forum_topic}) { return 0; }
258 return $self->{forum_topic};
261 sub set_forum_topic_id {
262 my $self = shift;
263 $self->{forum_topic}=shift;
266 =head2 accessors get_topic_name() and set_topic_name()
268 Synopsis:
269 Arguments:
270 Returns:
271 Side effects:
272 Description:
274 =cut
276 sub get_topic_name {
277 my $self = shift;
278 return ($self->{topic_name});
282 sub set_topic_name {
283 my $self = shift;
284 $self->{topic_name}=shift;
287 =head2 function get_topic_description
289 Synopsis:
290 Arguments:
291 Returns:
292 Side effects:
293 Description:
295 =cut
297 sub get_topic_description {
298 my $self=shift;
299 #my $formatted = $self->format_post_text($self->{topic_description});
300 return $self->{topic_description};
303 =head2 function set_topic_description
305 Synopsis:
306 Arguments:
307 Returns:
308 Side effects:
309 Description:
311 =cut
313 sub set_topic_description {
314 my $self=shift;
315 $self->{topic_description}=shift;
318 sub get_parent_topic {
319 my $self = shift;
320 if (!$self->{topic_parent}) {
321 return 0;
323 return $self->{topic_parent};
326 sub set_parent_topic {
327 my $self = shift;
328 $self->{topic_parent} = shift;
331 sub get_topic_class {
332 my $self = shift;
333 return $self->{topic_class};
336 sub set_topic_class {
337 my $self = shift;
338 $self->{topic_class} = shift;
341 =head2 accessors get_page_type() and set_page_type()
343 Synopsis:
344 Arguments:
345 Returns:
346 Side effects:
347 Description: the page type property is used for page
348 comments.
350 =cut
352 sub get_page_type {
353 my $self = shift;
354 return $self->{page_type};
357 sub set_page_type {
358 my $self = shift;
359 $self->{page_type} =shift;
362 =head2 accessors get_page_object_id() and set_page_object_id()
364 Synopsis:
365 Arguments:
366 Returns:
367 Side effects:
368 Description:
370 =cut
372 sub get_page_object_id {
373 my $self = shift;
374 if (!$self->{page_object_id}) { return 0; }
375 return $self->{page_object_id};
378 sub set_page_object_id {
379 my $self = shift;
380 $self->{page_object_id} = shift;
383 =head2 function get_all_posts()
385 Synopsis:
386 Arguments:
387 Returns: all posts belonging to this topic,
388 as CXGN::People::Forum::Post objects.
389 The objects are ordered in the order they
390 were entered (oldest first).
391 Side effects:
392 Description:
394 =cut
396 sub get_all_posts {
397 my $self = shift;
398 my $topic_id = $self->get_forum_topic_id();
400 $self->{post_level} =0;
401 @{$self->{posts}} = ();
403 $self->_get_children_posts(0, $topic_id);
405 return @{$self->{posts}};
408 sub _get_children_posts {
409 my $self = shift;
410 my $parent_post_id = shift;
411 my $topic_id = shift;
413 my $sort_order = $self->get_topic_sort_order();
414 #warn "SORT ORDER IS $sort_order\n";
415 # $sort_order = "ASC" unless $sort_order =~ /desc/i;
416 my $sth;
417 if ($sort_order =~ /asc/) { $sth = $self->get_sql('children_posts_asc'); }
418 else { $sth = $self->get_sql('children_posts_desc'); }
419 $sth->execute($topic_id);
420 return if $sth->rows()==0;
422 while (my ($post_id)= $sth->fetchrow_array()) {
423 my $post = CXGN::People::Forum::Post->new($self->get_dbh(), $post_id);
424 push @{$self->{posts}}, $post;
429 sub get_all_posts_by_person {
430 my $self = shift;
431 my $person_id = shift;
434 =head2 accessors set_topic_sort_order, get_topic_sort_order
436 Property:
437 Setter Args:
438 Getter Args:
439 Getter Ret:
440 Side Effects:
441 Description:
443 =cut
445 sub get_topic_sort_order {
446 my $self=shift;
447 if (!exists($self->{topic_sort_order})) { $self->{topic_sort_order}="asc"; }
448 return $self->{topic_sort_order};
451 sub set_topic_sort_order {
452 my $self=shift;
453 my $sort_order = shift;
454 if ($sort_order=~/asc|desc/i) {
455 $self->{topic_sort_order}=$sort_order;
457 else {
458 print STDERR "[Topic.pm] set_topic_sort_order $sort_order is not a legal sort order. Set to default (asc)\n";
459 $self->{topic_sort_order}="asc";
465 =head2 function delete()
467 Synopsis:
468 Arguments: none
469 Returns: the number of rows deleted, usually >0 if
470 successful (the number is the number of
471 associated posts deleted).
472 Side effects: the topic with the corresponding id is
473 permanently removed from the database,
474 including all associated posts.
475 Description:
477 =cut
479 sub delete {
480 my $self = shift;
481 my $h = $self->get_sql('delete_posts');
482 $h->execute($self->get_forum_topic_id());
483 $h = $self->get_sql('delete_topic');
484 $h->execute($self->get_forum_topic_id());
485 return $h->rows();
489 sub set_sql {
490 my $self = shift;
492 $self->{queries} = {
494 fetch =>
497 SELECT
498 forum_topic_id, person_id, topic_name, topic_description,
499 parent_topic, topic_class, page_type, page_object_id,
500 sort_order
501 FROM
502 sgn_people.forum_topic
503 WHERE
504 forum_topic_id=?
507 page_comment =>
510 SELECT forum_topic_id
511 FROM sgn_people.forum_topic
512 WHERE page_type=?
513 AND page_object_id=?
516 post_count =>
519 SELECT COUNT(*)
520 FROM sgn_people.forum_post
521 WHERE forum_topic_id=?
524 latest_post=>
527 SELECT MAX(forum_post_id)
528 FROM sgn_people.forum_post
529 WHERE forum_topic_id=?
532 all_topics =>
535 SELECT
536 forum_topic.forum_topic_id,
537 MAX(forum_post.post_time)
538 FROM sgn_people.forum_topic
539 LEFT JOIN sgn_people.forum_post
540 ON (forum_topic.forum_topic_id=forum_post.forum_topic_id)
541 WHERE page_type IS NULL
542 OR page_type=''
543 GROUP BY forum_topic.forum_topic_id
544 ORDER BY MAX(forum_post.post_time) DESC
547 all_topic_classes =>
550 SELECT DISTINCT(topic_class)
551 FROM sgn_people.forum_topic
554 update =>
557 UPDATE
558 sgn_people.forum_topic
559 SET
560 person_id=?, topic_name=?, topic_description=?, topic_class=?,
561 page_type=?, page_object_id=?, sort_order=?
562 WHERE
563 forum_topic_id =?
566 insert =>
569 INSERT INTO sgn_people.forum_topic
570 (person_id, topic_name, topic_description, parent_topic,
571 topic_class, page_type, page_object_id, sort_order)
572 VALUES
573 (?, ?, ?, ?,
574 ?, ?, ?, ?)
577 currval =>
579 " SELECT currval('sgn_people.forum_topic_forum_topic_id_seq') ",
581 children_posts_asc =>
584 SELECT forum_post_id, post_time
585 FROM sgn_people.forum_post
586 WHERE forum_topic_id=?
587 ORDER BY post_time"
590 children_posts_desc => "
591 SELECT forum_post_id, post_time
592 FROM sgn_people.forum_post
593 WHERE forum_topic_id=?
594 ORDER BY post_time desc"
599 delete_posts =>
601 " DELETE FROM sgn_people.forum_post WHERE forum_topic_id=? ",
603 delete_topic =>
605 " DELETE FROM sgn_people.forum_topic WHERE forum_topic_id=? ",
610 while(my($k,$v) = each %{$self->{queries}}){
611 $self->{query_handles}->{$k}= $self->get_dbh()->prepare($v);
615 sub get_sql {
616 my $self = shift;
617 my $name = shift;
618 return $self->{query_handles}->{$name};