Bug 9978: Replace license header with the correct license (GPLv3+)
[koha-equinox.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 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
44 # if OpacTopissue is disabled, leave immediately
45 if ( ! C4::Context->preference('OpacTopissue') ) {
46     print $input->redirect("/cgi-bin/koha/errors/404.pl");
47     exit;
48 }
49
50 my $branches = GetBranches();
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
76 my $whereclause = '';
77 $whereclause .= ' AND items.homebranch='.$dbh->quote($branch) if ($branch);
78 $whereclause .= ' AND TO_DAYS(NOW()) - TO_DAYS(biblio.datecreated) <= '.($timeLimit*30) if $timeLimit < 999;
79 $whereclause =~ s/ AND $// if $whereclause;
80 my $query;
81
82 if($advanced_search_types eq 'ccode'){
83     $whereclause .= ' AND authorised_values.authorised_value='.$dbh->quote($itemtype) if $itemtype;
84     $query = "SELECT datecreated, biblio.biblionumber, title,
85                     author, sum( items.issues ) AS tot, biblioitems.itemtype,
86                     biblioitems.publishercode, biblioitems.place, biblioitems.publicationyear, biblio.copyrightdate,
87                     authorised_values.lib as description, biblioitems.pages, biblioitems.size
88                     FROM biblio
89                     LEFT JOIN items USING (biblionumber)
90                     LEFT JOIN biblioitems USING (biblionumber)
91                     LEFT JOIN authorised_values ON items.ccode = authorised_values.authorised_value
92                     WHERE 1
93                     $whereclause
94                     AND authorised_values.category = 'ccode' 
95                     GROUP BY biblio.biblionumber
96                     HAVING tot >0
97                     ORDER BY tot DESC
98                     LIMIT ?
99                     ";
100     $template->param(ccodesearch => 1);
101 }else{
102     if ($itemtype){
103         if (C4::Context->preference('item-level_itypes')){
104             $whereclause .= ' AND items.itype = ' . $dbh->quote($itemtype);
105         }
106         else {
107             $whereclause .= ' AND biblioitems.itemtype='.$dbh->quote($itemtype);
108         }
109     }
110     $query = "SELECT datecreated, biblio.biblionumber, title,
111                     author, sum( items.issues ) AS tot, biblioitems.itemtype,
112                     biblioitems.publishercode, biblioitems.place, biblioitems.publicationyear, biblio.copyrightdate,
113                     itemtypes.description, biblioitems.pages, biblioitems.size
114                     FROM biblio
115                     LEFT JOIN items USING (biblionumber)
116                     LEFT JOIN biblioitems USING (biblionumber)
117                     LEFT JOIN itemtypes ON itemtypes.itemtype = biblioitems.itemtype
118                     WHERE 1
119                     $whereclause
120                     GROUP BY biblio.biblionumber
121                     HAVING tot >0
122                     ORDER BY tot DESC
123                     LIMIT ?
124                     ";
125      $template->param(itemtypesearch => 1);
126 }
127
128 my $sth = $dbh->prepare($query);
129 $sth->execute($limit);
130 my @results;
131 while (my $line= $sth->fetchrow_hashref) {
132     push @results, $line;
133 }
134
135 my $timeLimitFinite = $timeLimit;
136 if($timeLimit eq 999){ $timeLimitFinite = 0 };
137
138 $template->param(do_it => 1,
139                 limit => $limit,
140                 branch => $branches->{$branch}->{branchname},
141                 itemtype => $itemtypes->{$itemtype}->{description},
142                 timeLimit => $timeLimit,
143                 timeLimitFinite => $timeLimitFinite,
144                 results_loop => \@results,
145                 );
146
147 $template->param( branchloop => GetBranchesLoop($branch));
148
149 # the index parameter is different for item-level itemtypes
150 my $itype_or_itemtype = (C4::Context->preference("item-level_itypes"))?'itype':'itemtype';
151 $itemtypes = GetItemTypes;
152 my @itemtypesloop;
153 if (!$advanced_search_types or $advanced_search_types eq 'itemtypes') {
154         foreach my $thisitemtype ( sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'} } keys %$itemtypes ) {
155         my %row =( value => $thisitemtype,
156                    description => $itemtypes->{$thisitemtype}->{'description'},
157                    selected    => $thisitemtype eq $itemtype,
158             );
159         push @itemtypesloop, \%row;
160         }
161 } else {
162     my $advsearchtypes = GetAuthorisedValues($advanced_search_types, '', 'opac');
163         for my $thisitemtype (@$advsearchtypes) {
164                 my $selected;
165             $selected = 1 if $thisitemtype->{authorised_value} eq $itemtype;
166                 my %row =( value => $thisitemtype->{authorised_value},
167                 selected    => $thisitemtype eq $itemtype,
168                 description => $thisitemtype->{'lib'},
169             );
170                 push @itemtypesloop, \%row;
171         }
172 }
173
174 $template->param(
175                  itemtypeloop =>\@itemtypesloop,
176                 );
177 output_html_with_http_headers $input, $cookie, $template->output;
178