Bug 17062 - debian/control.in update: change maintainer
[koha.git] / tags / review.pl
blobb7e959b69bbf05e3ee4b63ca10ccd03fa2eb3251
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
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' :
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';
76 exit;
79 ### Below is the sad, boring, necessary non-AJAX HTML code.
81 my $input = CGI->new;
82 my ($template, $borrowernumber, $cookie) = get_template_and_user(
84 template_name => "tags/review.tt",
85 query => $input,
86 type => "intranet",
87 debug => 1,
88 authnotrequired => 0,
89 flagsrequired => $needed_flags,
93 my ($op, @errors, @tags);
95 foreach (qw( approve reject test )) {
96 $op = $_ if ( $input->param("op-$_") );
98 $op ||= 'none';
100 @tags = $input->multi_param('tags');
102 $borrowernumber == 0 and push @errors, {op_zero=>1};
103 if ($op eq 'approve') {
104 foreach (@tags) {
105 whitelist($borrowernumber,$_) or push @errors, {failed_ok=>$_};
107 } elsif ($op eq 'reject' ) {
108 foreach (@tags) {
109 blacklist($borrowernumber,$_) or push @errors, {failed_rej=>$_};
111 } elsif ($op eq 'test' ) {
112 my $tag = $input->param('test');
113 push @tags, $tag;
114 my $check = is_approved($tag);
115 $template->param(
116 test_term => $tag,
117 ( $check >= 1 ? 'verdict_ok' :
118 $check <= -1 ? 'verdict_rej' : 'verdict_indeterminate' ) => 1,
122 my $counts = &approval_counts;
123 foreach (keys %$counts) {
124 $template->param($_ => $counts->{$_});
127 sub pagination_calc ($;$) {
128 my $query = shift or return undef;
129 my $hardlimit = (@_) ? shift : 100; # hardcoded, could be another syspref
130 my $pagesize = $query->param('limit' ) || $hardlimit;
131 my $page = $query->param('page' ) || 1;
132 my $offset = $query->param('offset') || 0;
133 ($pagesize <= $hardlimit) or $pagesize = $hardlimit;
134 if ($page > 1) {
135 $offset = ($page-1)*$pagesize;
136 } else {
137 $page = 1;
139 return ($pagesize,$page,$offset);
142 my ($pagesize,$page,$offset) = pagination_calc($input,100);
144 my %filters = (
145 limit => $offset ? "$offset,$pagesize" : $pagesize,
146 sort => 'approved,-weight_total,+term',
148 my ($filter,$date_from,$date_to);
149 if (defined $input->param('approved')) { # 0 is valid value, must check defined
150 $filter = $input->param('approved');
151 } else {
152 $filter = 0;
154 if ($filter eq 'all') {
155 $template->param(filter_approved_all => 1);
156 } elsif ($filter =~ /-?[01]/) {
157 $filters{approved} = $filter;
158 $template->param(
159 ($filter == 1 ? 'filter_approved_ok' :
160 $filter == 0 ? 'filter_approved_pending' :
161 $filter == -1 ? 'filter_approved_rej' :
162 'filter_approved') => 1
166 # my $q_count = get_approval_rows({limit=>$pagesize, sort=>'approved,-weight_total,+term', count=>1});
167 if ($filter = $input->param('tag')) {
168 $template->param(filter_tag=>$filter);
169 $filters{term} = $filter;
171 if ($filter = $input->param('from')) {
172 $date_from = eval { output_pref( { dt => dt_from_string( $filter ), dateonly => 1, dateformat => 'iso' } ); };
173 if ( $date_from ) {
174 $template->param(filter_date_approved_from=>$filter);
175 $filters{date_approved} = ">=$date_from";
176 } else {
177 push @errors, {date_from=>$filter};
180 if ($filter = $input->param('to')) {
181 $date_to = eval { output_pref( { dt => dt_from_string( $filter ), dateonly => 1, dateformat => 'iso' } ); };
182 if ( $date_to ) {
183 $template->param(filter_date_approved_to=>$filter);
184 $filters{date_approved} = "<=$date_to";
185 } else {
186 push @errors, {date_to=>$filter};
189 if ($filter = $input->param('approver')) { # name (or borrowernumber) from input box
190 if ($filter =~ /^\d+$/ and $filter > 0) {
191 # $filter=get borrowernumber from name
192 # FIXME: get borrowernumber from name not implemented.
193 $template->param(filter_approver=>$filter);
194 $filters{approved_by} = $filter;
195 } else {
196 push @errors, {approver=>$filter};
199 if ($filter = $input->param('approved_by')) { # borrowernumber from link
200 if ($filter =~ /^\d+$/ and $filter > 0) {
201 $template->param(filter_approver=>$filter);
202 $filters{approved_by} = $filter;
203 } else {
204 push @errors, {approved_by=>$filter};
207 $debug and print STDERR "filters: " . Dumper(\%filters);
208 my $tagloop = get_approval_rows(\%filters);
209 my $qstring = $input->query_string;
210 $qstring =~ s/([&;])*\blimit=\d+//; # remove pagination var
211 $qstring =~ s/^;+//; # remove leading delims
212 $qstring = "limit=$pagesize" . ($qstring ? '&amp;' . $qstring : '');
213 $debug and print STDERR "number of approval_rows: " . scalar(@$tagloop) . "rows\n";
214 (scalar @errors) and $template->param(message_loop=>\@errors);
215 $template->param(
216 offset => $offset, # req'd for EXPR
217 op => $op,
218 op_count => scalar(@tags),
219 script_name => $script_name,
220 approved => 0, # dummy value (also EXPR)
221 tagloop => $tagloop,
222 pagination_bar => pagination_bar(
223 "$script_name?$qstring\&amp;",
224 ceil($counts->{approved_total}/$pagesize), # $page, 'page'
228 output_html_with_http_headers $input, $cookie, $template->output;
229 __END__
231 =head1 AUTHOR
233 Joe Atzberger
234 atz AT liblime.com
236 =cut