Bug 28572: Remove C4::Debug
[koha.git] / tags / review.pl
1 #!/usr/bin/perl
2
3 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
4
5 # Copyright 2008 LibLime
6 #
7 # This file is part of Koha.
8 #
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.
13 #
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.
18 #
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>.
21
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::Tags qw(get_tags get_approval_rows approval_counts whitelist blacklist is_approved);
34
35 my $script_name = "/cgi-bin/koha/tags/review.pl";
36 my $needed_flags = { tools => 'moderate_tags' };    # FIXME: replace when more specific permission is created.
37
38 sub ajax_auth_cgi { # returns CGI object
39     my $needed_flags = shift;
40     my %cookies = CGI::Cookie->fetch;
41     my $input = CGI->new;
42     my $sessid = $cookies{'CGISESSID'}->value;
43     my ($auth_status, $auth_sessid) = check_cookie_auth($sessid, $needed_flags);
44     if ($auth_status ne "ok") {
45         output_with_http_headers $input, undef,
46             "window.alert('Your CGI session cookie ($sessid) is not current.  " .
47             "Please refresh the page and try again.');\n", 'js';
48         exit 0;
49     }
50     return $input;
51 }
52
53 if (is_ajax()) {
54     my $input = &ajax_auth_cgi($needed_flags);
55     my $operator = C4::Context->userenv->{'number'};  # must occur AFTER auth
56     my ($tag, $js_reply);
57     if ($tag = $input->param('test')) {
58         my $check = is_approved($tag);
59         $js_reply = ( $check >=  1 ? 'success' : $check <= -1 ? 'failure' : 'indeterminate' ) . "_test('".uri_escape_utf8($tag)."');\n";
60     }
61     if ($tag = $input->param('ok')) {
62         $js_reply = (   whitelist($operator,$tag) ? 'success' : 'failure') . "_approve('".uri_escape_utf8($tag)."');\n";
63     }
64     if ($tag = $input->param('rej')) {
65         $js_reply = (   blacklist($operator,$tag) ? 'success' : 'failure')  . "_reject('".uri_escape_utf8($tag)."');\n";
66     }
67     output_with_http_headers $input, undef, $js_reply, 'js';
68     exit;
69 }
70
71 ### Below is the sad, boring, necessary non-AJAX HTML code.
72
73 my $input = CGI->new;
74 my ($template, $borrowernumber, $cookie) = get_template_and_user(
75     {
76         template_name   => "tags/review.tt",
77         query           => $input,
78         type            => "intranet",
79         flagsrequired   => $needed_flags,
80     }
81 );
82
83 my ($op, @errors, @tags);
84
85 foreach (qw( approve reject test )) {
86     $op = $_ if ( $input->param("op-$_") );
87 }
88 $op ||= 'none';
89
90 @tags = $input->multi_param('tags');
91
92 $borrowernumber == 0 and push @errors, {op_zero=>1};
93 if ($op eq 'approve') {
94     foreach (@tags) {
95         whitelist($borrowernumber,$_) or push @errors, {failed_ok=>$_};
96     }
97 } elsif ($op eq 'reject' ) {
98     foreach (@tags) {
99          blacklist($borrowernumber,$_) or push @errors, {failed_rej=>$_};
100     }
101 } elsif ($op eq 'test'   ) {
102     my $tag = $input->param('test');
103     push @tags, $tag;
104     my $check = is_approved($tag);
105     $template->param(
106         test_term => $tag,
107         ( $check >=  1 ? 'verdict_ok' :
108           $check <= -1 ? 'verdict_rej' : 'verdict_indeterminate' ) => 1,
109      );
110 }
111
112 my $counts = &approval_counts;
113 foreach (keys %$counts) {
114     $template->param($_ => $counts->{$_});
115 }
116
117 sub pagination_calc {
118     my $query = shift or return;
119     my $hardlimit = (@_) ? shift : 100;     # hardcoded, could be another syspref
120     my $pagesize = $query->param('limit' ) || $hardlimit;
121     my $page     = $query->param('page'  ) || 1;
122     my $offset   = $query->param('offset') || 0;
123     ($pagesize <= $hardlimit) or $pagesize = $hardlimit;
124     if ($page > 1) {
125         $offset = ($page-1)*$pagesize;
126     } else {
127         $page = 1;
128     }
129     return ($pagesize,$page,$offset);
130 }
131
132 my ($pagesize,$page,$offset) = pagination_calc($input,100);
133
134 my %filters = (
135     limit => $offset ? "$offset,$pagesize" : $pagesize,
136      sort => 'approved,-weight_total,+term',
137 );
138 my ($filter,$date_from,$date_to);
139 if (defined $input->param('approved')) { # 0 is valid value, must check defined
140     $filter = $input->param('approved');
141 } else {
142     $filter = 0;
143 }
144 if ($filter eq 'all') {
145     $template->param(filter_approved_all => 1);
146 } elsif ($filter =~ /-?[01]/) {
147     $filters{approved} = $filter;
148     $template->param(
149         ($filter == 1  ? 'filter_approved_ok'      :
150          $filter == 0  ? 'filter_approved_pending' :
151          $filter == -1 ? 'filter_approved_rej'     :
152         'filter_approved') => 1
153     );
154 }
155
156 # my $q_count = get_approval_rows({limit=>$pagesize, sort=>'approved,-weight_total,+term', count=>1});
157 if ($filter = $input->param('tag')) {
158     $template->param(filter_tag=>$filter);
159     $filters{term} = $filter;
160 }
161 if ($filter = $input->param('from')) {
162         $date_from = eval { output_pref( { dt => dt_from_string( $filter ), dateonly => 1, dateformat => 'iso' } ); };
163         if ( $date_from ) {
164         $template->param(filter_date_approved_from=>$filter);
165         $filters{date_approved} = ">=$date_from";
166     } else {
167         push @errors, {date_from=>$filter};
168     }
169 }
170 if ($filter = $input->param('to')) {
171         $date_to = eval { output_pref( { dt => dt_from_string( $filter ), dateonly => 1, dateformat => 'iso' } ); };
172         if ( $date_to ) {
173         $template->param(filter_date_approved_to=>$filter);
174         $filters{date_approved} = "<=$date_to";
175     } else {
176         push @errors, {date_to=>$filter};
177     }
178 }
179 if ($filter = $input->param('approver')) {      # name (or borrowernumber) from input box
180     if ($filter =~ /^\d+$/ and $filter > 0) {
181         # $filter=get borrowernumber from name
182         # FIXME: get borrowernumber from name not implemented.
183         $template->param(filter_approver=>$filter);
184         $filters{approved_by} = $filter;
185     } else {
186         push @errors, {approver=>$filter};
187     }
188 }
189 if ($filter = $input->param('approved_by')) {   # borrowernumber from link
190     if ($filter =~ /^\d+$/ and $filter > 0) {
191         $template->param(filter_approver=>$filter);
192         $filters{approved_by} = $filter;
193     } else {
194         push @errors, {approved_by=>$filter};
195     }
196 }
197 my $tagloop = get_approval_rows(\%filters);
198 my $qstring = $input->query_string;
199 $qstring =~ s/([&;])*\blimit=\d+//;         # remove pagination var
200 $qstring =~ s/^;+//;                        # remove leading delims
201 $qstring = "limit=$pagesize" . ($qstring ? '&amp;' . $qstring : '');
202 (scalar @errors) and $template->param(message_loop=>\@errors);
203 $template->param(
204     offset => $offset,  # req'd for EXPR
205     op => $op,
206     op_count => scalar(@tags),
207     script_name => $script_name,
208     approved => 0,      # dummy value (also EXPR)
209     tagloop => $tagloop,
210     pagination_bar => pagination_bar(
211         "$script_name?$qstring\&amp;",
212         ceil($counts->{approved_total}/$pagesize),  # $page, 'page'
213     )
214 );
215
216 output_html_with_http_headers $input, $cookie, $template->output;
217 __END__
218
219 =head1 AUTHOR
220
221 Joe Atzberger
222 atz AT liblime.com
223
224 =cut