Bug 14045: DBRev 3.21.00.035
[koha.git] / opac / opac-topissues.pl
blobc88cb17cbe079e06d24f36d97e1c1fe3fe44f552
1 #!/usr/bin/perl
4 # Copyright 2000-2002 Katipo Communications
5 # Parts Copyright Catalyst IT 2011
7 # This file is part of Koha.
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.
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.
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>.
22 use strict;
23 use warnings;
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;
34 =head1 NAME
36 plugin that shows a stats on borrowers
38 =head1 DESCRIPTION
40 =cut
42 my $input = new CGI;
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;
50 my $branches = GetBranches();
51 my $itemtypes = GetItemTypes();
53 my ($template, $borrowernumber, $cookie) = get_template_and_user(
55 template_name => 'opac-topissues.tt',
56 query => $input,
57 type => "opac",
58 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
59 debug => 1,
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
72 my $itemtype = $input->param('itemtype') || '';
73 my $timeLimit = $input->param('timeLimit') || 3;
74 my $advanced_search_types = C4::Context->preference('AdvancedSearchTypes');
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;
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 ?
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);
106 else {
107 $whereclause .= ' AND biblioitems.itemtype='.$dbh->quote($itemtype);
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 ?
125 $template->param(itemtypesearch => 1);
128 my $sth = $dbh->prepare($query);
129 $sth->execute($limit);
130 my @results;
131 while (my $line= $sth->fetchrow_hashref) {
132 push @results, $line;
135 my $timeLimitFinite = $timeLimit;
136 if($timeLimit eq 999){ $timeLimitFinite = 0 };
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,
147 $template->param( branchloop => GetBranchesLoop($branch));
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,
159 push @itemtypesloop, \%row;
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'},
170 push @itemtypesloop, \%row;
174 $template->param(
175 itemtypeloop =>\@itemtypesloop,
177 output_html_with_http_headers $input, $cookie, $template->output;