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>.
25 use CGI::Cookie; # need to check cookies before having CGI parse the POST request
26 use URI::Escape qw( uri_escape_utf8 );
27 use C4::Auth qw( check_cookie_auth get_template_and_user );
29 use C4::Output qw( output_with_http_headers is_ajax pagination_bar output_html_with_http_headers );
38 my $script_name = "/cgi-bin/koha/tags/review.pl";
39 my $needed_flags = { tools => 'moderate_tags' }; # FIXME: replace when more specific permission is created.
41 sub ajax_auth_cgi { # returns CGI object
42 my $needed_flags = shift;
43 my %cookies = CGI::Cookie->fetch;
45 my $sessid = $cookies{'CGISESSID'}->value;
46 my ($auth_status) = check_cookie_auth($sessid, $needed_flags);
47 if ($auth_status ne "ok") {
48 output_with_http_headers $input, undef,
49 "window.alert('Your CGI session cookie ($sessid) is not current. " .
50 "Please refresh the page and try again.');\n", 'js';
57 my $input = &ajax_auth_cgi($needed_flags);
58 my $operator = C4::Context->userenv->{'number'}; # must occur AFTER auth
60 if ($tag = $input->param('test')) {
61 my $check = is_approved($tag);
62 $js_reply = ( $check >= 1 ? 'success' : $check <= -1 ? 'failure' : 'indeterminate' ) . "_test('".uri_escape_utf8($tag)."');\n";
64 if ($tag = $input->param('ok')) {
65 $js_reply = ( whitelist($operator,$tag) ? 'success' : 'failure') . "_approve('".uri_escape_utf8($tag)."');\n";
67 if ($tag = $input->param('rej')) {
68 $js_reply = ( blacklist($operator,$tag) ? 'success' : 'failure') . "_reject('".uri_escape_utf8($tag)."');\n";
70 output_with_http_headers $input, undef, $js_reply, 'js';
74 ### Below is the sad, boring, necessary non-AJAX HTML code.
77 my ($template, $borrowernumber, $cookie) = get_template_and_user(
79 template_name => "tags/review.tt",
82 flagsrequired => $needed_flags,
86 my ($op, @errors, @tags);
88 foreach (qw( approve reject test )) {
89 $op = $_ if ( $input->param("op-$_") );
93 @tags = $input->multi_param('tags');
95 $borrowernumber == 0 and push @errors, {op_zero=>1};
96 if ($op eq 'approve') {
98 whitelist($borrowernumber,$_) or push @errors, {failed_ok=>$_};
100 } elsif ($op eq 'reject' ) {
102 blacklist($borrowernumber,$_) or push @errors, {failed_rej=>$_};
104 } elsif ($op eq 'test' ) {
105 my $tag = $input->param('test');
107 my $check = is_approved($tag);
110 ( $check >= 1 ? 'verdict_ok' :
111 $check <= -1 ? 'verdict_rej' : 'verdict_indeterminate' ) => 1,
115 my $counts = &approval_counts;
116 foreach (keys %$counts) {
117 $template->param($_ => $counts->{$_});
120 sub pagination_calc {
121 my $query = shift or return;
122 my $hardlimit = (@_) ? shift : 100; # hardcoded, could be another syspref
123 my $pagesize = $query->param('limit' ) || $hardlimit;
124 my $page = $query->param('page' ) || 1;
125 my $offset = $query->param('offset') || 0;
126 ($pagesize <= $hardlimit) or $pagesize = $hardlimit;
128 $offset = ($page-1)*$pagesize;
132 return ($pagesize,$page,$offset);
135 my ($pagesize,$page,$offset) = pagination_calc($input,100);
138 limit => $offset ? "$offset,$pagesize" : $pagesize,
139 sort => 'approved,-weight_total,+term',
141 my ($filter,$date_from,$date_to);
142 if (defined $input->param('approved')) { # 0 is valid value, must check defined
143 $filter = $input->param('approved');
147 if ($filter eq 'all') {
148 $template->param(filter_approved_all => 1);
149 } elsif ($filter =~ /-?[01]/) {
150 $filters{approved} = $filter;
152 ($filter == 1 ? 'filter_approved_ok' :
153 $filter == 0 ? 'filter_approved_pending' :
154 $filter == -1 ? 'filter_approved_rej' :
155 'filter_approved') => 1
159 # my $q_count = get_approval_rows({limit=>$pagesize, sort=>'approved,-weight_total,+term', count=>1});
160 if ($filter = $input->param('tag')) {
161 $template->param(filter_tag=>$filter);
162 $filters{term} = $filter;
164 if ( $filter = $input->param('from') ) {
165 $date_from = $filter;
166 $template->param( filter_date_approved_from => $filter );
167 $filters{date_approved} = ">=$date_from";
169 if ( $filter = $input->param('to') ) {
171 $template->param( filter_date_approved_to => $filter );
172 $filters{date_approved} = "<=$date_to";
174 if ($filter = $input->param('approver')) { # name (or borrowernumber) from input box
175 if ($filter =~ /^\d+$/ and $filter > 0) {
176 # $filter=get borrowernumber from name
177 # FIXME: get borrowernumber from name not implemented.
178 $template->param(filter_approver=>$filter);
179 $filters{approved_by} = $filter;
181 push @errors, {approver=>$filter};
184 if ($filter = $input->param('approved_by')) { # borrowernumber from link
185 if ($filter =~ /^\d+$/ and $filter > 0) {
186 $template->param(filter_approver=>$filter);
187 $filters{approved_by} = $filter;
189 push @errors, {approved_by=>$filter};
192 my $tagloop = get_approval_rows(\%filters);
193 my $qstring = $input->query_string;
194 $qstring =~ s/([&;])*\blimit=\d+//; # remove pagination var
195 $qstring =~ s/^;+//; # remove leading delims
196 $qstring = "limit=$pagesize" . ($qstring ? '&' . $qstring : '');
197 (scalar @errors) and $template->param(message_loop=>\@errors);
199 offset => $offset, # req'd for EXPR
201 op_count => scalar(@tags),
202 script_name => $script_name,
203 approved => 0, # dummy value (also EXPR)
205 pagination_bar => pagination_bar(
206 "$script_name?$qstring\&",
207 ceil($counts->{approved_total}/$pagesize), # $page, 'page'
211 output_html_with_http_headers $input, $cookie, $template->output;