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