Fix for Bug 3659, Add place hold option from patron checkout tab
[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 =cut
27
28 =head1 DESCRIPTION
29
30 This script needs a biblionumber as parameter
31
32 It shows the biblio in a (nice) MARC format depending on MARC
33 parameters tables.
34
35 The template is in <templates_dir>/catalogue/MARCdetail.tmpl.
36 this template must be divided into 11 "tabs".
37
38 The first 10 tabs present the biblio, the 11th one presents
39 the items attached to the biblio
40
41 =head1 FUNCTIONS
42
43 =cut
44
45 use strict;
46 #use warnings; FIXME - Bug 2505
47
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::Items;
56 use C4::Acquisition;
57 use C4::Members; # to use GetMember
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 if($query->cookie("holdfor")){ 
89     my $holdfor_patron = GetMember('borrowernumber' => $query->cookie("holdfor"));
90     $template->param(
91         holdfor => $query->cookie("holdfor"),
92         holdfor_surname => $holdfor_patron->{'surname'},
93         holdfor_firstname => $holdfor_patron->{'firstname'},
94         holdfor_cardnumber => $holdfor_patron->{'cardnumber'},
95     );
96 }
97
98 #count of item linked
99 my $itemcount = GetItemsCount($biblionumber);
100 $template->param( count => $itemcount,
101                                         bibliotitle => $biblio->{title}, );
102
103 # Getting the list of all frameworks
104 # get framework list
105 my $frameworks = getframeworks;
106 my @frameworkcodeloop;
107 foreach my $thisframeworkcode ( keys %$frameworks ) {
108     my %row = (
109         value         => $thisframeworkcode,
110         frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
111     );
112     if ($frameworkcode eq $thisframeworkcode){
113         $row{'selected'}= 1;
114         }
115     push @frameworkcodeloop, \%row;
116 }
117 $template->param( frameworkcodeloop => \@frameworkcodeloop, );
118 # fill arrays
119 my @loop_data = ();
120 my $tag;
121
122 # loop through each tab 0 through 9
123 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
124
125     # loop through each tag
126     my @fields    = $record->fields();
127     my @loop_data = ();
128     my @subfields_data;
129
130     # deal with leader
131     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop )
132     {    #  or ($tagslib->{'000'}->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/ )) {
133         my %subfield_data;
134         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
135         $subfield_data{marc_value}    = $record->leader();
136         $subfield_data{marc_subfield} = '@';
137         $subfield_data{marc_tag}      = '000';
138         push( @subfields_data, \%subfield_data );
139         my %tag_data;
140         $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
141         my @tmp = @subfields_data;
142         $tag_data{subfield} = \@tmp;
143         push( @loop_data, \%tag_data );
144         undef @subfields_data;
145     }
146     @fields = $record->fields();
147     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
148
149         # if tag <10, there's no subfield, use the "@" trick
150         if ( $fields[$x_i]->tag() < 10 ) {
151             next
152               if (
153                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
154             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
155             my %subfield_data;
156             $subfield_data{marc_lib} =
157               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
158             $subfield_data{marc_value}    = $fields[$x_i]->data();
159             $subfield_data{marc_subfield} = '@';
160             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
161             push( @subfields_data, \%subfield_data );
162         }
163         else {
164             my @subf = $fields[$x_i]->subfields;
165
166             # loop through each subfield
167             for my $i ( 0 .. $#subf ) {
168                 $subf[$i][0] = "@" unless $subf[$i][0];
169                 next
170                   if (
171                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{tab}
172                     ne $tabloop );
173                 next
174                   if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
175                     ->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
176                 my %subfield_data;
177                 $subfield_data{short_desc} = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
178                 $subfield_data{long_desc} =
179                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
180                 $subfield_data{link} =
181                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{link};
182
183 #                 warn "tag : ".$tagslib->{$fields[$x_i]->tag()}." subfield :".$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}."lien koha? : "$subfield_data{link};
184                 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
185                     ->{isurl} )
186                 {
187                     $subfield_data{marc_value} = $subf[$i][1];
188                                         $subfield_data{is_url} = 1;
189                 }
190                 elsif ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
191                     ->{kohafield} eq "biblioitems.isbn" )
192                 {
193
194 #                    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]);
195                     $subfield_data{marc_value} = $subf[$i][1];
196                 }
197                 else {
198                     if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
199                         ->{authtypecode} )
200                     {
201                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
202                     }
203                     $subfield_data{marc_value} =
204                       GetAuthorisedValueDesc( $fields[$x_i]->tag(),
205                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
206
207                 }
208                 $subfield_data{marc_subfield} = $subf[$i][0];
209                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
210                 push( @subfields_data, \%subfield_data );
211             }
212         }
213         if ( $#subfields_data == 0 ) {
214             $subfields_data[0]->{marc_lib}      = '';
215 #            $subfields_data[0]->{marc_subfield} = '';
216         }
217         if ( $#subfields_data >= 0) {
218             my %tag_data;
219             if ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() && (C4::Context->preference('LabelMARCView') eq 'economical')) {
220                 $tag_data{tag} = "";
221             }
222             else {
223                 if ( C4::Context->preference('hide_marc') ) {
224                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
225                 }
226                 else {
227                     $tag_data{tag} =
228                         $fields[$x_i]->tag() 
229                       . ' '
230                       . C4::Koha::display_marc_indicators($fields[$x_i])
231                       . ' - '
232                       . $tagslib->{ $fields[$x_i]->tag() }->{lib};
233                 }
234             }
235             my @tmp = @subfields_data;
236             $tag_data{subfield} = \@tmp;
237             push( @loop_data, \%tag_data );
238             undef @subfields_data;
239         }
240     }
241     $template->param( $tabloop . "XX" => \@loop_data );
242 }
243
244 # now, build item tab !
245 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
246 # loop through each tag
247 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
248 # then construct template.
249 my @fields = $record->fields();
250 my %witness
251   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
252 my @big_array;
253 my $norequests = 1;
254 foreach my $field (@fields) {
255     next if ( $field->tag() < 10 );
256     my @subf = $field->subfields;
257     my %this_row;
258
259     # loop through each subfield
260     for my $i ( 0 .. $#subf ) {
261         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab} ne 10 );
262         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
263         $witness{ $subf[$i][0] } =
264         $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
265         $this_row{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(),
266                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
267         $norequests = 0 if $subf[$i][1] ==0 and $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield} eq 'items.notforloan';
268     }
269     if (%this_row) {
270         push( @big_array, \%this_row );
271     }
272 }
273
274 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
275 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
276
277 #fill big_row with missing datas
278 foreach my $subfield_code ( keys(%witness) ) {
279     for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
280         $big_array[$i]{$subfield_code} = "&nbsp;"
281           unless ( $big_array[$i]{$subfield_code} );
282     }
283 }
284
285 # now, construct template !
286 my @item_value_loop;
287 my @header_value_loop;
288 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
289     my $items_data;
290     foreach my $subfield_code ( keys(%witness) ) {
291         $items_data .= "<td>" . $big_array[$i]{$subfield_code} . "</td>";
292     }
293     my %row_data;
294     $row_data{item_value} = $items_data;
295     push( @item_value_loop, \%row_data );
296 }
297 foreach my $subfield_code ( keys(%witness) ) {
298     my %header_value;
299     $header_value{header_value} = $witness{$subfield_code};
300     push( @header_value_loop, \%header_value );
301 }
302
303 my $subscriptionscount = CountSubscriptionFromBiblionumber($biblionumber);
304
305 if ($subscriptionscount) {
306     my $subscriptions = GetSubscriptionsFromBiblionumber($biblionumber);
307     my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
308     $template->param(
309         subscriptiontitle   => $subscriptiontitle,
310         subscriptionsnumber => $subscriptionscount,
311     );
312 }
313
314 $template->param (
315     norequests              => $norequests, 
316     item_loop               => \@item_value_loop,
317     item_header_loop        => \@header_value_loop,
318     biblionumber            => $biblionumber,
319     popup                   => $popup,
320     hide_marc               => C4::Context->preference('hide_marc'),
321         marcview => 1,
322         z3950_search_params             => C4::Search::z3950_search_args($biblio),
323         C4::Search::enabled_staff_search_views,
324 );
325
326 output_html_with_http_headers $query, $cookie, $template->output;