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