2 =head1 PACKAGE CXGN::People::Forum::Topic
7 package CXGN
::People
::Forum
::Topic
;
9 use base qw
| CXGN
::People
::Forum
|;
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.
29 my $self = $class->SUPER::new
($dbh);
33 if ($id and $id=~/^\d+$/) {
34 $self->set_forum_topic_id($id);
44 =head2 function new_page_comment()
46 Synopsis: my $t = CXGN::People::Forum::Topic
47 ->new_page_comment($dbh, "BAC", $id)
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.
59 sub new_page_comment
{
62 my $page_type = shift;
64 my $self = $class->new($dbh);
65 my $sth = $self->get_sql('page_comment');
66 $sth->execute($page_type, $page_id);
68 my ($topic_id) = $sth->fetchrow_array();
69 $self->set_forum_topic_id($topic_id);
70 $self->fetch_topic($topic_id);
73 #print STDERR "Couldn't find topic...\n";
80 =head2 function all_topics()
86 Description: returns a list of all topics, as
87 CXGN::People::Forum::Topic objects
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)
97 my $forum = CXGN
::People
::Forum
::Topic
->new($dbh);
98 my $forum_handle = $forum->get_sql('all_topics');
99 $forum_handle -> execute
();
101 while (my ($id) = $forum_handle->fetchrow_array()) {
102 push @topics, CXGN
::People
::Forum
::Topic
->new($dbh, $id);
110 sub all_topics_by_class
{
116 sub all_topic_classes
{
118 my $forum = CXGN
::People
::Forum
->new($self->get_dbh());
119 my $forum_handle = $forum->get_sql('all_topic_classes');
120 $forum_handle->execute();
122 while (my ($class) = $forum_handle->fetchrow_array()) {
123 push @classes, $class;
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);
148 #print STDERR "Storing Topic...".$self->get_topic_name()."\n";
149 if ($self->get_forum_topic_id()) {
153 my $uh = $self->get_sql('update');
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)
167 my $ih = $self->get_sql('insert');
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');
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()
190 Returns: the number of posts associated with the
191 topic_id associated with this object.
199 my $h = $self->get_sql('post_count');
200 $h->execute($self->get_forum_topic_id());
201 my ($count) = $h->fetchrow_array();
205 =head2 function get_most_recent_post_date()
209 Returns: a formatted string representing the date
210 and time of the most recent posting to this
217 sub get_most_recent_post_date
{
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
231 Description: the person_id refers to the sgn_people.sp_person.person_id
237 return $self->{person_id
};
242 $self->{person_id
} = shift;
245 =head2 accessors get_forum_topic_id() and set_forum_topic_id()
255 sub get_forum_topic_id
{
257 if (!$self->{forum_topic
}) { return 0; }
258 return $self->{forum_topic
};
261 sub set_forum_topic_id
{
263 $self->{forum_topic
}=shift;
266 =head2 accessors get_topic_name() and set_topic_name()
278 return ($self->{topic_name
});
284 $self->{topic_name
}=shift;
287 =head2 function get_topic_description
297 sub get_topic_description
{
299 #my $formatted = $self->format_post_text($self->{topic_description});
300 return $self->{topic_description
};
303 =head2 function set_topic_description
313 sub set_topic_description
{
315 $self->{topic_description
}=shift;
318 sub get_parent_topic
{
320 if (!$self->{topic_parent
}) {
323 return $self->{topic_parent
};
326 sub set_parent_topic
{
328 $self->{topic_parent
} = shift;
331 sub get_topic_class
{
333 return $self->{topic_class
};
336 sub set_topic_class
{
338 $self->{topic_class
} = shift;
341 =head2 accessors get_page_type() and set_page_type()
347 Description: the page type property is used for page
354 return $self->{page_type
};
359 $self->{page_type
} =shift;
362 =head2 accessors get_page_object_id() and set_page_object_id()
372 sub get_page_object_id
{
374 if (!$self->{page_object_id
}) { return 0; }
375 return $self->{page_object_id
};
378 sub set_page_object_id
{
380 $self->{page_object_id
} = shift;
383 =head2 function get_all_posts()
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).
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
{
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;
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
{
431 my $person_id = shift;
434 =head2 accessors set_topic_sort_order, get_topic_sort_order
445 sub get_topic_sort_order
{
447 if (!exists($self->{topic_sort_order
})) { $self->{topic_sort_order
}="asc"; }
448 return $self->{topic_sort_order
};
451 sub set_topic_sort_order
{
453 my $sort_order = shift;
454 if ($sort_order=~/asc|desc/i) {
455 $self->{topic_sort_order
}=$sort_order;
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()
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.
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());
498 forum_topic_id, person_id, topic_name, topic_description,
499 parent_topic, topic_class, page_type, page_object_id,
502 sgn_people.forum_topic
510 SELECT forum_topic_id
511 FROM sgn_people.forum_topic
520 FROM sgn_people.forum_post
521 WHERE forum_topic_id=?
527 SELECT MAX(forum_post_id)
528 FROM sgn_people.forum_post
529 WHERE forum_topic_id=?
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
543 GROUP BY forum_topic.forum_topic_id
544 ORDER BY MAX(forum_post.post_time) DESC
550 SELECT DISTINCT(topic_class)
551 FROM sgn_people.forum_topic
558 sgn_people.forum_topic
560 person_id=?, topic_name=?, topic_description=?, topic_class=?,
561 page_type=?, page_object_id=?, sort_order=?
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)
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=?
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"
601 " DELETE FROM sgn_people.forum_post WHERE forum_topic_id=? ",
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);
618 return $self->{query_handles
}->{$name};