]> git.sesse.net Git - pr0n/blob - perl/Sesse/pr0n/Select.pm
Make sure rotation and selection invalidate the listing cache.
[pr0n] / perl / Sesse / pr0n / Select.pm
1 package Sesse::pr0n::Select;
2 use strict;
3 use warnings;
4
5 use Sesse::pr0n::Common qw(error dberror);
6 use Apache2::Request;
7
8 sub handler {
9         my $r = shift;
10         my $apr = Apache2::Request->new($r);
11         my $dbh = Sesse::pr0n::Common::get_dbh();
12         my ($user, $takenby) = Sesse::pr0n::Common::check_access($r);
13         if (!defined($user)) {
14                 return Apache2::Const::OK;
15         }
16
17         my $event = $apr->param('event');
18
19         Sesse::pr0n::Common::header($r, "Selection results");
20
21         {
22                 # Enable transactions and error raising temporarily
23                 local $dbh->{AutoCommit} = 0;
24                 local $dbh->{RaiseError} = 1;
25
26                 if (defined($apr->param('mode')) && $apr->param('mode') eq 'single') {
27                         # single mode; enable one (FIXME: need to support disable too)
28                         my $filename = $apr->param('filename');
29                         $dbh->do('UPDATE images SET selected=\'t\' WHERE event=? AND filename=?', undef, $event, $filename);
30                 } else {
31                         # traditional multi-mode
32                         $dbh->do('UPDATE images SET selected=\'f\' WHERE event=?', undef, $event);
33                 
34                         my @params = $apr->param();
35                         my $key;
36                         for $key (@params) {
37                                 if ($key =~ /^sel-(\d+)/ && $apr->param($key) eq 'on') {
38                                         my $id = $1;
39                                         my $q = $dbh->do('UPDATE images SET selected=\'t\' WHERE id=?', undef, $id)
40                                                 or dberror($r, "Selection of $id failed: $!");
41                                         $r->print("    <p>Selected image ID `$id'.</p>\n");
42                                 }
43                         }
44                 }
45         }
46
47         $dbh->do('UPDATE events SET last_update=CURRENT_TIMESTAMP WHERE id=?', undef, $event)
48                 or dberror($r, "Cache invalidation failed");
49
50         Sesse::pr0n::Common::footer($r);
51
52         return Apache2::Const::OK;
53 }
54
55 1;
56
57