7439 Mailmap for master
[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 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 => ( 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');
60
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;
66
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
84                     ";
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);
90         }
91         else {
92             $whereclause .= ' AND biblioitems.itemtype='.$dbh->quote($itemtype);
93         }
94     }
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
109                     ";
110      $template->param(itemtypesearch => 1);
111 }
112
113 my $sth = $dbh->prepare($query);
114 $sth->execute();
115 my @results;
116 while (my $line= $sth->fetchrow_hashref) {
117     push @results, $line;
118 }
119
120 my $timeLimitFinite = $timeLimit;
121 if($timeLimit eq 999){ $timeLimitFinite = 0 };
122
123 $template->param(do_it => 1,
124                 limit => $limit,
125                 branch => $branches->{$branch}->{branchname} || 'all locations',
126                 itemtype => $itemtypes->{$itemtype}->{description} || 'item types',
127                 timeLimit => $timeLimit,
128                 timeLimitFinite => $timeLimit,
129                 results_loop => \@results,
130                 );
131
132 $template->param( branchloop => GetBranchesLoop(C4::Context->userenv->{'branch'}));
133
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,
143             );
144         push @itemtypesloop, \%row;
145         }
146 } else {
147     my $advsearchtypes = GetAuthorisedValues($advanced_search_types, '', 'opac');
148         for my $thisitemtype (@$advsearchtypes) {
149                 my $selected = 1 if $thisitemtype->{authorised_value} eq $itemtype;
150                 my %row =( value => $thisitemtype->{authorised_value},
151                 selected    => $thisitemtype eq $itemtype,
152                 description => $thisitemtype->{'lib'},
153             );
154                 push @itemtypesloop, \%row;
155         }
156 }
157
158 $template->param(
159                  itemtypeloop =>\@itemtypesloop,
160                  dateformat    => C4::Context->preference("dateformat"),
161                 );
162 output_html_with_http_headers $input, $cookie, $template->output;
163