Fix for LL Bug 405: Syndetics Enhanced Content: some cover imags are being generated...
[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 = new CGI;
59
60 my $dbh = C4::Context->dbh;
61
62 my $biblionumber = $query->param('biblionumber');
63 my $itemtype     = &GetFrameworkCode($biblionumber);
64 my $tagslib      = &GetMarcStructure( 1, $itemtype );
65
66 my $marcflavour      = C4::Context->preference("marcflavour");
67 my $record = GetMarcBiblio($biblionumber);
68
69 # some useful variables for enhanced content;
70 # in each case, we're grabbing the first value we find in
71 # the record and normalizing it
72 my $upc = GetNormalizedUPC($record,$marcflavour);
73 my $ean = GetNormalizedEAN($record,$marcflavour);
74 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
75 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
76 my $content_identifier_exists = 1 if ($isbn or $ean or $oclc);
77 $template->param(
78     normalized_upc => $upc,
79     normalized_ean => $ean,
80     normalized_oclc => $oclc,
81     normalized_isbn => $isbn,
82         content_identifier_exists => $content_identifier_exists,
83 );
84
85 #coping with subscriptions
86 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
87 my $dat                 = TransformMarcToKoha( $dbh, $record );
88 my @subscriptions       =
89   GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
90 my @subs;
91 foreach my $subscription (@subscriptions) {
92     my %cell;
93     $cell{subscriptionid}    = $subscription->{subscriptionid};
94     $cell{subscriptionnotes} = $subscription->{notes};
95     $cell{branchcode}        = $subscription->{branchcode};
96
97     #get the three latest serials.
98     $cell{latestserials} =
99       GetLatestSerials( $subscription->{subscriptionid}, 3 );
100     push @subs, \%cell;
101 }
102
103 # open template
104 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
105     {
106         template_name   => "opac-ISBDdetail.tmpl",
107         query           => $query,
108         type            => "opac",
109         authnotrequired => 1,
110         debug           => 1,
111     }
112 );
113 $template->param(
114     subscriptions       => \@subs,
115     subscriptionsnumber => $subscriptionsnumber,
116 );
117
118 my $ISBD = C4::Context->preference('ISBD');
119
120 # my @blocs = split /\@/,$ISBD;
121 # my @fields = $record->fields();
122 my $res;
123
124 # foreach my $bloc (@blocs) {
125 #     $bloc =~ s/\n//g;
126 my $bloc = $ISBD;
127 my $blocres;
128 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$itemtype);
129
130 foreach my $isbdfield ( split /#/, $bloc ) {
131
132     #         $isbdfield= /(.?.?.?)/;
133     $isbdfield =~ /(\d\d\d)([^\|])?\|(.*)\|(.*)\|(.*)/;
134     my $fieldvalue    = $1;
135     my $subfvalue = $2;
136     my $textbefore    = $3;
137     my $analysestring = $4;
138     my $textafter     = $5;
139
140     #         warn "==> $1 / $2 / $3 / $4";
141     #         my $fieldvalue=substr($isbdfield,0,3);
142     if ( $fieldvalue > 0 ) {
143         my $hasputtextbefore = 0;
144         my @fieldslist = $record->field($fieldvalue);
145         @fieldslist = sort {$a->subfield($holdingbrtagsubf) cmp $b->subfield($holdingbrtagsubf)} @fieldslist if ($fieldvalue eq $holdingbrtagf);
146
147         #         warn "ERROR IN ISBD DEFINITION at : $isbdfield" unless $fieldvalue;
148         #             warn "FV : $fieldvalue";
149         if ($subfvalue ne ""){
150           foreach my $field ( @fieldslist ) {
151             foreach my $subfield ($field->subfield($subfvalue)){
152               warn $fieldvalue."$subfvalue";    
153               my $calculated = $analysestring;
154               my $tag        = $field->tag();
155               if ( $tag < 10 ) {
156               }
157               else {
158                 my $subfieldvalue =
159                 GetAuthorisedValueDesc( $tag, $subfvalue,
160                   $subfield, '', $tagslib );
161                 my $tagsubf = $tag . $subfvalue;
162                 $calculated =~
163                       s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
164                 $calculated =~s#/cgi-bin/koha/[^/]+/([^.]*.pl\?.*)$#opac-$1#g;
165             
166                 # field builded, store the result
167                 if ( $calculated && !$hasputtextbefore )
168                 {    # put textbefore if not done
169                 $blocres .= $textbefore;
170                 $hasputtextbefore = 1;
171                 }
172             
173                 # remove punctuation at start
174                 $calculated =~ s/^( |;|:|\.|-)*//g;
175                 $blocres .= $calculated;
176                             
177               }         
178             }          
179           }
180           $blocres .= $textafter if $hasputtextbefore;
181         } else {    
182         foreach my $field ( @fieldslist ) {
183           my $calculated = $analysestring;
184           my $tag        = $field->tag();
185           if ( $tag < 10 ) {
186           }
187           else {
188             my @subf = $field->subfields;
189             for my $i ( 0 .. $#subf ) {
190             my $subfieldcode  = $subf[$i][0];
191             my $subfieldvalue =
192             GetAuthorisedValueDesc( $tag, $subf[$i][0],
193               $subf[$i][1], '', $tagslib );
194             my $tagsubf = $tag . $subfieldcode;
195             $calculated =~
196         s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
197         $calculated =~s#/cgi-bin/koha/[^/]+/([^.]*.pl\?.*)$#opac-$1#g;
198             }
199         
200             # field builded, store the result
201             if ( $calculated && !$hasputtextbefore )
202             {    # put textbefore if not done
203             $blocres .= $textbefore;
204             $hasputtextbefore = 1;
205             }
206         
207             # remove punctuation at start
208             $calculated =~ s/^( |;|:|\.|-)*//g;
209             $blocres .= $calculated;
210           }
211         }
212         $blocres .= $textafter if $hasputtextbefore;
213         }       
214     }
215     else {
216         $blocres .= $isbdfield;
217     }
218 }
219 $res .= $blocres;
220
221 # }
222 $res =~ s/\{(.*?)\}//g;
223 $res =~ s/\\n/\n/g;
224 $res =~ s/\n/<br\/>/g;
225
226 # remove empty ()
227 $res =~ s/\(\)//g;
228
229 my $reviews = getreviews( $biblionumber, 1 );
230 foreach ( @$reviews ) {
231     my $borrower_number_review = $_->{borrowernumber};
232     my $borrowerData           = GetMember($borrower_number_review,'borrowernumber');
233     # setting some borrower info into this hash
234     $_->{title}     = $borrowerData->{'title'};
235     $_->{surname}   = $borrowerData->{'surname'};
236     $_->{firstname} = $borrowerData->{'firstname'};
237 }
238
239
240 $template->param(
241     ISBD         => $res,
242     biblionumber => $biblionumber,
243     reviews             => $reviews,
244 );
245
246 ## Amazon.com stuff
247 #not used unless preference set
248 if ( C4::Context->preference("OPACAmazonEnabled") == 1 ) {
249
250     my $amazon_details = &get_amazon_details( $isbn, $record, $marcflavour );
251
252     foreach my $result ( @{ $amazon_details->{Details} } ) {
253         $template->param( item_description => $result->{ProductDescription} );
254         $template->param( image            => $result->{ImageUrlMedium} );
255         $template->param( list_price       => $result->{ListPrice} );
256         $template->param( amazon_url       => $result->{url} );
257     }
258
259     my @products;
260     my @reviews;
261     for my $details ( @{ $amazon_details->{Details} } ) {
262         next unless $details->{SimilarProducts};
263         for my $product ( @{ $details->{SimilarProducts}->{Product} } ) {
264             push @products, +{ Product => $product };
265         }
266         next unless $details->{Reviews};
267         for my $product ( @{ $details->{Reviews}->{AvgCustomerRating} } ) {
268             $template->param( rating => $product * 20 );
269         }
270         for my $reviews ( @{ $details->{Reviews}->{CustomerReview} } ) {
271             push @reviews,
272               +{
273                 Summary => $reviews->{Summary},
274                 Comment => $reviews->{Comment},
275               };
276         }
277     }
278     $template->param( SIMILAR_PRODUCTS => \@products );
279     $template->param( AMAZONREVIEWS    => \@reviews );
280 }
281
282 output_html_with_http_headers $query, $cookie, $template->output;