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