Bug 19418: (bug 12833 follow-up) Add missing use statement
[koha.git] / tags / review.pl
blobf1484f1a87f473e637a24b4cdc6f08bae2782e39
1 #!/usr/bin/perl
3 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
5 # Copyright 2008 LibLime
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use warnings;
23 use strict;
24 use Data::Dumper;
25 use POSIX;
26 use CGI qw ( -utf8 );
27 use CGI::Cookie; # need to check cookies before having CGI parse the POST request
28 use URI::Escape;
29 use C4::Auth qw(:DEFAULT check_cookie_auth);
30 use C4::Context;
31 use Koha::DateUtils;
32 # use C4::Koha;
33 use C4::Output qw(:html :ajax pagination_bar);
34 use C4::Debug;
35 use C4::Tags qw(get_tags get_approval_rows approval_counts whitelist blacklist is_approved);
37 my $script_name = "/cgi-bin/koha/tags/review.pl";
38 my $needed_flags = { tools => 'moderate_tags' }; # FIXME: replace when more specific permission is created.
40 sub ajax_auth_cgi ($) { # returns CGI object
41 my $needed_flags = shift;
42 my %cookies = CGI::Cookie->fetch;
43 my $input = CGI->new;
44 my $sessid = $cookies{'CGISESSID'}->value;
45 my ($auth_status, $auth_sessid) = check_cookie_auth($sessid, $needed_flags);
46 $debug and
47 print STDERR "($auth_status, $auth_sessid) = check_cookie_auth($sessid," . Dumper($needed_flags) . ")\n";
48 if ($auth_status ne "ok") {
49 output_with_http_headers $input, undef,
50 "window.alert('Your CGI session cookie ($sessid) is not current. " .
51 "Please refresh the page and try again.');\n", 'js';
52 exit 0;
54 $debug and print STDERR "AJAX request: " . Dumper($input),
55 "\n(\$auth_status,\$auth_sessid) = ($auth_status,$auth_sessid)\n";
56 return $input;
59 if (is_ajax()) {
60 my $input = &ajax_auth_cgi($needed_flags);
61 my $operator = C4::Context->userenv->{'number'}; # must occur AFTER auth
62 $debug and print STDERR "op: " . Dumper($operator) . "\n";
63 my ($tag, $js_reply);
64 if ($tag = $input->param('test')) {
65 my $check = is_approved($tag);
66 $js_reply = ( $check >= 1 ? 'success' : $check <= -1 ? 'failure' : 'indeterminate' ) . "_test('".uri_escape_utf8($tag)."');\n";
68 if ($tag = $input->param('ok')) {
69 $js_reply = ( whitelist($operator,$tag) ? 'success' : 'failure') . "_approve('".uri_escape_utf8($tag)."');\n";
71 if ($tag = $input->param('rej')) {
72 $js_reply = ( blacklist($operator,$tag) ? 'success' : 'failure') . "_reject('".uri_escape_utf8($tag)."');\n";
74 output_with_http_headers $input, undef, $js_reply, 'js';
75 exit;
78 ### Below is the sad, boring, necessary non-AJAX HTML code.
80 my $input = CGI->new;
81 my ($template, $borrowernumber, $cookie) = get_template_and_user(
83 template_name => "tags/review.tt",
84 query => $input,
85 type => "intranet",
86 debug => 1,
87 authnotrequired => 0,
88 flagsrequired => $needed_flags,
92 my ($op, @errors, @tags);
94 foreach (qw( approve reject test )) {
95 $op = $_ if ( $input->param("op-$_") );
97 $op ||= 'none';
99 @tags = $input->multi_param('tags');
101 $borrowernumber == 0 and push @errors, {op_zero=>1};
102 if ($op eq 'approve') {
103 foreach (@tags) {
104 whitelist($borrowernumber,$_) or push @errors, {failed_ok=>$_};
106 } elsif ($op eq 'reject' ) {
107 foreach (@tags) {
108 blacklist($borrowernumber,$_) or push @errors, {failed_rej=>$_};
110 } elsif ($op eq 'test' ) {
111 my $tag = $input->param('test');
112 push @tags, $tag;
113 my $check = is_approved($tag);
114 $template->param(
115 test_term => $tag,
116 ( $check >= 1 ? 'verdict_ok' :
117 $check <= -1 ? 'verdict_rej' : 'verdict_indeterminate' ) => 1,
121 my $counts = &approval_counts;
122 foreach (keys %$counts) {
123 $template->param($_ => $counts->{$_});
126 sub pagination_calc ($;$) {
127 my $query = shift or return undef;
128 my $hardlimit = (@_) ? shift : 100; # hardcoded, could be another syspref
129 my $pagesize = $query->param('limit' ) || $hardlimit;
130 my $page = $query->param('page' ) || 1;
131 my $offset = $query->param('offset') || 0;
132 ($pagesize <= $hardlimit) or $pagesize = $hardlimit;
133 if ($page > 1) {
134 $offset = ($page-1)*$pagesize;
135 } else {
136 $page = 1;
138 return ($pagesize,$page,$offset);
141 my ($pagesize,$page,$offset) = pagination_calc($input,100);
143 my %filters = (
144 limit => $offset ? "$offset,$pagesize" : $pagesize,
145 sort => 'approved,-weight_total,+term',
147 my ($filter,$date_from,$date_to);
148 if (defined $input->param('approved')) { # 0 is valid value, must check defined
149 $filter = $input->param('approved');
150 } else {
151 $filter = 0;
153 if ($filter eq 'all') {
154 $template->param(filter_approved_all => 1);
155 } elsif ($filter =~ /-?[01]/) {
156 $filters{approved} = $filter;
157 $template->param(
158 ($filter == 1 ? 'filter_approved_ok' :
159 $filter == 0 ? 'filter_approved_pending' :
160 $filter == -1 ? 'filter_approved_rej' :
161 'filter_approved') => 1
165 # my $q_count = get_approval_rows({limit=>$pagesize, sort=>'approved,-weight_total,+term', count=>1});
166 if ($filter = $input->param('tag')) {
167 $template->param(filter_tag=>$filter);
168 $filters{term} = $filter;
170 if ($filter = $input->param('from')) {
171 $date_from = eval { output_pref( { dt => dt_from_string( $filter ), dateonly => 1, dateformat => 'iso' } ); };
172 if ( $date_from ) {
173 $template->param(filter_date_approved_from=>$filter);
174 $filters{date_approved} = ">=$date_from";
175 } else {
176 push @errors, {date_from=>$filter};
179 if ($filter = $input->param('to')) {
180 $date_to = eval { output_pref( { dt => dt_from_string( $filter ), dateonly => 1, dateformat => 'iso' } ); };
181 if ( $date_to ) {
182 $template->param(filter_date_approved_to=>$filter);
183 $filters{date_approved} = "<=$date_to";
184 } else {
185 push @errors, {date_to=>$filter};
188 if ($filter = $input->param('approver')) { # name (or borrowernumber) from input box
189 if ($filter =~ /^\d+$/ and $filter > 0) {
190 # $filter=get borrowernumber from name
191 # FIXME: get borrowernumber from name not implemented.
192 $template->param(filter_approver=>$filter);
193 $filters{approved_by} = $filter;
194 } else {
195 push @errors, {approver=>$filter};
198 if ($filter = $input->param('approved_by')) { # borrowernumber from link
199 if ($filter =~ /^\d+$/ and $filter > 0) {
200 $template->param(filter_approver=>$filter);
201 $filters{approved_by} = $filter;
202 } else {
203 push @errors, {approved_by=>$filter};
206 $debug and print STDERR "filters: " . Dumper(\%filters);
207 my $tagloop = get_approval_rows(\%filters);
208 my $qstring = $input->query_string;
209 $qstring =~ s/([&;])*\blimit=\d+//; # remove pagination var
210 $qstring =~ s/^;+//; # remove leading delims
211 $qstring = "limit=$pagesize" . ($qstring ? '&amp;' . $qstring : '');
212 $debug and print STDERR "number of approval_rows: " . scalar(@$tagloop) . "rows\n";
213 (scalar @errors) and $template->param(message_loop=>\@errors);
214 $template->param(
215 offset => $offset, # req'd for EXPR
216 op => $op,
217 op_count => scalar(@tags),
218 script_name => $script_name,
219 approved => 0, # dummy value (also EXPR)
220 tagloop => $tagloop,
221 pagination_bar => pagination_bar(
222 "$script_name?$qstring\&amp;",
223 ceil($counts->{approved_total}/$pagesize), # $page, 'page'
227 output_html_with_http_headers $input, $cookie, $template->output;
228 __END__
230 =head1 AUTHOR
232 Joe Atzberger
233 atz AT liblime.com
235 =cut