Merge branch 'bug2505_patches' of git://git.catalyst.net.nz/koha into to-push
[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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 #use warnings; FIXME - Bug 2505
48
49 use C4::Auth;
50 use C4::Context;
51 use C4::Output;
52 use CGI;
53 use C4::Koha;
54 use MARC::Record;
55 use C4::Biblio;
56 use C4::Items;
57 use C4::Acquisition;
58 use C4::Serials;    #uses getsubscriptionsfrombiblionumber GetSubscriptionsFromBiblionumber
59 use C4::Search;         # enabled_staff_search_views
60
61
62 my $query        = new CGI;
63 my $dbh          = C4::Context->dbh;
64 my $biblionumber = $query->param('biblionumber');
65 my $frameworkcode = $query->param('frameworkcode');
66 $frameworkcode = GetFrameworkCode( $biblionumber ) unless ($frameworkcode);
67 my $popup        =
68   $query->param('popup')
69   ;    # if set to 1, then don't insert links, it's just to show the biblio
70 my $subscriptionid = $query->param('subscriptionid');
71
72 my $tagslib = &GetMarcStructure(1,$frameworkcode);
73
74 my $record = GetMarcBiblio($biblionumber);
75 my $biblio = GetBiblioData($biblionumber);
76 # open template
77 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
78     {
79         template_name   => "catalogue/MARCdetail.tmpl",
80         query           => $query,
81         type            => "intranet",
82         authnotrequired => 0,
83         flagsrequired   => { catalogue => 1 },
84         debug           => 1,
85     }
86 );
87
88 #count of item linked
89 my $itemcount = GetItemsCount($biblionumber);
90 $template->param( count => $itemcount,
91                                         bibliotitle => $biblio->{title}, );
92
93 #Getting the list of all frameworks
94 my $queryfwk =
95   $dbh->prepare("select frameworktext, frameworkcode from biblio_framework");
96 $queryfwk->execute;
97 my %select_fwk;
98 my @select_fwk;
99 my $curfwk;
100 push @select_fwk, "Default";
101 $select_fwk{"Default"} = "Default";
102
103 while ( my ( $description, $fwk ) = $queryfwk->fetchrow ) {
104     push @select_fwk, $fwk;
105     $select_fwk{$fwk} = $description;
106 }
107 $curfwk=$frameworkcode;
108 my $framework=CGI::scrolling_list( -name     => 'Frameworks',
109             -id => 'Frameworks',
110             -default => $curfwk,
111             -OnChange => 'Changefwk(this);',
112             -values   => \@select_fwk,
113             -labels   => \%select_fwk,
114             -size     => 1,
115             -multiple => 0 );
116 $template->param(framework => $framework);
117 # fill arrays
118 my @loop_data = ();
119 my $tag;
120
121 # loop through each tab 0 through 9
122 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
123
124     # loop through each tag
125     my @fields    = $record->fields();
126     my @loop_data = ();
127     my @subfields_data;
128
129     # deal with leader
130     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop )
131     {    #  or ($tagslib->{'000'}->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/ )) {
132         my %subfield_data;
133         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
134         $subfield_data{marc_value}    = $record->leader();
135         $subfield_data{marc_subfield} = '@';
136         $subfield_data{marc_tag}      = '000';
137         push( @subfields_data, \%subfield_data );
138         my %tag_data;
139         $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
140         my @tmp = @subfields_data;
141         $tag_data{subfield} = \@tmp;
142         push( @loop_data, \%tag_data );
143         undef @subfields_data;
144     }
145     @fields = $record->fields();
146     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
147
148         # if tag <10, there's no subfield, use the "@" trick
149         if ( $fields[$x_i]->tag() < 10 ) {
150             next
151               if (
152                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
153             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
154             my %subfield_data;
155             $subfield_data{marc_lib} =
156               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
157             $subfield_data{marc_value}    = $fields[$x_i]->data();
158             $subfield_data{marc_subfield} = '@';
159             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
160             push( @subfields_data, \%subfield_data );
161         }
162         else {
163             my @subf = $fields[$x_i]->subfields;
164
165             # loop through each subfield
166             for my $i ( 0 .. $#subf ) {
167                 $subf[$i][0] = "@" unless $subf[$i][0];
168                 next
169                   if (
170                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{tab}
171                     ne $tabloop );
172                 next
173                   if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
174                     ->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
175                 my %subfield_data;
176                 $subfield_data{short_desc} = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
177                 $subfield_data{long_desc} =
178                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
179                 $subfield_data{link} =
180                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{link};
181
182 #                 warn "tag : ".$tagslib->{$fields[$x_i]->tag()}." subfield :".$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}."lien koha? : "$subfield_data{link};
183                 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
184                     ->{isurl} )
185                 {
186                     $subfield_data{marc_value} = $subf[$i][1];
187                                         $subfield_data{is_url} = 1;
188                 }
189                 elsif ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
190                     ->{kohafield} eq "biblioitems.isbn" )
191                 {
192
193 #                    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]);
194                     $subfield_data{marc_value} = $subf[$i][1];
195                 }
196                 else {
197                     if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
198                         ->{authtypecode} )
199                     {
200                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
201                     }
202                     $subfield_data{marc_value} =
203                       GetAuthorisedValueDesc( $fields[$x_i]->tag(),
204                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
205
206                 }
207                 $subfield_data{marc_subfield} = $subf[$i][0];
208                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
209                 push( @subfields_data, \%subfield_data );
210             }
211         }
212         if ( $#subfields_data == 0 ) {
213             $subfields_data[0]->{marc_lib}      = '';
214 #            $subfields_data[0]->{marc_subfield} = '';
215         }
216         if ( $#subfields_data >= 0) {
217             my %tag_data;
218             if ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() && (C4::Context->preference('LabelMARCView') eq 'economical')) {
219                 $tag_data{tag} = "";
220             }
221             else {
222                 if ( C4::Context->preference('hide_marc') ) {
223                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
224                 }
225                 else {
226                     $tag_data{tag} =
227                         $fields[$x_i]->tag() 
228                       . ' '
229                       . C4::Koha::display_marc_indicators($fields[$x_i])
230                       . ' - '
231                       . $tagslib->{ $fields[$x_i]->tag() }->{lib};
232                 }
233             }
234             my @tmp = @subfields_data;
235             $tag_data{subfield} = \@tmp;
236             push( @loop_data, \%tag_data );
237             undef @subfields_data;
238         }
239     }
240     $template->param( $tabloop . "XX" => \@loop_data );
241 }
242
243 # now, build item tab !
244 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
245 # loop through each tag
246 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
247 # then construct template.
248 my @fields = $record->fields();
249 my %witness
250   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
251 my @big_array;
252 my $norequests = 1;
253 foreach my $field (@fields) {
254     next if ( $field->tag() < 10 );
255     my @subf = $field->subfields;
256     my %this_row;
257
258     # loop through each subfield
259     for my $i ( 0 .. $#subf ) {
260         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab} ne 10 );
261         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
262         $witness{ $subf[$i][0] } =
263         $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
264         $this_row{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(),
265                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
266         $norequests = 0 if $subf[$i][1] ==0 and $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield} eq 'items.notforloan';
267     }
268     if (%this_row) {
269         push( @big_array, \%this_row );
270     }
271 }
272
273 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
274 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
275
276 #fill big_row with missing datas
277 foreach my $subfield_code ( keys(%witness) ) {
278     for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
279         $big_array[$i]{$subfield_code} = "&nbsp;"
280           unless ( $big_array[$i]{$subfield_code} );
281     }
282 }
283
284 # now, construct template !
285 my @item_value_loop;
286 my @header_value_loop;
287 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
288     my $items_data;
289     foreach my $subfield_code ( keys(%witness) ) {
290         $items_data .= "<td>" . $big_array[$i]{$subfield_code} . "</td>";
291     }
292     my %row_data;
293     $row_data{item_value} = $items_data;
294     push( @item_value_loop, \%row_data );
295 }
296 foreach my $subfield_code ( keys(%witness) ) {
297     my %header_value;
298     $header_value{header_value} = $witness{$subfield_code};
299     push( @header_value_loop, \%header_value );
300 }
301
302 my $subscriptionscount = CountSubscriptionFromBiblionumber($biblionumber);
303
304 if ($subscriptionscount) {
305     my $subscriptions = GetSubscriptionsFromBiblionumber($biblionumber);
306     my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
307     $template->param(
308         subscriptiontitle   => $subscriptiontitle,
309         subscriptionsnumber => $subscriptionscount,
310     );
311 }
312
313 $template->param (
314     norequests              => $norequests, 
315     item_loop               => \@item_value_loop,
316     item_header_loop        => \@header_value_loop,
317     biblionumber            => $biblionumber,
318     popup                   => $popup,
319     hide_marc               => C4::Context->preference('hide_marc'),
320         marcview => 1,
321         z3950_search_params             => C4::Search::z3950_search_args($biblio),
322         C4::Search::enabled_staff_search_views,
323 );
324
325 output_html_with_http_headers $query, $cookie, $template->output;