Adding SpecifyDueDate
[koha.git] / tags / review.pl
blob421da4e64c184b40dd6e8f9ded2e620ba3384d1c
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 under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA 02111-1307 USA
22 use warnings;
23 use strict;
24 use Data::Dumper;
25 use POSIX;
26 use CGI;
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 C4::Dates qw(format_date format_date_in_iso);
32 # use C4::Koha;
33 use C4::Output 3.02 qw(:html :ajax pagination_bar);
34 use C4::Debug;
35 use C4::Tags 0.03 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_comments' }; # FIXME: replace when more specific permission is created.
40 sub ajax_auth_cgi ($) { # returns CGI object
41 my $needed_flags = shift;
42 my %cookies = fetch CGI::Cookie;
43 my $input = CGI->new;
44 my $sessid = $cookies{'CGISESSID'}->value || $input->param('CGISESSID');
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({
83 template_name => "tags/review.tmpl",
84 query => $input,
85 type => "intranet",
86 debug => 1,
87 authnotrequired => 0,
88 flagsrequired => $needed_flags,
89 });
91 my ($op, @errors, @tags);
92 $op = lc($input->param('op')) || 'none';
93 @tags = $input->param('tags');
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=>$_};
100 } elsif ($op eq 'reject' ) {
101 foreach (@tags) {
102 blacklist($borrowernumber,$_) or push @errors, {failed_rej=>$_};
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,
115 my $counts = &approval_counts;
116 foreach (keys %$counts) {
117 $template->param($_ => $counts->{$_});
120 sub pagination_calc ($;$) {
121 my $query = shift or return undef;
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;
132 return ($pagesize,$page,$offset);
135 my ($pagesize,$page,$offset) = pagination_calc($input,100);
137 my %filters = (
138 limit => $offset ? "$offset,$pagesize" : $pagesize,
139 sort => 'approved,-weight_total,+term',
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;
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
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;
164 if ($filter = $input->param('from')) {
165 if ($date_from = format_date_in_iso($filter)) {
166 $template->param(filter_date_approved_from=>$filter);
167 $filters{date_approved} = ">=$date_from";
168 } else {
169 push @errors, {date_from=>$filter};
172 if ($filter = $input->param('to')) {
173 if ($date_to = format_date_in_iso($filter)) {
174 $template->param(filter_date_approved_to=>$filter);
175 $filters{date_approved} = "<=$date_to";
176 } else {
177 push @errors, {date_to=>$filter};
180 if ($filter = $input->param('approver')) { # name (or borrowernumber) from input box
181 if (($filter =~ /^\d+$/ and $filter > 0) or
182 (1) ){ # $filter=get borrowernumber from name
183 $template->param(filter_approver=>$filter);
184 $filters{approved_by} = $filter;
185 # } else {
186 push @errors, {approver=>$filter};
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};
197 $debug and print STDERR "filters: " . Dumper(\%filters);
198 my $tagloop = get_approval_rows(\%filters);
199 my $qstring = $input->query_string;
200 $qstring =~ s/([&;])*\blimit=\d+//; # remove pagination var
201 $qstring =~ s/^;+//; # remove leading delims
202 $qstring = "limit=$pagesize" . ($qstring ? '&amp;' . $qstring : '');
203 $debug and print STDERR "number of approval_rows: " . scalar(@$tagloop) . "rows\n";
204 (scalar @errors) and $template->param(message_loop=>\@errors);
205 $template->param(
206 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
207 offset => $offset, # req'd for EXPR
208 op => $op,
209 op_count => scalar(@tags),
210 script_name => $script_name,
211 approved => 0, # dummy value (also EXPR)
212 tagloop => $tagloop,
213 pagination_bar => pagination_bar(
214 "$script_name?$qstring\&amp;",
215 ceil($counts->{approved_total}/$pagesize), # $page, 'page'
219 output_html_with_http_headers $input, $cookie, $template->output;
220 __END__
222 =head1 AUTHOR
224 Joe Atzberger
225 atz AT liblime.com
227 =cut