b746abde6724cdb8ff984f0c37cd22c380461438
[koha.git] / opac / opac-basket.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use CGI qw ( -utf8 );
21
22 use C4::Koha;
23 use C4::Biblio;
24 use C4::Items;
25 use C4::Circulation;
26 use C4::Auth;
27 use C4::Output;
28 use Koha::RecordProcessor;
29 use Koha::CsvProfiles;
30 use Koha::AuthorisedValues;
31 use Koha::Biblios;
32
33 my $query = CGI->new;
34
35 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
36     {
37         template_name   => "opac-basket.tt",
38         query           => $query,
39         type            => "opac",
40         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
41     }
42 );
43
44 my $bib_list     = $query->param('bib_list');
45 my $verbose      = $query->param('verbose');
46
47 if ($verbose)      { $template->param( verbose      => 1 ); }
48
49 my @bibs = split( /\//, $bib_list );
50 my @results;
51
52 my $num = 1;
53 my $marcflavour = C4::Context->preference('marcflavour');
54 if (C4::Context->preference('TagsEnabled')) {
55         $template->param(TagsEnabled => 1);
56         foreach (qw(TagsShowOnList TagsInputOnList)) {
57                 C4::Context->preference($_) and $template->param($_ => 1);
58         }
59 }
60
61 my $borcat = q{};
62 if ( C4::Context->preference('OpacHiddenItemsExceptions') ) {
63     # we need to fetch the borrower info here, so we can pass the category
64     my $patron = Koha::Patrons->find($borrowernumber);
65     $borcat = $patron ? $patron->categorycode : $borcat;
66 }
67
68 my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
69 foreach my $biblionumber ( @bibs ) {
70     $template->param( biblionumber => $biblionumber );
71
72     my $dat              = &GetBiblioData($biblionumber);
73     next unless $dat;
74     my $biblio           = Koha::Biblios->find( $biblionumber );
75
76     # No filtering on the item records needed for the record itself
77     # since the only reason item information is grabbed is because of branchcodes.
78     my $record = &GetMarcBiblio({ biblionumber => $biblionumber });
79     my $framework = &GetFrameworkCode( $biblionumber );
80     $record_processor->options({
81         interface => 'opac',
82         frameworkcode => $framework
83     });
84     $record_processor->process($record);
85     next unless $record;
86     my $marcnotesarray   = $biblio->get_marc_notes({ marcflavour => $marcflavour, opac => 1 });
87     my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
88     my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
89     my $marcseriesarray  = GetMarcSeries  ($record,$marcflavour);
90     my $marcurlsarray    = GetMarcUrls    ($record,$marcflavour);
91
92     # grab all the items...
93     my @all_items        = &GetItemsInfo( $biblionumber );
94
95     # determine which ones should be hidden / visible
96     my @hidden_items     = GetHiddenItemnumbers({ items => \@all_items, borcat => $borcat });
97
98     # If every item is hidden, then the biblio should be hidden too.
99     next if (scalar @all_items >= 1 && scalar @hidden_items == scalar @all_items);
100
101     # copy the visible ones into the items array.
102     my @items;
103     foreach my $item (@all_items) {
104
105             # next if item is hidden
106             next  if  grep  { $item->{itemnumber} eq $_  } @hidden_items ;
107
108             my $reserve_status = C4::Reserves::GetReserveStatus($item->{itemnumber});
109             if( $reserve_status eq "Waiting"){ $item->{'waiting'} = 1; }
110             if( $reserve_status eq "Reserved"){ $item->{'onhold'} = 1; }
111             push @items, $item;
112     }
113
114     my $hasauthors = 0;
115     if($dat->{'author'} || @$marcauthorsarray) {
116       $hasauthors = 1;
117     }
118     my $collections =
119       { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.ccode' } ) };
120     my $shelflocations =
121       { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.location' } ) };
122
123         # COinS format FIXME: for books Only
124         my $fmt = substr $record->leader(), 6,2;
125         my $fmts;
126         $fmts->{'am'} = 'book';
127         $dat->{ocoins_format} = $fmts->{$fmt};
128
129     if ( $num % 2 == 1 ) {
130         $dat->{'even'} = 1;
131     }
132
133     for my $itm (@items) {
134         if ($itm->{'location'}){
135             $itm->{'location_opac'} = $shelflocations->{$itm->{'location'} };
136         }
137         my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($itm->{itemnumber});
138         if ( defined( $transfertwhen ) && $transfertwhen ne '' ) {
139              $itm->{transfertwhen} = $transfertwhen;
140              $itm->{transfertfrom} = $transfertfrom;
141              $itm->{transfertto}   = $transfertto;
142         }
143     }
144     $num++;
145     $dat->{biblionumber} = $biblionumber;
146     $dat->{ITEM_RESULTS}   = \@items;
147     $dat->{MARCNOTES}      = $marcnotesarray;
148     $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
149     $dat->{MARCAUTHORS}    = $marcauthorsarray;
150     $dat->{MARCSERIES}  = $marcseriesarray;
151     $dat->{MARCURLS}    = $marcurlsarray;
152     $dat->{HASAUTHORS}  = $hasauthors;
153
154     if ( C4::Context->preference("BiblioDefaultView") eq "normal" ) {
155         $dat->{dest} = "opac-detail.pl";
156     }
157     elsif ( C4::Context->preference("BiblioDefaultView") eq "marc" ) {
158         $dat->{dest} = "opac-MARCdetail.pl";
159     }
160     else {
161         $dat->{dest} = "opac-ISBDdetail.pl";
162     }
163     push( @results, $dat );
164 }
165
166 my $resultsarray = \@results;
167
168 # my $itemsarray=\@items;
169
170 $template->param(
171     csv_profiles => [
172         Koha::CsvProfiles->search(
173             { type => 'marc', used_for => 'export_records', staff_only => 0 }
174         )
175     ],
176     bib_list => $bib_list,
177     BIBLIO_RESULTS => $resultsarray,
178 );
179
180 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };