Bug 18989: (QA follow-up) Make controllers use Koha::Biblio->hidden_in_opac
[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 my $rules = C4::Context->yaml_preference('OpacHiddenItems');
70
71 foreach my $biblionumber ( @bibs ) {
72     $template->param( biblionumber => $biblionumber );
73
74     my $dat              = &GetBiblioData($biblionumber);
75     next unless $dat;
76     my $biblio           = Koha::Biblios->find( $biblionumber );
77
78     # No filtering on the item records needed for the record itself
79     # since the only reason item information is grabbed is because of branchcodes.
80     my $record = &GetMarcBiblio({ biblionumber => $biblionumber });
81     my $framework = &GetFrameworkCode( $biblionumber );
82     $record_processor->options({
83         interface => 'opac',
84         frameworkcode => $framework
85     });
86     $record_processor->process($record);
87     next unless $record;
88     my $marcnotesarray   = $biblio->get_marc_notes({ marcflavour => $marcflavour, opac => 1 });
89     my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
90     my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
91     my $marcseriesarray  = GetMarcSeries  ($record,$marcflavour);
92     my $marcurlsarray    = GetMarcUrls    ($record,$marcflavour);
93
94     # grab all the items...
95     my @all_items        = &GetItemsInfo( $biblionumber );
96
97     # determine which ones should be hidden / visible
98     my @hidden_items     = GetHiddenItemnumbers({ items => \@all_items, borcat => $borcat });
99
100     # If every item is hidden, then the biblio should be hidden too.
101     next
102       if $biblio->hidden_in_opac({ rules => $rules });
103
104     # copy the visible ones into the items array.
105     my @items;
106     foreach my $item (@all_items) {
107
108             # next if item is hidden
109             next  if  grep  { $item->{itemnumber} eq $_  } @hidden_items ;
110
111             my $reserve_status = C4::Reserves::GetReserveStatus($item->{itemnumber});
112             if( $reserve_status eq "Waiting"){ $item->{'waiting'} = 1; }
113             if( $reserve_status eq "Reserved"){ $item->{'onhold'} = 1; }
114             push @items, $item;
115     }
116
117     my $hasauthors = 0;
118     if($dat->{'author'} || @$marcauthorsarray) {
119       $hasauthors = 1;
120     }
121     my $collections =
122       { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.ccode' } ) };
123     my $shelflocations =
124       { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.location' } ) };
125
126         # COinS format FIXME: for books Only
127         my $fmt = substr $record->leader(), 6,2;
128         my $fmts;
129         $fmts->{'am'} = 'book';
130         $dat->{ocoins_format} = $fmts->{$fmt};
131
132     if ( $num % 2 == 1 ) {
133         $dat->{'even'} = 1;
134     }
135
136     for my $itm (@items) {
137         if ($itm->{'location'}){
138             $itm->{'location_opac'} = $shelflocations->{$itm->{'location'} };
139         }
140         my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($itm->{itemnumber});
141         if ( defined( $transfertwhen ) && $transfertwhen ne '' ) {
142              $itm->{transfertwhen} = $transfertwhen;
143              $itm->{transfertfrom} = $transfertfrom;
144              $itm->{transfertto}   = $transfertto;
145         }
146     }
147     $num++;
148     $dat->{biblionumber} = $biblionumber;
149     $dat->{ITEM_RESULTS}   = \@items;
150     $dat->{MARCNOTES}      = $marcnotesarray;
151     $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
152     $dat->{MARCAUTHORS}    = $marcauthorsarray;
153     $dat->{MARCSERIES}  = $marcseriesarray;
154     $dat->{MARCURLS}    = $marcurlsarray;
155     $dat->{HASAUTHORS}  = $hasauthors;
156
157     if ( C4::Context->preference("BiblioDefaultView") eq "normal" ) {
158         $dat->{dest} = "opac-detail.pl";
159     }
160     elsif ( C4::Context->preference("BiblioDefaultView") eq "marc" ) {
161         $dat->{dest} = "opac-MARCdetail.pl";
162     }
163     else {
164         $dat->{dest} = "opac-ISBDdetail.pl";
165     }
166     push( @results, $dat );
167 }
168
169 my $resultsarray = \@results;
170
171 # my $itemsarray=\@items;
172
173 $template->param(
174     csv_profiles => [
175         Koha::CsvProfiles->search(
176             { type => 'marc', used_for => 'export_records', staff_only => 0 }
177         )
178     ],
179     bib_list => $bib_list,
180     BIBLIO_RESULTS => $resultsarray,
181 );
182
183 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };