reduce bib fetches during search and OPAC display
[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;       # use getitemtypeinfo
55 use C4::Members;    # GetMember
56
57 my $query = new CGI;
58
59 my $dbh = C4::Context->dbh;
60
61 my $biblionumber = $query->param('biblionumber');
62 my $itemtype     = &GetFrameworkCode($biblionumber);
63 my $tagslib      = &GetMarcStructure( 1, $itemtype );
64
65 my $marcflavour      = C4::Context->preference("marcflavour");
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     $cell{branchcode}        = $subscription->{branchcode};
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 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$itemtype);
112
113 foreach my $isbdfield ( split /#/, $bloc ) {
114
115     #         $isbdfield= /(.?.?.?)/;
116     $isbdfield =~ /(\d\d\d)([^\|])?\|(.*)\|(.*)\|(.*)/;
117     my $fieldvalue    = $1;
118     my $subfvalue = $2;
119     my $textbefore    = $3;
120     my $analysestring = $4;
121     my $textafter     = $5;
122
123     #         warn "==> $1 / $2 / $3 / $4";
124     #         my $fieldvalue=substr($isbdfield,0,3);
125     if ( $fieldvalue > 0 ) {
126         my $hasputtextbefore = 0;
127         my @fieldslist = $record->field($fieldvalue);
128         @fieldslist = sort {$a->subfield($holdingbrtagsubf) cmp $b->subfield($holdingbrtagsubf)} @fieldslist if ($fieldvalue eq $holdingbrtagf);
129
130         #         warn "ERROR IN ISBD DEFINITION at : $isbdfield" unless $fieldvalue;
131         #             warn "FV : $fieldvalue";
132         if ($subfvalue ne ""){
133           foreach my $field ( @fieldslist ) {
134             foreach my $subfield ($field->subfield($subfvalue)){
135               warn $fieldvalue."$subfvalue";    
136               my $calculated = $analysestring;
137               my $tag        = $field->tag();
138               if ( $tag < 10 ) {
139               }
140               else {
141                 my $subfieldvalue =
142                 GetAuthorisedValueDesc( $tag, $subfvalue,
143                   $subfield, '', $tagslib );
144                 my $tagsubf = $tag . $subfvalue;
145                 $calculated =~
146                       s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
147                 $calculated =~s#/cgi-bin/koha/[^/]+/([^.]*.pl\?.*)$#opac-$1#g;
148             
149                 # field builded, store the result
150                 if ( $calculated && !$hasputtextbefore )
151                 {    # put textbefore if not done
152                 $blocres .= $textbefore;
153                 $hasputtextbefore = 1;
154                 }
155             
156                 # remove punctuation at start
157                 $calculated =~ s/^( |;|:|\.|-)*//g;
158                 $blocres .= $calculated;
159                             
160               }         
161             }          
162           }
163           $blocres .= $textafter if $hasputtextbefore;
164         } else {    
165         foreach my $field ( @fieldslist ) {
166           my $calculated = $analysestring;
167           my $tag        = $field->tag();
168           if ( $tag < 10 ) {
169           }
170           else {
171             my @subf = $field->subfields;
172             for my $i ( 0 .. $#subf ) {
173             my $valuecode   = $subf[$i][1];
174             my $subfieldcode  = $subf[$i][0];
175             my $subfieldvalue =
176             GetAuthorisedValueDesc( $tag, $subf[$i][0],
177               $subf[$i][1], '', $tagslib );
178             my $tagsubf = $tag . $subfieldcode;
179
180             $calculated =~ s/                  # replace all {{}} codes by the value code.
181                               \{\{$tagsubf\}\} # catch the {{actualcode}}
182                             /
183                               $valuecode     # replace by the value code
184                            /gx;
185
186             $calculated =~
187         s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
188         $calculated =~s#/cgi-bin/koha/[^/]+/([^.]*.pl\?.*)$#opac-$1#g;
189             }
190
191             # field builded, store the result
192             if ( $calculated && !$hasputtextbefore )
193             {    # put textbefore if not done
194             $blocres .= $textbefore;
195             $hasputtextbefore = 1;
196             }
197
198             # remove punctuation at start
199             $calculated =~ s/^( |;|:|\.|-)*//g;
200             $blocres .= $calculated;
201           }
202         }
203         $blocres .= $textafter if $hasputtextbefore;
204         }       
205     }
206     else {
207         $blocres .= $isbdfield;
208     }
209 }
210 $res .= $blocres;
211
212 # }
213 $res =~ s/\{(.*?)\}//g;
214 $res =~ s/\\n/\n/g;
215 $res =~ s/\n/<br\/>/g;
216
217 # remove empty ()
218 $res =~ s/\(\)//g;
219
220 my $reviews = getreviews( $biblionumber, 1 );
221 foreach ( @$reviews ) {
222     my $borrower_number_review = $_->{borrowernumber};
223     my $borrowerData           = GetMember($borrower_number_review,'borrowernumber');
224     # setting some borrower info into this hash
225     $_->{title}     = $borrowerData->{'title'};
226     $_->{surname}   = $borrowerData->{'surname'};
227     $_->{firstname} = $borrowerData->{'firstname'};
228 }
229
230
231 $template->param(
232     ISBD         => $res,
233     biblionumber => $biblionumber,
234     reviews             => $reviews,
235 );
236
237 ## Amazon.com stuff
238 #not used unless preference set
239 if ( C4::Context->preference("AmazonContent") == 1 ) {
240     use C4::Amazon;
241     $dat->{'amazonisbn'} = $dat->{'isbn'};
242     $dat->{'amazonisbn'} =~ s|-||g;
243
244     $template->param( amazonisbn => $dat->{amazonisbn} );
245
246     my $amazon_details = &get_amazon_details( $dat->{amazonisbn}, $record, $marcflavour );
247
248     foreach my $result ( @{ $amazon_details->{Details} } ) {
249         $template->param( item_description => $result->{ProductDescription} );
250         $template->param( image            => $result->{ImageUrlMedium} );
251         $template->param( list_price       => $result->{ListPrice} );
252         $template->param( amazon_url       => $result->{url} );
253     }
254
255     my @products;
256     my @reviews;
257     for my $details ( @{ $amazon_details->{Details} } ) {
258         next unless $details->{SimilarProducts};
259         for my $product ( @{ $details->{SimilarProducts}->{Product} } ) {
260             push @products, +{ Product => $product };
261         }
262         next unless $details->{Reviews};
263         for my $product ( @{ $details->{Reviews}->{AvgCustomerRating} } ) {
264             $template->param( rating => $product * 20 );
265         }
266         for my $reviews ( @{ $details->{Reviews}->{CustomerReview} } ) {
267             push @reviews,
268               +{
269                 Summary => $reviews->{Summary},
270                 Comment => $reviews->{Comment},
271               };
272         }
273     }
274     $template->param( SIMILAR_PRODUCTS => \@products );
275     $template->param( AMAZONREVIEWS    => \@reviews );
276 }
277
278 output_html_with_http_headers $query, $cookie, $template->output;