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 under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 use CGI
::Cookie
; # need to check cookies before having CGI parse the POST request
29 use C4
::Auth
qw(:DEFAULT check_cookie_auth);
31 use C4
::Dates
qw(format_date format_date_in_iso);
33 use C4
::Output
qw(:html :ajax pagination_bar);
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 = fetch CGI
::Cookie
;
44 my $sessid = $cookies{'CGISESSID'}->value;
45 my ($auth_status, $auth_sessid) = check_cookie_auth
($sessid, $needed_flags);
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';
54 $debug and print STDERR
"AJAX request: " . Dumper
($input),
55 "\n(\$auth_status,\$auth_sessid) = ($auth_status,$auth_sessid)\n";
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";
64 if ($tag = $input->param('test')) {
65 my $check = is_approved
($tag);
66 $js_reply = ( $check >= 1 ?
'success' :
67 $check <= -1 ?
'failure' : 'indeterminate' ) . "_test('$tag');\n";
69 if ($tag = $input->param('ok')) {
70 $js_reply = ( whitelist
($operator,$tag) ?
'success' : 'failure') . "_approve('$tag');\n";
72 if ($tag = $input->param('rej')) {
73 $js_reply = ( blacklist
($operator,$tag) ?
'success' : 'failure') . "_reject('$tag');\n";
75 output_with_http_headers
$input, undef, $js_reply, 'js';
79 ### Below is the sad, boring, necessary non-AJAX HTML code.
82 my ($template, $borrowernumber, $cookie) = get_template_and_user
({
83 template_name
=> "tags/review.tmpl",
88 flagsrequired
=> $needed_flags,
91 my ($op, @errors, @tags);
93 foreach (qw( approve reject test )) {
94 $op = $_ if ( $input->param("op-$_") );
98 @tags = $input->param('tags');
100 $borrowernumber == 0 and push @errors, {op_zero
=>1};
101 if ($op eq 'approve') {
103 whitelist
($borrowernumber,$_) or push @errors, {failed_ok
=>$_};
105 } elsif ($op eq 'reject' ) {
107 blacklist
($borrowernumber,$_) or push @errors, {failed_rej
=>$_};
109 } elsif ($op eq 'test' ) {
110 my $tag = $input->param('test');
112 my $check = is_approved
($tag);
115 ( $check >= 1 ?
'verdict_ok' :
116 $check <= -1 ?
'verdict_rej' : 'verdict_indeterminate' ) => 1,
120 my $counts = &approval_counts
;
121 foreach (keys %$counts) {
122 $template->param($_ => $counts->{$_});
125 sub pagination_calc
($;$) {
126 my $query = shift or return undef;
127 my $hardlimit = (@_) ?
shift : 100; # hardcoded, could be another syspref
128 my $pagesize = $query->param('limit' ) || $hardlimit;
129 my $page = $query->param('page' ) || 1;
130 my $offset = $query->param('offset') || 0;
131 ($pagesize <= $hardlimit) or $pagesize = $hardlimit;
133 $offset = ($page-1)*$pagesize;
137 return ($pagesize,$page,$offset);
140 my ($pagesize,$page,$offset) = pagination_calc
($input,100);
143 limit
=> $offset ?
"$offset,$pagesize" : $pagesize,
144 sort => 'approved,-weight_total,+term',
146 my ($filter,$date_from,$date_to);
147 if (defined $input->param('approved')) { # 0 is valid value, must check defined
148 $filter = $input->param('approved');
152 if ($filter eq 'all') {
153 $template->param(filter_approved_all
=> 1);
154 } elsif ($filter =~ /-?[01]/) {
155 $filters{approved
} = $filter;
157 ($filter == 1 ?
'filter_approved_ok' :
158 $filter == 0 ?
'filter_approved_pending' :
159 $filter == -1 ?
'filter_approved_rej' :
160 'filter_approved') => 1
164 # my $q_count = get_approval_rows({limit=>$pagesize, sort=>'approved,-weight_total,+term', count=>1});
165 if ($filter = $input->param('tag')) {
166 $template->param(filter_tag
=>$filter);
167 $filters{term
} = $filter;
169 if ($filter = $input->param('from')) {
170 if ($date_from = format_date_in_iso
($filter)) {
171 $template->param(filter_date_approved_from
=>$filter);
172 $filters{date_approved
} = ">=$date_from";
174 push @errors, {date_from
=>$filter};
177 if ($filter = $input->param('to')) {
178 if ($date_to = format_date_in_iso
($filter)) {
179 $template->param(filter_date_approved_to
=>$filter);
180 $filters{date_approved
} = "<=$date_to";
182 push @errors, {date_to
=>$filter};
185 if ($filter = $input->param('approver')) { # name (or borrowernumber) from input box
186 if ($filter =~ /^\d+$/ and $filter > 0) {
187 # $filter=get borrowernumber from name
188 # FIXME: get borrowernumber from name not implemented.
189 $template->param(filter_approver
=>$filter);
190 $filters{approved_by
} = $filter;
192 push @errors, {approver
=>$filter};
195 if ($filter = $input->param('approved_by')) { # borrowernumber from link
196 if ($filter =~ /^\d+$/ and $filter > 0) {
197 $template->param(filter_approver
=>$filter);
198 $filters{approved_by
} = $filter;
200 push @errors, {approved_by
=>$filter};
203 $debug and print STDERR
"filters: " . Dumper
(\
%filters);
204 my $tagloop = get_approval_rows
(\
%filters);
205 my $qstring = $input->query_string;
206 $qstring =~ s/([&;])*\blimit=\d+//; # remove pagination var
207 $qstring =~ s/^;+//; # remove leading delims
208 $qstring = "limit=$pagesize" . ($qstring ?
'&' . $qstring : '');
209 $debug and print STDERR
"number of approval_rows: " . scalar(@
$tagloop) . "rows\n";
210 (scalar @errors) and $template->param(message_loop
=>\
@errors);
212 offset
=> $offset, # req'd for EXPR
214 op_count
=> scalar(@tags),
215 script_name
=> $script_name,
216 approved
=> 0, # dummy value (also EXPR)
218 pagination_bar
=> pagination_bar
(
219 "$script_name?$qstring\&",
220 ceil
($counts->{approved_total
}/$pagesize), # $page, 'page'
224 output_html_with_http_headers
$input, $cookie, $template->output;