Merge pull request #5230 from solgenomics/topic/open_pollinated
[sgn.git] / lib / CXGN / People / Forum / Post.pm
blobe5ca69a2e455568aff72b23943eec3cbb3fde7b8
2 =head1 PACKAGE CXGN::People::Forum::Post
4 A package that deals with the posts.
6 =cut
9 use strict;
11 package CXGN::People::Forum::Post;
13 use base qw | CXGN::People::Forum |;
15 =head2 constructor new()
17 Synopsis: constructor
18 Arguments: an database handle and an id for a post
19 or undef as id to create an empty object.
20 Returns: a CXGN::People::Forum::Post object
21 Side effects:
22 Description:
24 =cut
26 sub new {
27 my $class = shift;
28 my $dbh = shift;
29 my $id = shift;
31 my $self = $class->SUPER::new($dbh);
32 $self->set_sql();
33 if ($id) {
34 $self->set_forum_post_id($id);
35 $self->fetch_forum_post();
37 return $self;
41 # internal function
43 sub fetch_forum_post {
44 my $self = shift;
45 my $h = $self->get_sql('fetch');
46 $h->execute($self->get_forum_post_id());
47 while (my ($forum_post_id, $post_text, $parent_post_id, $person_id, $post_time, $subject)=$h->fetchrow_array()) {
48 $post_text = $self->format_post_text($post_text);
49 $self->set_post_text($post_text);
50 $self->set_parent_post_id($parent_post_id);
51 $self->set_person_id($person_id);
52 $self->set_post_time($post_time);
53 $self->set_subject($subject);
60 =head2 function store()
62 Synopsis: $post->store();
63 Arguments: none
64 Returns: the id of the new object in the database
65 if successful, undef otherwise
66 Side effects: stores the post to the database.
67 If an id is already available, an update occurs.
68 if an id is not available, an insert occurs.
69 Description:
71 =cut
73 sub store {
74 my $self = shift;
75 my $return_value = 0;
76 if ($self->get_forum_post_id()) {
77 my $sth = $self->get_sql('update');
78 $sth->execute(
79 $self->get_post_text(), $self->get_person_id(), $self->get_forum_post_id(), $self->get_forum_topic_id(), $self->get_subject()
80 , $self->get_forum_post_id
83 else {
85 my $sth = $self->get_sql('insert');
86 $sth->execute(
87 $self->get_post_text(),
88 $self->get_person_id(),
89 $self->get_forum_topic_id(),
90 $self->get_subject()
93 $sth = $self->get_sql('currval');
94 $sth->execute();
95 my ($lid) = $sth->fetchrow_array();
97 $self->set_forum_post_id($lid);
98 $return_value = $lid;
101 my $subject="[Forum.pm] New post stored: ".$self->get_subject();
102 my $body="New post: \n".$self->get_post_text()."\n\n";
103 eval { CXGN::Contact::send_email($subject,$body,'cxgn-devel\@sgn.cornell.edu'); };
104 return $return_value;
107 =head2 function delete()
109 Synopsis:
110 Arguments: none
111 Returns: the number of rows deleted, usually 1 if
112 successful
113 Side effects: the post with the corresponding id is
114 permanently removed from the database.
115 Description:
117 =cut
119 sub delete {
120 my $self = shift;
121 my $sth = $self->get_sql('delete');
122 $sth->execute($self->get_forum_post_id());
123 return $sth->rows();
126 =head2 function get_forum_post_id()
128 Synopsis: retrieves the forum_post_id of this object
129 Arguments:
130 Returns:
131 Side effects:
132 Description: Posts with no id are automatically
133 treated as inserts in the store() function, and
134 the new id is then available using the accessor.
136 =cut
138 sub get_forum_post_id {
139 my $self = shift;
140 return $self->{forum_post_id};
143 =head2 Other accessor functions
145 get_subject
146 set_subject
148 get_forum_topic_id
149 set_forum_topic_id
151 get_person_id
152 set_person_id
154 get_post_text
155 set_post_text
157 =cut
159 sub get_subject {
160 my $self = shift;
161 return $self->{subject};
164 sub set_subject {
165 my $self = shift;
166 $self->{subject} = shift;
168 sub set_forum_post_id {
169 my $self = shift;
170 $self->{forum_post_id} = shift;
173 sub get_forum_topic_id {
174 my $self = shift;
175 return $self->{forum_topic_id};
178 sub set_forum_topic_id {
179 my $self = shift;
180 $self->{forum_topic_id} = shift;
183 sub get_person_id {
184 my $self = shift;
185 return $self->{person_id};
188 sub set_person_id {
189 my $self = shift;
190 $self->{person_id} = shift;
193 sub get_post_text {
194 my $self = shift;
195 return $self->{post_text};
198 sub set_post_text {
199 my $self = shift;
200 $self->{post_text} = shift;
203 sub get_parent_post_id {
204 my $self = shift;
205 return $self->{parent_post_id};
208 sub set_parent_post_id {
209 my $self = shift;
210 $self->{parent_post_id} = shift;
213 sub get_post_time {
214 my $self = shift;
215 return $self->{timestamp};
218 sub get_formatted_post_time {
219 my $self = shift;
220 my $posttime = $self->get_post_time();
221 if ($posttime =~ m/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/) {
222 $posttime = "$2\/$3\/$1 $4\:$5";
224 if ($posttime =~/(.*)(\..*)$/) {
225 $posttime = $1;
227 return $posttime;
230 sub set_post_time {
231 my $self = shift;
232 $self->{timestamp} = shift;
235 sub get_post_level {
236 my $self = shift;
237 return $self->{post_level};
240 sub set_post_level {
241 my $self = shift;
242 $self->{post_level}=shift;
245 sub set_sql {
246 my $self =shift;
247 $self->{queries} = {
249 fetch =>
252 SELECT
253 forum_post_id, post_text, parent_post_id,
254 person_id, post_time, subject
255 FROM
256 sgn_people.forum_post
257 WHERE
258 forum_post_id=?
261 update =>
264 UPDATE sgn_people.forum_post
265 SET
267 post_text=?, person_id=?, forum_post_id=?,
268 forum_topic_id=?, subject=?
270 WHERE forum_post=?
273 insert =>
276 INSERT INTO sgn_people.forum_post
277 (post_text, person_id, forum_topic_id, subject)
278 VALUES (?, ?, ?, ?)
281 currval =>
284 SELECT currval('sgn_people.forum_post_forum_post_id_seq')
287 delete =>
290 DELETE FROM sgn_people.forum_post
291 WHERE forum_post_id=?
296 while(my($k,$v) = each %{$self->{queries}}){
297 $self->{query_handles}->{$k}=$self->get_dbh()->prepare($v);
302 sub get_sql {
303 my $self =shift;
304 my $name = shift;
305 return $self->{query_handles}->{$name};
310 return 1;