Bug 14778: Make Barcodes_ValueBuilder.t db dependent
[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
49 use C4::Auth;
50 use C4::Context;
51 use C4::Output;
52 use CGI qw ( -utf8 );
53 use C4::Koha;
54 use MARC::Record;
55 use C4::Biblio;
56 use C4::Items;
57 use C4::Acquisition;
58 use C4::Members; # to use GetMember
59 use C4::Serials;    #uses getsubscriptionsfrombiblionumber GetSubscriptionsFromBiblionumber
60 use C4::Search;         # enabled_staff_search_views
61
62 use List::MoreUtils qw( uniq );
63
64 my $query        = new CGI;
65 my $dbh          = C4::Context->dbh;
66 my $biblionumber = $query->param('biblionumber');
67 my $frameworkcode = $query->param('frameworkcode');
68 $frameworkcode = GetFrameworkCode( $biblionumber ) unless ($frameworkcode);
69 my $popup        =
70   $query->param('popup')
71   ;    # if set to 1, then don't insert links, it's just to show the biblio
72 my $subscriptionid = $query->param('subscriptionid');
73
74 # open template
75 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
76     {
77         template_name   => "catalogue/MARCdetail.tt",
78         query           => $query,
79         type            => "intranet",
80         authnotrequired => 0,
81         flagsrequired   => { catalogue => 1 },
82         debug           => 1,
83     }
84 );
85
86 my $record = GetMarcBiblio($biblionumber, 1);
87 $template->param( ocoins => GetCOinSBiblio($record) );
88
89 if ( not defined $record ) {
90     # biblionumber invalid -> report and exit
91     $template->param( unknownbiblionumber => 1,
92                 biblionumber => $biblionumber
93     );
94     output_html_with_http_headers $query, $cookie, $template->output;
95     exit;
96 }
97
98 my $tagslib = &GetMarcStructure(1,$frameworkcode);
99 my $biblio = GetBiblioData($biblionumber);
100
101 if($query->cookie("holdfor")){ 
102     my $holdfor_patron = GetMember('borrowernumber' => $query->cookie("holdfor"));
103     $template->param(
104         holdfor => $query->cookie("holdfor"),
105         holdfor_surname => $holdfor_patron->{'surname'},
106         holdfor_firstname => $holdfor_patron->{'firstname'},
107         holdfor_cardnumber => $holdfor_patron->{'cardnumber'},
108     );
109 }
110
111 #count of item linked
112 my $itemcount = GetItemsCount($biblionumber);
113 $template->param( count => $itemcount,
114                                         bibliotitle => $biblio->{title}, );
115
116 # Getting the list of all frameworks
117 # get framework list
118 my $frameworks = getframeworks;
119 my @frameworkcodeloop;
120 foreach my $thisframeworkcode ( keys %$frameworks ) {
121     my %row = (
122         value         => $thisframeworkcode,
123         frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
124     );
125     if ($frameworkcode eq $thisframeworkcode){
126         $row{'selected'}= 1;
127         }
128     push @frameworkcodeloop, \%row;
129 }
130 $template->param( frameworkcodeloop => \@frameworkcodeloop, );
131 # fill arrays
132 my @loop_data = ();
133 my $tag;
134
135 # loop through each tab 0 through 9
136 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
137
138     # loop through each tag
139     my @fields    = $record->fields();
140     my @loop_data = ();
141     my @subfields_data;
142
143     # deal with leader
144     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop )
145     {    #  or ($tagslib->{'000'}->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/ )) {
146         my %subfield_data;
147         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
148         $subfield_data{marc_value}    = $record->leader();
149         $subfield_data{marc_subfield} = '@';
150         $subfield_data{marc_tag}      = '000';
151         push( @subfields_data, \%subfield_data );
152         my %tag_data;
153         $tag_data{tag} = '000';
154         $tag_data{tag_desc} = $tagslib->{'000'}->{lib};
155         my @tmp = @subfields_data;
156         $tag_data{subfield} = \@tmp;
157         push( @loop_data, \%tag_data );
158         undef @subfields_data;
159     }
160     @fields = $record->fields();
161     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
162
163         # if tag <10, there's no subfield, use the "@" trick
164         if ( $fields[$x_i]->tag() < 10 ) {
165             next
166               if (
167                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
168             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
169             my %subfield_data;
170             $subfield_data{marc_lib} =
171               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
172             $subfield_data{marc_value}    = $fields[$x_i]->data();
173             $subfield_data{marc_subfield} = '@';
174             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
175             push( @subfields_data, \%subfield_data );
176         }
177         else {
178             my @subf = $fields[$x_i]->subfields;
179
180             # loop through each subfield
181             for my $i ( 0 .. $#subf ) {
182                 $subf[$i][0] = "@" unless defined $subf[$i][0];
183                 next
184                   if (
185                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{tab}
186                     ne $tabloop );
187                 next
188                   if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
189                     ->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
190                 my %subfield_data;
191                 $subfield_data{short_desc} = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
192                 $subfield_data{long_desc} =
193                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
194                 $subfield_data{link} =
195                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{link};
196
197 #                 warn "tag : ".$tagslib->{$fields[$x_i]->tag()}." subfield :".$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}."lien koha? : "$subfield_data{link};
198                 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
199                     ->{isurl} )
200                 {
201                     $subfield_data{marc_value} = $subf[$i][1];
202                                         $subfield_data{is_url} = 1;
203                 }
204                 elsif ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
205                     ->{kohafield} eq "biblioitems.isbn" )
206                 {
207
208 #                    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]);
209                     $subfield_data{marc_value} = $subf[$i][1];
210                 }
211                 else {
212                     if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
213                         ->{authtypecode} )
214                     {
215                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
216                     }
217                     $subfield_data{marc_value} =
218                       GetAuthorisedValueDesc( $fields[$x_i]->tag(),
219                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
220
221                 }
222                 $subfield_data{marc_subfield} = $subf[$i][0];
223                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
224                 push( @subfields_data, \%subfield_data );
225             }
226         }
227         if ( $#subfields_data == 0 ) {
228             $subfields_data[0]->{marc_lib}      = '';
229 #            $subfields_data[0]->{marc_subfield} = '';
230         }
231         if ( $#subfields_data >= 0) {
232             my %tag_data;
233             if ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() && (C4::Context->preference('LabelMARCView') eq 'economical')) {
234                 $tag_data{tag} = "";
235             }
236             else {
237                 if ( C4::Context->preference('hide_marc') ) {
238                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
239                 }
240                 else {
241                     $tag_data{tag} = $fields[$x_i]->tag();
242             $tag_data{tag_ind} = C4::Koha::display_marc_indicators($fields[$x_i]);
243             $tag_data{tag_desc} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
244                 }
245             }
246             my @tmp = @subfields_data;
247             $tag_data{subfield} = \@tmp;
248             push( @loop_data, \%tag_data );
249             undef @subfields_data;
250         }
251     }
252     $template->param( "tab" . $tabloop . "XX" => \@loop_data );
253 }
254
255 # now, build item tab !
256 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
257 # loop through each tag
258 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
259 # then construct template.
260 my @fields = $record->fields();
261 my %witness
262   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
263 my @item_subfield_codes;
264 my @item_loop;
265 my $norequests = 1;
266 foreach my $field (@fields) {
267     next if ( $field->tag() < 10 );
268     my @subf = $field->subfields;
269     my $item;
270
271     # loop through each subfield
272     for my $i ( 0 .. $#subf ) {
273         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab} ne 10 );
274         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
275         push @item_subfield_codes, $subf[$i][0];
276         $witness{ $subf[$i][0] } =
277         $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
278         $item->{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(),
279                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
280         $norequests = 0 if $subf[$i][1] ==0 and $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield} eq 'items.notforloan';
281     }
282     push @item_loop, $item if $item;
283 }
284
285 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
286 @item_loop = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @item_loop;
287
288 @item_subfield_codes = uniq @item_subfield_codes;
289 # fill item info
290 my @item_header_loop;
291 for my $subfield_code ( @item_subfield_codes ) {
292     push @item_header_loop, $witness{$subfield_code};
293     for my $item_data ( @item_loop ) {
294         $item_data->{$subfield_code} ||= "&nbsp;"
295     }
296 }
297
298 my $subscriptionscount = CountSubscriptionFromBiblionumber($biblionumber);
299
300 if ($subscriptionscount) {
301     my $subscriptions = GetSubscriptionsFromBiblionumber($biblionumber);
302     my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
303     $template->param(
304         subscriptiontitle   => $subscriptiontitle,
305         subscriptionsnumber => $subscriptionscount,
306     );
307 }
308
309 $template->param (
310     norequests              => $norequests,
311     item_loop               => \@item_loop,
312     item_header_loop        => \@item_header_loop,
313     item_subfield_codes     => \@item_subfield_codes,
314     biblionumber            => $biblionumber,
315     popup                   => $popup,
316     hide_marc               => C4::Context->preference('hide_marc'),
317         marcview => 1,
318         z3950_search_params             => C4::Search::z3950_search_args($biblio),
319         C4::Search::enabled_staff_search_views,
320     searchid            => $query->param('searchid'),
321 );
322
323 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
324 my @deletedorders_using_biblio;
325 my @orders_using_biblio;
326 my @baskets_orders;
327 my @baskets_deletedorders;
328
329 foreach my $myorder (@allorders_using_biblio) {
330     my $basket = $myorder->{'basketno'};
331     if ((defined $myorder->{'datecancellationprinted'}) and  ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
332         push @deletedorders_using_biblio, $myorder;
333         unless (grep(/^$basket$/, @baskets_deletedorders)){
334             push @baskets_deletedorders,$myorder->{'basketno'};
335         }
336     }
337     else {
338         push @orders_using_biblio, $myorder;
339         unless (grep(/^$basket$/, @baskets_orders)){
340             push @baskets_orders,$myorder->{'basketno'};
341             }
342     }
343 }
344
345 my $count_orders_using_biblio = scalar @orders_using_biblio ;
346 $template->param (countorders => $count_orders_using_biblio);
347
348 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
349 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
350
351 my $holds = C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
352 my $holdcount = scalar( @$holds );
353 $template->param( holdcount => scalar ( @$holds ) );
354
355 output_html_with_http_headers $query, $cookie, $template->output;