Bug 30579: Fix rebase issue
[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 POSIX qw( ceil );
24 use CGI qw ( -utf8 );
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 );
28 use C4::Context;
29 use C4::Output qw( output_with_http_headers is_ajax pagination_bar output_html_with_http_headers );
30 use C4::Tags qw(
31     approval_counts
32     blacklist
33     get_approval_rows
34     is_approved
35     whitelist
36 );
37
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.
40
41 sub ajax_auth_cgi { # returns CGI object
42     my $needed_flags = shift;
43     my %cookies = CGI::Cookie->fetch;
44     my $input = CGI->new;
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';
51         exit 0;
52     }
53     return $input;
54 }
55
56 if (is_ajax()) {
57     my $input = &ajax_auth_cgi($needed_flags);
58     my $operator = C4::Context->userenv->{'number'};  # must occur AFTER auth
59     my $js_reply;
60     my $op = $input->param('op') || q{};
61     my $tag = $input->param('tag');
62     if ($op eq 'test') {
63         my $check = is_approved($tag);
64         $js_reply = ( $check >=  1 ? 'success' : $check <= -1 ? 'failure' : 'indeterminate' ) . "_test('".uri_escape_utf8($tag)."');\n";
65     }
66     elsif ($op eq 'cud-approve') {
67         $js_reply = (   whitelist($operator,$tag) ? 'success' : 'failure') . "_approve('".uri_escape_utf8($tag)."');\n";
68     }
69     elsif ($op eq 'cud-reject') {
70         $js_reply = (   blacklist($operator,$tag) ? 'success' : 'failure')  . "_reject('".uri_escape_utf8($tag)."');\n";
71     }
72     output_with_http_headers $input, undef, $js_reply, 'js';
73     exit;
74 }
75
76 ### Below is the sad, boring, necessary non-AJAX HTML code.
77
78 my $input = CGI->new;
79 my ($template, $borrowernumber, $cookie) = get_template_and_user(
80     {
81         template_name   => "tags/review.tt",
82         query           => $input,
83         type            => "intranet",
84         flagsrequired   => $needed_flags,
85     }
86 );
87
88 my (@errors, @tags);
89
90 my $op = $input->param('op') || q{};
91
92 @tags = $input->multi_param('tags');
93
94 if ($op eq 'cud-approve') {
95     foreach (@tags) {
96         whitelist($borrowernumber,$_) or push @errors, {failed_ok=>$_};
97     }
98 } elsif ($op eq 'cud-reject' ) {
99     foreach (@tags) {
100          blacklist($borrowernumber,$_) or push @errors, {failed_rej=>$_};
101     }
102 } elsif ($op eq 'test'   ) {
103     my $tag = $input->param('test');
104     push @tags, $tag;
105     my $check = is_approved($tag);
106     $template->param(
107         test_term => $tag,
108         ( $check >=  1 ? 'verdict_ok' :
109           $check <= -1 ? 'verdict_rej' : 'verdict_indeterminate' ) => 1,
110      );
111 }
112
113 my $counts = &approval_counts;
114 foreach (keys %$counts) {
115     $template->param($_ => $counts->{$_});
116 }
117
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;
125     if ($page > 1) {
126         $offset = ($page-1)*$pagesize;
127     } else {
128         $page = 1;
129     }
130     return ($pagesize,$page,$offset);
131 }
132
133 my ($pagesize,$page,$offset) = pagination_calc($input,100);
134
135 my %filters = (
136     limit => $offset ? "$offset,$pagesize" : $pagesize,
137      sort => 'approved,-weight_total,+term',
138 );
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');
142 } else {
143     $filter = 0;
144 }
145 if ($filter eq 'all') {
146     $template->param(filter_approved_all => 1);
147 } elsif ($filter =~ /-?[01]/) {
148     $filters{approved} = $filter;
149     $template->param(
150         ($filter == 1  ? 'filter_approved_ok'      :
151          $filter == 0  ? 'filter_approved_pending' :
152          $filter == -1 ? 'filter_approved_rej'     :
153         'filter_approved') => 1
154     );
155 }
156
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;
161 }
162 if ( $filter = $input->param('from') ) {
163     $date_from = $filter;
164     $template->param( filter_date_approved_from => $filter );
165     $filters{date_approved} = ">=$date_from";
166 }
167 if ( $filter = $input->param('to') ) {
168     $date_to = $filter;
169     $template->param( filter_date_approved_to => $filter );
170     $filters{date_approved} = "<=$date_to";
171 }
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;
178     } else {
179         push @errors, {approver=>$filter};
180     }
181 }
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;
186     } else {
187         push @errors, {approved_by=>$filter};
188     }
189 }
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 ? '&amp;' . $qstring : '');
195 (scalar @errors) and $template->param(message_loop=>\@errors);
196 $template->param(
197     offset => $offset,  # req'd for EXPR
198     op => $op,
199     op_count => scalar(@tags),
200     script_name => $script_name,
201     approved => 0,      # dummy value (also EXPR)
202     tagloop => $tagloop,
203     pagination_bar => pagination_bar(
204         "$script_name?$qstring\&amp;",
205         ceil($counts->{approved_total}/$pagesize),  # $page, 'page'
206     )
207 );
208
209 output_html_with_http_headers $input, $cookie, $template->output;
210 __END__
211
212 =head1 AUTHOR
213
214 Joe Atzberger
215 atz AT liblime.com
216
217 =cut