Bug 25793: OAI 'Set' and 'Metadata' dropdowns broken by OPAC jQuery upgrade
[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
66 use List::MoreUtils qw( uniq );
67
68 my $query        = new CGI;
69 my $dbh          = C4::Context->dbh;
70 my $biblionumber = $query->param('biblionumber');
71 $biblionumber = HTML::Entities::encode($biblionumber);
72 my $frameworkcode = $query->param('frameworkcode') // GetFrameworkCode( $biblionumber );
73 my $popup        =
74   $query->param('popup')
75   ;    # if set to 1, then don't insert links, it's just to show the biblio
76 my $subscriptionid = $query->param('subscriptionid');
77
78 # open template
79 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
80     {
81         template_name   => "catalogue/MARCdetail.tt",
82         query           => $query,
83         type            => "intranet",
84         authnotrequired => 0,
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 $template->param( ocoins => $biblio_object->get_coins );
118
119 #count of item linked
120 my $itemcount = $biblio_object->items->count;
121 $template->param( count => $itemcount,
122                                         bibliotitle => $biblio->{title}, );
123
124 my $frameworks = Koha::BiblioFrameworks->search( {}, { order_by => ['frameworktext'] } );
125 $template->param(
126     frameworks    => $frameworks,
127     frameworkcode => $frameworkcode,
128 );
129 # fill arrays
130 my @loop_data = ();
131
132 # loop through each tab 0 through 9
133 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
134
135     # loop through each tag
136     my @fields    = $record->fields();
137     my @loop_data = ();
138     my @subfields_data;
139
140     # deal with leader
141     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop )
142     {    #  or ($tagslib->{'000'}->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/ )) {
143         my %subfield_data;
144         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
145         $subfield_data{marc_value}    = $record->leader();
146         $subfield_data{marc_subfield} = '@';
147         $subfield_data{marc_tag}      = '000';
148         push( @subfields_data, \%subfield_data );
149         my %tag_data;
150         $tag_data{tag} = '000';
151         $tag_data{tag_desc} = $tagslib->{'000'}->{lib};
152         my @tmp = @subfields_data;
153         $tag_data{subfield} = \@tmp;
154         push( @loop_data, \%tag_data );
155         undef @subfields_data;
156     }
157     @fields = $record->fields();
158     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
159
160         # if tag <10, there's no subfield, use the "@" trick
161         if ( $fields[$x_i]->tag() < 10 ) {
162             next
163               if (
164                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
165             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
166             my %subfield_data;
167             $subfield_data{marc_lib} =
168               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
169             $subfield_data{marc_value}    = $fields[$x_i]->data();
170             $subfield_data{marc_subfield} = '@';
171             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
172             push( @subfields_data, \%subfield_data );
173         }
174         else {
175             my @subf = $fields[$x_i]->subfields;
176
177             # loop through each subfield
178             for my $i ( 0 .. $#subf ) {
179                 $subf[$i][0] = "@" unless defined $subf[$i][0];
180                 next
181                   if (
182                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{tab}
183                     ne $tabloop );
184                 next
185                   if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
186                     ->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
187                 my %subfield_data;
188                 $subfield_data{short_desc} = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
189                 $subfield_data{long_desc} =
190                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
191                 $subfield_data{link} =
192                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{link};
193
194 #                 warn "tag : ".$tagslib->{$fields[$x_i]->tag()}." subfield :".$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}."lien koha? : "$subfield_data{link};
195                 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
196                     ->{isurl} )
197                 {
198                     $subfield_data{marc_value} = $subf[$i][1];
199                                         $subfield_data{is_url} = 1;
200                 }
201                 elsif ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
202                     ->{kohafield} eq "biblioitems.isbn" )
203                 {
204
205 #                    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]);
206                     $subfield_data{marc_value} = $subf[$i][1];
207                 }
208                 else {
209                     if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
210                         ->{authtypecode} )
211                     {
212                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
213                     }
214                     $subfield_data{marc_value} =
215                       GetAuthorisedValueDesc( $fields[$x_i]->tag(),
216                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
217
218                 }
219                 $subfield_data{marc_subfield} = $subf[$i][0];
220                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
221                 push( @subfields_data, \%subfield_data );
222             }
223         }
224         if ( $#subfields_data == 0 ) {
225             $subfields_data[0]->{marc_lib}      = '';
226 #            $subfields_data[0]->{marc_subfield} = '';
227         }
228         if ( $#subfields_data >= 0) {
229             my %tag_data;
230             if ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() && (C4::Context->preference('LabelMARCView') eq 'economical')) {
231                 $tag_data{tag} = "";
232             }
233             else {
234                 if ( C4::Context->preference('hide_marc') ) {
235                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
236                 }
237                 else {
238                     $tag_data{tag} = $fields[$x_i]->tag();
239             $tag_data{tag_ind} = C4::Koha::display_marc_indicators($fields[$x_i]);
240             $tag_data{tag_desc} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
241                 }
242             }
243             my @tmp = @subfields_data;
244             $tag_data{subfield} = \@tmp;
245             push( @loop_data, \%tag_data );
246             undef @subfields_data;
247         }
248     }
249     $template->param( "tab" . $tabloop . "XX" => \@loop_data );
250 }
251
252 # now, build item tab !
253 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
254 # loop through each tag
255 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
256 # then construct template.
257 my @fields = $record->fields();
258 my %witness
259   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
260 my @item_subfield_codes;
261 my @item_loop;
262 my $norequests = 1;
263
264 foreach my $field (@fields) {
265     next if ( $field->tag() < 10 );
266     my @subf = $field->subfields;
267     my $item;
268
269     # loop through each subfield
270     for my $i ( 0 .. $#subf ) {
271         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab} ne 10 );
272         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
273
274         push @item_subfield_codes, $subf[$i][0];
275         $witness{ $subf[$i][0] } =
276         $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
277
278         # Allow repeatables (BZ 13574)
279         if( $item->{$subf[$i][0]}) {
280             $item->{$subf[$i][0]} .= ' | ';
281         } else {
282             $item->{$subf[$i][0]} = q{};
283         }
284         if( $tagslib->{$field->tag()}->{$subf[$i][0]}->{isurl} ) {
285             $item->{$subf[$i][0]} .= "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
286         } else {
287             $item->{ $subf[$i][0] } .= GetAuthorisedValueDesc( $field->tag(), $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
288         }
289
290         $norequests = 0 if  $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield} eq 'items.notforloan' and $subf[$i][1] == 0;
291
292         my $kohafield = $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield};
293         $item->{ $subf[$i][0] } = output_pref( { str => $item->{ $subf[$i][0] }, dateonly => 1 } )
294           if grep { $kohafield eq $_ }
295               qw( items.dateaccessioned items.onloan items.datelastseen items.datelastborrowed items.replacementpricedate );
296     }
297     push @item_loop, $item if $item;
298 }
299
300 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField( "items.holdingbranch" );
301 @item_loop = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @item_loop;
302
303 @item_subfield_codes = uniq @item_subfield_codes;
304 # fill item info
305 my @item_header_loop;
306 for my $subfield_code ( @item_subfield_codes ) {
307     push @item_header_loop, $witness{$subfield_code};
308     for my $item_data ( @item_loop ) {
309         $item_data->{$subfield_code} ||= "&nbsp;"
310     }
311 }
312
313 my $subscriptionscount = CountSubscriptionFromBiblionumber($biblionumber);
314
315 if ($subscriptionscount) {
316     my $subscriptions = GetSubscriptionsFromBiblionumber($biblionumber);
317     my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
318     $template->param(
319         subscriptiontitle   => $subscriptiontitle,
320         subscriptionsnumber => $subscriptionscount,
321     );
322 }
323
324 $template->param (
325     norequests              => $norequests,
326     item_loop               => \@item_loop,
327     item_header_loop        => \@item_header_loop,
328     item_subfield_codes     => \@item_subfield_codes,
329     biblionumber            => $biblionumber,
330     popup                   => $popup,
331     hide_marc               => C4::Context->preference('hide_marc'),
332         marcview => 1,
333         z3950_search_params             => C4::Search::z3950_search_args($biblio),
334         C4::Search::enabled_staff_search_views,
335     searchid                => scalar $query->param('searchid'),
336     biblio                  => $biblio_object->unblessed,
337 );
338
339 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
340 my @deletedorders_using_biblio;
341 my @orders_using_biblio;
342 my @baskets_orders;
343 my @baskets_deletedorders;
344
345 foreach my $myorder (@allorders_using_biblio) {
346     my $basket = $myorder->{'basketno'};
347     if ((defined $myorder->{'datecancellationprinted'}) and  ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
348         push @deletedorders_using_biblio, $myorder;
349         unless (grep(/^$basket$/, @baskets_deletedorders)){
350             push @baskets_deletedorders,$myorder->{'basketno'};
351         }
352     }
353     else {
354         push @orders_using_biblio, $myorder;
355         unless (grep(/^$basket$/, @baskets_orders)){
356             push @baskets_orders,$myorder->{'basketno'};
357             }
358     }
359 }
360
361 my $count_orders_using_biblio = scalar @orders_using_biblio ;
362 $template->param (countorders => $count_orders_using_biblio);
363
364 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
365 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
366
367 $biblio = Koha::Biblios->find( $biblionumber );
368 my $holds = $biblio->holds;
369 $template->param( holdcount => $holds->count );
370
371 output_html_with_http_headers $query, $cookie, $template->output;