Bug 28036: Improve breadcrumbs of serial claims page
[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 Modern::Perl;
47 use CGI qw ( -utf8 );
48 use HTML::Entities;
49
50 use C4::Auth;
51 use C4::Context;
52 use C4::Output;
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 use Koha::Biblios;
62 use Koha::BiblioFrameworks;
63 use Koha::Patrons;
64 use Koha::DateUtils;
65 use Koha::Virtualshelves;
66
67 use List::MoreUtils qw( uniq );
68
69 my $query        = CGI->new;
70 my $dbh          = C4::Context->dbh;
71 my $biblionumber = $query->param('biblionumber');
72 $biblionumber = HTML::Entities::encode($biblionumber);
73 my $frameworkcode = $query->param('frameworkcode') // GetFrameworkCode( $biblionumber );
74 my $popup        =
75   $query->param('popup')
76   ;    # if set to 1, then don't insert links, it's just to show the biblio
77 my $subscriptionid = $query->param('subscriptionid');
78
79 # open template
80 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
81     {
82         template_name   => "catalogue/MARCdetail.tt",
83         query           => $query,
84         type            => "intranet",
85         flagsrequired   => { catalogue => 1 },
86         debug           => 1,
87     }
88 );
89
90 my $record = GetMarcBiblio({
91     biblionumber => $biblionumber,
92     embed_items  => 1 });
93
94 if ( not defined $record ) {
95     # biblionumber invalid -> report and exit
96     $template->param( unknownbiblionumber => 1,
97                 biblionumber => $biblionumber
98     );
99     output_html_with_http_headers $query, $cookie, $template->output;
100     exit;
101 }
102
103 my $biblio_object = Koha::Biblios->find( $biblionumber ); # FIXME Should replace $biblio
104 my $tagslib = &GetMarcStructure(1,$frameworkcode);
105 my $biblio = GetBiblioData($biblionumber);
106
107 if($query->cookie("holdfor")){ 
108     my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") );
109     $template->param(
110         holdfor => $query->cookie("holdfor"),
111         holdfor_surname => $holdfor_patron->surname,
112         holdfor_firstname => $holdfor_patron->firstname,
113         holdfor_cardnumber => $holdfor_patron->cardnumber,
114     );
115 }
116
117 if( $query->cookie("searchToOrder") ){
118     my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") );
119     $template->param(
120         searchtoorder_basketno => $basketno,
121         searchtoorder_vendorid => $vendorid
122     );
123 }
124
125 $template->param( ocoins => $biblio_object->get_coins );
126
127 #count of item linked
128 my $itemcount = $biblio_object->items->count;
129 $template->param( count => $itemcount,
130                                         bibliotitle => $biblio->{title}, );
131
132 my $frameworks = Koha::BiblioFrameworks->search( {}, { order_by => ['frameworktext'] } );
133 $template->param(
134     frameworks    => $frameworks,
135     frameworkcode => $frameworkcode,
136 );
137 # fill arrays
138 my @loop_data = ();
139
140 # loop through each tab 0 through 9
141 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
142
143     # loop through each tag
144     my @fields    = $record->fields();
145     my @loop_data = ();
146     my @subfields_data;
147
148     # deal with leader
149     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop )
150     {    #  or ($tagslib->{'000'}->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/ )) {
151         my %subfield_data;
152         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
153         $subfield_data{marc_value}    = $record->leader();
154         $subfield_data{marc_subfield} = '@';
155         $subfield_data{marc_tag}      = '000';
156         push( @subfields_data, \%subfield_data );
157         my %tag_data;
158         $tag_data{tag} = '000';
159         $tag_data{tag_desc} = $tagslib->{'000'}->{lib};
160         my @tmp = @subfields_data;
161         $tag_data{subfield} = \@tmp;
162         push( @loop_data, \%tag_data );
163         undef @subfields_data;
164     }
165     @fields = $record->fields();
166     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
167
168         # if tag <10, there's no subfield, use the "@" trick
169         if ( $fields[$x_i]->tag() < 10 ) {
170             next
171               if (
172                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
173             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
174             my %subfield_data;
175             $subfield_data{marc_lib} =
176               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
177             $subfield_data{marc_value}    = $fields[$x_i]->data();
178             $subfield_data{marc_subfield} = '@';
179             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
180             push( @subfields_data, \%subfield_data );
181         }
182         else {
183             my @subf = $fields[$x_i]->subfields;
184
185             # loop through each subfield
186             for my $i ( 0 .. $#subf ) {
187                 $subf[$i][0] = "@" unless defined $subf[$i][0];
188                 next
189                   if (
190                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{tab}
191                     ne $tabloop );
192                 next
193                   if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
194                     ->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
195                 my %subfield_data;
196                 $subfield_data{short_desc} = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
197                 $subfield_data{long_desc} =
198                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
199                 $subfield_data{link} =
200                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{link};
201
202 #                 warn "tag : ".$tagslib->{$fields[$x_i]->tag()}." subfield :".$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}."lien koha? : "$subfield_data{link};
203                 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
204                     ->{isurl} )
205                 {
206                     $subfield_data{marc_value} = $subf[$i][1];
207                                         $subfield_data{is_url} = 1;
208                 }
209                 elsif ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
210                     ->{kohafield} eq "biblioitems.isbn" )
211                 {
212
213 #                    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]);
214                     $subfield_data{marc_value} = $subf[$i][1];
215                 }
216                 else {
217                     if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
218                         ->{authtypecode} )
219                     {
220                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
221                     }
222                     $subfield_data{marc_value} =
223                       GetAuthorisedValueDesc( $fields[$x_i]->tag(),
224                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
225
226                 }
227                 $subfield_data{marc_subfield} = $subf[$i][0];
228                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
229                 push( @subfields_data, \%subfield_data );
230             }
231         }
232         if ( $#subfields_data == 0 ) {
233             $subfields_data[0]->{marc_lib}      = '';
234 #            $subfields_data[0]->{marc_subfield} = '';
235         }
236         if ( $#subfields_data >= 0) {
237             my %tag_data;
238             if ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() && (C4::Context->preference('LabelMARCView') eq 'economical')) {
239                 $tag_data{tag} = "";
240             }
241             else {
242                 if ( C4::Context->preference('hide_marc') ) {
243                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
244                 }
245                 else {
246                     $tag_data{tag} = $fields[$x_i]->tag();
247             $tag_data{tag_ind} = C4::Koha::display_marc_indicators($fields[$x_i]);
248             $tag_data{tag_desc} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
249                 }
250             }
251             my @tmp = @subfields_data;
252             $tag_data{subfield} = \@tmp;
253             push( @loop_data, \%tag_data );
254             undef @subfields_data;
255         }
256     }
257     $template->param( "tab" . $tabloop . "XX" => \@loop_data );
258 }
259
260 # now, build item tab !
261 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
262 # loop through each tag
263 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
264 # then construct template.
265 my @fields = $record->fields();
266 my %witness
267   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
268 my @item_subfield_codes;
269 my @item_loop;
270
271 foreach my $field (@fields) {
272     next if ( $field->tag() < 10 );
273     my @subf = $field->subfields;
274     my $item;
275
276     # loop through each subfield
277     for my $i ( 0 .. $#subf ) {
278         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab} ne 10 );
279         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
280
281         push @item_subfield_codes, $subf[$i][0];
282         $witness{ $subf[$i][0] } =
283         $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
284
285         # Allow repeatables (BZ 13574)
286         if( $item->{$subf[$i][0]}) {
287             $item->{$subf[$i][0]} .= ' | ';
288         } else {
289             $item->{$subf[$i][0]} = q{};
290         }
291         if( $tagslib->{$field->tag()}->{$subf[$i][0]}->{isurl} ) {
292             $item->{$subf[$i][0]} .= "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
293         } else {
294             $item->{ $subf[$i][0] } .= GetAuthorisedValueDesc( $field->tag(), $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
295         }
296
297         my $kohafield = $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield};
298         $item->{ $subf[$i][0] } = output_pref( { str => $item->{ $subf[$i][0] }, dateonly => 1 } )
299           if grep { $kohafield eq $_ }
300               qw( items.dateaccessioned items.onloan items.datelastseen items.datelastborrowed items.replacementpricedate );
301     }
302     push @item_loop, $item if $item;
303 }
304
305 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField( "items.holdingbranch" );
306 @item_loop = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @item_loop;
307
308 @item_subfield_codes = uniq @item_subfield_codes;
309 # fill item info
310 my @item_header_loop;
311 for my $subfield_code ( @item_subfield_codes ) {
312     push @item_header_loop, $witness{$subfield_code};
313     for my $item_data ( @item_loop ) {
314         $item_data->{$subfield_code} ||= "&nbsp;"
315     }
316 }
317
318 my $subscriptionscount = CountSubscriptionFromBiblionumber($biblionumber);
319
320 if ($subscriptionscount) {
321     my $subscriptions = GetSubscriptionsFromBiblionumber($biblionumber);
322     my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
323     $template->param(
324         subscriptiontitle   => $subscriptiontitle,
325         subscriptionsnumber => $subscriptionscount,
326     );
327 }
328
329 # get biblionumbers stored in the cart
330 my @cart_list;
331
332 if($query->cookie("intranet_bib_list")){
333     my $cart_list = $query->cookie("intranet_bib_list");
334     @cart_list = split(/\//, $cart_list);
335     if ( grep {$_ eq $biblionumber} @cart_list) {
336         $template->param( incart => 1 );
337     }
338 }
339
340 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
341     {
342         borrowernumber => $loggedinuser,
343         add_allowed    => 1,
344         category       => 1,
345     }
346 );
347 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
348     {
349         borrowernumber => $loggedinuser,
350         add_allowed    => 1,
351         category       => 2,
352     }
353 );
354
355
356 $template->param(
357     add_to_some_private_shelves => $some_private_shelves,
358     add_to_some_public_shelves  => $some_public_shelves,
359 );
360
361 $template->param (
362     item_loop               => \@item_loop,
363     item_header_loop        => \@item_header_loop,
364     item_subfield_codes     => \@item_subfield_codes,
365     biblionumber            => $biblionumber,
366     popup                   => $popup,
367     hide_marc               => C4::Context->preference('hide_marc'),
368         marcview => 1,
369         z3950_search_params             => C4::Search::z3950_search_args($biblio),
370         C4::Search::enabled_staff_search_views,
371     searchid                => scalar $query->param('searchid'),
372     biblio                  => $biblio_object,
373     loggedinuser => $loggedinuser,
374 );
375
376 $template->param( holdcount => $biblio_object->holds->count );
377
378 output_html_with_http_headers $query, $cookie, $template->output;