Using get_record() from C4::Search now (ie fetching the record from zebra)
[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 =head1 NAME
21
22 MARCdetail.pl : script to show a biblio in MARC format
23
24 =head1 SYNOPSIS
25
26
27 =head1 DESCRIPTION
28
29 This script needs a biblionumber in bib parameter (bibnumber
30 from koha style DB.  Automaticaly maps to marc biblionumber).
31
32 It shows the biblio in a (nice) MARC format depending on MARC
33 parameters tables.
34
35 The template is in <templates_dir>/catalogue/MARCdetail.tmpl.
36 this template must be divided into 11 "tabs".
37
38 The first 10 tabs present the biblio, the 11th one presents
39 the items attached to the biblio
40
41 =head1 FUNCTIONS
42
43 =over 2
44
45 =cut
46
47
48 use strict;
49 require Exporter;
50 use C4::Auth;
51 use C4::Context;
52 use C4::Output;
53 use C4::Interface::CGI::Output;
54 use CGI;
55 use C4::Search;
56 use MARC::Record;
57 use C4::Biblio;
58 use C4::Acquisition;
59 use C4::Bull; #uses getsubscriptionfrom biblionumber
60 use HTML::Template;
61
62 my $query=new CGI;
63
64 my $dbh=C4::Context->dbh;
65
66 my $biblionumber=$query->param('bib');
67 my $bibid = $query->param('bibid');
68 #$bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber) unless $bibid;
69 #$biblionumber = &MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid) unless $biblionumber;
70 #my $itemtype = &MARCfind_frameworkcode($dbh,$bibid);
71 my $itemtype="";
72 my $tagslib = &MARCgettagslib($dbh,1,$itemtype);
73
74 my $record =get_record($biblionumber);
75
76 #coping with subscriptions
77 my $subscriptionsnumber = getsubscriptionfrombiblionumber($biblionumber);
78 my $dat = MARCmarc2koha($dbh,$record);
79 my @subscriptions = getsubscriptions($dat->{title},$dat->{issn},$biblionumber);
80 my @subs;
81 foreach my $subscription (@subscriptions){
82         my %cell;
83         $cell{subscriptionid}= $subscription->{subscriptionid};
84         $cell{subscriptionnotes}= $subscription->{notes};
85         #get the three latest serials.
86         $cell{latestserials}=getlatestserials($subscription->{subscriptionid},3);
87         push @subs, \%cell;
88 }
89
90 # open template
91 my ($template, $loggedinuser, $cookie)
92                 = get_template_and_user({template_name => "opac-ISBDdetail.tmpl",
93                              query => $query,
94                              type => "opac",
95                              authnotrequired => 1,
96                              debug => 1,
97                              });
98 $template->param(LibraryName => C4::Context->preference("LibraryName"),
99                                 suggestion => C4::Context->preference("suggestion"),
100                                 virtualshelves => C4::Context->preference("virtualshelves"),
101                                 subscriptions => \@subs,
102                                 subscriptionsnumber => $subscriptionsnumber,
103 );
104
105 my $ISBD = C4::Context->preference('ISBD');
106 # my @blocs = split /\@/,$ISBD;
107 # my @fields = $record->fields();
108 my $res;
109 # foreach my $bloc (@blocs) {
110 #       $bloc =~ s/\n//g;
111         my $bloc = $ISBD;
112         my $blocres;
113         foreach my $isbdfield (split /#/,$bloc) {
114 #               $isbdfield= /(.?.?.?)/;
115                 $isbdfield =~ /(\d\d\d)\|(.*)\|(.*)\|(.*)/;
116                 my $fieldvalue=$1;
117                 my $textbefore=$2;
118                 my $analysestring=$3;
119                 my $textafter=$4;
120 #               warn "==> $1 / $2 / $3 / $4";
121 #               my $fieldvalue=substr($isbdfield,0,3);
122                 if ($fieldvalue>0) {
123         #               warn "ERROR IN ISBD DEFINITION at : $isbdfield" unless $fieldvalue;
124 #                       warn "FV : $fieldvalue";
125                         my $hasputtextbefore=0;
126                         foreach my $field ($record->field($fieldvalue)) {
127                                 my $calculated = $analysestring;
128                                 my $tag = $field->tag();
129                                 if ($tag<10) {
130                                 } else {
131                                         my @subf = $field->subfields;
132                                         for my $i (0..$#subf) {
133                                                 my $subfieldcode = $subf[$i][0];
134                                                 my $subfieldvalue = get_authorised_value_desc($tag, $subf[$i][0], $subf[$i][1], '', $dbh);
135                                                 my $tagsubf = $tag.$subfieldcode;
136                                                 $calculated =~ s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
137                                         }
138                                         # field builded, store the result
139                                         if ($calculated && !$hasputtextbefore) { # put textbefore if not done
140                                                 $blocres .=$textbefore;
141                                                 $hasputtextbefore=1
142                                         }
143                                         # remove punctuation at start
144                                         $calculated =~ s/^( |;|:|\.|-)*//g;
145                                         $blocres.=$calculated;
146                                 }
147                         }
148                         $blocres .=$textafter if $hasputtextbefore;
149                 } else {
150                         $blocres.=$isbdfield;
151                 }
152         }
153         $res.=$blocres;
154 # }
155 $res =~ s/\{(.*?)\}//g;
156 $res =~ s/\\n/\n/g;
157 $res =~ s/\n/<br\/>/g;
158 # remove empty ()
159 $res =~ s/\(\)//g;
160 $template->param(ISBD => $res,
161                                 biblionumber => $biblionumber);
162
163 output_html_with_http_headers $query, $cookie, $template->output;
164
165 sub get_authorised_value_desc ($$$$$) {
166    my($tag, $subfield, $value, $framework, $dbh) = @_;
167
168    #---- branch
169     if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
170        return getbranchdetail($value)->{branchname};
171     }
172
173    #---- itemtypes
174    if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) {
175        return ItemType($value);
176     }
177
178    #---- "true" authorized value
179    my $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'};
180
181    if ($category ne "") {
182        my $sth = $dbh->prepare("select lib from authorised_values where category = ? and authorised_value = ?");
183        $sth->execute($category, $value);
184        my $data = $sth->fetchrow_hashref;
185        return $data->{'lib'};
186    } else {
187        return $value; # if nothing is found return the original value
188    }
189 }