Bug 14385: Squash of a lot of patches rebased
[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 use C4::Koha;
22 use C4::Biblio;
23 use C4::Items;
24 use C4::Circulation;
25 use C4::Auth;
26 use C4::Output;
27 use Koha::RecordProcessor;
28
29 use Koha::AuthorisedValues;
30
31 my $query = new CGI;
32
33 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
34     {
35         template_name   => "opac-basket.tt",
36         query           => $query,
37         type            => "opac",
38         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
39     }
40 );
41
42 my $bib_list     = $query->param('bib_list');
43 my $verbose      = $query->param('verbose');
44
45 if ($verbose)      { $template->param( verbose      => 1 ); }
46
47 my @bibs = split( /\//, $bib_list );
48 my @results;
49
50 my $num = 1;
51 my $marcflavour = C4::Context->preference('marcflavour');
52 if (C4::Context->preference('TagsEnabled')) {
53         $template->param(TagsEnabled => 1);
54         foreach (qw(TagsShowOnList TagsInputOnList)) {
55                 C4::Context->preference($_) and $template->param($_ => 1);
56         }
57 }
58
59 my $borcat = q{};
60 if ( C4::Context->preference('OpacHiddenItemsExceptions') ) {
61     # we need to fetch the borrower info here, so we can pass the category
62     my $borrower = Koha::Patron->find( { borrowernumber -> $borrowernumber } );
63     $borcat = $borrower ? $borrower->categorycode : $borcat;
64 }
65
66 my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
67 foreach my $biblionumber ( @bibs ) {
68     $template->param( biblionumber => $biblionumber );
69
70     my $dat              = &GetBiblioData($biblionumber);
71     next unless $dat;
72
73     # No filtering on the item records needed for the record itself
74     # since the only reason item information is grabbed is because of branchcodes.
75     my $record = &GetMarcBiblio({ biblionumber => $biblionumber });
76     my $framework = &GetFrameworkCode( $biblionumber );
77     $record_processor->options({
78         interface => 'opac',
79         frameworkcode => $framework
80     });
81     $record_processor->process($record);
82     next unless $record;
83     my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
84     my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
85     my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
86     my $marcseriesarray  = GetMarcSeries  ($record,$marcflavour);
87     my $marcurlsarray    = GetMarcUrls    ($record,$marcflavour);
88
89     # grab all the items...
90     my @all_items        = &GetItemsInfo( $biblionumber );
91
92     # determine which ones should be hidden / visible
93     my @hidden_items     = GetHiddenItemnumbers({ items => \@all_items, borcat => $borcat });
94
95     # If every item is hidden, then the biblio should be hidden too.
96     next if (scalar @all_items >= 1 && scalar @hidden_items == scalar @all_items);
97
98     # copy the visible ones into the items array.
99     my @items;
100     foreach my $item (@all_items) {
101         if ( none { $item->{itemnumber} ne $_ } @hidden_items ) {
102             push @items, $item;
103         }
104     }
105
106     my $subtitle         = GetRecordValue('subtitle', $record, GetFrameworkCode($biblionumber));
107
108     my $hasauthors = 0;
109     if($dat->{'author'} || @$marcauthorsarray) {
110       $hasauthors = 1;
111     }
112     my $collections =
113       { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.ccode' } ) };
114     my $shelflocations =
115       { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.location' } ) };
116
117         # COinS format FIXME: for books Only
118         my $coins_format;
119         my $fmt = substr $record->leader(), 6,2;
120         my $fmts;
121         $fmts->{'am'} = 'book';
122         $dat->{ocoins_format} = $fmts->{$fmt};
123
124     if ( $num % 2 == 1 ) {
125         $dat->{'even'} = 1;
126     }
127
128     for my $itm (@items) {
129         if ($itm->{'location'}){
130             $itm->{'location_opac'} = $shelflocations->{$itm->{'location'} };
131         }
132         my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($itm->{itemnumber});
133         if ( defined( $transfertwhen ) && $transfertwhen ne '' ) {
134              $itm->{transfertwhen} = $transfertwhen;
135              $itm->{transfertfrom} = $transfertfrom;
136              $itm->{transfertto}   = $transfertto;
137         }
138     }
139     $num++;
140     $dat->{biblionumber} = $biblionumber;
141     $dat->{ITEM_RESULTS}   = \@items;
142     $dat->{MARCNOTES}      = $marcnotesarray;
143     $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
144     $dat->{MARCAUTHORS}    = $marcauthorsarray;
145     $dat->{MARCSERIES}  = $marcseriesarray;
146     $dat->{MARCURLS}    = $marcurlsarray;
147     $dat->{HASAUTHORS}  = $hasauthors;
148     $dat->{subtitle} = $subtitle;
149
150     if ( C4::Context->preference("BiblioDefaultView") eq "normal" ) {
151         $dat->{dest} = "opac-detail.pl";
152     }
153     elsif ( C4::Context->preference("BiblioDefaultView") eq "marc" ) {
154         $dat->{dest} = "opac-MARCdetail.pl";
155     }
156     else {
157         $dat->{dest} = "opac-ISBDdetail.pl";
158     }
159     push( @results, $dat );
160 }
161
162 my $resultsarray = \@results;
163
164 # my $itemsarray=\@items;
165
166 $template->param(
167     bib_list => $bib_list,
168     BIBLIO_RESULTS => $resultsarray,
169 );
170
171 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };