Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[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 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 my $norequests = 1;
271
272 foreach my $field (@fields) {
273     next if ( $field->tag() < 10 );
274     my @subf = $field->subfields;
275     my $item;
276
277     # loop through each subfield
278     for my $i ( 0 .. $#subf ) {
279         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab} ne 10 );
280         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
281
282         push @item_subfield_codes, $subf[$i][0];
283         $witness{ $subf[$i][0] } =
284         $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
285
286         # Allow repeatables (BZ 13574)
287         if( $item->{$subf[$i][0]}) {
288             $item->{$subf[$i][0]} .= ' | ';
289         } else {
290             $item->{$subf[$i][0]} = q{};
291         }
292         if( $tagslib->{$field->tag()}->{$subf[$i][0]}->{isurl} ) {
293             $item->{$subf[$i][0]} .= "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
294         } else {
295             $item->{ $subf[$i][0] } .= GetAuthorisedValueDesc( $field->tag(), $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
296         }
297
298         $norequests = 0 if  $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield} eq 'items.notforloan' and $subf[$i][1] == 0;
299
300         my $kohafield = $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield};
301         $item->{ $subf[$i][0] } = output_pref( { str => $item->{ $subf[$i][0] }, dateonly => 1 } )
302           if grep { $kohafield eq $_ }
303               qw( items.dateaccessioned items.onloan items.datelastseen items.datelastborrowed items.replacementpricedate );
304     }
305     push @item_loop, $item if $item;
306 }
307
308 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField( "items.holdingbranch" );
309 @item_loop = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @item_loop;
310
311 @item_subfield_codes = uniq @item_subfield_codes;
312 # fill item info
313 my @item_header_loop;
314 for my $subfield_code ( @item_subfield_codes ) {
315     push @item_header_loop, $witness{$subfield_code};
316     for my $item_data ( @item_loop ) {
317         $item_data->{$subfield_code} ||= "&nbsp;"
318     }
319 }
320
321 my $subscriptionscount = CountSubscriptionFromBiblionumber($biblionumber);
322
323 if ($subscriptionscount) {
324     my $subscriptions = GetSubscriptionsFromBiblionumber($biblionumber);
325     my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
326     $template->param(
327         subscriptiontitle   => $subscriptiontitle,
328         subscriptionsnumber => $subscriptionscount,
329     );
330 }
331
332 $template->param (
333     norequests              => $norequests,
334     item_loop               => \@item_loop,
335     item_header_loop        => \@item_header_loop,
336     item_subfield_codes     => \@item_subfield_codes,
337     biblionumber            => $biblionumber,
338     popup                   => $popup,
339     hide_marc               => C4::Context->preference('hide_marc'),
340         marcview => 1,
341         z3950_search_params             => C4::Search::z3950_search_args($biblio),
342         C4::Search::enabled_staff_search_views,
343     searchid                => scalar $query->param('searchid'),
344     biblio                  => $biblio_object->unblessed,
345 );
346
347 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
348 my @deletedorders_using_biblio;
349 my @orders_using_biblio;
350 my @baskets_orders;
351 my @baskets_deletedorders;
352
353 foreach my $myorder (@allorders_using_biblio) {
354     my $basket = $myorder->{'basketno'};
355     if ((defined $myorder->{'datecancellationprinted'}) and  ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
356         push @deletedorders_using_biblio, $myorder;
357         unless (grep{ $_ eq $basket } @baskets_deletedorders){
358             push @baskets_deletedorders,$myorder->{'basketno'};
359         }
360     }
361     else {
362         push @orders_using_biblio, $myorder;
363         unless (grep { $_ eq $basket } @baskets_orders){
364             push @baskets_orders,$myorder->{'basketno'};
365             }
366     }
367 }
368
369 my $count_orders_using_biblio = scalar @orders_using_biblio ;
370 $template->param (countorders => $count_orders_using_biblio);
371
372 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
373 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
374
375 $biblio = Koha::Biblios->find( $biblionumber );
376 my $holds = $biblio->holds;
377 $template->param( holdcount => $holds->count );
378
379 output_html_with_http_headers $query, $cookie, $template->output;