Bug 11592: (QA followup) Simplify code
[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 = GetMarcBiblio($biblionumber,1);
96 if ( ! $record ) {
97     print $query->redirect("/cgi-bin/koha/errors/404.pl");
98     exit;
99 }
100 my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
101 $record_processor->process($record);
102
103 # some useful variables for enhanced content;
104 # in each case, we're grabbing the first value we find in
105 # the record and normalizing it
106 my $upc = GetNormalizedUPC($record,$marcflavour);
107 my $ean = GetNormalizedEAN($record,$marcflavour);
108 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
109 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
110 my $content_identifier_exists;
111 if ( $isbn or $ean or $oclc or $upc ) {
112     $content_identifier_exists = 1;
113 }
114 $template->param(
115     normalized_upc => $upc,
116     normalized_ean => $ean,
117     normalized_oclc => $oclc,
118     normalized_isbn => $isbn,
119     content_identifier_exists => $content_identifier_exists,
120 );
121
122 #coping with subscriptions
123 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
124 my $dbh = C4::Context->dbh;
125 my $dat                 = TransformMarcToKoha( $record );
126
127 my @subscriptions       = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
128 my @subs;
129 foreach my $subscription (@subscriptions) {
130     my %cell;
131         my $serials_to_display;
132     $cell{subscriptionid}    = $subscription->{subscriptionid};
133     $cell{subscriptionnotes} = $subscription->{notes};
134     $cell{branchcode}        = $subscription->{branchcode};
135
136     #get the three latest serials.
137         $serials_to_display = $subscription->{opacdisplaycount};
138         $serials_to_display = C4::Context->preference('OPACSerialIssueDisplayCount') unless $serials_to_display;
139         $cell{opacdisplaycount} = $serials_to_display;
140     $cell{latestserials} =
141       GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
142     push @subs, \%cell;
143 }
144
145 $template->param(
146     subscriptions       => \@subs,
147     subscriptionsnumber => $subscriptionsnumber,
148 );
149
150 my $norequests = 1;
151 my $allow_onshelf_holds;
152 my $framework = GetFrameworkCode( $biblionumber );
153 my $res = GetISBDView({
154     'record'    => $record,
155     'template'  => 'opac',
156     'framework' => $framework
157 });
158
159 my $itemtypes = GetItemTypes();
160 my $borrower = GetMember( 'borrowernumber' => $loggedinuser );
161 for my $itm (@items) {
162     $norequests = 0
163       if $norequests
164         && !$itm->{'withdrawn'}
165         && !$itm->{'itemlost'}
166         && ($itm->{'itemnotforloan'}<0 || not $itm->{'itemnotforloan'})
167         && !$itemtypes->{$itm->{'itype'}}->{notforloan}
168         && $itm->{'itemnumber'};
169
170     $allow_onshelf_holds = C4::Reserves::OnShelfHoldsAllowed($itm, $borrower)
171       unless $allow_onshelf_holds;
172 }
173
174 my $reviews = getreviews( $biblionumber, 1 );
175 foreach ( @$reviews ) {
176     my $borrower_number_review = $_->{borrowernumber};
177     my $borrowerData           = GetMember('borrowernumber' =>$borrower_number_review);
178     # setting some borrower info into this hash
179     $_->{title}     = $borrowerData->{'title'};
180     $_->{surname}   = $borrowerData->{'surname'};
181     $_->{firstname} = $borrowerData->{'firstname'};
182 }
183
184
185 $template->param(
186     RequestOnOpac       => C4::Context->preference("RequestOnOpac"),
187     AllowOnShelfHolds   => $allow_onshelf_holds,
188     norequests   => $norequests,
189     ISBD         => $res,
190     biblionumber => $biblionumber,
191     reviews             => $reviews,
192 );
193
194 #Search for title in links
195 my $marccontrolnumber   = GetMarcControlnumber ($record, $marcflavour);
196 my $marcissns = GetMarcISSN ( $record, $marcflavour );
197 my $issn = $marcissns->[0] || '';
198
199 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
200     $dat->{title} =~ s/\/+$//; # remove trailing slash
201     $dat->{title} =~ s/\s+$//; # remove trailing space
202     $search_for_title = parametrized_url(
203         $search_for_title,
204         {
205             TITLE         => $dat->{title},
206             AUTHOR        => $dat->{author},
207             ISBN          => $isbn,
208             ISSN          => $issn,
209             CONTROLNUMBER => $marccontrolnumber,
210             BIBLIONUMBER  => $biblionumber,
211         }
212     );
213     $template->param('OPACSearchForTitleIn' => $search_for_title);
214 }
215
216 output_html_with_http_headers $query, $cookie, $template->output;