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