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