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