Bug 24663: (follow-up) Remove authnotrequired if set to 0
[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         flagsrequired   => { catalogue => 1 },
85         debug           => 1,
86     }
87 );
88
89 my $record = GetMarcBiblio({
90     biblionumber => $biblionumber,
91     embed_items  => 1 });
92
93 if ( not defined $record ) {
94     # biblionumber invalid -> report and exit
95     $template->param( unknownbiblionumber => 1,
96                 biblionumber => $biblionumber
97     );
98     output_html_with_http_headers $query, $cookie, $template->output;
99     exit;
100 }
101
102 my $biblio_object = Koha::Biblios->find( $biblionumber ); # FIXME Should replace $biblio
103 my $tagslib = &GetMarcStructure(1,$frameworkcode);
104 my $biblio = GetBiblioData($biblionumber);
105
106 if($query->cookie("holdfor")){ 
107     my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") );
108     $template->param(
109         holdfor => $query->cookie("holdfor"),
110         holdfor_surname => $holdfor_patron->surname,
111         holdfor_firstname => $holdfor_patron->firstname,
112         holdfor_cardnumber => $holdfor_patron->cardnumber,
113     );
114 }
115
116 if( $query->cookie("searchToOrder") ){
117     my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") );
118     $template->param(
119         searchtoorder_basketno => $basketno,
120         searchtoorder_vendorid => $vendorid
121     );
122 }
123
124 $template->param( ocoins => $biblio_object->get_coins );
125
126 #count of item linked
127 my $itemcount = $biblio_object->items->count;
128 $template->param( count => $itemcount,
129                                         bibliotitle => $biblio->{title}, );
130
131 my $frameworks = Koha::BiblioFrameworks->search( {}, { order_by => ['frameworktext'] } );
132 $template->param(
133     frameworks    => $frameworks,
134     frameworkcode => $frameworkcode,
135 );
136 # fill arrays
137 my @loop_data = ();
138
139 # loop through each tab 0 through 9
140 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
141
142     # loop through each tag
143     my @fields    = $record->fields();
144     my @loop_data = ();
145     my @subfields_data;
146
147     # deal with leader
148     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop )
149     {    #  or ($tagslib->{'000'}->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/ )) {
150         my %subfield_data;
151         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
152         $subfield_data{marc_value}    = $record->leader();
153         $subfield_data{marc_subfield} = '@';
154         $subfield_data{marc_tag}      = '000';
155         push( @subfields_data, \%subfield_data );
156         my %tag_data;
157         $tag_data{tag} = '000';
158         $tag_data{tag_desc} = $tagslib->{'000'}->{lib};
159         my @tmp = @subfields_data;
160         $tag_data{subfield} = \@tmp;
161         push( @loop_data, \%tag_data );
162         undef @subfields_data;
163     }
164     @fields = $record->fields();
165     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
166
167         # if tag <10, there's no subfield, use the "@" trick
168         if ( $fields[$x_i]->tag() < 10 ) {
169             next
170               if (
171                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
172             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
173             my %subfield_data;
174             $subfield_data{marc_lib} =
175               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
176             $subfield_data{marc_value}    = $fields[$x_i]->data();
177             $subfield_data{marc_subfield} = '@';
178             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
179             push( @subfields_data, \%subfield_data );
180         }
181         else {
182             my @subf = $fields[$x_i]->subfields;
183
184             # loop through each subfield
185             for my $i ( 0 .. $#subf ) {
186                 $subf[$i][0] = "@" unless defined $subf[$i][0];
187                 next
188                   if (
189                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{tab}
190                     ne $tabloop );
191                 next
192                   if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
193                     ->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
194                 my %subfield_data;
195                 $subfield_data{short_desc} = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
196                 $subfield_data{long_desc} =
197                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
198                 $subfield_data{link} =
199                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{link};
200
201 #                 warn "tag : ".$tagslib->{$fields[$x_i]->tag()}." subfield :".$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}."lien koha? : "$subfield_data{link};
202                 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
203                     ->{isurl} )
204                 {
205                     $subfield_data{marc_value} = $subf[$i][1];
206                                         $subfield_data{is_url} = 1;
207                 }
208                 elsif ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
209                     ->{kohafield} eq "biblioitems.isbn" )
210                 {
211
212 #                    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]);
213                     $subfield_data{marc_value} = $subf[$i][1];
214                 }
215                 else {
216                     if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
217                         ->{authtypecode} )
218                     {
219                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
220                     }
221                     $subfield_data{marc_value} =
222                       GetAuthorisedValueDesc( $fields[$x_i]->tag(),
223                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
224
225                 }
226                 $subfield_data{marc_subfield} = $subf[$i][0];
227                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
228                 push( @subfields_data, \%subfield_data );
229             }
230         }
231         if ( $#subfields_data == 0 ) {
232             $subfields_data[0]->{marc_lib}      = '';
233 #            $subfields_data[0]->{marc_subfield} = '';
234         }
235         if ( $#subfields_data >= 0) {
236             my %tag_data;
237             if ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() && (C4::Context->preference('LabelMARCView') eq 'economical')) {
238                 $tag_data{tag} = "";
239             }
240             else {
241                 if ( C4::Context->preference('hide_marc') ) {
242                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
243                 }
244                 else {
245                     $tag_data{tag} = $fields[$x_i]->tag();
246             $tag_data{tag_ind} = C4::Koha::display_marc_indicators($fields[$x_i]);
247             $tag_data{tag_desc} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
248                 }
249             }
250             my @tmp = @subfields_data;
251             $tag_data{subfield} = \@tmp;
252             push( @loop_data, \%tag_data );
253             undef @subfields_data;
254         }
255     }
256     $template->param( "tab" . $tabloop . "XX" => \@loop_data );
257 }
258
259 # now, build item tab !
260 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
261 # loop through each tag
262 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
263 # then construct template.
264 my @fields = $record->fields();
265 my %witness
266   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
267 my @item_subfield_codes;
268 my @item_loop;
269 my $norequests = 1;
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         $norequests = 0 if  $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield} eq 'items.notforloan' and $subf[$i][1] == 0;
298
299         my $kohafield = $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield};
300         $item->{ $subf[$i][0] } = output_pref( { str => $item->{ $subf[$i][0] }, dateonly => 1 } )
301           if grep { $kohafield eq $_ }
302               qw( items.dateaccessioned items.onloan items.datelastseen items.datelastborrowed items.replacementpricedate );
303     }
304     push @item_loop, $item if $item;
305 }
306
307 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField( "items.holdingbranch" );
308 @item_loop = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @item_loop;
309
310 @item_subfield_codes = uniq @item_subfield_codes;
311 # fill item info
312 my @item_header_loop;
313 for my $subfield_code ( @item_subfield_codes ) {
314     push @item_header_loop, $witness{$subfield_code};
315     for my $item_data ( @item_loop ) {
316         $item_data->{$subfield_code} ||= "&nbsp;"
317     }
318 }
319
320 my $subscriptionscount = CountSubscriptionFromBiblionumber($biblionumber);
321
322 if ($subscriptionscount) {
323     my $subscriptions = GetSubscriptionsFromBiblionumber($biblionumber);
324     my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
325     $template->param(
326         subscriptiontitle   => $subscriptiontitle,
327         subscriptionsnumber => $subscriptionscount,
328     );
329 }
330
331 $template->param (
332     norequests              => $norequests,
333     item_loop               => \@item_loop,
334     item_header_loop        => \@item_header_loop,
335     item_subfield_codes     => \@item_subfield_codes,
336     biblionumber            => $biblionumber,
337     popup                   => $popup,
338     hide_marc               => C4::Context->preference('hide_marc'),
339         marcview => 1,
340         z3950_search_params             => C4::Search::z3950_search_args($biblio),
341         C4::Search::enabled_staff_search_views,
342     searchid                => scalar $query->param('searchid'),
343     biblio                  => $biblio_object->unblessed,
344 );
345
346 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
347 my @deletedorders_using_biblio;
348 my @orders_using_biblio;
349 my @baskets_orders;
350 my @baskets_deletedorders;
351
352 foreach my $myorder (@allorders_using_biblio) {
353     my $basket = $myorder->{'basketno'};
354     if ((defined $myorder->{'datecancellationprinted'}) and  ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
355         push @deletedorders_using_biblio, $myorder;
356         unless (grep{ $_ eq $basket } @baskets_deletedorders){
357             push @baskets_deletedorders,$myorder->{'basketno'};
358         }
359     }
360     else {
361         push @orders_using_biblio, $myorder;
362         unless (grep { $_ eq $basket } @baskets_orders){
363             push @baskets_orders,$myorder->{'basketno'};
364             }
365     }
366 }
367
368 my $count_orders_using_biblio = scalar @orders_using_biblio ;
369 $template->param (countorders => $count_orders_using_biblio);
370
371 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
372 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
373
374 $biblio = Koha::Biblios->find( $biblionumber );
375 my $holds = $biblio->holds;
376 $template->param( holdcount => $holds->count );
377
378 output_html_with_http_headers $query, $cookie, $template->output;