Fix for 1279: Detail page should display error message for invalid biblionumbers
[koha.git] / opac / opac-topissues.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
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 with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
23 use CGI;
24 use C4::Auth;
25 use C4::Context;
26 use C4::Search;
27 use C4::Output;
28 use C4::Koha;
29 use C4::Branch;
30 use Date::Manip;
31
32 =head1 NAME
33
34 plugin that shows a stats on borrowers
35
36 =head1 DESCRIPTION
37
38 =over 2
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 => 1,
51                                 debug => 1,
52                                 });
53 my $dbh = C4::Context->dbh;
54 # Displaying results
55 my $limit = $input->param('limit') || 10;
56 my $branch = $input->param('branch');
57 my $itemtype = $input->param('itemtype');
58 my $timeLimit = $input->param('timeLimit') || 3;
59 my $whereclause;
60 $whereclause .= 'items.homebranch='.$dbh->quote($branch)." AND " if ($branch); 
61 $whereclause .= 'biblioitems.itemtype='.$dbh->quote($itemtype)." AND " if $itemtype;
62 $whereclause .= 'TO_DAYS(NOW()) - TO_DAYS(biblio.timestamp) <= '.$timeLimit*30 if $timeLimit;
63 $whereclause =~ s/ AND $//;
64 $whereclause = " WHERE ".$whereclause if $whereclause;
65 my $query = "SELECT biblio.timestamp, biblio.biblionumber, title, 
66                 author, sum( items.issues ) AS tot, biblioitems.itemtype,
67                 biblioitems.publishercode,biblioitems.publicationyear,
68                 itemtypes.description
69                 FROM biblio
70                 LEFT JOIN items USING (biblionumber)
71                 LEFT JOIN biblioitems USING (biblionumber)
72                 LEFT JOIN itemtypes ON itemtypes.itemtype = biblioitems.itemtype
73                 $whereclause
74                 GROUP BY biblio.biblionumber
75                 ORDER BY tot DESC
76                 LIMIT $limit
77                 ";
78
79 my $sth = $dbh->prepare($query);
80 $sth->execute();
81 my @results;
82 while (my $line= $sth->fetchrow_hashref) {
83     push @results, $line;
84 }
85
86 $template->param(do_it => 1,
87                 limit => $limit,
88                 branch => $branches->{$branch}->{branchname},
89                 itemtype => $itemtypes->{$itemtype}->{description},
90                 timeLimit => $timeLimit,
91                 results_loop => \@results,
92                 );
93
94 # load the branches
95 my $branches = GetBranches();
96 my @branch_loop;
97 for my $branch_hash ( keys %$branches ) {
98     my $selected=(C4::Context->userenv && ($branch_hash eq C4::Context->userenv->{branch})) if (C4::Context->preference('SearchMyLibraryFirst'));
99     push @branch_loop,
100       {
101         value      => "branch: $branch_hash",
102         branchname => $branches->{$branch_hash}->{'branchname'},
103         selected => $selected
104       };
105 }
106 $template->param( branchloop => \@branch_loop, "mylibraryfirst"=>C4::Context->preference("SearchMyLibraryFirst"));
107
108 #doctype
109 my $itemtypes = GetItemTypes;
110 my @itemtypeloop;
111 foreach my $thisitemtype (keys %$itemtypes) {
112         my %row =(value => $thisitemtype,
113                     description => $itemtypes->{$thisitemtype}->{'description'},
114                         );
115         push @itemtypeloop, \%row;
116 }
117
118 $template->param(
119                  itemtypeloop =>\@itemtypeloop,
120                 );
121 output_html_with_http_headers $input, $cookie, $template->output;
122