Bug 11592: MARCView and ISBD followup
[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 MARC::Record;
49 use C4::Biblio;
50 use C4::Items;
51 use C4::Reserves;
52 use C4::Acquisition;
53 use C4::Review;
54 use C4::Serials;    # uses getsubscriptionfrom biblionumber
55 use C4::Koha;
56 use C4::Members;    # GetMember
57 use Koha::RecordProcessor;
58
59 my $query = CGI->new();
60 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
61     {
62         template_name   => "opac-ISBDdetail.tt",
63         query           => $query,
64         type            => "opac",
65         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
66         debug           => 1,
67     }
68 );
69
70 my $biblionumber = $query->param('biblionumber');
71 $biblionumber = int($biblionumber);
72
73 # get biblionumbers stored in the cart
74 if(my $cart_list = $query->cookie("bib_list")){
75     my @cart_list = split(/\//, $cart_list);
76     if ( grep {$_ eq $biblionumber} @cart_list) {
77         $template->param( incart => 1 );
78     }
79 }
80
81 $template->param( 'ItemsIssued' => CountItemsIssued( $biblionumber ) );
82
83 my $marcflavour      = C4::Context->preference("marcflavour");
84
85 my @items = GetItemsInfo($biblionumber);
86 if (scalar @items >= 1) {
87     my @hiddenitems = GetHiddenItemnumbers(@items);
88
89     if (scalar @hiddenitems == scalar @items ) {
90         print $query->redirect("/cgi-bin/koha/errors/404.pl"); # escape early
91         exit;
92     }
93 }
94
95 my $record_unfiltered = GetMarcBiblio($biblionumber,1);
96 if ( ! $record_unfiltered ) {
97     print $query->redirect("/cgi-bin/koha/errors/404.pl");
98     exit;
99 }
100 my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
101 my $record_filtered  = $record_unfiltered->clone();
102 my $record           = $record_processor->process($record_filtered);
103
104 # some useful variables for enhanced content;
105 # in each case, we're grabbing the first value we find in
106 # the record and normalizing it
107 my $upc = GetNormalizedUPC($record,$marcflavour);
108 my $ean = GetNormalizedEAN($record,$marcflavour);
109 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
110 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
111 my $content_identifier_exists;
112 if ( $isbn or $ean or $oclc or $upc ) {
113     $content_identifier_exists = 1;
114 }
115 $template->param(
116     normalized_upc => $upc,
117     normalized_ean => $ean,
118     normalized_oclc => $oclc,
119     normalized_isbn => $isbn,
120     content_identifier_exists => $content_identifier_exists,
121 );
122
123 #coping with subscriptions
124 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
125 my $dbh = C4::Context->dbh;
126 my $dat                 = TransformMarcToKoha( $record );
127
128 my @subscriptions       = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
129 my @subs;
130 foreach my $subscription (@subscriptions) {
131     my %cell;
132         my $serials_to_display;
133     $cell{subscriptionid}    = $subscription->{subscriptionid};
134     $cell{subscriptionnotes} = $subscription->{notes};
135     $cell{branchcode}        = $subscription->{branchcode};
136
137     #get the three latest serials.
138         $serials_to_display = $subscription->{opacdisplaycount};
139         $serials_to_display = C4::Context->preference('OPACSerialIssueDisplayCount') unless $serials_to_display;
140         $cell{opacdisplaycount} = $serials_to_display;
141     $cell{latestserials} =
142       GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
143     push @subs, \%cell;
144 }
145
146 $template->param(
147     subscriptions       => \@subs,
148     subscriptionsnumber => $subscriptionsnumber,
149 );
150
151 my $norequests = 1;
152 my $allow_onshelf_holds;
153 my $framework = GetFrameworkCode( $biblionumber );
154 my $res = GetISBDView({
155     'record'    => $record,
156     'template'  => 'opac',
157     'framework' => $framework
158 });
159
160 my $itemtypes = GetItemTypes();
161 my $borrower = GetMember( 'borrowernumber' => $loggedinuser );
162 for my $itm (@items) {
163     $norequests = 0
164       if $norequests
165         && !$itm->{'withdrawn'}
166         && !$itm->{'itemlost'}
167         && ($itm->{'itemnotforloan'}<0 || not $itm->{'itemnotforloan'})
168         && !$itemtypes->{$itm->{'itype'}}->{notforloan}
169         && $itm->{'itemnumber'};
170
171     $allow_onshelf_holds = C4::Reserves::OnShelfHoldsAllowed($itm, $borrower)
172       unless $allow_onshelf_holds;
173 }
174
175 my $reviews = getreviews( $biblionumber, 1 );
176 foreach ( @$reviews ) {
177     my $borrower_number_review = $_->{borrowernumber};
178     my $borrowerData           = GetMember('borrowernumber' =>$borrower_number_review);
179     # setting some borrower info into this hash
180     $_->{title}     = $borrowerData->{'title'};
181     $_->{surname}   = $borrowerData->{'surname'};
182     $_->{firstname} = $borrowerData->{'firstname'};
183 }
184
185
186 $template->param(
187     RequestOnOpac       => C4::Context->preference("RequestOnOpac"),
188     AllowOnShelfHolds   => $allow_onshelf_holds,
189     norequests   => $norequests,
190     ISBD         => $res,
191     biblionumber => $biblionumber,
192     reviews             => $reviews,
193 );
194
195 #Search for title in links
196 my $marccontrolnumber   = GetMarcControlnumber ($record, $marcflavour);
197 my $marcissns = GetMarcISSN ( $record, $marcflavour );
198 my $issn = $marcissns->[0] || '';
199
200 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
201     $dat->{title} =~ s/\/+$//; # remove trailing slash
202     $dat->{title} =~ s/\s+$//; # remove trailing space
203     $search_for_title = parametrized_url(
204         $search_for_title,
205         {
206             TITLE         => $dat->{title},
207             AUTHOR        => $dat->{author},
208             ISBN          => $isbn,
209             ISSN          => $issn,
210             CONTROLNUMBER => $marccontrolnumber,
211             BIBLIONUMBER  => $biblionumber,
212         }
213     );
214     $template->param('OPACSearchForTitleIn' => $search_for_title);
215 }
216
217 output_html_with_http_headers $query, $cookie, $template->output;