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 Koha::DateUtils qw( dt_from_string output_pref );
30 use C4::Output qw( output_with_http_headers is_ajax pagination_bar output_html_with_http_headers );
39 my $script_name = "/cgi-bin/koha/tags/review.pl";
40 my $needed_flags = { tools => 'moderate_tags' }; # FIXME: replace when more specific permission is created.
42 sub ajax_auth_cgi { # returns CGI object
43 my $needed_flags = shift;
44 my %cookies = CGI::Cookie->fetch;
46 my $sessid = $cookies{'CGISESSID'}->value;
47 my ($auth_status) = check_cookie_auth($sessid, $needed_flags);
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';
58 my $input = &ajax_auth_cgi($needed_flags);
59 my $operator = C4::Context->userenv->{'number'}; # must occur AFTER auth
61 if ($tag = $input->param('test')) {
62 my $check = is_approved($tag);
63 $js_reply = ( $check >= 1 ? 'success' : $check <= -1 ? 'failure' : 'indeterminate' ) . "_test('".uri_escape_utf8($tag)."');\n";
65 if ($tag = $input->param('ok')) {
66 $js_reply = ( whitelist($operator,$tag) ? 'success' : 'failure') . "_approve('".uri_escape_utf8($tag)."');\n";
68 if ($tag = $input->param('rej')) {
69 $js_reply = ( blacklist($operator,$tag) ? 'success' : 'failure') . "_reject('".uri_escape_utf8($tag)."');\n";
71 output_with_http_headers $input, undef, $js_reply, 'js';
75 ### Below is the sad, boring, necessary non-AJAX HTML code.
78 my ($template, $borrowernumber, $cookie) = get_template_and_user(
80 template_name => "tags/review.tt",
83 flagsrequired => $needed_flags,
87 my ($op, @errors, @tags);
89 foreach (qw( approve reject test )) {
90 $op = $_ if ( $input->param("op-$_") );
94 @tags = $input->multi_param('tags');
96 $borrowernumber == 0 and push @errors, {op_zero=>1};
97 if ($op eq 'approve') {
99 whitelist($borrowernumber,$_) or push @errors, {failed_ok=>$_};
101 } elsif ($op eq 'reject' ) {
103 blacklist($borrowernumber,$_) or push @errors, {failed_rej=>$_};
105 } elsif ($op eq 'test' ) {
106 my $tag = $input->param('test');
108 my $check = is_approved($tag);
111 ( $check >= 1 ? 'verdict_ok' :
112 $check <= -1 ? 'verdict_rej' : 'verdict_indeterminate' ) => 1,
116 my $counts = &approval_counts;
117 foreach (keys %$counts) {
118 $template->param($_ => $counts->{$_});
121 sub pagination_calc {
122 my $query = shift or return;
123 my $hardlimit = (@_) ? shift : 100; # hardcoded, could be another syspref
124 my $pagesize = $query->param('limit' ) || $hardlimit;
125 my $page = $query->param('page' ) || 1;
126 my $offset = $query->param('offset') || 0;
127 ($pagesize <= $hardlimit) or $pagesize = $hardlimit;
129 $offset = ($page-1)*$pagesize;
133 return ($pagesize,$page,$offset);
136 my ($pagesize,$page,$offset) = pagination_calc($input,100);
139 limit => $offset ? "$offset,$pagesize" : $pagesize,
140 sort => 'approved,-weight_total,+term',
142 my ($filter,$date_from,$date_to);
143 if (defined $input->param('approved')) { # 0 is valid value, must check defined
144 $filter = $input->param('approved');
148 if ($filter eq 'all') {
149 $template->param(filter_approved_all => 1);
150 } elsif ($filter =~ /-?[01]/) {
151 $filters{approved} = $filter;
153 ($filter == 1 ? 'filter_approved_ok' :
154 $filter == 0 ? 'filter_approved_pending' :
155 $filter == -1 ? 'filter_approved_rej' :
156 'filter_approved') => 1
160 # my $q_count = get_approval_rows({limit=>$pagesize, sort=>'approved,-weight_total,+term', count=>1});
161 if ($filter = $input->param('tag')) {
162 $template->param(filter_tag=>$filter);
163 $filters{term} = $filter;
165 if ($filter = $input->param('from')) {
166 $date_from = eval { output_pref( { dt => dt_from_string( $filter ), dateonly => 1, dateformat => 'iso' } ); };
168 $template->param(filter_date_approved_from=>$filter);
169 $filters{date_approved} = ">=$date_from";
171 push @errors, {date_from=>$filter};
174 if ($filter = $input->param('to')) {
175 $date_to = eval { output_pref( { dt => dt_from_string( $filter ), dateonly => 1, dateformat => 'iso' } ); };
177 $template->param(filter_date_approved_to=>$filter);
178 $filters{date_approved} = "<=$date_to";
180 push @errors, {date_to=>$filter};
183 if ($filter = $input->param('approver')) { # name (or borrowernumber) from input box
184 if ($filter =~ /^\d+$/ and $filter > 0) {
185 # $filter=get borrowernumber from name
186 # FIXME: get borrowernumber from name not implemented.
187 $template->param(filter_approver=>$filter);
188 $filters{approved_by} = $filter;
190 push @errors, {approver=>$filter};
193 if ($filter = $input->param('approved_by')) { # borrowernumber from link
194 if ($filter =~ /^\d+$/ and $filter > 0) {
195 $template->param(filter_approver=>$filter);
196 $filters{approved_by} = $filter;
198 push @errors, {approved_by=>$filter};
201 my $tagloop = get_approval_rows(\%filters);
202 my $qstring = $input->query_string;
203 $qstring =~ s/([&;])*\blimit=\d+//; # remove pagination var
204 $qstring =~ s/^;+//; # remove leading delims
205 $qstring = "limit=$pagesize" . ($qstring ? '&' . $qstring : '');
206 (scalar @errors) and $template->param(message_loop=>\@errors);
208 offset => $offset, # req'd for EXPR
210 op_count => scalar(@tags),
211 script_name => $script_name,
212 approved => 0, # dummy value (also EXPR)
214 pagination_bar => pagination_bar(
215 "$script_name?$qstring\&",
216 ceil($counts->{approved_total}/$pagesize), # $page, 'page'
220 output_html_with_http_headers $input, $cookie, $template->output;