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