bug 5380: remove copy-and-paste from authorities/detail.pl
[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 # get framework list
95 my $frameworks = getframeworks;
96 my @frameworkcodeloop;
97 foreach my $thisframeworkcode ( keys %$frameworks ) {
98     my %row = (
99         value         => $thisframeworkcode,
100         frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
101     );
102     if ($frameworkcode eq $thisframeworkcode){
103         $row{'selected'}= 1;
104         }
105     push @frameworkcodeloop, \%row;
106 }
107 $template->param( frameworkcodeloop => \@frameworkcodeloop, );
108 # fill arrays
109 my @loop_data = ();
110 my $tag;
111
112 # loop through each tab 0 through 9
113 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
114
115     # loop through each tag
116     my @fields    = $record->fields();
117     my @loop_data = ();
118     my @subfields_data;
119
120     # deal with leader
121     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop )
122     {    #  or ($tagslib->{'000'}->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/ )) {
123         my %subfield_data;
124         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
125         $subfield_data{marc_value}    = $record->leader();
126         $subfield_data{marc_subfield} = '@';
127         $subfield_data{marc_tag}      = '000';
128         push( @subfields_data, \%subfield_data );
129         my %tag_data;
130         $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
131         my @tmp = @subfields_data;
132         $tag_data{subfield} = \@tmp;
133         push( @loop_data, \%tag_data );
134         undef @subfields_data;
135     }
136     @fields = $record->fields();
137     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
138
139         # if tag <10, there's no subfield, use the "@" trick
140         if ( $fields[$x_i]->tag() < 10 ) {
141             next
142               if (
143                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
144             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
145             my %subfield_data;
146             $subfield_data{marc_lib} =
147               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
148             $subfield_data{marc_value}    = $fields[$x_i]->data();
149             $subfield_data{marc_subfield} = '@';
150             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
151             push( @subfields_data, \%subfield_data );
152         }
153         else {
154             my @subf = $fields[$x_i]->subfields;
155
156             # loop through each subfield
157             for my $i ( 0 .. $#subf ) {
158                 $subf[$i][0] = "@" unless $subf[$i][0];
159                 next
160                   if (
161                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{tab}
162                     ne $tabloop );
163                 next
164                   if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
165                     ->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
166                 my %subfield_data;
167                 $subfield_data{short_desc} = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
168                 $subfield_data{long_desc} =
169                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
170                 $subfield_data{link} =
171                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{link};
172
173 #                 warn "tag : ".$tagslib->{$fields[$x_i]->tag()}." subfield :".$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}."lien koha? : "$subfield_data{link};
174                 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
175                     ->{isurl} )
176                 {
177                     $subfield_data{marc_value} = $subf[$i][1];
178                                         $subfield_data{is_url} = 1;
179                 }
180                 elsif ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
181                     ->{kohafield} eq "biblioitems.isbn" )
182                 {
183
184 #                    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]);
185                     $subfield_data{marc_value} = $subf[$i][1];
186                 }
187                 else {
188                     if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
189                         ->{authtypecode} )
190                     {
191                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
192                     }
193                     $subfield_data{marc_value} =
194                       GetAuthorisedValueDesc( $fields[$x_i]->tag(),
195                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
196
197                 }
198                 $subfield_data{marc_subfield} = $subf[$i][0];
199                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
200                 push( @subfields_data, \%subfield_data );
201             }
202         }
203         if ( $#subfields_data == 0 ) {
204             $subfields_data[0]->{marc_lib}      = '';
205 #            $subfields_data[0]->{marc_subfield} = '';
206         }
207         if ( $#subfields_data >= 0) {
208             my %tag_data;
209             if ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() && (C4::Context->preference('LabelMARCView') eq 'economical')) {
210                 $tag_data{tag} = "";
211             }
212             else {
213                 if ( C4::Context->preference('hide_marc') ) {
214                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
215                 }
216                 else {
217                     $tag_data{tag} =
218                         $fields[$x_i]->tag() 
219                       . ' '
220                       . C4::Koha::display_marc_indicators($fields[$x_i])
221                       . ' - '
222                       . $tagslib->{ $fields[$x_i]->tag() }->{lib};
223                 }
224             }
225             my @tmp = @subfields_data;
226             $tag_data{subfield} = \@tmp;
227             push( @loop_data, \%tag_data );
228             undef @subfields_data;
229         }
230     }
231     $template->param( $tabloop . "XX" => \@loop_data );
232 }
233
234 # now, build item tab !
235 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
236 # loop through each tag
237 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
238 # then construct template.
239 my @fields = $record->fields();
240 my %witness
241   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
242 my @big_array;
243 my $norequests = 1;
244 foreach my $field (@fields) {
245     next if ( $field->tag() < 10 );
246     my @subf = $field->subfields;
247     my %this_row;
248
249     # loop through each subfield
250     for my $i ( 0 .. $#subf ) {
251         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab} ne 10 );
252         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
253         $witness{ $subf[$i][0] } =
254         $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
255         $this_row{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(),
256                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
257         $norequests = 0 if $subf[$i][1] ==0 and $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield} eq 'items.notforloan';
258     }
259     if (%this_row) {
260         push( @big_array, \%this_row );
261     }
262 }
263
264 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
265 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
266
267 #fill big_row with missing datas
268 foreach my $subfield_code ( keys(%witness) ) {
269     for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
270         $big_array[$i]{$subfield_code} = "&nbsp;"
271           unless ( $big_array[$i]{$subfield_code} );
272     }
273 }
274
275 # now, construct template !
276 my @item_value_loop;
277 my @header_value_loop;
278 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
279     my $items_data;
280     foreach my $subfield_code ( keys(%witness) ) {
281         $items_data .= "<td>" . $big_array[$i]{$subfield_code} . "</td>";
282     }
283     my %row_data;
284     $row_data{item_value} = $items_data;
285     push( @item_value_loop, \%row_data );
286 }
287 foreach my $subfield_code ( keys(%witness) ) {
288     my %header_value;
289     $header_value{header_value} = $witness{$subfield_code};
290     push( @header_value_loop, \%header_value );
291 }
292
293 my $subscriptionscount = CountSubscriptionFromBiblionumber($biblionumber);
294
295 if ($subscriptionscount) {
296     my $subscriptions = GetSubscriptionsFromBiblionumber($biblionumber);
297     my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
298     $template->param(
299         subscriptiontitle   => $subscriptiontitle,
300         subscriptionsnumber => $subscriptionscount,
301     );
302 }
303
304 $template->param (
305     norequests              => $norequests, 
306     item_loop               => \@item_value_loop,
307     item_header_loop        => \@header_value_loop,
308     biblionumber            => $biblionumber,
309     popup                   => $popup,
310     hide_marc               => C4::Context->preference('hide_marc'),
311         marcview => 1,
312         z3950_search_params             => C4::Search::z3950_search_args($biblio),
313         C4::Search::enabled_staff_search_views,
314 );
315
316 output_html_with_http_headers $query, $cookie, $template->output;