root/veekun/trunk/lib/Vee/Utils/Forum.pm

Revision 326, 2.2 KB (checked in by latiass, 3 years ago)

Added post editing. This is still lacking in a list of edits, but that will come soon. Worked fine for me, but please report any problems you have. Also majorly refactored post utilities. May or may not work right, please try to avoid deleting until I can confirm.

Line 
1package Vee::Utils::Forum;
2
3use strict;
4use warnings;
5
6use Exporter 'import';
7our @EXPORT = qw/can_post can_thread toggleflag/;
8
9=head1 NAME
10
11Vee::Utils::Forum - Forum utilities
12
13=head1 SYNOPSIS
14
15Coming soon
16
17=head1 DESCRIPTION
18
19Currently, this file just contains shortcuts for checking posting permissions.
20It may later be replaced, merged elsewhere, removed, or mauled by Latiases.
21
22=head1 FUNCTIONS
23
24=head2 can_post
25
26Returns true if the current user can post on the given thread, false otherwise.
27
28=cut
29
30sub can_post {
31    my ($c, $thread) = @_;
32
33    return undef if $thread->flags =~ /locked/
34        and not $c->can_i(override_thread_locked => $thread->forum->id);
35
36    return undef if Vee::Utils::in( $thread->forum->accessibility => qw/archive hidden/ )
37        and not $c->can_i('override_forum_' . $thread->forum->accessibility => $thread->forum->id);
38
39    return $c->can_i(create_post => $thread->id);
40}
41
42=head2 can_thread
43
44Returns true if the current user can create threads in the given forum, false
45otherwise.
46
47=cut
48
49sub can_thread {
50    my ($c, $forum) = @_;
51
52    return undef if $forum->accessibility ne 'normal'
53        and not $c->can_i('override_forum_' . $forum->accessibility => $forum->id);
54
55    return $c->can_i(create_thread => $forum->id);
56}
57
58=head2 toggleflag
59
60Adds or removes a flag for a thread/post.
61
62NOTE: This should work for toggling multiple flags *only* if you
63pass $flag in the same order as it is in the SET().
64PS: SET() is useful, but retarded (hence all the regexes).
65
66=cut
67
68sub toggleflag {
69    my ($object, $flag) = @_;
70       
71        return undef unless defined ($object && $flag);
72        if ($object->flags =~ /$flag/) {
73            # flag matched, unset it
74                if ($object->flags =~ /,$flag/) { $flag =~ s/$flag/,$flag/g }
75                elsif ($object->flags =~ /$flag,/) { $flag =~ s/$flag/$flag,/g }
76                $object->flags(\"REPLACE(flags, '$flag', '')");
77        } else {
78            # flag not matched, set it
79                $flag = ",$flag" if $object->flags;
80                $object->flags(\"CONCAT(flags, '$flag')");
81        }
82        $object->update;
83}
84
85=head1 AUTHOR
86
87Maintainer: Alex "Eevee" Munroe (C<veekun@veekun.com>)
88
89See the included F<AUTHORS> file for a full list of contributers.
90
91=head1 LICENSE
92
93See the included F<LICENSE> file.
94
95=cut
96
971;
Note: See TracBrowser for help on using the browser.