fix non-interactive return value... 0 means passed, not 1.
[sgn.git] / cgi-bin / forum / add_post.pl
blob85d640ec339ad552925d9119f07b335c454c7992
2 =head1 add_post.pl
4 add_post.pl adds posts to topics or comments to webpages.
6 For the former, add_post.pl takes a topic_id parameter. For the latter, add_post takes two parameters, page_type and page_object_id. If not parameter is specified, add_post.pl shows an error message. If all three parameters are specified, the topic_id overrides the other two.
8 If no post_text is supplied, add_post.pl displays an input mask. Otherwise it processes the entry and tries to add post_text to the database, with the logged in user as the submitter and the topic id as specified above.
10 A refering_page parameter can be used to generate a link back to the calling page.
12 =cut
14 use strict;
16 use CXGN::Page;
17 use CXGN::DB::Connection;
18 use CXGN::Login;
19 use CXGN::People;
20 use CXGN::People::Forum;
21 use CXGN::People::Person;
22 use CXGN::Page::FormattingHelpers qw/ page_title_html
23 blue_section_html /;
25 my $SIZELIMIT = 10000; # the maximal number of bytes in a post.
27 my $page = CXGN::Page->new( "SGN Forum | Add Post", "Lukas");
28 my $dbh = CXGN::DB::Connection->new();
31 # get information about the poster
33 my ( $person, $name, $sp_person_id ) = do {
34 # to enable anonymous comments, change the verify_session to has_session
35 if( my $sp_person_id = CXGN::Login->new($dbh)->verify_session ) {
36 my $person = CXGN::People::Person -> new($dbh, $sp_person_id);
37 my $name = $person->get_first_name()." ".$person->get_last_name();
38 $sp_person_id = $person -> get_sp_person_id();
39 ( $person, $name, $sp_person_id )
40 } else {
41 ( (undef) x 3 )
46 my ($subject, $post_text, $parent_post_id, $topic_id, $page_type, $page_object_id, $refering_page, $action) = $page -> get_encoded_arguments("subject", "post_text", "parent_post_id", "topic_id", "page_type", "page_object_id", "refering_page", "action");
48 # we define 3 actions:
49 # 1) input
50 # 2) review
51 # 3) save
52 # all other values for the action parameter will show the input as default.
55 my $parent_post = "";
56 my $parent_subject = "";
57 my $display_subject = "";
58 my $parent_post_text = "";
59 my $parent_post_explain = "";
60 my $topic;
61 my $topic_name = "";
62 my $subject_input = "";
64 # if a topic_id is supplied, simply add the post to the given topic id.
66 if ($topic_id) {
67 $parent_post = CXGN::People::Forum::Post -> new($dbh, $parent_post_id);
68 $parent_subject = $parent_post->get_subject();
69 $parent_post_text = $parent_post->get_post_text();
70 $parent_post_explain = "";
71 $display_subject = "RE: $parent_subject";
72 if ($parent_post_text) {
73 $parent_post_explain = "Your message will be attached to the following message:
74 <table summary=\"\" class=\"sgnblue\"><tr><td><pre>$parent_post_text</pre></td></tr></table>
75 <br />";
76 $subject_input = "<input type=\"text\" name=\"subject\" value=\"$display_subject\" /><br /><br /> ";
79 $topic = CXGN::People::Forum::Topic -> new($dbh, $topic_id);
80 if (!$topic) {
81 $page->error_page("No legal topic id was supplied. [topic_id=$topic_id]");
83 $topic_name = $topic -> get_topic_name();
86 # if the page_type and page_object_id are supplied, it is a page comment. Get the
87 # topic id for that page and id.
89 elsif ($page_type && $page_object_id) {
90 $topic = CXGN::People::Forum::Topic->new_page_comment($dbh, $page_type, $page_object_id);
91 if (!$topic->get_topic_name()) {
92 $topic->set_topic_name($page_type." ".$page_object_id);
93 $topic->set_person_id($sp_person_id);
96 $topic_name = "$page_type id: $page_object_id";
98 else
100 $page->error_page("Sorry, but there was insufficient data to add this post.");
104 # create a post object to work from
106 my $post = CXGN::People::Forum::Post->new($dbh);
107 $post->set_subject($subject);
108 $post->set_person_id($sp_person_id);
109 $post->set_parent_post_id($parent_post_id);
110 $post->set_forum_topic_id($topic_id);
111 my $formatted_post_text = $post->format_post_text($post_text);
114 # create $link with a link back to the original posting page
115 # (this can be a detail page or it can be a topic_id specific posts.pl page
117 my $link;
118 if ($refering_page) {
119 $link = "<a href=\"$refering_page\">Back to posts</a>";
121 else {
122 $link = "<a href=\"posts.pl?topic_id=$topic_id\">Back to posts</a>";
125 # perform the appropriate action, depending on the
126 # action parameter
128 if ($action eq "review") {
130 my $truncated = 0;
131 my $display_post_text = "";
132 if (length($post_text)>$SIZELIMIT) {
133 $display_post_text = substr($post_text, 0, $SIZELIMIT);
134 $truncated = 1;
136 else {
137 $display_post_text = $post_text;
140 my $truncated_warning = "";
141 if ($truncated) {
142 $truncated_warning = "<b>Note:</b> Your post exceeds $SIZELIMIT characters and has been truncated.
143 Please use the 'go back' button to edit your post, or submit to accept the
144 truncated version.<br /><br />\n";
148 $page -> header();
150 print page_title_html("Review your post");
152 print <<HTML;
153 <div class="container-fluid">
154 Enter post -> <b>Review post</b> -> Store post<br /><br />
155 This is how the text of your post will appear. Please use the 'store post' button for permanently adding the post
156 or the 'modify post' button to go back and revise the post. To cancel, click on 'back to posts'.<br /><br />
158 $truncated_warning
160 <div class="panel panel-primary"><div class="panel-body">$formatted_post_text</div></div>
162 <br /><br />
164 <table width="100%"><tr><td align="left">
165 <form action="add_post.pl" method="post">
166 <input type="hidden" name="post_text" value="$post_text" />
167 <input type="hidden" name="page_type" value="$page_type" />
168 <input type="hidden" name="page_object_id" value="$page_object_id" />
169 <input type="hidden" name="refering_page" value="$refering_page" />
170 <input type="hidden" name="action" value="input" />
171 <input type="hidden" name="topic_id" value="$topic_id" />
172 <input class="btn btn-info" type="submit" value="modify post" />
173 </form>
174 <td><td>
175 $link
176 </td><td align="right">
177 <form action="add_post.pl" method="post">
178 <input type="hidden" name="post_text" value="$post_text" />
179 <input type="hidden" name="page_type" value="$page_type" />
180 <input type="hidden" name="page_object_id" value="$page_object_id" />
181 <input type="hidden" name="refering_page" value="$refering_page" />
182 <input type="hidden" name="action" value="save" />
183 <input type="hidden" name="topic_id" value="$topic_id" />
184 <input class="btn btn-primary" type="submit" value="store post" />
185 </form>
186 </td></tr></table>
187 </div>
188 HTML
190 $page -> footer();
193 elsif ($action eq "save") {
195 # save the object if action is "save"
197 $topic->set_page_type($page_type);
198 $topic->set_page_object_id($page_object_id);
199 $topic->store();
201 $topic_id = $topic->get_forum_topic_id();
202 $post->set_forum_topic_id($topic_id);
204 my $truncated = 0;
205 if (length($post_text)>$SIZELIMIT) {
206 $post_text = substr($post_text, 0, $SIZELIMIT);
207 $truncated = 1;
210 $post->set_post_text($post_text);
211 $post->store();
213 $page->header();
215 print page_title_html("Your post was successfully stored");
216 print "Enter post -> Review post -> <b>Store post</b></a><br /><br />\n";
218 if ($truncated) {
219 print "<b>Note:</b>Your post has been truncated to $SIZELIMIT characters.<br />\n";
222 my $display_post_text = $post->format_post_text($post_text);
223 #print "submitter: $sp_person_id topic: $topic_id, $page_type, $page_object_id. text:<br /><br />";
224 print "<div class=\"panel panel-primary\" ><div class=\"panel-body\">$display_post_text</div></div>";
225 print "<br /><br /><br />\n";
228 print "$link<br /><br /><br />\n";
230 $page->footer();
234 else {
236 # the action is "input" or anything else.
238 $page -> header();
240 print page_title_html("Enter Post for topic \"$topic_name\"");
242 print <<HTML;
244 <form method="post" action="add_post.pl">
246 Steps: <b>Enter post</b> -> Review post -> Store post<br /><br />
247 $parent_post_explain<br />
249 Please enter your comment below. You can delete the post later if you are logged in.<br />
250 HTML tags are not supported. You can add links to your post by using square brackets. For example:<br />
251 <tt>[url]sgn.cornell.edu[/url]</tt> will appear as <a href="http://sgn.cornell.edu">sgn.cornell.edu</a> in the post.<br /><br />
253 Size limit per post is $SIZELIMIT characters, including spaces and punctuation.<br /><br />
254 <b>Note:</b> this service is provided as a courtesy. SGN reserves the right to delete posts at any time for any reason.
255 <br /><br />
256 <input type="hidden" name="parent_post_id" value="$parent_post_id" />
257 <input type="hidden" name="topic_id" value="$topic_id" />
258 <input type="hidden" name="page_type" value="$page_type" />
259 <input type="hidden" name="page_object_id" value="$page_object_id" />
260 <input type="hidden" name="refering_page" value="$refering_page" />
261 <input type="hidden" name="action" value="review" />
262 $subject_input
264 Post Text:<br />
265 <textarea class="form-control" rows="12" cols="80" name="post_text">$post_text</textarea>
266 <br /><br />
268 <table width="100%"><tr><td align="left">
269 $link
270 </td><td align="right">
271 <input class="btn btn-primary" type=submit value="Review post" />
272 </form>
273 </td></tr></table>
275 <br />
278 <br />
279 <br />
281 HTML
283 $page->footer();