Bug 14385: Extend OpacHiddenItems to allow specifying exempt borrower categories
[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 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
64     {
65         template_name   => "opac-ISBDdetail.tt",
66         query           => $query,
67         type            => "opac",
68         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
69         debug           => 1,
70     }
71 );
72
73 my $biblionumber = $query->param('biblionumber');
74 $biblionumber = int($biblionumber);
75
76 # get biblionumbers stored in the cart
77 if(my $cart_list = $query->cookie("bib_list")){
78     my @cart_list = split(/\//, $cart_list);
79     if ( grep {$_ eq $biblionumber} @cart_list) {
80         $template->param( incart => 1 );
81     }
82 }
83
84 my $marcflavour      = C4::Context->preference("marcflavour");
85
86 my @items = GetItemsInfo($biblionumber);
87 if (scalar @items >= 1) {
88     my $borcat;
89     if ( C4::Context->preference('OpacHiddenItemsExceptions') ) {
90
91         # we need to fetch the borrower info here, so we can pass the category
92         my $borrower = GetMember( borrowernumber => $borrowernumber );
93         $borcat = $borrower->{categorycode};
94     }
95
96     my @hiddenitems = GetHiddenItemnumbers( { items => \@items, borcat => $borcat });
97
98     if (scalar @hiddenitems == scalar @items ) {
99         print $query->redirect("/cgi-bin/koha/errors/404.pl"); # escape early
100         exit;
101     }
102 }
103
104 my $record = GetMarcBiblio({
105     biblionumber => $biblionumber,
106     embed_items  => 1 });
107 if ( ! $record ) {
108     print $query->redirect("/cgi-bin/koha/errors/404.pl");
109     exit;
110 }
111
112 my $biblio = Koha::Biblios->find( $biblionumber );
113
114 my $framework = GetFrameworkCode( $biblionumber );
115 my $record_processor = Koha::RecordProcessor->new({
116     filters => 'ViewPolicy',
117     options => {
118         interface => 'opac',
119         frameworkcode => $framework
120     }
121 });
122 $record_processor->process($record);
123
124 # some useful variables for enhanced content;
125 # in each case, we're grabbing the first value we find in
126 # the record and normalizing it
127 my $upc = GetNormalizedUPC($record,$marcflavour);
128 my $ean = GetNormalizedEAN($record,$marcflavour);
129 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
130 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
131 my $content_identifier_exists;
132 if ( $isbn or $ean or $oclc or $upc ) {
133     $content_identifier_exists = 1;
134 }
135 $template->param(
136     normalized_upc => $upc,
137     normalized_ean => $ean,
138     normalized_oclc => $oclc,
139     normalized_isbn => $isbn,
140     content_identifier_exists => $content_identifier_exists,
141 );
142
143 #coping with subscriptions
144 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
145 my $dbh = C4::Context->dbh;
146 my $dat                 = TransformMarcToKoha( $record );
147
148 my @subscriptions       = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
149 my @subs;
150 foreach my $subscription (@subscriptions) {
151     my %cell;
152         my $serials_to_display;
153     $cell{subscriptionid}    = $subscription->{subscriptionid};
154     $cell{subscriptionnotes} = $subscription->{notes};
155     $cell{branchcode}        = $subscription->{branchcode};
156
157     #get the three latest serials.
158         $serials_to_display = $subscription->{opacdisplaycount};
159         $serials_to_display = C4::Context->preference('OPACSerialIssueDisplayCount') unless $serials_to_display;
160         $cell{opacdisplaycount} = $serials_to_display;
161     $cell{latestserials} =
162       GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
163     push @subs, \%cell;
164 }
165
166 $template->param(
167     subscriptions       => \@subs,
168     subscriptionsnumber => $subscriptionsnumber,
169 );
170
171 my $norequests = 1;
172 my $allow_onshelf_holds;
173 my $res = GetISBDView({
174     'record'    => $record,
175     'template'  => 'opac',
176     'framework' => $framework
177 });
178
179 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
180 my $patron = Koha::Patrons->find( $loggedinuser );
181 for my $itm (@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;