]> ruin.nu Git - ndwebbie.git/blobdiff - lib/NDWeb/Controller/Forum.pm
Make it possible to move a thread from the thread view
[ndwebbie.git] / lib / NDWeb / Controller / Forum.pm
index c7e8b4d7bc2464db758e8f7f99e96c74b8102f39..1a796246688d090a717e7046e02af5bb52c12e16 100644 (file)
@@ -182,33 +182,10 @@ sub board : Local {
        }
        $c->stash(threads => \@threads);
 
-       if ($board->{moderate}){
-               my $categories = $dbh->prepare(q{SELECT fcid,category FROM forum_categories ORDER BY fcid});
-               my $boards = $dbh->prepare(q{SELECT fb.fbid,fb.board, bool_or(fa.post) AS post
-                       FROM forum_boards fb NATURAL JOIN forum_access fa
-                       WHERE fb.fcid = $1 AND
-                               gid IN (SELECT groups($2))
-                       GROUP BY fb.fbid,fb.board
-                       ORDER BY fb.fbid
-               });
-               $categories->execute;
-               my @categories;
-               while (my $category = $categories->fetchrow_hashref){
-                       $boards->execute($category->{fcid},$c->stash->{UID});
-
-                       my @boards;
-                       while (my $b = $boards->fetchrow_hashref){
-                               next if (not $b->{post} or $b->{fbid} == $board->{fbid});
-                               push @boards,$b;
-                       }
-                       $category->{boards} = \@boards;
-                       push @categories,$category if @boards;
-               }
-               $c->stash(categories => \@categories);
-       }
+       $c->forward('listModeratorBoards', [$board->{fbid}]) if $board->{moderate};
+       
 }
 
