Bug 9002 - Remove Problematic Logic from Patron Messaging Preferences Form
[koha.git] / opac / opac-topissues.pl
blob3d5f3b84ebac3b7b4407758ade19f00a39810bcb
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 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.
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.
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.
22 use strict;
23 use warnings;
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;
34 =head1 NAME
36 plugin that shows a stats on borrowers
38 =head1 DESCRIPTION
40 =cut
42 my $input = new CGI;
43 my $branches = GetBranches();
44 my $itemtypes = GetItemTypes();
46 my ($template, $borrowernumber, $cookie)
47 = get_template_and_user({template_name => 'opac-topissues.tmpl',
48 query => $input,
49 type => "opac",
50 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
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 $advanced_search_types = C4::Context->preference('AdvancedSearchTypes');
61 my $whereclause = '';
62 $whereclause .= ' AND items.homebranch='.$dbh->quote($branch) if ($branch);
63 $whereclause .= ' AND TO_DAYS(NOW()) - TO_DAYS(biblio.datecreated) <= '.($timeLimit*30) if $timeLimit < 999;
64 $whereclause =~ s/ AND $// if $whereclause;
65 my $query;
67 if($advanced_search_types eq 'ccode'){
68 $whereclause .= ' AND authorised_values.authorised_value='.$dbh->quote($itemtype) if $itemtype;
69 $query = "SELECT datecreated, biblio.biblionumber, title,
70 author, sum( items.issues ) AS tot, biblioitems.itemtype,
71 biblioitems.publishercode,biblioitems.publicationyear,
72 authorised_values.lib as description
73 FROM biblio
74 LEFT JOIN items USING (biblionumber)
75 LEFT JOIN biblioitems USING (biblionumber)
76 LEFT JOIN authorised_values ON items.ccode = authorised_values.authorised_value
77 WHERE 1
78 $whereclause
79 AND authorised_values.category = 'ccode'
80 GROUP BY biblio.biblionumber
81 HAVING tot >0
82 ORDER BY tot DESC
83 LIMIT $limit
85 $template->param(ccodesearch => 1);
86 }else{
87 if ($itemtype){
88 if (C4::Context->preference('item-level_itypes')){
89 $whereclause .= ' AND items.itype = ' . $dbh->quote($itemtype);
91 else {
92 $whereclause .= ' AND biblioitems.itemtype='.$dbh->quote($itemtype);
95 $query = "SELECT datecreated, biblio.biblionumber, title,
96 author, sum( items.issues ) AS tot, biblioitems.itemtype,
97 biblioitems.publishercode,biblioitems.publicationyear,
98 itemtypes.description
99 FROM biblio
100 LEFT JOIN items USING (biblionumber)
101 LEFT JOIN biblioitems USING (biblionumber)
102 LEFT JOIN itemtypes ON itemtypes.itemtype = biblioitems.itemtype
103 WHERE 1
104 $whereclause
105 GROUP BY biblio.biblionumber
106 HAVING tot >0
107 ORDER BY tot DESC
108 LIMIT $limit
110 $template->param(itemtypesearch => 1);
113 my $sth = $dbh->prepare($query);
114 $sth->execute();
115 my @results;
116 while (my $line= $sth->fetchrow_hashref) {
117 push @results, $line;
120 my $timeLimitFinite = $timeLimit;
121 if($timeLimit eq 999){ $timeLimitFinite = 0 };
123 $template->param(do_it => 1,
124 limit => $limit,
125 branch => $branches->{$branch}->{branchname},
126 itemtype => $itemtypes->{$itemtype}->{description},
127 timeLimit => $timeLimit,
128 timeLimitFinite => $timeLimit,
129 results_loop => \@results,
132 $template->param( branchloop => GetBranchesLoop(C4::Context->userenv?C4::Context->userenv->{'branch'}:''));
134 # the index parameter is different for item-level itemtypes
135 my $itype_or_itemtype = (C4::Context->preference("item-level_itypes"))?'itype':'itemtype';
136 $itemtypes = GetItemTypes;
137 my @itemtypesloop;
138 if (!$advanced_search_types or $advanced_search_types eq 'itemtypes') {
139 foreach my $thisitemtype ( sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'} } keys %$itemtypes ) {
140 my %row =( value => $thisitemtype,
141 description => $itemtypes->{$thisitemtype}->{'description'},
142 selected => $thisitemtype eq $itemtype,
144 push @itemtypesloop, \%row;
146 } else {
147 my $advsearchtypes = GetAuthorisedValues($advanced_search_types, '', 'opac');
148 for my $thisitemtype (@$advsearchtypes) {
149 my $selected;
150 $selected = 1 if $thisitemtype->{authorised_value} eq $itemtype;
151 my %row =( value => $thisitemtype->{authorised_value},
152 selected => $thisitemtype eq $itemtype,
153 description => $thisitemtype->{'lib'},
155 push @itemtypesloop, \%row;
159 $template->param(
160 itemtypeloop =>\@itemtypesloop,
162 output_html_with_http_headers $input, $cookie, $template->output;