Patch from Joe Atzberger to remove $Id$ and $Log$ from scripts
[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 require Exporter;
46 use C4::Auth;
47 use C4::Context;
48 use C4::Output;
49 use CGI;
50 use MARC::Record;
51 use C4::Biblio;
52 use C4::Acquisition;
53 use C4::Review;
54 use C4::Serials;    # uses getsubscriptionfrom biblionumber
55 use C4::Koha;       # use getitemtypeinfo
56 use C4::Members;    # GetMember
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 $record = GetMarcBiblio($biblionumber);
67
68 #coping with subscriptions
69 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
70 my $dat                 = TransformMarcToKoha( $dbh, $record );
71 my @subscriptions       =
72   GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
73 my @subs;
74 foreach my $subscription (@subscriptions) {
75     my %cell;
76     $cell{subscriptionid}    = $subscription->{subscriptionid};
77     $cell{subscriptionnotes} = $subscription->{notes};
78
79     #get the three latest serials.
80     $cell{latestserials} =
81       GetLatestSerials( $subscription->{subscriptionid}, 3 );
82     push @subs, \%cell;
83 }
84
85 # open template
86 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
87     {
88         template_name   => "opac-ISBDdetail.tmpl",
89         query           => $query,
90         type            => "opac",
91         authnotrequired => 1,
92         debug           => 1,
93     }
94 );
95 $template->param(
96     subscriptions       => \@subs,
97     subscriptionsnumber => $subscriptionsnumber,
98 );
99
100 my $ISBD = C4::Context->preference('ISBD');
101
102 # my @blocs = split /\@/,$ISBD;
103 # my @fields = $record->fields();
104 my $res;
105
106 # foreach my $bloc (@blocs) {
107 #     $bloc =~ s/\n//g;
108 my $bloc = $ISBD;
109 my $blocres;
110 foreach my $isbdfield ( split /#/, $bloc ) {
111
112     #         $isbdfield= /(.?.?.?)/;
113     $isbdfield =~ /(\d\d\d)\|(.*)\|(.*)\|(.*)/;
114     my $fieldvalue    = $1;
115     my $textbefore    = $2;
116     my $analysestring = $3;
117     my $textafter     = $4;
118
119     #         warn "==> $1 / $2 / $3 / $4";
120     #         my $fieldvalue=substr($isbdfield,0,3);
121     if ( $fieldvalue > 0 ) {
122
123         #         warn "ERROR IN ISBD DEFINITION at : $isbdfield" unless $fieldvalue;
124         #             warn "FV : $fieldvalue";
125         my $hasputtextbefore = 0;
126         foreach my $field ( $record->field($fieldvalue) ) {
127             my $calculated = $analysestring;
128             my $tag        = $field->tag();
129             if ( $tag < 10 ) {
130             }
131             else {
132                 my @subf = $field->subfields;
133                 for my $i ( 0 .. $#subf ) {
134                     my $subfieldcode  = $subf[$i][0];
135                     my $subfieldvalue =
136                       GetAuthorisedValueDesc( $tag, $subf[$i][0],
137                         $subf[$i][1], '', $tagslib );
138                     my $tagsubf = $tag . $subfieldcode;
139                     $calculated =~
140 s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
141                 }
142
143                 # field builded, store the result
144                 if ( $calculated && !$hasputtextbefore )
145                 {    # put textbefore if not done
146                     $blocres .= $textbefore;
147                     $hasputtextbefore = 1;
148                 }
149
150                 # remove punctuation at start
151                 $calculated =~ s/^( |;|:|\.|-)*//g;
152                 $blocres .= $calculated;
153             }
154         }
155         $blocres .= $textafter if $hasputtextbefore;
156     }
157     else {
158         $blocres .= $isbdfield;
159     }
160 }
161 $res .= $blocres;
162
163 # }
164 $res =~ s/\{(.*?)\}//g;
165 $res =~ s/\\n/\n/g;
166 $res =~ s/\n/<br\/>/g;
167
168 # remove empty ()
169 $res =~ s/\(\)//g;
170
171 my $reviews = getreviews( $biblionumber, 1 );
172 foreach ( @$reviews ) {
173     my $borrower_number_review = $_->{borrowernumber};
174     my $borrowerData           = GetMember($borrower_number_review,'borrowernumber');
175     # setting some borrower info into this hash
176     $_->{title}     = $borrowerData->{'title'};
177     $_->{surname}   = $borrowerData->{'surname'};
178     $_->{firstname} = $borrowerData->{'firstname'};
179 }
180
181
182 $template->param(
183     ISBD         => $res,
184     biblionumber => $biblionumber,
185     reviews             => $reviews,
186 );
187
188 ## Amazon.com stuff
189 #not used unless preference set
190 if ( C4::Context->preference("AmazonContent") == 1 ) {
191     use C4::Amazon;
192     $dat->{'amazonisbn'} = $dat->{'isbn'};
193     $dat->{'amazonisbn'} =~ s|-||g;
194
195     $template->param( amazonisbn => $dat->{amazonisbn} );
196
197     my $amazon_details = &get_amazon_details( $dat->{amazonisbn} );
198
199     foreach my $result ( @{ $amazon_details->{Details} } ) {
200         $template->param( item_description => $result->{ProductDescription} );
201         $template->param( image            => $result->{ImageUrlMedium} );
202         $template->param( list_price       => $result->{ListPrice} );
203         $template->param( amazon_url       => $result->{url} );
204     }
205
206     my @products;
207     my @reviews;
208     for my $details ( @{ $amazon_details->{Details} } ) {
209         next unless $details->{SimilarProducts};
210         for my $product ( @{ $details->{SimilarProducts}->{Product} } ) {
211             push @products, +{ Product => $product };
212         }
213         next unless $details->{Reviews};
214         for my $product ( @{ $details->{Reviews}->{AvgCustomerRating} } ) {
215             $template->param( rating => $product * 20 );
216         }
217         for my $reviews ( @{ $details->{Reviews}->{CustomerReview} } ) {
218             push @reviews,
219               +{
220                 Summary => $reviews->{Summary},
221                 Comment => $reviews->{Comment},
222               };
223         }
224     }
225     $template->param( SIMILAR_PRODUCTS => \@products );
226     $template->param( AMAZONREVIEWS    => \@reviews );
227 }
228
229 output_html_with_http_headers $query, $cookie, $template->output;