Bug 23794: (RM follow-up) Make DB update consistent
[koha.git] / catalogue / MARCdetail.pl
blob44bc8d0801bdac00dc61425f0d8b89b0de056a6c
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 BibLibre
6 # This file is part of Koha.
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.
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.
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>.
21 =head1 NAME
23 MARCdetail.pl : script to show a biblio in MARC format
25 =head1 SYNOPSIS
27 =cut
29 =head1 DESCRIPTION
31 This script needs a biblionumber as parameter
33 It shows the biblio in a (nice) MARC format depending on MARC
34 parameters tables.
36 The template is in <templates_dir>/catalogue/MARCdetail.tt.
37 this template must be divided into 11 "tabs".
39 The first 10 tabs present the biblio, the 11th one presents
40 the items attached to the biblio
42 =head1 FUNCTIONS
44 =cut
46 use Modern::Perl;
47 use CGI qw ( -utf8 );
48 use HTML::Entities;
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
61 use Koha::Biblios;
62 use Koha::BiblioFrameworks;
63 use Koha::Patrons;
64 use Koha::DateUtils;
66 use List::MoreUtils qw( uniq );
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');
78 # open template
79 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
81 template_name => "catalogue/MARCdetail.tt",
82 query => $query,
83 type => "intranet",
84 authnotrequired => 0,
85 flagsrequired => { catalogue => 1 },
86 debug => 1,
90 my $record = GetMarcBiblio({
91 biblionumber => $biblionumber,
92 embed_items => 1 });
94 if ( not defined $record ) {
95 # biblionumber invalid -> report and exit
96 $template->param( unknownbiblionumber => 1,
97 biblionumber => $biblionumber
99 output_html_with_http_headers $query, $cookie, $template->output;
100 exit;
103 my $biblio_object = Koha::Biblios->find( $biblionumber ); # FIXME Should replace $biblio
104 my $tagslib = &GetMarcStructure(1,$frameworkcode);
105 my $biblio = GetBiblioData($biblionumber);
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,
117 if( $query->cookie("searchToOrder") ){
118 my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") );
119 $template->param(
120 searchtoorder_basketno => $basketno,
121 searchtoorder_vendorid => $vendorid
125 $template->param( ocoins => $biblio_object->get_coins );
127 #count of item linked
128 my $itemcount = $biblio_object->items->count;
129 $template->param( count => $itemcount,
130 bibliotitle => $biblio->{title}, );
132 my $frameworks = Koha::BiblioFrameworks->search( {}, { order_by => ['frameworktext'] } );
133 $template->param(
134 frameworks => $frameworks,
135 frameworkcode => $frameworkcode,
137 # fill arrays
138 my @loop_data = ();
140 # loop through each tab 0 through 9
141 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
143 # loop through each tag
144 my @fields = $record->fields();
145 my @loop_data = ();
146 my @subfields_data;
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;
165 @fields = $record->fields();
166 for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
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 );
182 else {
183 my @subf = $fields[$x_i]->subfields;
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};
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} )
206 $subfield_data{marc_value} = $subf[$i][1];
207 $subfield_data{is_url} = 1;
209 elsif ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
210 ->{kohafield} eq "biblioitems.isbn" )
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];
216 else {
217 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
218 ->{authtypecode} )
220 $subfield_data{authority} = $fields[$x_i]->subfield(9);
222 $subfield_data{marc_value} =
223 GetAuthorisedValueDesc( $fields[$x_i]->tag(),
224 $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
227 $subfield_data{marc_subfield} = $subf[$i][0];
228 $subfield_data{marc_tag} = $fields[$x_i]->tag();
229 push( @subfields_data, \%subfield_data );
232 if ( $#subfields_data == 0 ) {
233 $subfields_data[0]->{marc_lib} = '';
234 # $subfields_data[0]->{marc_subfield} = '';
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} = "";
241 else {
242 if ( C4::Context->preference('hide_marc') ) {
243 $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
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};
251 my @tmp = @subfields_data;
252 $tag_data{subfield} = \@tmp;
253 push( @loop_data, \%tag_data );
254 undef @subfields_data;
257 $template->param( "tab" . $tabloop . "XX" => \@loop_data );
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;
272 foreach my $field (@fields) {
273 next if ( $field->tag() < 10 );
274 my @subf = $field->subfields;
275 my $item;
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/);
282 push @item_subfield_codes, $subf[$i][0];
283 $witness{ $subf[$i][0] } =
284 $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
286 # Allow repeatables (BZ 13574)
287 if( $item->{$subf[$i][0]}) {
288 $item->{$subf[$i][0]} .= ' | ';
289 } else {
290 $item->{$subf[$i][0]} = q{};
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];
298 $norequests = 0 if $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield} eq 'items.notforloan' and $subf[$i][1] == 0;
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 );
305 push @item_loop, $item if $item;
308 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField( "items.holdingbranch" );
309 @item_loop = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @item_loop;
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;"
321 my $subscriptionscount = CountSubscriptionFromBiblionumber($biblionumber);
323 if ($subscriptionscount) {
324 my $subscriptions = GetSubscriptionsFromBiblionumber($biblionumber);
325 my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
326 $template->param(
327 subscriptiontitle => $subscriptiontitle,
328 subscriptionsnumber => $subscriptionscount,
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,
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;
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'};
361 else {
362 push @orders_using_biblio, $myorder;
363 unless (grep { $_ eq $basket } @baskets_orders){
364 push @baskets_orders,$myorder->{'basketno'};
369 my $count_orders_using_biblio = scalar @orders_using_biblio ;
370 $template->param (countorders => $count_orders_using_biblio);
372 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
373 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
375 $biblio = Koha::Biblios->find( $biblionumber );
376 my $holds = $biblio->holds;
377 $template->param( holdcount => $holds->count );
379 output_html_with_http_headers $query, $cookie, $template->output;