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