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