Bug 10590 - parameterise the limit option
[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 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.
13 #
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.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22 use strict;
23 use warnings;
24
25 use CGI;
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 Date::Manip;
33
34 =head1 NAME
35
36 plugin that shows a stats on borrowers
37
38 =head1 DESCRIPTION
39
40 =cut
41
42 my $input = new CGI;
43 my $branches = GetBranches();
44 my $itemtypes = GetItemTypes();
45
46 my ($template, $borrowernumber, $cookie)
47         = get_template_and_user({template_name => 'opac-topissues.tmpl',
48                                 query => $input,
49                                 type => "opac",
50                authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
51                                 debug => 1,
52                                 });
53 my $dbh = C4::Context->dbh;
54 # Displaying results
55 my $limit = $input->param('limit');
56 $limit = 10 unless ($limit && $limit =~ /^\d+$/); # control user input for SQL query
57 $limit = 100 if $limit > 100;
58 my $branch = $input->param('branch') || '';
59 my $itemtype = $input->param('itemtype') || '';
60 my $timeLimit = $input->param('timeLimit') || 3;
61 my $advanced_search_types = C4::Context->preference('AdvancedSearchTypes');
62
63 my $whereclause = '';
64 $whereclause .= ' AND items.homebranch='.$dbh->quote($branch) if ($branch);
65 $whereclause .= ' AND TO_DAYS(NOW()) - TO_DAYS(biblio.datecreated) <= '.($timeLimit*30) if $timeLimit < 999;
66 $whereclause =~ s/ AND $// if $whereclause;
67 my $query;
68
69 if($advanced_search_types eq 'ccode'){
70     $whereclause .= ' AND authorised_values.authorised_value='.$dbh->quote($itemtype) if $itemtype;
71     $query = "SELECT datecreated, biblio.biblionumber, title,
72                     author, sum( items.issues ) AS tot, biblioitems.itemtype,
73                     biblioitems.publishercode,biblioitems.publicationyear,
74                     authorised_values.lib as description
75                     FROM biblio
76                     LEFT JOIN items USING (biblionumber)
77                     LEFT JOIN biblioitems USING (biblionumber)
78                     LEFT JOIN authorised_values ON items.ccode = authorised_values.authorised_value
79                     WHERE 1
80                     $whereclause
81                     AND authorised_values.category = 'ccode' 
82                     GROUP BY biblio.biblionumber
83                     HAVING tot >0
84                     ORDER BY tot DESC
85                     LIMIT ?
86                     ";
87     $template->param(ccodesearch => 1);
88 }else{
89     if ($itemtype){
90         if (C4::Context->preference('item-level_itypes')){
91             $whereclause .= ' AND items.itype = ' . $dbh->quote($itemtype);
92         }
93         else {
94             $whereclause .= ' AND biblioitems.itemtype='.$dbh->quote($itemtype);
95         }
96     }
97     $query = "SELECT datecreated, biblio.biblionumber, title,
98                     author, sum( items.issues ) AS tot, biblioitems.itemtype,
99                     biblioitems.publishercode,biblioitems.publicationyear,
100                     itemtypes.description
101                     FROM biblio
102                     LEFT JOIN items USING (biblionumber)
103                     LEFT JOIN biblioitems USING (biblionumber)
104                     LEFT JOIN itemtypes ON itemtypes.itemtype = biblioitems.itemtype
105                     WHERE 1
106                     $whereclause
107                     GROUP BY biblio.biblionumber
108                     HAVING tot >0
109                     ORDER BY tot DESC
110                     LIMIT ?
111                     ";
112      $template->param(itemtypesearch => 1);
113 }
114
115 my $sth = $dbh->prepare($query);
116 $sth->execute($limit);
117 my @results;
118 while (my $line= $sth->fetchrow_hashref) {
119     push @results, $line;
120 }
121
122 my $timeLimitFinite = $timeLimit;
123 if($timeLimit eq 999){ $timeLimitFinite = 0 };
124
125 $template->param(do_it => 1,
126                 limit => $limit,
127                 branch => $branches->{$branch}->{branchname},
128                 itemtype => $itemtypes->{$itemtype}->{description},
129                 timeLimit => $timeLimit,
130                 timeLimitFinite => $timeLimitFinite,
131                 results_loop => \@results,
132                 );
133
134 $template->param( branchloop => GetBranchesLoop(C4::Context->userenv?C4::Context->userenv->{'branch'}:''));
135
136 # the index parameter is different for item-level itemtypes
137 my $itype_or_itemtype = (C4::Context->preference("item-level_itypes"))?'itype':'itemtype';
138 $itemtypes = GetItemTypes;
139 my @itemtypesloop;
140 if (!$advanced_search_types or $advanced_search_types eq 'itemtypes') {
141         foreach my $thisitemtype ( sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'} } keys %$itemtypes ) {
142         my %row =( value => $thisitemtype,
143                    description => $itemtypes->{$thisitemtype}->{'description'},
144                    selected    => $thisitemtype eq $itemtype,
145             );
146         push @itemtypesloop, \%row;
147         }
148 } else {
149     my $advsearchtypes = GetAuthorisedValues($advanced_search_types, '', 'opac');
150         for my $thisitemtype (@$advsearchtypes) {
151                 my $selected;
152             $selected = 1 if $thisitemtype->{authorised_value} eq $itemtype;
153                 my %row =( value => $thisitemtype->{authorised_value},
154                 selected    => $thisitemtype eq $itemtype,
155                 description => $thisitemtype->{'lib'},
156             );
157                 push @itemtypesloop, \%row;
158         }
159 }
160
161 $template->param(
162                  itemtypeloop =>\@itemtypesloop,
163                 );
164 output_html_with_http_headers $input, $cookie, $template->output;
165