Ported marc21 framework sql to PostgreSQL
[koha.git] / catalogue / MARCdetail.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 as parameter
30
31 It shows the biblio in a (nice) MARC format depending on MARC
32 parameters tables.
33
34 The template is in <templates_dir>/catalogue/MARCdetail.tmpl.
35 this template must be divided into 11 "tabs".
36
37 The first 10 tabs present the biblio, the 11th one presents
38 the items attached to the biblio
39
40 =head1 FUNCTIONS
41
42 =over 2
43
44 =cut
45
46 use strict;
47 require Exporter;
48 use C4::Auth;
49 use C4::Context;
50 use C4::Output;
51 use CGI;
52 use C4::Koha;
53 use MARC::Record;
54 use C4::Biblio;
55 use C4::Acquisition;
56 use C4::Serials;    #uses getsubscriptionsfrombiblionumber GetSubscriptionsFromBiblionumber
57
58 #---- Internal function ---
59 sub get_authorised_value_desc ($$$$$$) {
60     my ( $tagslib, $tag, $subfield, $value, $framework, $dbh ) = @_;
61
62     #---- branch
63     if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
64
65 #                return GetBranchDetail($value)->{branchname};
66                return $value;
67     }
68
69     #---- itemtypes
70     if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) {
71
72                   my $itemtypedef = getitemtypeinfo($value);
73               return $itemtypedef->{description};
74     }
75
76     #---- "true" authorized value
77     my $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'};
78
79     if ( $category ne "" ) {
80         my $sth =
81           $dbh->prepare(
82 "select lib from authorised_values where category = ? and authorised_value = ?"
83           );
84         $sth->execute( $category, $value );
85         my $data = $sth->fetchrow_hashref;
86         return $data->{'lib'};
87     }
88     else {
89         return $value;    # if nothing is found return the original value
90     }
91 }
92
93 #---------
94
95 my $query        = new CGI;
96 my $dbh          = C4::Context->dbh;
97 my $biblionumber = $query->param('biblionumber');
98 my $frameworkcode = $query->param('frameworkcode');
99 $frameworkcode = GetFrameworkCode( $biblionumber ) unless ($frameworkcode);
100 my $popup        =
101   $query->param('popup')
102   ;    # if set to 1, then don't insert links, it's just to show the biblio
103 my $subscriptionid = $query->param('subscriptionid');
104
105 my $tagslib = &GetMarcStructure(1,$frameworkcode);
106
107 my $record = GetMarcBiblio($biblionumber);
108
109 # open template
110 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
111     {
112         template_name   => "catalogue/MARCdetail.tmpl",
113         query           => $query,
114         type            => "intranet",
115         authnotrequired => 0,
116         flagsrequired   => { catalogue => 1 },
117         debug           => 1,
118     }
119 );
120
121 #count of item linked
122 my $itemcount = GetItemsCount($biblionumber);
123 $template->param( count => $itemcount);
124
125 #Getting the list of all frameworks
126 my $queryfwk =
127   $dbh->prepare("select frameworktext, frameworkcode from biblio_framework");
128 $queryfwk->execute;
129 my %select_fwk;
130 my @select_fwk;
131 my $curfwk;
132 push @select_fwk, "Default";
133 $select_fwk{"Default"} = "Default";
134
135 while ( my ( $description, $fwk ) = $queryfwk->fetchrow ) {
136     push @select_fwk, $fwk;
137     $select_fwk{$fwk} = $description;
138 }
139 $curfwk=$frameworkcode;
140 my $framework=CGI::scrolling_list( -name     => 'Frameworks',
141             -id => 'Frameworks',
142             -default => $curfwk,
143             -OnChange => 'Changefwk(this);',
144             -values   => \@select_fwk,
145             -labels   => \%select_fwk,
146             -size     => 1,
147             -multiple => 0 );
148 $template->param(framework => $framework);
149 # fill arrays
150 my @loop_data = ();
151 my $tag;
152
153 # loop through each tab 0 through 9
154 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
155
156     # loop through each tag
157     my @fields    = $record->fields();
158     my @loop_data = ();
159     my @subfields_data;
160
161     # deal with leader
162     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop )
163     {    #  or ($tagslib->{'000'}->{'@'}->{hidden}==(-7|-4|-3|-2|2|3|5|8))) {
164         my %subfield_data;
165         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
166         $subfield_data{marc_value}    = $record->leader();
167         $subfield_data{marc_subfield} = '@';
168         $subfield_data{marc_tag}      = '000';
169         push( @subfields_data, \%subfield_data );
170         my %tag_data;
171         $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
172         my @tmp = @subfields_data;
173         $tag_data{subfield} = \@tmp;
174         push( @loop_data, \%tag_data );
175         undef @subfields_data;
176     }
177     @fields = $record->fields();
178     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
179
180         # if tag <10, there's no subfield, use the "@" trick
181         if ( $fields[$x_i]->tag() < 10 ) {
182             next
183               if (
184                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
185             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} );
186             my %subfield_data;
187             $subfield_data{marc_lib} =
188               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
189             $subfield_data{marc_value}    = $fields[$x_i]->data();
190             $subfield_data{marc_subfield} = '@';
191             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
192             push( @subfields_data, \%subfield_data );
193         }
194         else {
195             my @subf = $fields[$x_i]->subfields;
196
197             # loop through each subfield
198             for my $i ( 0 .. $#subf ) {
199                 $subf[$i][0] = "@" unless $subf[$i][0];
200                 next
201                   if (
202                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{tab}
203                     ne $tabloop );
204                 next
205                   if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
206                     ->{hidden} );
207                 my %subfield_data;
208                 $subfield_data{short_desc} = substr(
209                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib},
210                     0, 20
211                 );
212                 $subfield_data{long_desc} =
213                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
214                 $subfield_data{link} =
215                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{link};
216
217 #                 warn "tag : ".$tagslib->{$fields[$x_i]->tag()}." subfield :".$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}."lien koha? : "$subfield_data{link};
218                 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
219                     ->{isurl} )
220                 {
221                     $subfield_data{marc_value} =
222                       "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
223                 }
224                 elsif ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
225                     ->{kohafield} eq "biblioitems.isbn" )
226                 {
227
228 #                    warn " tag : ".$tagslib->{$fields[$x_i]->tag()}." subfield :".$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}. "ISBN : ".$subf[$i][1]."PosttraitementISBN :".DisplayISBN($subf[$i][1]);
229                     $subfield_data{marc_value} = DisplayISBN( $subf[$i][1] );
230                 }
231                 else {
232                     if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
233                         ->{authtypecode} )
234                     {
235                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
236                     }
237                     $subfield_data{marc_value} =
238                       get_authorised_value_desc( $tagslib, $fields[$x_i]->tag(),
239                         $subf[$i][0], $subf[$i][1], '', $dbh );
240                 }
241                 $subfield_data{marc_subfield} = $subf[$i][0];
242                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
243                 push( @subfields_data, \%subfield_data );
244             }
245         }
246         if ( $#subfields_data == 0 ) {
247             $subfields_data[0]->{marc_lib}      = '';
248             $subfields_data[0]->{marc_subfield} = '';
249         }
250         if ( $#subfields_data >= 0 ) {
251             my %tag_data;
252             if ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() ) {
253                 $tag_data{tag} = "";
254             }
255             else {
256                 if ( C4::Context->preference('hide_marc') ) {
257                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
258                 }
259                 else {
260                     $tag_data{tag} =
261                         $fields[$x_i]->tag() . ' -'
262                       . $tagslib->{ $fields[$x_i]->tag() }->{lib};
263                 }
264             }
265             my @tmp = @subfields_data;
266             $tag_data{subfield} = \@tmp;
267             push( @loop_data, \%tag_data );
268             undef @subfields_data;
269         }
270     }
271     $template->param( $tabloop . "XX" => \@loop_data );
272 }
273
274 # now, build item tab !
275 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
276 # loop through each tag
277 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
278 # then construct template.
279 my @fields = $record->fields();
280 my %witness
281   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
282 my @big_array;
283 foreach my $field (@fields) {
284     next if ( $field->tag() < 10 );
285     my @subf = $field->subfields;
286     my %this_row;
287
288     # loop through each subfield
289     for my $i ( 0 .. $#subf ) {
290         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab} ne 10 );
291         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} );
292         $witness{ $subf[$i][0] } =
293           $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
294         $this_row{ $subf[$i][0] } = $subf[$i][1];
295     }
296     if (%this_row) {
297         push( @big_array, \%this_row );
298     }
299 }
300 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
301 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
302
303 #fill big_row with missing datas
304 foreach my $subfield_code ( keys(%witness) ) {
305     for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
306         $big_array[$i]{$subfield_code} = "&nbsp;"
307           unless ( $big_array[$i]{$subfield_code} );
308     }
309 }
310
311 # now, construct template !
312 my @item_value_loop;
313 my @header_value_loop;
314 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
315     my $items_data;
316     foreach my $subfield_code ( keys(%witness) ) {
317         $items_data .= "<td>" . $big_array[$i]{$subfield_code} . "</td>";
318     }
319     my %row_data;
320     $row_data{item_value} = $items_data;
321     push( @item_value_loop, \%row_data );
322 }
323 foreach my $subfield_code ( keys(%witness) ) {
324     my %header_value;
325     $header_value{header_value} = $witness{$subfield_code};
326     push( @header_value_loop, \%header_value );
327 }
328
329 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
330
331 if ($subscriptionsnumber) {
332     my $subscriptions = GetSubscriptionsFromBiblionumber($biblionumber);
333     my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
334     $template->param(
335         subscriptiontitle   => $subscriptiontitle,
336         subscriptionsnumber => $subscriptionsnumber,
337     );
338 }
339
340 $template->param (
341     item_loop               => \@item_value_loop,
342     item_header_loop        => \@header_value_loop,
343     biblionumber            => $biblionumber,
344     popup                   => $popup,
345     hide_marc               => C4::Context->preference('hide_marc'),
346 );
347
348 output_html_with_http_headers $query, $cookie, $template->output;