Resynching Savannah
[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 in bib parameter (bibnumber
30 from koha style DB.  Automaticaly maps to marc biblionumber).
31
32 =head1 FUNCTIONS
33
34 =over 2
35
36 =cut
37
38
39 use strict;
40
41 use C4::Auth;
42 use C4::Context;
43 use C4::AuthoritiesMarc;
44 use C4::Interface::CGI::Output;
45 use CGI;
46 use C4::Search;
47 use C4::Biblio;
48 use C4::Acquisition;
49 use C4::Koha;
50
51 my $query=new CGI;
52
53 my $dbh=C4::Context->dbh;
54
55 my $biblionumber=$query->param('biblionumber');
56
57 my $itemtype = &MARCfind_frameworkcode($dbh,$biblionumber);
58 my $tagslib = &MARCgettagslib($dbh,1,$itemtype);
59
60 my $record =XMLgetbibliohash($dbh,$biblionumber);
61 # open template
62 my ($template, $loggedinuser, $cookie)
63                 = get_template_and_user({template_name => "catalogue/ISBDdetail.tmpl",
64                              query => $query,
65                              type => "intranet",
66                              authnotrequired => 1,
67                              debug => 1,
68                              });
69
70 my $ISBD = C4::Context->preference('ISBD');
71 my $res;
72         my $bloc = $ISBD;
73         my $blocres;
74         foreach my $isbdfield (split /#/,$bloc) {
75                 $isbdfield =~ /(\d\d\d)\|(.*)\|(.*)\|(.*)/;
76                 my $fieldvalue=$1;
77                 my $textbefore=$2;
78                 my $analysestring=$3;
79                 my $textafter=$4;
80                 if ($fieldvalue>0) {
81                         my $hasputtextbefore=0;
82                         
83                                 my $calculated = $analysestring;
84                                 my $tag = $fieldvalue;
85                                 if ($tag<10) {
86                                 my $value=XML_readline_onerecord($record,"","",$tag);
87                                 my $subfieldcode = "@";
88                                                 my $subfieldvalue = get_authorised_value_desc($tag, "", $value, '', $dbh);;
89                                                 my $tagsubf = $tag.$subfieldcode;
90                                                 $calculated =~ s/\{(.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue\{$1$tagsubf$2\}$2/g;
91                                         
92                                 } else {
93                                         my @subf = XML_readline_withtags($record,"","",$tag);
94                                 
95                                         for my $i (0..$#subf) {
96                                                 my $subfieldcode = $subf[$i][0];
97                                                 my $subfieldvalue = get_authorised_value_desc($tag, $subf[$i][0], $subf[$i][1], '', $dbh);;
98                                                 my $tagsubf = $tag.$subfieldcode;
99                                                 $calculated =~ s/\{(.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue\{$1$tagsubf$2\}$2/g;
100                                         }
101                                         # field builded, store the result
102                                         if ($calculated && !$hasputtextbefore) { # put textbefore if not done
103                                                 $blocres .=$textbefore;
104                                                 $hasputtextbefore=1
105                                         }
106                                         # remove punctuation at start
107                                         $calculated =~ s/^( |;|:|\.|-)*//g;
108                                         $blocres.=$calculated;
109                                 }
110                         
111                         $blocres .=$textafter if $hasputtextbefore;
112                 } else {
113                         $blocres.=$isbdfield;
114                 }
115         }
116         $res.=$blocres;
117 # }
118 $res =~ s/\{(.*?)\}//g;
119 $res =~ s/\\n/\n/g;
120 $res =~ s/\n/<br\/>/g;
121 # remove empty ()
122 $res =~ s/\(\)//g;
123 $template->param(ISBD => $res,
124                                 biblionumber => $biblionumber);
125
126 output_html_with_http_headers $query, $cookie, $template->output;
127
128 sub get_authorised_value_desc ($$$$$) {
129    my($tag, $subfield, $value, $framework, $dbh) = @_;
130
131    #---- branch
132     if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
133        return getbranchname($value);
134     }
135
136    #---- itemtypes
137    if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) {
138        return ItemType($value);
139     }
140
141    #---- "true" authorized value
142    my $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'};
143
144    if ($category ne "") {
145        my $sth = $dbh->prepare("select lib from authorised_values where category = ? and authorised_value = ?");
146        $sth->execute($category, $value);
147        my $data = $sth->fetchrow_hashref;
148        return $data->{'lib'};
149    } else {
150        return $value; # if nothing is found return the original value
151    }
152 }