]> ruin.nu Git - ndwebbie.git/blobdiff - ND/Web/Forum.pm
only put old message in text field when previewing, not after submitting
[ndwebbie.git] / ND / Web / Forum.pm
index 8e194199413b726c261636baaaec04394c12d457..76adfb2915560654bf96268d48d6d32751204605 100644 (file)
@@ -27,18 +27,17 @@ require Exporter;
 
 our @ISA = qw/Exporter/;
 
-our @EXPORT = qw/viewForumThread addForumPost markThreadAsRead/;
+our @EXPORT = qw/viewForumThread addForumPost addForumThread markThreadAsRead/;
 
 sub viewForumThread {
        my ($thread) = @_;
 
        my $template = HTML::Template->new(filename => "templates/viewthread.tmpl", global_vars => 1, cache => 1);
 
-       $template->param(Subject => $thread->{subject});
        $template->param(Id => $thread->{id});
        $template->param(Post => $thread->{post});
 
-       my $posts = $ND::DBH->prepare(q{SELECT u.username,date_trunc('minute',fp.time::timestamp) AS time,fp.message,COALESCE(fp.time > ftv.time,TRUE) AS unread
+       my $posts = $ND::DBH->prepare(q{SELECT u.username,date_trunc('seconds',fp.time::timestamp) AS time,fp.message,COALESCE(fp.time > ftv.time,TRUE) AS unread
 FROM forum_threads ft JOIN forum_posts fp USING (ftid) JOIN users u USING (uid) LEFT OUTER JOIN (SELECT * FROM forum_thread_visits WHERE uid = $2) ftv ON ftv.ftid = ft.ftid
 WHERE ft.ftid = $1
 ORDER BY fp.time ASC
@@ -54,6 +53,13 @@ ORDER BY fp.time ASC
                $post->{message} = parseMarkup($post->{message});
                push @posts,$post;
        }
+
+       if (defined param('cmd') && param('cmd') eq 'Preview'){
+               my $text = parseMarkup(escapeHTML(param('message')));
+               $text .= p b $@ if $@;
+               push @posts,{message => $text, unread => 1, username => 'PREVIEW', Time => 'Not submitted yet', NewPosts => $old ? 1 : 0};
+               $template->param(Message => param('message'));
+       }
        $template->param(Posts => \@posts);
 
        markThreadAsRead($thread->{id});
@@ -71,6 +77,21 @@ sub addForumPost {
        return 1;
 }
 
+sub addForumThread {
+       my ($dbh,$board,$uid,$subject) = @_;
+
+       my $insert = $dbh->prepare(q{INSERT INTO forum_threads (fbid,subject) VALUES($1,$2)});
+
+       if ($insert->execute($board->{id},escapeHTML($subject))){
+               my $id = $dbh->last_insert_id(undef,undef,undef,undef,"forum_threads_ftid_seq");
+               return $dbh->selectrow_hashref(q{SELECT ftid AS id, subject, $2::boolean AS post FROM forum_threads WHERE ftid = $1}
+                       ,undef,$id,$board->{post})
+                       or $ND::ERROR .= p($dbh->errstr);
+       }else{
+               $ND::ERROR .= p($dbh->errstr);
+       }
+}
+
 sub markThreadAsRead {
        my ($thread) = @_;
        my $rows = $ND::DBH->do(q{UPDATE forum_thread_visits SET time = now()