Bug 10595: don't display OpacTopissue page when system preference is turned off
[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
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)
54         = get_template_and_user({template_name => 'opac-topissues.tmpl',
55                                 query => $input,
56                                 type => "opac",
57                authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
58                                 debug => 1,
59                                 });
60 my $dbh = C4::Context->dbh;
61 # Displaying results
62 my $limit = $input->param('limit');
63 $limit = 10 unless ($limit && $limit =~ /^\d+$/); # control user input for SQL query
64 $limit = 100 if $limit > 100;
65 my $branch = $input->param('branch') || '';
66 my $itemtype = $input->param('itemtype') || '';
67 my $timeLimit = $input->param('timeLimit') || 3;
68 my $advanced_search_types = C4::Context->preference('AdvancedSearchTypes');
69
70 my $whereclause = '';
71 $whereclause .= ' AND items.homebranch='.$dbh->quote($branch) if ($branch);
72 $whereclause .= ' AND TO_DAYS(NOW()) - TO_DAYS(biblio.datecreated) <= '.($timeLimit*30) if $timeLimit < 999;
73 $whereclause =~ s/ AND $// if $whereclause;
74 my $query;
75
76 if($advanced_search_types eq 'ccode'){
77     $whereclause .= ' AND authorised_values.authorised_value='.$dbh->quote($itemtype) if $itemtype;
78     $query = "SELECT datecreated, biblio.biblionumber, title,
79                     author, sum( items.issues ) AS tot, biblioitems.itemtype,
80                     biblioitems.publishercode,biblioitems.publicationyear,
81                     authorised_values.lib as description
82                     FROM biblio
83                     LEFT JOIN items USING (biblionumber)
84                     LEFT JOIN biblioitems USING (biblionumber)
85                     LEFT JOIN authorised_values ON items.ccode = authorised_values.authorised_value
86                     WHERE 1
87                     $whereclause
88                     AND authorised_values.category = 'ccode' 
89                     GROUP BY biblio.biblionumber
90                     HAVING tot >0
91                     ORDER BY tot DESC
92                     LIMIT ?
93                     ";
94     $template->param(ccodesearch => 1);
95 }else{
96     if ($itemtype){
97         if (C4::Context->preference('item-level_itypes')){
98             $whereclause .= ' AND items.itype = ' . $dbh->quote($itemtype);
99         }
100         else {
101             $whereclause .= ' AND biblioitems.itemtype='.$dbh->quote($itemtype);
102         }
103     }
104     $query = "SELECT datecreated, biblio.biblionumber, title,
105                     author, sum( items.issues ) AS tot, biblioitems.itemtype,
106                     biblioitems.publishercode,biblioitems.publicationyear,
107                     itemtypes.description
108                     FROM biblio
109                     LEFT JOIN items USING (biblionumber)
110                     LEFT JOIN biblioitems USING (biblionumber)
111                     LEFT JOIN itemtypes ON itemtypes.itemtype = biblioitems.itemtype
112                     WHERE 1
113                     $whereclause
114                     GROUP BY biblio.biblionumber
115                     HAVING tot >0
116                     ORDER BY tot DESC
117                     LIMIT ?
118                     ";
119      $template->param(itemtypesearch => 1);
120 }
121
122 my $sth = $dbh->prepare($query);
123 $sth->execute($limit);
124 my @results;
125 while (my $line= $sth->fetchrow_hashref) {
126     push @results, $line;
127 }
128
129 my $timeLimitFinite = $timeLimit;
130 if($timeLimit eq 999){ $timeLimitFinite = 0 };
131
132 $template->param(do_it => 1,
133                 limit => $limit,
134                 branch => $branches->{$branch}->{branchname},
135                 itemtype => $itemtypes->{$itemtype}->{description},
136                 timeLimit => $timeLimit,
137                 timeLimitFinite => $timeLimit,
138                 results_loop => \@results,
139                 );
140
141 $template->param( branchloop => GetBranchesLoop(C4::Context->userenv?C4::Context->userenv->{'branch'}:''));
142
143 # the index parameter is different for item-level itemtypes
144 my $itype_or_itemtype = (C4::Context->preference("item-level_itypes"))?'itype':'itemtype';
145 $itemtypes = GetItemTypes;
146 my @itemtypesloop;
147 if (!$advanced_search_types or $advanced_search_types eq 'itemtypes') {
148         foreach my $thisitemtype ( sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'} } keys %$itemtypes ) {
149         my %row =( value => $thisitemtype,
150                    description => $itemtypes->{$thisitemtype}->{'description'},
151                    selected    => $thisitemtype eq $itemtype,
152             );
153         push @itemtypesloop, \%row;
154         }
155 } else {
156     my $advsearchtypes = GetAuthorisedValues($advanced_search_types, '', 'opac');
157         for my $thisitemtype (@$advsearchtypes) {
158                 my $selected;
159             $selected = 1 if $thisitemtype->{authorised_value} eq $itemtype;
160                 my %row =( value => $thisitemtype->{authorised_value},
161                 selected    => $thisitemtype eq $itemtype,
162                 description => $thisitemtype->{'lib'},
163             );
164                 push @itemtypesloop, \%row;
165         }
166 }
167
168 $template->param(
169                  itemtypeloop =>\@itemtypesloop,
170                  dateformat    => C4::Context->preference("dateformat"),
171                 );
172 output_html_with_http_headers $input, $cookie, $template->output;
173