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 my $op = $input->param('op') || q{};
61 my $tag = $input->param('tag');
63 my $check = is_approved($tag);
64 $js_reply = ( $check >= 1 ? 'success' : $check <= -1 ? 'failure' : 'indeterminate' ) . "_test('".uri_escape_utf8($tag)."');\n";
66 elsif ($op eq 'cud-approve') {
67 $js_reply = ( whitelist($operator,$tag) ? 'success' : 'failure') . "_approve('".uri_escape_utf8($tag)."');\n";
69 elsif ($op eq 'cud-reject') {
70 $js_reply = ( blacklist($operator,$tag) ? 'success' : 'failure') . "_reject('".uri_escape_utf8($tag)."');\n";
72 output_with_http_headers $input, undef, $js_reply, 'js';
76 ### Below is the sad, boring, necessary non-AJAX HTML code.
79 my ($template, $borrowernumber, $cookie) = get_template_and_user(
81 template_name => "tags/review.tt",
84 flagsrequired => $needed_flags,
90 my $op = $input->param('op') || q{};
92 @tags = $input->multi_param('tags');
94 if ($op eq 'cud-approve') {
96 whitelist($borrowernumber,$_) or push @errors, {failed_ok=>$_};
98 } elsif ($op eq 'cud-reject' ) {
100 blacklist($borrowernumber,$_) or push @errors, {failed_rej=>$_};
102 } elsif ($op eq 'test' ) {
103 my $tag = $input->param('test');
105 my $check = is_approved($tag);
108 ( $check >= 1 ? 'verdict_ok' :
109 $check <= -1 ? 'verdict_rej' : 'verdict_indeterminate' ) => 1,
113 my $counts = &approval_counts;
114 foreach (keys %$counts) {
115 $template->param($_ => $counts->{$_});
118 sub pagination_calc {
119 my $query = shift or return;
120 my $hardlimit = (@_) ? shift : 100; # hardcoded, could be another syspref
121 my $pagesize = $query->param('limit' ) || $hardlimit;
122 my $page = $query->param('page' ) || 1;
123 my $offset = $query->param('offset') || 0;
124 ($pagesize <= $hardlimit) or $pagesize = $hardlimit;
126 $offset = ($page-1)*$pagesize;
130 return ($pagesize,$page,$offset);
133 my ($pagesize,$page,$offset) = pagination_calc($input,100);
136 limit => $offset ? "$offset,$pagesize" : $pagesize,
137 sort => 'approved,-weight_total,+term',
139 my ($filter,$date_from,$date_to);
140 if (defined $input->param('approved')) { # 0 is valid value, must check defined
141 $filter = $input->param('approved');
145 if ($filter eq 'all') {
146 $template->param(filter_approved_all => 1);
147 } elsif ($filter =~ /-?[01]/) {
148 $filters{approved} = $filter;
150 ($filter == 1 ? 'filter_approved_ok' :
151 $filter == 0 ? 'filter_approved_pending' :
152 $filter == -1 ? 'filter_approved_rej' :
153 'filter_approved') => 1
157 # my $q_count = get_approval_rows({limit=>$pagesize, sort=>'approved,-weight_total,+term', count=>1});
158 if ($filter = $input->param('tag')) {
159 $template->param(filter_tag=>$filter);
160 $filters{term} = $filter;
162 if ( $filter = $input->param('from') ) {
163 $date_from = $filter;
164 $template->param( filter_date_approved_from => $filter );
165 $filters{date_approved} = ">=$date_from";
167 if ( $filter = $input->param('to') ) {
169 $template->param( filter_date_approved_to => $filter );
170 $filters{date_approved} = "<=$date_to";
172 if ($filter = $input->param('approver')) { # name (or borrowernumber) from input box
173 if ($filter =~ /^\d+$/ and $filter > 0) {
174 # $filter=get borrowernumber from name
175 # FIXME: get borrowernumber from name not implemented.
176 $template->param(filter_approver=>$filter);
177 $filters{approved_by} = $filter;
179 push @errors, {approver=>$filter};
182 if ($filter = $input->param('approved_by')) { # borrowernumber from link
183 if ($filter =~ /^\d+$/ and $filter > 0) {
184 $template->param(filter_approver=>$filter);
185 $filters{approved_by} = $filter;
187 push @errors, {approved_by=>$filter};
190 my $tagloop = get_approval_rows(\%filters);
191 my $qstring = $input->query_string;
192 $qstring =~ s/([&;])*\blimit=\d+//; # remove pagination var
193 $qstring =~ s/^;+//; # remove leading delims
194 $qstring = "limit=$pagesize" . ($qstring ? '&' . $qstring : '');
195 (scalar @errors) and $template->param(message_loop=>\@errors);
197 offset => $offset, # req'd for EXPR
199 op_count => scalar(@tags),
200 script_name => $script_name,
201 approved => 0, # dummy value (also EXPR)
203 pagination_bar => pagination_bar(
204 "$script_name?$qstring\&",
205 ceil($counts->{approved_total}/$pagesize), # $page, 'page'
209 output_html_with_http_headers $input, $cookie, $template->output;