| 1 | package Vee::Utils::Forum; |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | |
|---|
| 6 | use Exporter 'import'; |
|---|
| 7 | our @EXPORT = qw/can_post can_thread toggleflag/; |
|---|
| 8 | |
|---|
| 9 | =head1 NAME |
|---|
| 10 | |
|---|
| 11 | Vee::Utils::Forum - Forum utilities |
|---|
| 12 | |
|---|
| 13 | =head1 SYNOPSIS |
|---|
| 14 | |
|---|
| 15 | Coming soon |
|---|
| 16 | |
|---|
| 17 | =head1 DESCRIPTION |
|---|
| 18 | |
|---|
| 19 | Currently, this file just contains shortcuts for checking posting permissions. |
|---|
| 20 | It may later be replaced, merged elsewhere, removed, or mauled by Latiases. |
|---|
| 21 | |
|---|
| 22 | =head1 FUNCTIONS |
|---|
| 23 | |
|---|
| 24 | =head2 can_post |
|---|
| 25 | |
|---|
| 26 | Returns true if the current user can post on the given thread, false otherwise. |
|---|
| 27 | |
|---|
| 28 | =cut |
|---|
| 29 | |
|---|
| 30 | sub 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 | |
|---|
| 44 | Returns true if the current user can create threads in the given forum, false |
|---|
| 45 | otherwise. |
|---|
| 46 | |
|---|
| 47 | =cut |
|---|
| 48 | |
|---|
| 49 | sub 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 | |
|---|
| 60 | Adds or removes a flag for a thread/post. |
|---|
| 61 | |
|---|
| 62 | NOTE: This should work for toggling multiple flags *only* if you |
|---|
| 63 | pass $flag in the same order as it is in the SET(). |
|---|
| 64 | PS: SET() is useful, but retarded (hence all the regexes). |
|---|
| 65 | |
|---|
| 66 | =cut |
|---|
| 67 | |
|---|
| 68 | sub 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 | |
|---|
| 87 | Maintainer: Alex "Eevee" Munroe (C<veekun@veekun.com>) |
|---|
| 88 | |
|---|
| 89 | See the included F<AUTHORS> file for a full list of contributers. |
|---|
| 90 | |
|---|
| 91 | =head1 LICENSE |
|---|
| 92 | |
|---|
| 93 | See the included F<LICENSE> file. |
|---|
| 94 | |
|---|
| 95 | =cut |
|---|
| 96 | |
|---|
| 97 | 1; |
|---|