Bug 14788: Add unit tests for GetTopIssues
[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::Search;
29 use C4::Output;
30 use C4::Koha;
31 use C4::Branch;
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 $branches = GetBranches();
52 my $itemtypes = GetItemTypes();
53
54 my ($template, $borrowernumber, $cookie) = get_template_and_user(
55     {
56         template_name   => 'opac-topissues.tt',
57         query           => $input,
58         type            => "opac",
59         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
60         debug           => 1,
61     }
62 );
63 my $dbh = C4::Context->dbh;
64 # Displaying results
65 my $do_it = $input->param('do_it') || 0; # as form been posted
66 my $limit = $input->param('limit');
67 $limit = 10 unless ($limit && $limit =~ /^\d+$/); # control user input for SQL query
68 $limit = 100 if $limit > 100;
69 my $branch = $input->param('branch') || '';
70 if (!$do_it && C4::Context->userenv && C4::Context->userenv->{'branch'} ) {
71     $branch = C4::Context->userenv->{'branch'}; # select user branch by default
72 }
73 my $itemtype = $input->param('itemtype') || '';
74 my $timeLimit = $input->param('timeLimit') || 3;
75 my $advanced_search_types = C4::Context->preference('AdvancedSearchTypes');
76
77
78 my $params = {
79     count => $limit,
80     branch => $branch,
81     newness => $timeLimit < 999 ? $timeLimit * 30 : undef,
82 };
83
84 if($advanced_search_types eq 'ccode'){
85     $params->{ccode} = $itemtype;
86     $template->param(ccodesearch => 1);
87 } else {
88     $params->{itemtype} = $itemtype;
89     $template->param(itemtypesearch => 1);
90 }
91
92 my @results = GetTopIssues($params);
93
94 $template->param(do_it => 1,
95                 limit => $limit,
96                 branch => $branches->{$branch}->{branchname},
97                 itemtype => $itemtypes->{$itemtype}->{description},
98                 timeLimit => $timeLimit,
99                 results => \@results,
100                 );
101
102 $template->param( branchloop => GetBranchesLoop($branch));
103
104 # the index parameter is different for item-level itemtypes
105 my $itype_or_itemtype = (C4::Context->preference("item-level_itypes"))?'itype':'itemtype';
106 $itemtypes = GetItemTypes;
107 my @itemtypesloop;
108 if (!$advanced_search_types or $advanced_search_types eq 'itemtypes') {
109         foreach my $thisitemtype ( sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'} } keys %$itemtypes ) {
110         my %row =( value => $thisitemtype,
111                    description => $itemtypes->{$thisitemtype}->{'description'},
112                    selected    => $thisitemtype eq $itemtype,
113             );
114         push @itemtypesloop, \%row;
115         }
116 } else {
117     my $advsearchtypes = GetAuthorisedValues($advanced_search_types, '', 'opac');
118         for my $thisitemtype (@$advsearchtypes) {
119                 my $selected;
120             $selected = 1 if $thisitemtype->{authorised_value} eq $itemtype;
121                 my %row =( value => $thisitemtype->{authorised_value},
122                 selected    => $thisitemtype eq $itemtype,
123                 description => $thisitemtype->{'lib'},
124             );
125                 push @itemtypesloop, \%row;
126         }
127 }
128
129 $template->param(
130                  itemtypeloop =>\@itemtypesloop,
131                 );
132 output_html_with_http_headers $input, $cookie, $template->output;
133