bug 3088: fix crash in opac-ISBDdetail.pl
[koha.git] / opac / opac-ISBDdetail.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20
21 =head1 NAME
22
23 opac-ISBDdetail.pl : script to show a biblio in ISBD format
24
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.tmpl.
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 =over 2
41
42 =cut
43
44 use strict;
45 use C4::Auth;
46 use C4::Context;
47 use C4::Output;
48 use CGI;
49 use MARC::Record;
50 use C4::Biblio;
51 use C4::Acquisition;
52 use C4::Review;
53 use C4::Serials;    # uses getsubscriptionfrom biblionumber
54 use C4::Koha;
55 use C4::Members;    # GetMember
56 use C4::External::Amazon;
57
58 my $query = CGI->new();
59 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
60     {
61         template_name   => "opac-ISBDdetail.tmpl",
62         query           => $query,
63         type            => "opac",
64         authnotrequired => 1,
65         debug           => 1,
66     }
67 );
68
69 my $biblionumber = $query->param('biblionumber');
70 my $itemtype     = &GetFrameworkCode($biblionumber);
71 my $tagslib      = &GetMarcStructure( 1, $itemtype );
72
73 my $marcflavour      = C4::Context->preference("marcflavour");
74 my $record = GetMarcBiblio($biblionumber);
75
76 # some useful variables for enhanced content;
77 # in each case, we're grabbing the first value we find in
78 # the record and normalizing it
79 my $upc = GetNormalizedUPC($record,$marcflavour);
80 my $ean = GetNormalizedEAN($record,$marcflavour);
81 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
82 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
83 my $content_identifier_exists = 1 if ($isbn or $ean or $oclc or $upc);
84 $template->param(
85     normalized_upc => $upc,
86     normalized_ean => $ean,
87     normalized_oclc => $oclc,
88     normalized_isbn => $isbn,
89         content_identifier_exists => $content_identifier_exists,
90 );
91
92 #coping with subscriptions
93 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
94 my $dbh = C4::Context->dbh;
95 my $dat                 = TransformMarcToKoha( $dbh, $record );
96 my @subscriptions       =
97   GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
98 my @subs;
99 foreach my $subscription (@subscriptions) {
100     my %cell;
101         my $serials_to_display;
102     $cell{subscriptionid}    = $subscription->{subscriptionid};
103     $cell{subscriptionnotes} = $subscription->{notes};
104     $cell{branchcode}        = $subscription->{branchcode};
105
106     #get the three latest serials.
107         $serials_to_display = $subscription->{opacdisplaycount};
108         $serials_to_display = C4::Context->preference('OPACSerialIssueDisplayCount') unless $serials_to_display;
109         $cell{opacdisplaycount} = $serials_to_display;
110     $cell{latestserials} =
111       GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
112     push @subs, \%cell;
113 }
114
115 $template->param(
116     subscriptions       => \@subs,
117     subscriptionsnumber => $subscriptionsnumber,
118 );
119
120 my $ISBD = C4::Context->preference('ISBD');
121
122 # my @blocs = split /\@/,$ISBD;
123 # my @fields = $record->fields();
124 my $res;
125
126 # foreach my $bloc (@blocs) {
127 #     $bloc =~ s/\n//g;
128 my $bloc = $ISBD;
129 my $blocres;
130 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$itemtype);
131
132 foreach my $isbdfield ( split /#/, $bloc ) {
133
134     #         $isbdfield= /(.?.?.?)/;
135     $isbdfield =~ /(\d\d\d)([^\|])?\|(.*)\|(.*)\|(.*)/;
136     my $fieldvalue    = $1;
137     my $subfvalue = $2;
138     my $textbefore    = $3;
139     my $analysestring = $4;
140     my $textafter     = $5;
141
142     #         warn "==> $1 / $2 / $3 / $4";
143     #         my $fieldvalue=substr($isbdfield,0,3);
144     if ( $fieldvalue > 0 ) {
145         my $hasputtextbefore = 0;
146         my @fieldslist = $record->field($fieldvalue);
147         @fieldslist = sort {$a->subfield($holdingbrtagsubf) cmp $b->subfield($holdingbrtagsubf)} @fieldslist if ($fieldvalue eq $holdingbrtagf);
148
149         #         warn "ERROR IN ISBD DEFINITION at : $isbdfield" unless $fieldvalue;
150         #             warn "FV : $fieldvalue";
151         if ($subfvalue ne ""){
152           foreach my $field ( @fieldslist ) {
153             foreach my $subfield ($field->subfield($subfvalue)){
154               warn $fieldvalue."$subfvalue";    
155               my $calculated = $analysestring;
156               my $tag        = $field->tag();
157               if ( $tag < 10 ) {
158               }
159               else {
160                 my $subfieldvalue =
161                 GetAuthorisedValueDesc( $tag, $subfvalue,
162                   $subfield, '', $tagslib );
163                 my $tagsubf = $tag . $subfvalue;
164                 $calculated =~
165                       s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
166                 $calculated =~s#/cgi-bin/koha/[^/]+/([^.]*.pl\?.*)$#opac-$1#g;
167             
168                 # field builded, store the result
169                 if ( $calculated && !$hasputtextbefore )
170                 {    # put textbefore if not done
171                 $blocres .= $textbefore;
172                 $hasputtextbefore = 1;
173                 }
174             
175                 # remove punctuation at start
176                 $calculated =~ s/^( |;|:|\.|-)*//g;
177                 $blocres .= $calculated;
178                             
179               }         
180             }          
181           }
182           $blocres .= $textafter if $hasputtextbefore;
183         } else {    
184         foreach my $field ( @fieldslist ) {
185           my $calculated = $analysestring;
186           my $tag        = $field->tag();
187           if ( $tag < 10 ) {
188           }
189           else {
190             my @subf = $field->subfields;
191             for my $i ( 0 .. $#subf ) {
192             my $subfieldcode  = $subf[$i][0];
193             my $subfieldvalue =
194             GetAuthorisedValueDesc( $tag, $subf[$i][0],
195               $subf[$i][1], '', $tagslib );
196             my $tagsubf = $tag . $subfieldcode;
197             $calculated =~
198         s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
199         $calculated =~s#/cgi-bin/koha/[^/]+/([^.]*.pl\?.*)$#opac-$1#g;
200             }
201         
202             # field builded, store the result
203             if ( $calculated && !$hasputtextbefore )
204             {    # put textbefore if not done
205             $blocres .= $textbefore;
206             $hasputtextbefore = 1;
207             }
208         
209             # remove punctuation at start
210             $calculated =~ s/^( |;|:|\.|-)*//g;
211             $blocres .= $calculated;
212           }
213         }
214         $blocres .= $textafter if $hasputtextbefore;
215         }       
216     }
217     else {
218         $blocres .= $isbdfield;
219     }
220 }
221 $res .= $blocres;
222
223 # }
224 $res =~ s/\{(.*?)\}//g;
225 $res =~ s/\\n/\n/g;
226 $res =~ s/\n/<br\/>/g;
227
228 # remove empty ()
229 $res =~ s/\(\)//g;
230
231 my $reviews = getreviews( $biblionumber, 1 );
232 foreach ( @$reviews ) {
233     my $borrower_number_review = $_->{borrowernumber};
234     my $borrowerData           = GetMember($borrower_number_review,'borrowernumber');
235     # setting some borrower info into this hash
236     $_->{title}     = $borrowerData->{'title'};
237     $_->{surname}   = $borrowerData->{'surname'};
238     $_->{firstname} = $borrowerData->{'firstname'};
239 }
240
241
242 $template->param(
243     ISBD         => $res,
244     biblionumber => $biblionumber,
245     reviews             => $reviews,
246 );
247
248 ## Amazon.com stuff
249 #not used unless preference set
250 if ( C4::Context->preference("OPACAmazonEnabled") == 1 ) {
251
252     my $amazon_details = &get_amazon_details( $isbn, $record, $marcflavour );
253
254     foreach my $result ( @{ $amazon_details->{Details} } ) {
255         $template->param( item_description => $result->{ProductDescription} );
256         $template->param( image            => $result->{ImageUrlMedium} );
257         $template->param( list_price       => $result->{ListPrice} );
258         $template->param( amazon_url       => $result->{url} );
259     }
260
261     my @products;
262     my @reviews;
263     for my $details ( @{ $amazon_details->{Details} } ) {
264         next unless $details->{SimilarProducts};
265         for my $product ( @{ $details->{SimilarProducts}->{Product} } ) {
266             push @products, +{ Product => $product };
267         }
268         next unless $details->{Reviews};
269         for my $product ( @{ $details->{Reviews}->{AvgCustomerRating} } ) {
270             $template->param( rating => $product * 20 );
271         }
272         for my $reviews ( @{ $details->{Reviews}->{CustomerReview} } ) {
273             push @reviews,
274               +{
275                 Summary => $reviews->{Summary},
276                 Comment => $reviews->{Comment},
277               };
278         }
279     }
280     $template->param( SIMILAR_PRODUCTS => \@products );
281     $template->param( AMAZONREVIEWS    => \@reviews );
282 }
283
284 output_html_with_http_headers $query, $cookie, $template->output;