item rework: moved GetItem
[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
59 my $query        = new CGI;
60 my $dbh          = C4::Context->dbh;
61 my $biblionumber = $query->param('biblionumber');
62 my $frameworkcode = $query->param('frameworkcode');
63 $frameworkcode = GetFrameworkCode( $biblionumber ) unless ($frameworkcode);
64 my $popup        =
65   $query->param('popup')
66   ;    # if set to 1, then don't insert links, it's just to show the biblio
67 my $subscriptionid = $query->param('subscriptionid');
68
69 my $tagslib = &GetMarcStructure(1,$frameworkcode);
70
71 my $record = GetMarcBiblio($biblionumber);
72
73 # open template
74 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
75     {
76         template_name   => "catalogue/MARCdetail.tmpl",
77         query           => $query,
78         type            => "intranet",
79         authnotrequired => 0,
80         flagsrequired   => { catalogue => 1 },
81         debug           => 1,
82     }
83 );
84
85 #count of item linked
86 my $itemcount = GetItemsCount($biblionumber);
87 $template->param( count => $itemcount,
88                                         bibliotitle => $record->title(), );
89
90 #Getting the list of all frameworks
91 my $queryfwk =
92   $dbh->prepare("select frameworktext, frameworkcode from biblio_framework");
93 $queryfwk->execute;
94 my %select_fwk;
95 my @select_fwk;
96 my $curfwk;
97 push @select_fwk, "Default";
98 $select_fwk{"Default"} = "Default";
99
100 while ( my ( $description, $fwk ) = $queryfwk->fetchrow ) {
101     push @select_fwk, $fwk;
102     $select_fwk{$fwk} = $description;
103 }
104 $curfwk=$frameworkcode;
105 my $framework=CGI::scrolling_list( -name     => 'Frameworks',
106             -id => 'Frameworks',
107             -default => $curfwk,
108             -OnChange => 'Changefwk(this);',
109             -values   => \@select_fwk,
110             -labels   => \%select_fwk,
111             -size     => 1,
112             -multiple => 0 );
113 $template->param(framework => $framework);
114 # fill arrays
115 my @loop_data = ();
116 my $tag;
117
118 # loop through each tab 0 through 9
119 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
120
121     # loop through each tag
122     my @fields    = $record->fields();
123     my @loop_data = ();
124     my @subfields_data;
125
126     # deal with leader
127     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop )
128     {    #  or ($tagslib->{'000'}->{'@'}->{hidden}==(-7|-4|-3|-2|2|3|5|8))) {
129         my %subfield_data;
130         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
131         $subfield_data{marc_value}    = $record->leader();
132         $subfield_data{marc_subfield} = '@';
133         $subfield_data{marc_tag}      = '000';
134         push( @subfields_data, \%subfield_data );
135         my %tag_data;
136         $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
137         my @tmp = @subfields_data;
138         $tag_data{subfield} = \@tmp;
139         push( @loop_data, \%tag_data );
140         undef @subfields_data;
141     }
142     @fields = $record->fields();
143     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
144
145         # if tag <10, there's no subfield, use the "@" trick
146         if ( $fields[$x_i]->tag() < 10 ) {
147             next
148               if (
149                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
150             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} );
151             my %subfield_data;
152             $subfield_data{marc_lib} =
153               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
154             $subfield_data{marc_value}    = $fields[$x_i]->data();
155             $subfield_data{marc_subfield} = '@';
156             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
157             push( @subfields_data, \%subfield_data );
158         }
159         else {
160             my @subf = $fields[$x_i]->subfields;
161
162             # loop through each subfield
163             for my $i ( 0 .. $#subf ) {
164                 $subf[$i][0] = "@" unless $subf[$i][0];
165                 next
166                   if (
167                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{tab}
168                     ne $tabloop );
169                 next
170                   if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
171                     ->{hidden} );
172                 my %subfield_data;
173                 $subfield_data{short_desc} = substr(
174                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib},
175                     0, 20
176                 );
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} =
187                       "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
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} = DisplayISBN( $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);
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                       . $tagslib->{ $fields[$x_i]->tag() }->{lib};
229                 }
230             }
231             my @tmp = @subfields_data;
232             $tag_data{subfield} = \@tmp;
233             push( @loop_data, \%tag_data );
234             undef @subfields_data;
235         }
236     }
237     $template->param( $tabloop . "XX" => \@loop_data );
238 }
239
240 # now, build item tab !
241 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
242 # loop through each tag
243 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
244 # then construct template.
245 my @fields = $record->fields();
246 my %witness
247   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
248 my @big_array;
249 foreach my $field (@fields) {
250     next if ( $field->tag() < 10 );
251     my @subf = $field->subfields;
252     my %this_row;
253
254     # loop through each subfield
255     for my $i ( 0 .. $#subf ) {
256         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab} ne 10 );
257         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} );
258         $witness{ $subf[$i][0] } =
259         $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
260         $this_row{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(),
261                         $subf[$i][0], $subf[$i][1], '', $tagslib);
262     }
263     if (%this_row) {
264         push( @big_array, \%this_row );
265     }
266 }
267 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
268 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
269
270 #fill big_row with missing datas
271 foreach my $subfield_code ( keys(%witness) ) {
272     for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
273         $big_array[$i]{$subfield_code} = "&nbsp;"
274           unless ( $big_array[$i]{$subfield_code} );
275     }
276 }
277
278 # now, construct template !
279 my @item_value_loop;
280 my @header_value_loop;
281 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
282     my $items_data;
283     foreach my $subfield_code ( keys(%witness) ) {
284         $items_data .= "<td>" . $big_array[$i]{$subfield_code} . "</td>";
285     }
286     my %row_data;
287     $row_data{item_value} = $items_data;
288     push( @item_value_loop, \%row_data );
289 }
290 foreach my $subfield_code ( keys(%witness) ) {
291     my %header_value;
292     $header_value{header_value} = $witness{$subfield_code};
293     push( @header_value_loop, \%header_value );
294 }
295
296 my $subscriptionscount = CountSubscriptionFromBiblionumber($biblionumber);
297
298 if ($subscriptionscount) {
299     my $subscriptions = GetSubscriptionsFromBiblionumber($biblionumber);
300     my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
301     $template->param(
302         subscriptiontitle   => $subscriptiontitle,
303         subscriptionsnumber => $subscriptionscount,
304     );
305 }
306
307 $template->param (
308     item_loop               => \@item_value_loop,
309     item_header_loop        => \@header_value_loop,
310     biblionumber            => $biblionumber,
311     popup                   => $popup,
312     hide_marc               => C4::Context->preference('hide_marc'),
313         marcview => 1,
314 );
315
316 output_html_with_http_headers $query, $cookie, $template->output;