-
 sub thread : Local {
        my ( $self, $c, $thread ) = @_;
        my $dbh = $c->model;
@@ -218,8 +195,16 @@ sub thread : Local {
                $c->stash(template => 'access_denied.tt2');
                return;
        }
+       my $query = $dbh->prepare(q{SELECT uid,username FROM users u
+               JOIN forum_priv_access fta USING (uid) WHERE fta.ftid = $1});
+       $query->execute($thread);
+       $c->stash(access => $query->fetchall_arrayref({}) );
        $c->forward('findPosts');
        $c->forward('markThreadAsRead') if $c->user_exists;
+       if ($c->stash->{thread}->{moderate}) {
+               $c->forward('findUsers');
+               $c->forward('listModeratorBoards', [$c->stash->{thread}->{fbid}]);
+       }
 }
 
 sub findPosts :Private {
@@ -272,7 +257,7 @@ sub markBoardAsRead : Local {
                $c->forward('markThreadAsRead',[$thread->{ftid}]);
        }
        $dbh->commit;
-       $c->res->redirect($c->req->referer);
+       $c->forward('/redirect');
 }
 
 sub markThreadAsRead : Private {
@@ -385,6 +370,94 @@ sub setSticky : Local {
        $c->res->redirect($c->uri_for('thread',$thread));
 }
 
+sub postthreadaccess : Local {
+       my ( $self, $c, $thread) = @_;
+       my $dbh = $c->model;
+
+       $c->forward('findThread');
+       $dbh->begin_work;
+       unless ($c->stash->{thread}->{moderate}){
+               $c->acl_access_denied('test',$c->action,'No moderator access to board.')
+       }
+       if ($c->req->param('access')){
+               $c->req->parameters->{access} = [$c->req->parameters->{access}]
+                       unless ref $c->req->parameters->{access} eq 'ARRAY';
+               my $query = $dbh->prepare(q{DELETE From forum_priv_access
+                       WHERE ftid = $1 AND uid = ANY ($2)});
+               $query->execute($thread,$c->req->parameters->{access});
+               $dbh->do(q{INSERT INTO forum_posts (ftid,uid,message)
+                       VALUES((SELECT ftid FROM users WHERE uid = $1),$1,$2)
+                       }, undef, $c->user->id
+                       ,"Removed access on thread $thread for : @{$c->req->parameters->{access}}");
+       }
+       if ($c->req->param('uid')){
+               $c->forward('addaccess');
+       }
+       $dbh->commit;
+       $c->res->redirect($c->uri_for('thread',$thread));
+}
+
+sub removeownthreadaccess : Local {
+       my ( $self, $c, $thread) = @_;
+       my $dbh = $c->model;
+       $dbh->do(q{DELETE FROM forum_priv_access WHERE uid = $1 AND ftid = $2}
+               ,undef,$c->user->id,$thread);
+       $c->res->redirect($c->uri_for('allUnread'));
+}
+
+sub privmsg : Local {
+       my ( $self, $c, $uid ) = @_;
+
+       $uid ||= 0;
+       $c->stash(uid => $uid);
+
+       $c->forward('findUsers');
+}
+
+sub postprivmsg : Local {
+       my ( $self, $c ) = @_;
+       my $dbh = $c->model;
+
+       $dbh->begin_work;
+       $c->forward('insertThread',[-1999]);
+
+       $c->req->parameters->{uid} = [$c->req->parameters->{uid}]
+               unless ref $c->req->parameters->{uid} eq 'ARRAY';
+       push @{$c->req->parameters->{uid}}, $c->user->id;
+       $c->forward('addaccess',[$c->stash->{thread}]);
+
+       $c->forward('addPost',[$c->stash->{thread}]);
+       $dbh->commit;
+}
+
+sub addaccess : Private {
+       my ( $self, $c, $thread) = @_;
+       my $dbh = $c->model;
+
+       $c->req->parameters->{uid} = [$c->req->parameters->{uid}]
+               unless ref $c->req->parameters->{uid} eq 'ARRAY';
+       my $query = $dbh->prepare(q{INSERT INTO forum_priv_access (ftid,uid)
+               (SELECT $1,uid FROM users u WHERE uid = ANY ($2) AND NOT uid
+                       IN (SELECT uid FROM forum_priv_access WHERE ftid = $1))});
+       $query->execute($thread,$c->req->parameters->{uid});
+       $dbh->do(q{INSERT INTO forum_posts (ftid,uid,message)
+               VALUES((SELECT ftid FROM users WHERE uid = $1),$1,$2)
+               }, undef, $c->user->id
+               ,"Gave access on thread $thread to : @{$c->req->parameters->{uid}}");
+}
+
+sub findUsers : Private {
+       my ( $self, $c ) = @_;
+       my $dbh = $c->model;
+
+       my $query = $dbh->prepare(q{SELECT uid,username FROM users
+               WHERE uid > 0 AND uid IN (SELECT uid FROM groupmembers)
+               ORDER BY LOWER(username)});
+       $query->execute;
+
+       $c->stash(users => $query->fetchall_arrayref({}) );
+}
+
 sub findThread : Private {
        my ( $self, $c, $thread ) = @_;
        my $dbh = $c->model;
@@ -442,6 +515,35 @@ sub insertPost : Private {
        $insert->execute($thread,html_escape($c->req->param('message')),$c->stash->{UID});
 }
 
+sub listModeratorBoards : Private {
+       my ( $self, $c, $fbid ) = @_;
+       my $dbh = $c->model;
+
+       my $categories = $dbh->prepare(q{SELECT fcid,category FROM forum_categories ORDER BY fcid});
+       my $boards = $dbh->prepare(q{SELECT fb.fbid,fb.board, bool_or(fa.post) AS post
+               FROM forum_boards fb NATURAL JOIN forum_access fa
+               WHERE fb.fcid = $1
+                       AND gid IN (SELECT groups($2))
+                       AND moderate
+               GROUP BY fb.fbid,fb.board
+               ORDER BY fb.fbid
+               });
+       $categories->execute;
+       my @categories;
+       while (my $category = $categories->fetchrow_hashref){
+               $boards->execute($category->{fcid},$c->stash->{UID});
+
+               my @boards;
+               while (my $b = $boards->fetchrow_hashref){
+                       next if ($b->{fbid} == $fbid);
+                       push @boards,$b;
+               }
+               $category->{boards} = \@boards;
+               push @categories,$category if @boards;
+       }
+       $c->stash(categories => \@categories);
+}
+
 =head1 AUTHOR
 
 Michael Andreen (harv@ruin.nu)