Bug 16522: (follow-up) MARC display templates and get_marc_host fixes
[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 qw(
24     GetFrameworkCode
25     GetMarcSeries
26     GetMarcSubjects
27     GetMarcUrls
28 );
29 use C4::Auth qw( get_template_and_user );
30 use C4::Output qw( output_html_with_http_headers );
31 use Koha::RecordProcessor;
32 use Koha::CsvProfiles;
33 use Koha::AuthorisedValues;
34 use Koha::Biblios;
35 use Koha::Items;
36
37 my $query = CGI->new;
38
39 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
40     {
41         template_name   => "opac-basket.tt",
42         query           => $query,
43         type            => "opac",
44         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
45     }
46 );
47
48 my $bib_list     = $query->param('bib_list');
49 my $verbose      = $query->param('verbose');
50
51 if ($verbose)      { $template->param( verbose      => 1 ); }
52
53 my @bibs = split( /\//, $bib_list );
54 my @results;
55
56 my $num = 1;
57 my $marcflavour = C4::Context->preference('marcflavour');
58 if (C4::Context->preference('TagsEnabled')) {
59         $template->param(TagsEnabled => 1);
60         foreach (qw(TagsShowOnList TagsInputOnList)) {
61                 C4::Context->preference($_) and $template->param($_ => 1);
62         }
63 }
64
65 my $logged_in_user = Koha::Patrons->find($borrowernumber);
66
67 my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
68 my $rules = C4::Context->yaml_preference('OpacHiddenItems');
69
70 foreach my $biblionumber ( @bibs ) {
71     $template->param( biblionumber => $biblionumber );
72
73     my $biblio           = Koha::Biblios->find( $biblionumber ) or next;
74     my $dat              = $biblio->unblessed;
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 = $biblio->metadata->record;
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({ opac => 1 });
87     my $marcauthorsarray = $biblio->get_marc_contributors;
88     my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
89     my $marcseriesarray  = GetMarcSeries  ($record,$marcflavour);
90     my $marcurlsarray    = GetMarcUrls    ($record,$marcflavour);
91
92     # If every item is hidden, then the biblio should be hidden too.
93     next
94       if $biblio->hidden_in_opac({ rules => $rules });
95
96     my $hasauthors = 0;
97     if($dat->{'author'} || @$marcauthorsarray) {
98       $hasauthors = 1;
99     }
100
101         # COinS format FIXME: for books Only
102         my $fmt = substr $record->leader(), 6,2;
103         my $fmts;
104         $fmts->{'am'} = 'book';
105         $dat->{ocoins_format} = $fmts->{$fmt};
106
107     if ( $num % 2 == 1 ) {
108         $dat->{'even'} = 1;
109     }
110
111     $num++;
112     $dat->{biblionumber} = $biblionumber;
113     $dat->{ITEM_RESULTS}   = $biblio->items->filter_by_visible_in_opac({ patron => $logged_in_user });
114     $dat->{MARCNOTES}      = $marcnotesarray;
115     $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
116     $dat->{MARCAUTHORS}    = $marcauthorsarray;
117     $dat->{MARCSERIES}  = $marcseriesarray;
118     $dat->{MARCURLS}    = $marcurlsarray;
119     $dat->{HASAUTHORS}  = $hasauthors;
120     my ( $host, $relatedparts ) = $biblio->get_marc_host;
121     $dat->{HOSTITEMENTRIES} = $host;
122     $dat->{RELATEDPARTS} = $relatedparts;
123
124     push( @results, $dat );
125 }
126
127 my $resultsarray = \@results;
128
129 # my $itemsarray=\@items;
130
131 $template->param(
132     csv_profiles => Koha::CsvProfiles->search(
133         { type => 'marc', used_for => 'export_records', staff_only => 0 } ),
134     bib_list => $bib_list,
135     BIBLIO_RESULTS => $resultsarray,
136 );
137
138 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };