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