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