Adding subfield filter to ISBD
[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::Items;
46 use C4::Branch;     # GetBranchDetail
47 use C4::Serials;    # CountSubscriptionFromBiblionumber
48
49
50 #---- Internal function
51
52
53 my $query = new CGI;
54 my $dbh = C4::Context->dbh;
55
56 my $biblionumber = $query->param('biblionumber');
57 my $itemtype     = &GetFrameworkCode($biblionumber);
58 my $tagslib      = &GetMarcStructure( 1, $itemtype );
59
60 my $record = GetMarcBiblio($biblionumber);
61
62 # open template
63 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
64     {
65         template_name => "catalogue/ISBDdetail.tmpl",
66         query         => $query,
67         type          => "intranet",
68         authnotrequired => 0,
69         flagsrequired   => { catalogue => 1 },
70     }
71 );
72
73 my $ISBD = C4::Context->preference('ISBD');
74
75 # my @blocs = split /\@/,$ISBD;
76 # my @fields = $record->fields();
77 my $res;
78
79 # foreach my $bloc (@blocs) {
80 #     $bloc =~ s/\n//g;
81 my $bloc = $ISBD;
82 my $blocres;
83
84 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$itemtype);
85 # @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
86
87 foreach my $isbdfield ( split /#/, $bloc ) {
88
89     $isbdfield =~ /(\d\d\d)([^\|])?\|(.*)\|(.*)\|(.*)/;
90     my $fieldvalue    = $1;
91     my $subfvalue = $2;
92     my $textbefore    = $3;
93     my $analysestring = $4;
94     my $textafter     = $5;
95
96     #         warn "==> $1 / $2 / $3 / $4";
97     #         my $fieldvalue=substr($isbdfield,0,3);
98     if ( $fieldvalue > 0 ) {
99         my $hasputtextbefore = 0;
100         my @fieldslist = $record->field($fieldvalue);
101         @fieldslist = sort {$a->subfield($holdingbrtagsubf) cmp $b->subfield($holdingbrtagsubf)} @fieldslist if ($fieldvalue eq $holdingbrtagf);
102
103         #         warn "ERROR IN ISBD DEFINITION at : $isbdfield" unless $fieldvalue;
104         #             warn "FV : $fieldvalue";
105         if ($subfvalue ne ""){
106           foreach my $field ( @fieldslist ) {
107             foreach my $subfield ($field->subfield($subfvalue)){
108               warn $fieldvalue."$subfvalue";    
109               my $calculated = $analysestring;
110               my $tag        = $field->tag();
111               if ( $tag < 10 ) {
112               }
113               else {
114                 my $subfieldvalue =
115                 GetAuthorisedValueDesc( $tag, $subfvalue,
116                   $subfield, '', $tagslib );
117                 my $tagsubf = $tag . $subfvalue;
118                 $calculated =~
119                       s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
120 #                 $calculated =~s#/cgi-bin/koha/[^/]+/([^.]*.pl\?.*)$#opac-$1#g;
121             
122                 # field builded, store the result
123                 if ( $calculated && !$hasputtextbefore )
124                 {    # put textbefore if not done
125                 $blocres .= $textbefore;
126                 $hasputtextbefore = 1;
127                 }
128             
129                 # remove punctuation at start
130                 $calculated =~ s/^( |;|:|\.|-)*//g;
131                 $blocres .= $calculated;
132                             
133               }         
134             }          
135           }
136           $blocres .= $textafter if $hasputtextbefore;  
137         } else {    
138         foreach my $field ( @fieldslist ) {
139           my $calculated = $analysestring;
140           my $tag        = $field->tag();
141           if ( $tag < 10 ) {
142           }
143           else {
144             my @subf = $field->subfields;
145             for my $i ( 0 .. $#subf ) {
146             my $subfieldcode  = $subf[$i][0];
147             my $subfieldvalue =
148             GetAuthorisedValueDesc( $tag, $subf[$i][0],
149               $subf[$i][1], '', $tagslib );
150             my $tagsubf = $tag . $subfieldcode;
151             $calculated =~
152         s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
153 #         $calculated =~s#/cgi-bin/koha/[^/]+/([^.]*.pl\?.*)$#opac-$1#g;
154             }
155         
156             # field builded, store the result
157             if ( $calculated && !$hasputtextbefore )
158             {    # put textbefore if not done
159             $blocres .= $textbefore;
160             $hasputtextbefore = 1;
161             }
162         
163             # remove punctuation at start
164             $calculated =~ s/^( |;|:|\.|-)*//g;
165             $blocres .= $calculated;
166           }
167         }
168         $blocres .= $textafter if $hasputtextbefore;
169         }       
170     }
171     else {
172         $blocres .= $isbdfield;
173     }
174 }
175 $res .= $blocres;
176
177 # }
178 $res =~ s/\{(.*?)\}//g;
179 $res =~ s/\\n/\n/g;
180 $res =~ s/\n/<br\/>/g;
181
182 # remove empty ()
183 $res =~ s/\(\)//g;
184 # count of item linked with biblio
185 my $itemcount = GetItemsCount($biblionumber);
186 $template->param( count => $itemcount);
187 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
188  
189 if ($subscriptionsnumber) {
190     my $subscriptions     = GetSubscriptionsFromBiblionumber($biblionumber);
191     my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
192     $template->param(
193         subscriptionsnumber => $subscriptionsnumber,
194         subscriptiontitle   => $subscriptiontitle,
195     );
196 }
197
198 $template->param (
199     ISBD                => $res,
200     biblionumber        => $biblionumber,
201         isbdview => 1,
202 );
203
204 output_html_with_http_headers $query, $cookie, $template->output;
205