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