Bugfix 2339 - Place hold button display with AllowOnShelfHolds OFF
[koha.git] / opac / opac-topissues.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use CGI;
23 use C4::Auth;
24 use C4::Context;
25 use C4::Search;
26 use C4::Output;
27 use C4::Koha;
28 use C4::Branch;
29 use Date::Manip;
30
31 =head1 NAME
32
33 plugin that shows a stats on borrowers
34
35 =head1 DESCRIPTION
36
37 =over 2
38
39 =cut
40
41 my $input = new CGI;
42 my $branches = GetBranches();
43 my $itemtypes = GetItemTypes();
44
45 my ($template, $borrowernumber, $cookie)
46         = get_template_and_user({template_name => 'opac-topissues.tmpl',
47                                 query => $input,
48                                 type => "opac",
49                                 authnotrequired => 1,
50                                 debug => 1,
51                                 });
52 my $dbh = C4::Context->dbh;
53 # Displaying results
54 my $limit = $input->param('limit') || 10;
55 my $branch = $input->param('branch');
56 my $itemtype = $input->param('itemtype');
57 my $timeLimit = $input->param('timeLimit') || 3;
58 my $whereclause;
59 $whereclause .= ' AND items.homebranch='.$dbh->quote($branch) if ($branch);
60 $whereclause .= ' AND TO_DAYS(NOW()) - TO_DAYS(biblio.datecreated) <= '.($timeLimit*30) if $timeLimit < 999;
61 $whereclause =~ s/ AND $//;
62 my $query;
63 if(C4::Context->preference('AdvancedSearchTypes') eq 'ccode'){
64     $whereclause .= ' AND authorised_values.authorised_value='.$dbh->quote($itemtype) if $itemtype;
65     $query = "SELECT datecreated, biblio.biblionumber, title, 
66                     author, sum( items.issues ) AS tot, biblioitems.itemtype,
67                     biblioitems.publishercode,biblioitems.publicationyear,
68                     authorised_values.lib as description
69                     FROM biblio
70                     LEFT JOIN items USING (biblionumber)
71                     LEFT JOIN biblioitems USING (biblionumber)
72                     LEFT JOIN authorised_values ON items.ccode = authorised_values.authorised_value
73                     WHERE 1
74                     $whereclause
75                     AND authorised_values.category = 'ccode' 
76                     GROUP BY biblio.biblionumber
77                     HAVING tot >0
78                     ORDER BY tot DESC
79                     LIMIT $limit
80                     ";
81 }else{
82     $whereclause .= ' AND biblioitems.itemtype='.$dbh->quote($itemtype) if $itemtype;
83     $query = "SELECT datecreated, biblio.biblionumber, title, 
84                     author, sum( items.issues ) AS tot, biblioitems.itemtype,
85                     biblioitems.publishercode,biblioitems.publicationyear,
86                     itemtypes.description
87                     FROM biblio
88                     LEFT JOIN items USING (biblionumber)
89                     LEFT JOIN biblioitems USING (biblionumber)
90                     LEFT JOIN itemtypes ON itemtypes.itemtype = biblioitems.itemtype
91                     WHERE 1
92                     $whereclause
93                     GROUP BY biblio.biblionumber
94                     HAVING tot >0
95                     ORDER BY tot DESC
96                     LIMIT $limit
97                     ";
98 }
99 my $sth = $dbh->prepare($query);
100 $sth->execute();
101 my @results;
102 while (my $line= $sth->fetchrow_hashref) {
103     push @results, $line;
104 }
105
106 if($timeLimit eq 999){ $timeLimit = 0 };
107
108 $template->param(do_it => 1,
109                 limit => $limit,
110                 branch => $branches->{$branch}->{branchname},
111                 itemtype => $itemtypes->{$itemtype}->{description},
112                 timeLimit => $timeLimit,
113                 timeLimitFinite => $timeLimit,
114                 results_loop => \@results,
115                 );
116
117 # load the branches             ## again??
118 $branches = GetBranches();
119 my @branch_loop;
120 for my $branch_hash (sort keys %$branches ) {
121     my $selected=(C4::Context->userenv && ($branch_hash eq C4::Context->userenv->{branch})) if (C4::Context->preference('SearchMyLibraryFirst'));
122     push @branch_loop,
123       {
124         value      => "$branch_hash",
125         branchname => $branches->{$branch_hash}->{'branchname'},
126         selected => $selected
127       };
128 }
129 $template->param( branchloop => \@branch_loop, "mylibraryfirst"=>C4::Context->preference("SearchMyLibraryFirst"));
130
131 #doctype
132 $itemtypes = GetItemTypes;
133 my @itemtypeloop;
134 foreach my $thisitemtype (keys %$itemtypes) {
135         my %row =(value => $thisitemtype,
136                     description => $itemtypes->{$thisitemtype}->{'description'},
137                         );
138         push @itemtypeloop, \%row;
139 }
140
141 $template->param(
142                  itemtypeloop =>\@itemtypeloop,
143                  dateformat    => C4::Context->preference("dateformat"),
144                 );
145 output_html_with_http_headers $input, $cookie, $template->output;
146