Bug 15758: Koha::Libraries - Ultimate duel for C4::Branch
[koha.git] / opac / opac-topissues.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 # Parts Copyright Catalyst IT 2011
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 strict;
23 use warnings;
24
25 use CGI qw ( -utf8 );
26 use C4::Auth;
27 use C4::Context;
28 use C4::Languages;
29 use C4::Search;
30 use C4::Output;
31 use C4::Koha;
32 use C4::Circulation;
33 use Date::Manip;
34
35 =head1 NAME
36
37 plugin that shows a stats on borrowers
38
39 =head1 DESCRIPTION
40
41 =cut
42
43 my $input = new CGI;
44
45 # if OpacTopissue is disabled, leave immediately
46 if ( ! C4::Context->preference('OpacTopissue') ) {
47     print $input->redirect("/cgi-bin/koha/errors/404.pl");
48     exit;
49 }
50
51 my $itemtypes = GetItemTypes();
52
53 my ($template, $borrowernumber, $cookie) = get_template_and_user(
54     {
55         template_name   => 'opac-topissues.tt',
56         query           => $input,
57         type            => "opac",
58         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
59         debug           => 1,
60     }
61 );
62 my $dbh = C4::Context->dbh;
63 # Displaying results
64 my $do_it = $input->param('do_it') || 0; # as form been posted
65 my $limit = $input->param('limit');
66 $limit = 10 unless ($limit && $limit =~ /^\d+$/); # control user input for SQL query
67 $limit = 100 if $limit > 100;
68 my $branch = $input->param('branch') || '';
69 if (!$do_it && C4::Context->userenv && C4::Context->userenv->{'branch'} ) {
70     $branch = C4::Context->userenv->{'branch'}; # select user branch by default
71 }
72 my $itemtype = $input->param('itemtype') || '';
73 my $timeLimit = $input->param('timeLimit') || 3;
74 my $advanced_search_types = C4::Context->preference('AdvancedSearchTypes');
75 my @advanced_search_types = split /\|/, $advanced_search_types;
76
77 my $params = {
78     count => $limit,
79     branch => $branch,
80     newness => $timeLimit < 999 ? $timeLimit * 30 : undef,
81 };
82
83 @advanced_search_types = grep /^(ccode|itemtypes)$/, @advanced_search_types;
84 foreach my $type (@advanced_search_types) {
85     if ($type eq 'itemtypes') {
86         $type = 'itemtype';
87     }
88     $params->{$type} = $input->param($type);
89     $template->param('selected_' . $type => scalar $input->param($type));
90 }
91
92 my @results = GetTopIssues($params);
93
94 $template->param(
95     limit => $limit,
96     branch => $branch,
97     timeLimit => $timeLimit,
98     results => \@results,
99 );
100
101 output_html_with_http_headers $input, $cookie, $template->output;