Bug Fixing.
[koha.git] / catalogue / 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 ISBDdetail.pl : script to show a biblio in ISBD format
23
24 =head1 SYNOPSIS
25
26
27 =head1 DESCRIPTION
28
29 This script needs a biblionumber as parameter 
30
31 =head1 FUNCTIONS
32
33 =over 2
34
35 =cut
36
37 use strict;
38 require Exporter;
39 use C4::Auth;
40 use C4::Context;
41 use C4::Output;
42 use CGI;
43 use C4::Koha;
44 use C4::Biblio;
45 use C4::Branch;     # GetBranchDetail
46 use C4::Serials;    # CountSubscriptionFromBiblionumber
47
48
49 #---- Internal function
50
51 sub get_authorised_value_desc ($$$$$$$) {
52     my ( $itemtype, $tagslib, $tag, $subfield, $value, $framework, $dbh ) = @_;
53
54     #---- branch
55     if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
56         return GetBranchDetail($value)->{branchname};
57     }
58
59     #---- itemtypes
60     if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) {
61         my $itemtypedef = getitemtypeinfo($itemtype);
62         return $itemtypedef->{description};
63     }
64
65     #---- "true" authorized value
66     my $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'};
67
68     if ( $category ne "" ) {
69         my $sth =
70           $dbh->prepare(
71 "select lib from authorised_values where category = ? and authorised_value = ?"
72           );
73         $sth->execute( $category, $value );
74         my $data = $sth->fetchrow_hashref;
75         return $data->{'lib'};
76     }
77     else {
78         return $value;    # if nothing is found return the original value
79     }
80 }
81 # ------
82
83
84 my $query = new CGI;
85 my $dbh = C4::Context->dbh;
86
87 my $biblionumber = $query->param('biblionumber');
88 my $itemtype     = &GetFrameworkCode($biblionumber);
89 my $tagslib      = &GetMarcStructure( 1, $itemtype );
90
91 my $record = GetMarcBiblio($biblionumber);
92
93 # open template
94 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
95     {
96         template_name => "catalogue/ISBDdetail.tmpl",
97         query         => $query,
98         type          => "intranet",
99         authnotrequired => 0,
100         flagsrequired   => { catalogue => 1 },
101     }
102 );
103
104 my $ISBD = C4::Context->preference('ISBD');
105
106 # my @blocs = split /\@/,$ISBD;
107 # my @fields = $record->fields();
108 my $res;
109
110 # foreach my $bloc (@blocs) {
111 #     $bloc =~ s/\n//g;
112 my $bloc = $ISBD;
113 my $blocres;
114 foreach my $isbdfield ( split /#/, $bloc ) {
115
116     #         $isbdfield= /(.?.?.?)/;
117     $isbdfield =~ /(\d\d\d)\|(.*)\|(.*)\|(.*)/;
118     my $fieldvalue    = $1;
119     my $textbefore    = $2;
120     my $analysestring = $3;
121     my $textafter     = $4;
122
123     #         warn "==> $1 / $2 / $3 / $4";
124     #         my $fieldvalue=substr($isbdfield,0,3);
125     if ( $fieldvalue > 0 ) {
126
127    #         warn "ERROR IN ISBD DEFINITION at : $isbdfield" unless $fieldvalue;
128    #             warn "FV : $fieldvalue";
129         my $hasputtextbefore = 0;
130         foreach my $field ( $record->field($fieldvalue) ) {
131             my $calculated = $analysestring;
132             my $tag        = $field->tag();
133             if ( $tag < 10 ) {
134             }
135             else {
136                 my @subf = $field->subfields;
137                 for my $i ( 0 .. $#subf ) {
138                     my $subfieldcode  = $subf[$i][0];
139                     my $subfieldvalue =
140                       get_authorised_value_desc( $itemtype,$tagslib, $tag, $subf[$i][0],
141                         $subf[$i][1], '', $dbh );
142                     my $tagsubf = $tag . $subfieldcode;
143                     $calculated =~
144 s/\{(.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue\{$1$tagsubf$2\}$2/g;
145                 }
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         $blocres .= $textafter if $hasputtextbefore;
160     }
161     else {
162         $blocres .= $isbdfield;
163     }
164 }
165 $res .= $blocres;
166
167 # }
168 $res =~ s/\{(.*?)\}//g;
169 $res =~ s/\\n/\n/g;
170 $res =~ s/\n/<br\/>/g;
171
172 # remove empty ()
173 $res =~ s/\(\)//g;
174 # count of item linked with biblio
175 my $itemcount = GetItemsCount($biblionumber);
176 $template->param( count => $itemcount);
177 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
178  
179 if ($subscriptionsnumber) {
180     my $subscriptions     = GetSubscriptionsFromBiblionumber($biblionumber);
181     my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
182     $template->param(
183         subscriptionsnumber => $subscriptionsnumber,
184         subscriptiontitle   => $subscriptiontitle,
185     );
186 }
187
188 $template->param (
189     ISBD                => $res,
190     biblionumber        => $biblionumber,
191 );
192
193 output_html_with_http_headers $query, $cookie, $template->output;
194