Bug 23248: Avoid opac-ISBDdetail.pl breaking on invalid biblionumber
[koha.git] / opac / opac-ISBDdetail.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # parts copyright 2010 BibLibre
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21
22 =head1 NAME
23
24 opac-ISBDdetail.pl - script to show a biblio in ISBD format
25
26 =head1 DESCRIPTION
27
28 This script needs a biblionumber as parameter 
29
30 It shows the biblio
31
32 The template is in <templates_dir>/catalogue/ISBDdetail.tt.
33 this template must be divided into 11 "tabs".
34
35 The first 10 tabs present the biblio, the 11th one presents
36 the items attached to the biblio
37
38 =head1 FUNCTIONS
39
40 =cut
41
42 use Modern::Perl;
43
44 use C4::Auth;
45 use C4::Context;
46 use C4::Output;
47 use CGI qw ( -utf8 );
48 use C4::Biblio;
49 use C4::Items;
50 use C4::Reserves;
51 use C4::Acquisition;
52 use C4::Serials;    # uses getsubscriptionfrom biblionumber
53 use C4::Koha;
54 use Koha::IssuingRules;
55 use Koha::ItemTypes;
56 use Koha::Patrons;
57 use Koha::RecordProcessor;
58 use Koha::Biblios;
59
60 my $query = CGI->new();
61 my $biblionumber = $query->param('biblionumber');
62 if ( !$biblionumber ) {
63     print $query->redirect('/cgi-bin/koha/errors/404.pl');
64     exit;
65 }
66 $biblionumber = int($biblionumber);
67
68 #open template
69 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
70     {
71         template_name   => "opac-ISBDdetail.tt",
72         query           => $query,
73         type            => "opac",
74         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
75         debug           => 1,
76     }
77 );
78
79 my $biblio = Koha::Biblios->find( $biblionumber, { prefetch => [ 'metadata', 'items' ] } );
80 unless ( $biblio ) {
81     print $query->redirect("/cgi-bin/koha/errors/404.pl");
82     exit;
83 }
84
85 my $patron = Koha::Patrons->find($loggedinuser);
86
87 my $opachiddenitems_rules;
88 eval {
89     my $yaml = C4::Context->preference('OpacHiddenItems') . "\n\n";
90     $opachiddenitems_rules = YAML::Load($yaml);
91 };
92
93 unless ( $patron and $patron->category->override_hidden_items ) {
94     # only skip this check if there's a logged in user
95     # and its category overrides OpacHiddenItems
96     if ( $biblio->hidden_in_opac({ rules => $opachiddenitems_rules }) ) {
97         print $query->redirect('/cgi-bin/koha/errors/404.pl'); # escape early
98         exit;
99     }
100 }
101
102 my $record      = $biblio->metadata->record;
103 my $marcflavour = C4::Context->preference("marcflavour");
104
105 my $record_processor = Koha::RecordProcessor->new({
106     filters => 'ViewPolicy',
107     options => {
108         interface => 'opac',
109         frameworkcode => $biblio->frameworkcode
110     }
111 });
112 $record_processor->process($record);
113
114 # get biblionumbers stored in the cart
115 if(my $cart_list = $query->cookie("bib_list")){
116     my @cart_list = split(/\//, $cart_list);
117     if ( grep {$_ eq $biblionumber} @cart_list) {
118         $template->param( incart => 1 );
119     }
120 }
121
122 # some useful variables for enhanced content;
123 # in each case, we're grabbing the first value we find in
124 # the record and normalizing it
125 my $upc  = GetNormalizedUPC( $record, $marcflavour );
126 my $ean  = GetNormalizedEAN( $record, $marcflavour );
127 my $oclc = GetNormalizedOCLCNumber( $record, $marcflavour );
128 my $isbn = GetNormalizedISBN( undef, $record, $marcflavour );
129 my $content_identifier_exists;
130 if ( $isbn or $ean or $oclc or $upc ) {
131     $content_identifier_exists = 1;
132 }
133 $template->param(
134     normalized_upc            => $upc,
135     normalized_ean            => $ean,
136     normalized_oclc           => $oclc,
137     normalized_isbn           => $isbn,
138     content_identifier_exists => $content_identifier_exists,
139 );
140
141 #coping with subscriptions
142 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
143 my $dat                 = TransformMarcToKoha( $record );
144
145 my @subscriptions       = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
146 my @subs;
147 foreach my $subscription (@subscriptions) {
148     my %cell;
149         my $serials_to_display;
150     $cell{subscriptionid}    = $subscription->{subscriptionid};
151     $cell{subscriptionnotes} = $subscription->{notes};
152     $cell{branchcode}        = $subscription->{branchcode};
153
154     #get the three latest serials.
155         $serials_to_display = $subscription->{opacdisplaycount};
156         $serials_to_display = C4::Context->preference('OPACSerialIssueDisplayCount') unless $serials_to_display;
157         $cell{opacdisplaycount} = $serials_to_display;
158     $cell{latestserials} =
159       GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
160     push @subs, \%cell;
161 }
162
163 $template->param(
164     subscriptions       => \@subs,
165     subscriptionsnumber => $subscriptionsnumber,
166 );
167
168 my $norequests = 1;
169 my $allow_onshelf_holds;
170 my $res = GetISBDView({
171     'record'    => $record,
172     'template'  => 'opac',
173     'framework' => $biblio->frameworkcode
174 });
175
176 my $items = $biblio->items;
177 while ( my $item = $items->next ) {
178     $norequests = 0
179       if $norequests
180         && !$item->withdrawn
181         && !$item->itemlost
182         && ($item->notforloan < 0 || not $item->notforloan )
183         && !Koha::ItemTypes->find($item->effective_itemtype)->notforloan
184         && $item->itemnumber;
185
186     $allow_onshelf_holds = Koha::IssuingRules->get_onshelfholds_policy( { item => $item, patron => $patron } )
187       unless $allow_onshelf_holds;
188 }
189
190 if( $allow_onshelf_holds || CountItemsIssued($biblionumber) || $biblio->has_items_waiting_or_intransit ) {
191     $template->param( ReservableItems => 1 );
192 }
193
194 $template->param(
195     RequestOnOpac       => C4::Context->preference("RequestOnOpac"),
196     norequests   => $norequests,
197     ISBD         => $res,
198     biblio       => $biblio,
199 );
200
201 #Search for title in links
202 my $marccontrolnumber   = GetMarcControlnumber ($record, $marcflavour);
203 my $marcissns = GetMarcISSN ( $record, $marcflavour );
204 my $issn = $marcissns->[0] || '';
205
206 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
207     $dat->{title} =~ s/\/+$//; # remove trailing slash
208     $dat->{title} =~ s/\s+$//; # remove trailing space
209     $search_for_title = parametrized_url(
210         $search_for_title,
211         {
212             TITLE         => $dat->{title},
213             AUTHOR        => $dat->{author},
214             ISBN          => $isbn,
215             ISSN          => $issn,
216             CONTROLNUMBER => $marccontrolnumber,
217             BIBLIONUMBER  => $biblionumber,
218         }
219     );
220     $template->param('OPACSearchForTitleIn' => $search_for_title);
221 }
222
223 if( C4::Context->preference('ArticleRequests') ) {
224     my $itemtype = Koha::ItemTypes->find($biblio->itemtype);
225     my $artreqpossible = $patron
226         ? $biblio->can_article_request( $patron )
227         : $itemtype
228         ? $itemtype->may_article_request
229         : q{};
230     $template->param( artreqpossible => $artreqpossible );
231 }
232
233 output_html_with_http_headers $query, $cookie, $template->output;