Bug 27726: DBRev 20.12.00.029
[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 use YAML::XS;
44 use Encode;
45
46 use C4::Auth;
47 use C4::Context;
48 use C4::Output;
49 use CGI qw ( -utf8 );
50 use C4::Biblio;
51 use C4::Items;
52 use C4::Reserves;
53 use C4::Acquisition;
54 use C4::Serials;    # uses getsubscriptionfrom biblionumber
55 use C4::Koha;
56 use Koha::CirculationRules;
57 use Koha::ItemTypes;
58 use Koha::Patrons;
59 use Koha::RecordProcessor;
60 use Koha::Biblios;
61
62 my $query = CGI->new();
63 my $biblionumber = $query->param('biblionumber');
64 my $biblio;
65 $biblio = Koha::Biblios->find( $biblionumber, { prefetch => [ 'metadata', 'items' ] } ) if $biblionumber;
66 if( !$biblio ) {
67     print $query->redirect('/cgi-bin/koha/errors/404.pl');
68     exit;
69 }
70
71 #open template
72 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
73     {
74         template_name   => "opac-ISBDdetail.tt",
75         query           => $query,
76         type            => "opac",
77         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
78         debug           => 1,
79     }
80 );
81
82 my $patron = Koha::Patrons->find($loggedinuser);
83
84 my $opachiddenitems_rules;
85 eval {
86     my $yaml = C4::Context->preference('OpacHiddenItems') . "\n\n";
87     $opachiddenitems_rules = YAML::XS::Load(Encode::encode_utf8($yaml));
88 };
89
90 unless ( $patron and $patron->category->override_hidden_items ) {
91     # only skip this check if there's a logged in user
92     # and its category overrides OpacHiddenItems
93     if ( $biblio->hidden_in_opac({ rules => $opachiddenitems_rules }) ) {
94         print $query->redirect('/cgi-bin/koha/errors/404.pl'); # escape early
95         exit;
96     }
97 }
98
99 my $record      = $biblio->metadata->record;
100 my $marcflavour = C4::Context->preference("marcflavour");
101
102 my $record_processor = Koha::RecordProcessor->new({
103     filters => 'ViewPolicy',
104     options => {
105         interface => 'opac',
106         frameworkcode => $biblio->frameworkcode
107     }
108 });
109 $record_processor->process($record);
110
111 # get biblionumbers stored in the cart
112 if(my $cart_list = $query->cookie("bib_list")){
113     my @cart_list = split(/\//, $cart_list);
114     if ( grep {$_ eq $biblionumber} @cart_list) {
115         $template->param( incart => 1 );
116     }
117 }
118
119 # some useful variables for enhanced content;
120 # in each case, we're grabbing the first value we find in
121 # the record and normalizing it
122 my $upc  = GetNormalizedUPC( $record, $marcflavour );
123 my $ean  = GetNormalizedEAN( $record, $marcflavour );
124 my $oclc = GetNormalizedOCLCNumber( $record, $marcflavour );
125 my $isbn = GetNormalizedISBN( undef, $record, $marcflavour );
126 my $content_identifier_exists;
127 if ( $isbn or $ean or $oclc or $upc ) {
128     $content_identifier_exists = 1;
129 }
130 $template->param(
131     normalized_upc            => $upc,
132     normalized_ean            => $ean,
133     normalized_oclc           => $oclc,
134     normalized_isbn           => $isbn,
135     content_identifier_exists => $content_identifier_exists,
136 );
137
138 #coping with subscriptions
139 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
140 my $dat                 = TransformMarcToKoha( $record );
141
142 my @subscriptions       = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
143 my @subs;
144 foreach my $subscription (@subscriptions) {
145     my %cell;
146         my $serials_to_display;
147     $cell{subscriptionid}    = $subscription->{subscriptionid};
148     $cell{subscriptionnotes} = $subscription->{notes};
149     $cell{branchcode}        = $subscription->{branchcode};
150
151     #get the three latest serials.
152         $serials_to_display = $subscription->{opacdisplaycount};
153         $serials_to_display = C4::Context->preference('OPACSerialIssueDisplayCount') unless $serials_to_display;
154         $cell{opacdisplaycount} = $serials_to_display;
155     $cell{latestserials} =
156       GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
157     push @subs, \%cell;
158 }
159
160 $template->param(
161     subscriptions       => \@subs,
162     subscriptionsnumber => $subscriptionsnumber,
163 );
164
165 my $norequests = 1;
166 my $allow_onshelf_holds;
167 my $res = GetISBDView({
168     'record'    => $record,
169     'template'  => 'opac',
170     'framework' => $biblio->frameworkcode
171 });
172
173 my $items = $biblio->items;
174 while ( my $item = $items->next ) {
175     $norequests = 0
176       if $norequests
177         && !$item->withdrawn
178         && !$item->itemlost
179         && ($item->notforloan < 0 || not $item->notforloan )
180         && !Koha::ItemTypes->find($item->effective_itemtype)->notforloan
181         && $item->itemnumber;
182
183     $allow_onshelf_holds = Koha::CirculationRules->get_onshelfholds_policy( { item => $item, patron => $patron } )
184       unless $allow_onshelf_holds;
185 }
186
187 if( $allow_onshelf_holds || CountItemsIssued($biblionumber) || $biblio->has_items_waiting_or_intransit ) {
188     $template->param( ReservableItems => 1 );
189 }
190
191 $template->param(
192     RequestOnOpac       => C4::Context->preference("RequestOnOpac"),
193     norequests   => $norequests,
194     ISBD         => $res,
195     biblio       => $biblio,
196 );
197
198 #Search for title in links
199 my $marccontrolnumber   = GetMarcControlnumber ($record, $marcflavour);
200 my $marcissns = GetMarcISSN ( $record, $marcflavour );
201 my $issn = $marcissns->[0] || '';
202
203 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
204     $dat->{title} =~ s/\/+$//; # remove trailing slash
205     $dat->{title} =~ s/\s+$//; # remove trailing space
206     $search_for_title = parametrized_url(
207         $search_for_title,
208         {
209             TITLE         => $dat->{title},
210             AUTHOR        => $dat->{author},
211             ISBN          => $isbn,
212             ISSN          => $issn,
213             CONTROLNUMBER => $marccontrolnumber,
214             BIBLIONUMBER  => $biblionumber,
215         }
216     );
217     $template->param('OPACSearchForTitleIn' => $search_for_title);
218 }
219
220 if( C4::Context->preference('ArticleRequests') ) {
221     my $itemtype = Koha::ItemTypes->find($biblio->itemtype);
222     my $artreqpossible = $patron
223         ? $biblio->can_article_request( $patron )
224         : $itemtype
225         ? $itemtype->may_article_request
226         : q{};
227     $template->param( artreqpossible => $artreqpossible );
228 }
229
230 output_html_with_http_headers $query, $cookie, $template->output;