Bug 22818: (QA follow-up) Translation fix + Branch notices
[koha.git] / tags / review.pl
blob3acf28a9dc10227349deb6bcfca0e1021ca62fc7
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 Modern::Perl;
23 use Data::Dumper;
24 use POSIX;
25 use CGI qw ( -utf8 );
26 use CGI::Cookie; # need to check cookies before having CGI parse the POST request
27 use URI::Escape;
28 use C4::Auth qw(:DEFAULT check_cookie_auth);
29 use C4::Context;
30 use Koha::DateUtils;
31 # use C4::Koha;
32 use C4::Output qw(:html :ajax pagination_bar);
33 use C4::Debug;
34 use C4::Tags qw(get_tags get_approval_rows approval_counts whitelist blacklist is_approved);
36 my $script_name = "/cgi-bin/koha/tags/review.pl";
37 my $needed_flags = { tools => 'moderate_tags' }; # FIXME: replace when more specific permission is created.
39 sub ajax_auth_cgi { # returns CGI object
40 my $needed_flags = shift;
41 my %cookies = CGI::Cookie->fetch;
42 my $input = CGI->new;
43 my $sessid = $cookies{'CGISESSID'}->value;
44 my ($auth_status, $auth_sessid) = check_cookie_auth($sessid, $needed_flags);
45 $debug and
46 print STDERR "($auth_status, $auth_sessid) = check_cookie_auth($sessid," . Dumper($needed_flags) . ")\n";
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';
51 exit 0;
53 $debug and print STDERR "AJAX request: " . Dumper($input),
54 "\n(\$auth_status,\$auth_sessid) = ($auth_status,$auth_sessid)\n";
55 return $input;
58 if (is_ajax()) {
59 my $input = &ajax_auth_cgi($needed_flags);
60 my $operator = C4::Context->userenv->{'number'}; # must occur AFTER auth
61 $debug and print STDERR "op: " . Dumper($operator) . "\n";
62 my ($tag, $js_reply);
63 if ($tag = $input->param('test')) {
64 my $check = is_approved($tag);
65 $js_reply = ( $check >= 1 ? 'success' : $check <= -1 ? 'failure' : 'indeterminate' ) . "_test('".uri_escape_utf8($tag)."');\n";
67 if ($tag = $input->param('ok')) {
68 $js_reply = ( whitelist($operator,$tag) ? 'success' : 'failure') . "_approve('".uri_escape_utf8($tag)."');\n";
70 if ($tag = $input->param('rej')) {
71 $js_reply = ( blacklist($operator,$tag) ? 'success' : 'failure') . "_reject('".uri_escape_utf8($tag)."');\n";
73 output_with_http_headers $input, undef, $js_reply, 'js';
74 exit;
77 ### Below is the sad, boring, necessary non-AJAX HTML code.
79 my $input = CGI->new;
80 my ($template, $borrowernumber, $cookie) = get_template_and_user(
82 template_name => "tags/review.tt",
83 query => $input,
84 type => "intranet",
85 debug => 1,
86 flagsrequired => $needed_flags,
90 my ($op, @errors, @tags);
92 foreach (qw( approve reject test )) {
93 $op = $_ if ( $input->param("op-$_") );
95 $op ||= 'none';
97 @tags = $input->multi_param('tags');
99 $borrowernumber == 0 and push @errors, {op_zero=>1};
100 if ($op eq 'approve') {
101 foreach (@tags) {
102 whitelist($borrowernumber,$_) or push @errors, {failed_ok=>$_};
104 } elsif ($op eq 'reject' ) {
105 foreach (@tags) {
106 blacklist($borrowernumber,$_) or push @errors, {failed_rej=>$_};
108 } elsif ($op eq 'test' ) {
109 my $tag = $input->param('test');
110 push @tags, $tag;
111 my $check = is_approved($tag);
112 $template->param(
113 test_term => $tag,
114 ( $check >= 1 ? 'verdict_ok' :
115 $check <= -1 ? 'verdict_rej' : 'verdict_indeterminate' ) => 1,
119 my $counts = &approval_counts;
120 foreach (keys %$counts) {
121 $template->param($_ => $counts->{$_});
124 sub pagination_calc {
125 my $query = shift or return;
126 my $hardlimit = (@_) ? shift : 100; # hardcoded, could be another syspref
127 my $pagesize = $query->param('limit' ) || $hardlimit;
128 my $page = $query->param('page' ) || 1;
129 my $offset = $query->param('offset') || 0;
130 ($pagesize <= $hardlimit) or $pagesize = $hardlimit;
131 if ($page > 1) {
132 $offset = ($page-1)*$pagesize;
133 } else {
134 $page = 1;
136 return ($pagesize,$page,$offset);
139 my ($pagesize,$page,$offset) = pagination_calc($input,100);
141 my %filters = (
142 limit => $offset ? "$offset,$pagesize" : $pagesize,
143 sort => 'approved,-weight_total,+term',
145 my ($filter,$date_from,$date_to);
146 if (defined $input->param('approved')) { # 0 is valid value, must check defined
147 $filter = $input->param('approved');
148 } else {
149 $filter = 0;
151 if ($filter eq 'all') {
152 $template->param(filter_approved_all => 1);
153 } elsif ($filter =~ /-?[01]/) {
154 $filters{approved} = $filter;
155 $template->param(
156 ($filter == 1 ? 'filter_approved_ok' :
157 $filter == 0 ? 'filter_approved_pending' :
158 $filter == -1 ? 'filter_approved_rej' :
159 'filter_approved') => 1
163 # my $q_count = get_approval_rows({limit=>$pagesize, sort=>'approved,-weight_total,+term', count=>1});
164 if ($filter = $input->param('tag')) {
165 $template->param(filter_tag=>$filter);
166 $filters{term} = $filter;
168 if ($filter = $input->param('from')) {
169 $date_from = eval { output_pref( { dt => dt_from_string( $filter ), dateonly => 1, dateformat => 'iso' } ); };
170 if ( $date_from ) {
171 $template->param(filter_date_approved_from=>$filter);
172 $filters{date_approved} = ">=$date_from";
173 } else {
174 push @errors, {date_from=>$filter};
177 if ($filter = $input->param('to')) {
178 $date_to = eval { output_pref( { dt => dt_from_string( $filter ), dateonly => 1, dateformat => 'iso' } ); };
179 if ( $date_to ) {
180 $template->param(filter_date_approved_to=>$filter);
181 $filters{date_approved} = "<=$date_to";
182 } else {
183 push @errors, {date_to=>$filter};
186 if ($filter = $input->param('approver')) { # name (or borrowernumber) from input box
187 if ($filter =~ /^\d+$/ and $filter > 0) {
188 # $filter=get borrowernumber from name
189 # FIXME: get borrowernumber from name not implemented.
190 $template->param(filter_approver=>$filter);
191 $filters{approved_by} = $filter;
192 } else {
193 push @errors, {approver=>$filter};
196 if ($filter = $input->param('approved_by')) { # borrowernumber from link
197 if ($filter =~ /^\d+$/ and $filter > 0) {
198 $template->param(filter_approver=>$filter);
199 $filters{approved_by} = $filter;
200 } else {
201 push @errors, {approved_by=>$filter};
204 $debug and print STDERR "filters: " . Dumper(\%filters);
205 my $tagloop = get_approval_rows(\%filters);
206 my $qstring = $input->query_string;
207 $qstring =~ s/([&;])*\blimit=\d+//; # remove pagination var
208 $qstring =~ s/^;+//; # remove leading delims
209 $qstring = "limit=$pagesize" . ($qstring ? '&amp;' . $qstring : '');
210 $debug and print STDERR "number of approval_rows: " . scalar(@$tagloop) . "rows\n";
211 (scalar @errors) and $template->param(message_loop=>\@errors);
212 $template->param(
213 offset => $offset, # req'd for EXPR
214 op => $op,
215 op_count => scalar(@tags),
216 script_name => $script_name,
217 approved => 0, # dummy value (also EXPR)
218 tagloop => $tagloop,
219 pagination_bar => pagination_bar(
220 "$script_name?$qstring\&amp;",
221 ceil($counts->{approved_total}/$pagesize), # $page, 'page'
225 output_html_with_http_headers $input, $cookie, $template->output;
226 __END__
228 =head1 AUTHOR
230 Joe Atzberger
231 atz AT liblime.com
233 =cut