Bug 32692: Terminology - use staff interface for MARC framework subfield editor
[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 ($tag, $js_reply);
60     if ($tag = $input->param('test')) {
61         my $check = is_approved($tag);
62         $js_reply = ( $check >=  1 ? 'success' : $check <= -1 ? 'failure' : 'indeterminate' ) . "_test('".uri_escape_utf8($tag)."');\n";
63     }
64     if ($tag = $input->param('ok')) {
65         $js_reply = (   whitelist($operator,$tag) ? 'success' : 'failure') . "_approve('".uri_escape_utf8($tag)."');\n";
66     }
67     if ($tag = $input->param('rej')) {
68         $js_reply = (   blacklist($operator,$tag) ? 'success' : 'failure')  . "_reject('".uri_escape_utf8($tag)."');\n";
69     }
70     output_with_http_headers $input, undef, $js_reply, 'js';
71     exit;
72 }
73
74 ### Below is the sad, boring, necessary non-AJAX HTML code.
75
76 my $input = CGI->new;
77 my ($template, $borrowernumber, $cookie) = get_template_and_user(
78     {
79         template_name   => "tags/review.tt",
80         query           => $input,
81         type            => "intranet",
82         flagsrequired   => $needed_flags,
83     }
84 );
85
86 my ($op, @errors, @tags);
87
88 foreach (qw( approve reject test )) {
89     $op = $_ if ( $input->param("op-$_") );
90 }
91 $op ||= 'none';
92
93 @tags = $input->multi_param('tags');
94
95 $borrowernumber == 0 and push @errors, {op_zero=>1};
96 if ($op eq 'approve') {
97     foreach (@tags) {
98         whitelist($borrowernumber,$_) or push @errors, {failed_ok=>$_};
99     }
100 } elsif ($op eq 'reject' ) {
101     foreach (@tags) {
102          blacklist($borrowernumber,$_) or push @errors, {failed_rej=>$_};
103     }
104 } elsif ($op eq 'test'   ) {
105     my $tag = $input->param('test');
106     push @tags, $tag;
107     my $check = is_approved($tag);
108     $template->param(
109         test_term => $tag,
110         ( $check >=  1 ? 'verdict_ok' :
111           $check <= -1 ? 'verdict_rej' : 'verdict_indeterminate' ) => 1,
112      );
113 }
114
115 my $counts = &approval_counts;
116 foreach (keys %$counts) {
117     $template->param($_ => $counts->{$_});
118 }
119
120 sub pagination_calc {
121     my $query = shift or return;
122     my $hardlimit = (@_) ? shift : 100;     # hardcoded, could be another syspref
123     my $pagesize = $query->param('limit' ) || $hardlimit;
124     my $page     = $query->param('page'  ) || 1;
125     my $offset   = $query->param('offset') || 0;
126     ($pagesize <= $hardlimit) or $pagesize = $hardlimit;
127     if ($page > 1) {
128         $offset = ($page-1)*$pagesize;
129     } else {
130         $page = 1;
131     }
132     return ($pagesize,$page,$offset);
133 }
134
135 my ($pagesize,$page,$offset) = pagination_calc($input,100);
136
137 my %filters = (
138     limit => $offset ? "$offset,$pagesize" : $pagesize,
139      sort => 'approved,-weight_total,+term',
140 );
141 my ($filter,$date_from,$date_to);
142 if (defined $input->param('approved')) { # 0 is valid value, must check defined
143     $filter = $input->param('approved');
144 } else {
145     $filter = 0;
146 }
147 if ($filter eq 'all') {
148     $template->param(filter_approved_all => 1);
149 } elsif ($filter =~ /-?[01]/) {
150     $filters{approved} = $filter;
151     $template->param(
152         ($filter == 1  ? 'filter_approved_ok'      :
153          $filter == 0  ? 'filter_approved_pending' :
154          $filter == -1 ? 'filter_approved_rej'     :
155         'filter_approved') => 1
156     );
157 }
158
159 # my $q_count = get_approval_rows({limit=>$pagesize, sort=>'approved,-weight_total,+term', count=>1});
160 if ($filter = $input->param('tag')) {
161     $template->param(filter_tag=>$filter);
162     $filters{term} = $filter;
163 }
164 if ( $filter = $input->param('from') ) {
165     $date_from = $filter;
166     $template->param( filter_date_approved_from => $filter );
167     $filters{date_approved} = ">=$date_from";
168 }
169 if ( $filter = $input->param('to') ) {
170     $date_to = $filter;
171     $template->param( filter_date_approved_to => $filter );
172     $filters{date_approved} = "<=$date_to";
173 }
174 if ($filter = $input->param('approver')) {      # name (or borrowernumber) from input box
175     if ($filter =~ /^\d+$/ and $filter > 0) {
176         # $filter=get borrowernumber from name
177         # FIXME: get borrowernumber from name not implemented.
178         $template->param(filter_approver=>$filter);
179         $filters{approved_by} = $filter;
180     } else {
181         push @errors, {approver=>$filter};
182     }
183 }
184 if ($filter = $input->param('approved_by')) {   # borrowernumber from link
185     if ($filter =~ /^\d+$/ and $filter > 0) {
186         $template->param(filter_approver=>$filter);
187         $filters{approved_by} = $filter;
188     } else {
189         push @errors, {approved_by=>$filter};
190     }
191 }
192 my $tagloop = get_approval_rows(\%filters);
193 my $qstring = $input->query_string;
194 $qstring =~ s/([&;])*\blimit=\d+//;         # remove pagination var
195 $qstring =~ s/^;+//;                        # remove leading delims
196 $qstring = "limit=$pagesize" . ($qstring ? '&amp;' . $qstring : '');
197 (scalar @errors) and $template->param(message_loop=>\@errors);
198 $template->param(
199     offset => $offset,  # req'd for EXPR
200     op => $op,
201     op_count => scalar(@tags),
202     script_name => $script_name,
203     approved => 0,      # dummy value (also EXPR)
204     tagloop => $tagloop,
205     pagination_bar => pagination_bar(
206         "$script_name?$qstring\&amp;",
207         ceil($counts->{approved_total}/$pagesize),  # $page, 'page'
208     )
209 );
210
211 output_html_with_http_headers $input, $cookie, $template->output;
212 __END__
213
214 =head1 AUTHOR
215
216 Joe Atzberger
217 atz AT liblime.com
218
219 =cut