Bug 16699: Add borrowernumberQueryParam for reusability
[koha.git] / catalogue / MARCdetail.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 BibLibre
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 =head1 NAME
22
23 MARCdetail.pl : script to show a biblio in MARC format
24
25 =head1 SYNOPSIS
26
27 =cut
28
29 =head1 DESCRIPTION
30
31 This script needs a biblionumber as parameter
32
33 It shows the biblio in a (nice) MARC format depending on MARC
34 parameters tables.
35
36 The template is in <templates_dir>/catalogue/MARCdetail.tt.
37 this template must be divided into 11 "tabs".
38
39 The first 10 tabs present the biblio, the 11th one presents
40 the items attached to the biblio
41
42 =head1 FUNCTIONS
43
44 =cut
45
46 use strict;
47 #use warnings; FIXME - Bug 2505
48 use CGI qw ( -utf8 );
49 use HTML::Entities;
50
51 use C4::Auth;
52 use C4::Context;
53 use C4::Output;
54 use C4::Koha;
55 use MARC::Record;
56 use C4::Biblio;
57 use C4::Items;
58 use C4::Acquisition;
59 use C4::Members; # to use GetMember
60 use C4::Serials;    #uses getsubscriptionsfrombiblionumber GetSubscriptionsFromBiblionumber
61 use C4::Search;         # enabled_staff_search_views
62
63 use List::MoreUtils qw( uniq );
64
65 my $query        = new CGI;
66 my $dbh          = C4::Context->dbh;
67 my $biblionumber = $query->param('biblionumber');
68 $biblionumber = HTML::Entities::encode($biblionumber);
69 my $frameworkcode = $query->param('frameworkcode');
70 $frameworkcode = GetFrameworkCode( $biblionumber ) unless ($frameworkcode);
71 my $popup        =
72   $query->param('popup')
73   ;    # if set to 1, then don't insert links, it's just to show the biblio
74 my $subscriptionid = $query->param('subscriptionid');
75
76 # open template
77 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
78     {
79         template_name   => "catalogue/MARCdetail.tt",
80         query           => $query,
81         type            => "intranet",
82         authnotrequired => 0,
83         flagsrequired   => { catalogue => 1 },
84         debug           => 1,
85     }
86 );
87
88 my $record = GetMarcBiblio($biblionumber, 1);
89 $template->param( ocoins => GetCOinSBiblio($record) );
90
91 if ( not defined $record ) {
92     # biblionumber invalid -> report and exit
93     $template->param( unknownbiblionumber => 1,
94                 biblionumber => $biblionumber
95     );
96     output_html_with_http_headers $query, $cookie, $template->output;
97     exit;
98 }
99
100 my $tagslib = &GetMarcStructure(1,$frameworkcode);
101 my $biblio = GetBiblioData($biblionumber);
102
103 if($query->cookie("holdfor")){ 
104     my $holdfor_patron = GetMember('borrowernumber' => $query->cookie("holdfor"));
105     $template->param(
106         holdfor => $query->cookie("holdfor"),
107         holdfor_surname => $holdfor_patron->{'surname'},
108         holdfor_firstname => $holdfor_patron->{'firstname'},
109         holdfor_cardnumber => $holdfor_patron->{'cardnumber'},
110     );
111 }
112
113 #count of item linked
114 my $itemcount = GetItemsCount($biblionumber);
115 $template->param( count => $itemcount,
116                                         bibliotitle => $biblio->{title}, );
117
118 # Getting the list of all frameworks
119 # get framework list
120 my $frameworks = getframeworks;
121 my @frameworkcodeloop;
122 foreach my $thisframeworkcode ( keys %$frameworks ) {
123     my %row = (
124         value         => $thisframeworkcode,
125         frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
126     );
127     if ($frameworkcode eq $thisframeworkcode){
128         $row{'selected'}= 1;
129         }
130     push @frameworkcodeloop, \%row;
131 }
132 $template->param( frameworkcodeloop => \@frameworkcodeloop, );
133 # fill arrays
134 my @loop_data = ();
135
136 # loop through each tab 0 through 9
137 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
138
139     # loop through each tag
140     my @fields    = $record->fields();
141     my @loop_data = ();
142     my @subfields_data;
143
144     # deal with leader
145     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop )
146     {    #  or ($tagslib->{'000'}->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/ )) {
147         my %subfield_data;
148         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
149         $subfield_data{marc_value}    = $record->leader();
150         $subfield_data{marc_subfield} = '@';
151         $subfield_data{marc_tag}      = '000';
152         push( @subfields_data, \%subfield_data );
153         my %tag_data;
154         $tag_data{tag} = '000';
155         $tag_data{tag_desc} = $tagslib->{'000'}->{lib};
156         my @tmp = @subfields_data;
157         $tag_data{subfield} = \@tmp;
158         push( @loop_data, \%tag_data );
159         undef @subfields_data;
160     }
161     @fields = $record->fields();
162     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
163
164         # if tag <10, there's no subfield, use the "@" trick
165         if ( $fields[$x_i]->tag() < 10 ) {
166             next
167               if (
168                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
169             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
170             my %subfield_data;
171             $subfield_data{marc_lib} =
172               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
173             $subfield_data{marc_value}    = $fields[$x_i]->data();
174             $subfield_data{marc_subfield} = '@';
175             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
176             push( @subfields_data, \%subfield_data );
177         }
178         else {
179             my @subf = $fields[$x_i]->subfields;
180
181             # loop through each subfield
182             for my $i ( 0 .. $#subf ) {
183                 $subf[$i][0] = "@" unless defined $subf[$i][0];
184                 next
185                   if (
186                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{tab}
187                     ne $tabloop );
188                 next
189                   if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
190                     ->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
191                 my %subfield_data;
192                 $subfield_data{short_desc} = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
193                 $subfield_data{long_desc} =
194                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
195                 $subfield_data{link} =
196                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{link};
197
198 #                 warn "tag : ".$tagslib->{$fields[$x_i]->tag()}." subfield :".$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}."lien koha? : "$subfield_data{link};
199                 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
200                     ->{isurl} )
201                 {
202                     $subfield_data{marc_value} = $subf[$i][1];
203                                         $subfield_data{is_url} = 1;
204                 }
205                 elsif ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
206                     ->{kohafield} eq "biblioitems.isbn" )
207                 {
208
209 #                    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]);
210                     $subfield_data{marc_value} = $subf[$i][1];
211                 }
212                 else {
213                     if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
214                         ->{authtypecode} )
215                     {
216                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
217                     }
218                     $subfield_data{marc_value} =
219                       GetAuthorisedValueDesc( $fields[$x_i]->tag(),
220                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
221
222                 }
223                 $subfield_data{marc_subfield} = $subf[$i][0];
224                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
225                 push( @subfields_data, \%subfield_data );
226             }
227         }
228         if ( $#subfields_data == 0 ) {
229             $subfields_data[0]->{marc_lib}      = '';
230 #            $subfields_data[0]->{marc_subfield} = '';
231         }
232         if ( $#subfields_data >= 0) {
233             my %tag_data;
234             if ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() && (C4::Context->preference('LabelMARCView') eq 'economical')) {
235                 $tag_data{tag} = "";
236             }
237             else {
238                 if ( C4::Context->preference('hide_marc') ) {
239                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
240                 }
241                 else {
242                     $tag_data{tag} = $fields[$x_i]->tag();
243             $tag_data{tag_ind} = C4::Koha::display_marc_indicators($fields[$x_i]);
244             $tag_data{tag_desc} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
245                 }
246             }
247             my @tmp = @subfields_data;
248             $tag_data{subfield} = \@tmp;
249             push( @loop_data, \%tag_data );
250             undef @subfields_data;
251         }
252     }
253     $template->param( "tab" . $tabloop . "XX" => \@loop_data );
254 }
255
256 # now, build item tab !
257 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
258 # loop through each tag
259 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
260 # then construct template.
261 my @fields = $record->fields();
262 my %witness
263   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
264 my @item_subfield_codes;
265 my @item_loop;
266 my $norequests = 1;
267 foreach my $field (@fields) {
268     next if ( $field->tag() < 10 );
269     my @subf = $field->subfields;
270     my $item;
271
272     # loop through each subfield
273     for my $i ( 0 .. $#subf ) {
274         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab} ne 10 );
275         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
276         push @item_subfield_codes, $subf[$i][0];
277         $witness{ $subf[$i][0] } =
278         $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
279         $item->{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(),
280                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
281         $norequests = 0 if $subf[$i][1] ==0 and $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield} eq 'items.notforloan';
282     }
283     push @item_loop, $item if $item;
284 }
285
286 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
287 @item_loop = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @item_loop;
288
289 @item_subfield_codes = uniq @item_subfield_codes;
290 # fill item info
291 my @item_header_loop;
292 for my $subfield_code ( @item_subfield_codes ) {
293     push @item_header_loop, $witness{$subfield_code};
294     for my $item_data ( @item_loop ) {
295         $item_data->{$subfield_code} ||= "&nbsp;"
296     }
297 }
298
299 my $subscriptionscount = CountSubscriptionFromBiblionumber($biblionumber);
300
301 if ($subscriptionscount) {
302     my $subscriptions = GetSubscriptionsFromBiblionumber($biblionumber);
303     my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
304     $template->param(
305         subscriptiontitle   => $subscriptiontitle,
306         subscriptionsnumber => $subscriptionscount,
307     );
308 }
309
310 $template->param (
311     norequests              => $norequests,
312     item_loop               => \@item_loop,
313     item_header_loop        => \@item_header_loop,
314     item_subfield_codes     => \@item_subfield_codes,
315     biblionumber            => $biblionumber,
316     popup                   => $popup,
317     hide_marc               => C4::Context->preference('hide_marc'),
318         marcview => 1,
319         z3950_search_params             => C4::Search::z3950_search_args($biblio),
320         C4::Search::enabled_staff_search_views,
321     searchid            => scalar $query->param('searchid'),
322 );
323
324 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
325 my @deletedorders_using_biblio;
326 my @orders_using_biblio;
327 my @baskets_orders;
328 my @baskets_deletedorders;
329
330 foreach my $myorder (@allorders_using_biblio) {
331     my $basket = $myorder->{'basketno'};
332     if ((defined $myorder->{'datecancellationprinted'}) and  ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
333         push @deletedorders_using_biblio, $myorder;
334         unless (grep(/^$basket$/, @baskets_deletedorders)){
335             push @baskets_deletedorders,$myorder->{'basketno'};
336         }
337     }
338     else {
339         push @orders_using_biblio, $myorder;
340         unless (grep(/^$basket$/, @baskets_orders)){
341             push @baskets_orders,$myorder->{'basketno'};
342             }
343     }
344 }
345
346 my $count_orders_using_biblio = scalar @orders_using_biblio ;
347 $template->param (countorders => $count_orders_using_biblio);
348
349 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
350 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
351
352 my $holds = C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
353 my $holdcount = scalar( @$holds );
354 $template->param( holdcount => scalar ( @$holds ) );
355
356 output_html_with_http_headers $query, $cookie, $template->output;