error message return if image processing fails
[sgn.git] / cgi-bin / forum / add_topic.pl
blob281f01d44e078acd21513f539388b40a2bd4fbf9
2 =head1 NAME
4 add_topic.pl adds posts to topics or comments to webpages.
6 =head1 DESCRIPTION
8 This script handles the addition, removal, and editing of topic entries to the SOL forum. Users need to be logged in to use it, otherwise they are re-directed to the login page.
10 The topic information is stored in the relevant tables in the sgn_people schema of the cxgn database. The back-store functionality is wrapped by the CXGN::People::Forum classes, o which this code depends.
12 =head1 PARAMETERS
14 The following html parameters are supported:
16 =over 5
18 =item action
20 one of: edit, delete, review, save, add.
22 =item topic_description
24 A string with a short description of what the topic is supposed to be about and what it is not about.
26 =item
28 =item
30 =head1 AUTHOR
32 Lukas Mueller (lam87@cornell.edu)
34 =cut
36 use strict;
37 use warnings;
39 use CXGN::DB::Connection;
40 use CXGN::Page;
41 use CXGN::Login;
42 use CXGN::People;
43 use CXGN::Contact;
44 use CXGN::People::Forum;
45 use CXGN::Contact;
46 use CXGN::Page::FormattingHelpers qw/ page_title_html
47 blue_section_html /;
49 my $dbh = CXGN::DB::Connection->new();
50 my $page = CXGN::Page->new( "SGN Forum | Configure topic", "Lukas" );
52 my $sp_person_id = CXGN::Login->new($dbh)->verify_session();
54 my ( $subject, $topic_description, $action, $topic_id, $sort_order ) =
55 $page->get_encoded_arguments( "subject", "topic_description", "action",
56 "topic_id", "sort_order" );
58 # we define 3 actions:
59 # 1) input
60 # 2) review
61 # 3) save
62 # all other values for the action parameter will show the input as default.
65 # get information about the user
67 my $user = CXGN::People::Person->new( $dbh, $sp_person_id );
68 my $name = $user->get_first_name() . " " . $user->get_last_name();
69 my $user_id = $user->get_sp_person_id();
71 my $topic = CXGN::People::Forum::Topic->new( $dbh, $topic_id );
73 # check if the user has the necessary privileges to work with this
74 # topic. if there it is a topic to be entered, forgo the check.
76 if ( ( $topic->get_person_id() && ( $topic->get_person_id() ne $user_id ) )
77 && ( $user->get_user_type() ne "curator" ) )
79 $page->message_page(
80 "Error:\nYou don't have the privileges to edit this topic, because you did not create it.\n\n\n"
84 if ( $action eq "edit" ) {
85 if ($topic_description) {
86 $topic->set_topic_description($topic_description);
88 if ($subject) { $topic->set_topic_name($subject); }
89 if ($sort_order) { $topic->set_topic_sort_order($sort_order); }
90 show_form( $dbh, $topic, "review", $user, $page );
92 elsif ( $action eq "delete" ) {
93 delete_topic($topic, $page);
95 elsif ( $action eq "confirm_delete" ) {
96 confirm_delete_topic($topic, $page);
98 elsif ( $action eq "review" ) {
99 $topic->set_topic_description($topic_description);
100 $topic->set_topic_name($subject);
101 $topic->set_topic_sort_order($sort_order);
102 review_topic($topic, $page);
104 elsif ( $action eq "save" ) {
105 if ($topic_description) {
106 $topic->set_topic_description($topic_description);
108 if ($subject) {
109 $topic->set_topic_name($subject);
111 if ($sort_order) {
112 $topic->set_topic_sort_order($sort_order);
114 save_topic( $topic, $user, $page );
116 else {
119 # the action is "new" or anything else.
121 show_form( $dbh, $topic, "review", $user, $page );
124 sub save_topic {
125 my ($topic,$user,$page) = @_;
127 # don't clobber the old person id if this is an edit.
129 if ( !$topic->get_person_id() ) {
130 $topic->set_person_id( $user->get_sp_person_id() );
133 # store the topic -- performs an update if topic_id
134 # already exists, other new insert...
136 $topic->store();
138 $page->header();
140 print page_title_html("Store your topic");
142 print <<HTML;
144 Steps: Configure topic</b> -\> Review topic -\> <b>Store topic</b><br /><br />
146 <b>The topic has been successfully stored.</b>
147 <br /><br />
148 <a href="topics.pl">Return to topics.</a>
149 <br /><br />
151 HTML
153 $page->footer();
156 sub delete_topic {
157 my ($topic,$page) = @_;
158 my $topic_id = $topic->get_forum_topic_id();
159 my $topic_name = $topic->get_topic_name();
160 my $topic_post_count = $topic->get_post_count();
161 my $topic_desc = $topic->get_topic_description();
163 $page->header();
165 print page_title_html("Delete your topic");
167 print <<HTML;
168 <div class="container-fluid">
169 <b>SGN Forum:</b> Delete the following topic, including <b>$topic_post_count</b> posts?</b><br /><br />\n
171 <div class="panel panel-default"><div class="panel-body">
172 <b>$topic_name</b><br /><br />
173 $topic_desc
174 </div></div>
176 <br />
178 <table width="100%">
179 <tr><td align="left"><a href="topics.pl">Cancel</a>
180 </td><td align="right">
181 <form action="add_topic.pl?action=confirm_delete&amp;topic_id=$topic_id">
183 <input type="hidden" name="action" value="confirm_delete" />
184 <input type="hidden" name="topic_id" value="$topic_id" />
185 <input class="btn btn-primary"type="submit" value="delete topic" />\n
186 </form><br />\n
187 </td></tr></table>
188 <br /><br /><br />
189 </div>
191 HTML
193 $page->footer();
196 sub confirm_delete_topic {
197 my ($topic,$page) = @_;
199 my $topic_name = $topic->get_topic_name();
200 my $delete_count = ( $topic->delete() - 1 );
202 $page->header();
204 print page_title_html("Topic deleted");
205 print <<HTML;
207 <b>SOL Forum</b>: Topic deleted.<br /><br />
208 The topic $topic_name and $delete_count associated posts, have been successfully deleted.<br />\n
209 <a href="topics.pl">back to topics</a>\n
211 HTML
213 $page->footer();
217 sub review_topic {
218 my ($topic,$page) = @_;
220 my $topic_name = $topic->get_topic_name();
221 my $topic_desc = $topic->get_topic_description();
222 my $topic_id = $topic->get_forum_topic_id();
223 my $sort_order = $topic->get_topic_sort_order();
225 my $display_topic_desc = $topic->format_post_text($topic_desc);
227 my $sort_order_message = "";
228 if ( $sort_order =~ /asc/i ) {
229 $sort_order_message = "Latest post shown at the bottom.";
231 elsif ( $sort_order =~ /desc/i ) {
232 $sort_order_message = "Latest post shown at the top.";
235 $page->header();
237 print page_title_html("Review your topic");
239 print <<HTML;
240 <div class="container-fluid">
241 Steps: Configure topic -\> <b>Review topic</b> -\> Store topic<br /><br />
242 Please verify the following topic submission: <br /><br />
244 <div class="panel panel-primary"><div class="panel-body">
245 <b>Topic name</b>: $topic_name<br /><br />
246 <b>Topic Description</b>:<br />$display_topic_desc<br /><br />\n
247 <b>Other configurations</b>: $sort_order_message<br /><br />\n
248 </div></div>
249 <br />
251 <table width="100%"><tr><td align="left">
252 <form action="add_topic.pl">
253 <input type="hidden" name="action" value="edit" />
254 <input type="hidden" name="topic_id" value="$topic_id" />
255 <input type="hidden" name="subject" value="$topic_name" />
256 <input type="hidden" name="topic_description" value="$topic_desc" />
257 <input type="hidden" name="sort_order" value="$sort_order" />
258 <input class="btn btn-info" type="submit" value="Edit Topic" />
260 </form>
261 </td><td align="center">
262 <a href="topics.pl">Back to topics</a>
263 </td><td align="right">
265 <form action="add_topic.pl">
266 <input type="hidden" name="action" value="save" />
267 <input type="hidden" name="topic_id" value="$topic_id" />
268 <input type="hidden" name="subject" value="$topic_name" />
269 <input type="hidden" name="topic_description" value="$topic_desc" />
270 <input class="btn btn-primary" type="submit" value="store topic" />
272 </form>
273 </td></tr></table>
274 </div>
275 HTML
277 $page->footer();
281 sub show_form {
282 my ($dbh, $topic, $action, $user, $page) = @_;
284 my $subject = $topic->get_topic_name();
285 if ( !$subject ) { $subject = "new topic"; }
286 my $topic_description = $topic->get_topic_description();
287 my $topic_id = $topic->get_forum_topic_id();
288 my $topic_creator =
289 CXGN::People::Person->new( $dbh, $topic->get_person_id() );
290 my $topic_creator_name =
291 $topic_creator->get_first_name() . " " . $topic_creator->get_last_name();
293 if ( !$topic_creator->get_sp_person_id() ) {
294 $topic_creator_name =
295 $user->get_first_name() . " " . $user->get_last_name();
297 $page->header( "SGN | New topic",
298 "SGN Forum: Configure topic \"" . $subject . "\"" );
300 if ( !$action ) { $action = "review"; }
302 # print page_title_html("SGN Forum: Enter new topic");
304 print <<HTML;
306 <form method="post" action="add_topic.pl">
308 Steps: <b>Configure topic</b> -> Review topic -> Store topic<br /><br />
309 The owner of this topic is: <b>$topic_creator_name</b>.
310 The owner can edit and delete the topic at any time when logged in.<br />
311 HTML tags are not supported. You can add links to your post by using square brackets as follows:<br />
312 [url]sgn.cornell.edu[/url].
313 This will appear as <a href="http://sgn.cornell.edu">sgn.cornell.edu</a> in the post.<br /><br />
314 <b>Note:</b> this service is provided as a courtesy. SGN reserves the right to delete topics and posts at any time for any reason.
315 <br /><br />
316 <input type="hidden" name="action" value="$action" />
317 <input type="hidden" name="topic_id" value="$topic_id" />
318 Topic sort order: <br />
319 <input type="radio" name="sort_order" value="asc" />Latest entry at the top<br />
320 <input type="radio" name="sort_order" value="desc" checked="1" />Latest entry at the bottom<br />
321 <br />
322 Topic subject: <input class="form-control" name="subject" value="$subject"><br /><br />
323 Post Text:<br />
324 <textarea class="form-control" rows="5" cols="80" name="topic_description">$topic_description</textarea>
325 <br /><br />
327 <table width="100%" summary="">
328 <tr><td><a href="topics.pl">Back to forum</a></td>
329 <td align="right"><input class="btn btn-primary" type=submit value="Review topic" /></td>
330 </tr></table>
331 </form>
333 <br />
336 <br />
337 <br />
339 HTML
341 $page->footer();