Bug 19042: Add mock IndependentBranches=0 for the other test
[koha.git] / catalogue / MARCdetail.pl
blob2152ffd299bd6fde0c84868f70e4d1f4ad033271
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 strict;
47 #use warnings; FIXME - Bug 2505
48 use CGI qw ( -utf8 );
49 use HTML::Entities;
51 use C4::Auth;
52 use C4::Context;
53 use C4::Output;
54 use C4::Koha;
55 use MARC::Record;
56 use C4::Biblio;
57 use C4::Items;
58 use C4::Acquisition;
59 use C4::Serials; #uses getsubscriptionsfrombiblionumber GetSubscriptionsFromBiblionumber
60 use C4::Search; # enabled_staff_search_views
62 use Koha::Biblios;
63 use Koha::BiblioFrameworks;
64 use Koha::Patrons;
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');
73 $frameworkcode = GetFrameworkCode( $biblionumber ) unless ($frameworkcode);
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');
79 # open template
80 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
82 template_name => "catalogue/MARCdetail.tt",
83 query => $query,
84 type => "intranet",
85 authnotrequired => 0,
86 flagsrequired => { catalogue => 1 },
87 debug => 1,
91 my $record = GetMarcBiblio($biblionumber, 1);
92 $template->param( ocoins => GetCOinSBiblio($record) );
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 #count of item linked
118 my $itemcount = $biblio_object->items->count;
119 $template->param( count => $itemcount,
120 bibliotitle => $biblio->{title}, );
122 my $frameworks = Koha::BiblioFrameworks->search( {}, { order_by => ['frameworktext'] } );
123 $template->param(
124 frameworks => $frameworks,
125 frameworkcode => $frameworkcode,
127 # fill arrays
128 my @loop_data = ();
130 # loop through each tab 0 through 9
131 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
133 # loop through each tag
134 my @fields = $record->fields();
135 my @loop_data = ();
136 my @subfields_data;
138 # deal with leader
139 unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop )
140 { # or ($tagslib->{'000'}->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/ )) {
141 my %subfield_data;
142 $subfield_data{marc_lib} = $tagslib->{'000'}->{'@'}->{lib};
143 $subfield_data{marc_value} = $record->leader();
144 $subfield_data{marc_subfield} = '@';
145 $subfield_data{marc_tag} = '000';
146 push( @subfields_data, \%subfield_data );
147 my %tag_data;
148 $tag_data{tag} = '000';
149 $tag_data{tag_desc} = $tagslib->{'000'}->{lib};
150 my @tmp = @subfields_data;
151 $tag_data{subfield} = \@tmp;
152 push( @loop_data, \%tag_data );
153 undef @subfields_data;
155 @fields = $record->fields();
156 for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
158 # if tag <10, there's no subfield, use the "@" trick
159 if ( $fields[$x_i]->tag() < 10 ) {
160 next
161 if (
162 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
163 next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
164 my %subfield_data;
165 $subfield_data{marc_lib} =
166 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
167 $subfield_data{marc_value} = $fields[$x_i]->data();
168 $subfield_data{marc_subfield} = '@';
169 $subfield_data{marc_tag} = $fields[$x_i]->tag();
170 push( @subfields_data, \%subfield_data );
172 else {
173 my @subf = $fields[$x_i]->subfields;
175 # loop through each subfield
176 for my $i ( 0 .. $#subf ) {
177 $subf[$i][0] = "@" unless defined $subf[$i][0];
178 next
179 if (
180 $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{tab}
181 ne $tabloop );
182 next
183 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
184 ->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
185 my %subfield_data;
186 $subfield_data{short_desc} = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
187 $subfield_data{long_desc} =
188 $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
189 $subfield_data{link} =
190 $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{link};
192 # warn "tag : ".$tagslib->{$fields[$x_i]->tag()}." subfield :".$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}."lien koha? : "$subfield_data{link};
193 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
194 ->{isurl} )
196 $subfield_data{marc_value} = $subf[$i][1];
197 $subfield_data{is_url} = 1;
199 elsif ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
200 ->{kohafield} eq "biblioitems.isbn" )
203 # 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]);
204 $subfield_data{marc_value} = $subf[$i][1];
206 else {
207 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
208 ->{authtypecode} )
210 $subfield_data{authority} = $fields[$x_i]->subfield(9);
212 $subfield_data{marc_value} =
213 GetAuthorisedValueDesc( $fields[$x_i]->tag(),
214 $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
217 $subfield_data{marc_subfield} = $subf[$i][0];
218 $subfield_data{marc_tag} = $fields[$x_i]->tag();
219 push( @subfields_data, \%subfield_data );
222 if ( $#subfields_data == 0 ) {
223 $subfields_data[0]->{marc_lib} = '';
224 # $subfields_data[0]->{marc_subfield} = '';
226 if ( $#subfields_data >= 0) {
227 my %tag_data;
228 if ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() && (C4::Context->preference('LabelMARCView') eq 'economical')) {
229 $tag_data{tag} = "";
231 else {
232 if ( C4::Context->preference('hide_marc') ) {
233 $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
235 else {
236 $tag_data{tag} = $fields[$x_i]->tag();
237 $tag_data{tag_ind} = C4::Koha::display_marc_indicators($fields[$x_i]);
238 $tag_data{tag_desc} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
241 my @tmp = @subfields_data;
242 $tag_data{subfield} = \@tmp;
243 push( @loop_data, \%tag_data );
244 undef @subfields_data;
247 $template->param( "tab" . $tabloop . "XX" => \@loop_data );
250 # now, build item tab !
251 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
252 # loop through each tag
253 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
254 # then construct template.
255 my @fields = $record->fields();
256 my %witness
257 ; #---- stores the list of subfields used at least once, with the "meaning" of the code
258 my @item_subfield_codes;
259 my @item_loop;
260 my $norequests = 1;
261 foreach my $field (@fields) {
262 next if ( $field->tag() < 10 );
263 my @subf = $field->subfields;
264 my $item;
266 # loop through each subfield
267 for my $i ( 0 .. $#subf ) {
268 next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab} ne 10 );
269 next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
270 push @item_subfield_codes, $subf[$i][0];
271 $witness{ $subf[$i][0] } =
272 $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
273 $item->{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(),
274 $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
275 $norequests = 0 if $subf[$i][1] ==0 and $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield} eq 'items.notforloan';
277 push @item_loop, $item if $item;
280 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
281 @item_loop = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @item_loop;
283 @item_subfield_codes = uniq @item_subfield_codes;
284 # fill item info
285 my @item_header_loop;
286 for my $subfield_code ( @item_subfield_codes ) {
287 push @item_header_loop, $witness{$subfield_code};
288 for my $item_data ( @item_loop ) {
289 $item_data->{$subfield_code} ||= "&nbsp;"
293 my $subscriptionscount = CountSubscriptionFromBiblionumber($biblionumber);
295 if ($subscriptionscount) {
296 my $subscriptions = GetSubscriptionsFromBiblionumber($biblionumber);
297 my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
298 $template->param(
299 subscriptiontitle => $subscriptiontitle,
300 subscriptionsnumber => $subscriptionscount,
304 $template->param (
305 norequests => $norequests,
306 item_loop => \@item_loop,
307 item_header_loop => \@item_header_loop,
308 item_subfield_codes => \@item_subfield_codes,
309 biblionumber => $biblionumber,
310 popup => $popup,
311 hide_marc => C4::Context->preference('hide_marc'),
312 marcview => 1,
313 z3950_search_params => C4::Search::z3950_search_args($biblio),
314 C4::Search::enabled_staff_search_views,
315 searchid => scalar $query->param('searchid'),
318 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
319 my @deletedorders_using_biblio;
320 my @orders_using_biblio;
321 my @baskets_orders;
322 my @baskets_deletedorders;
324 foreach my $myorder (@allorders_using_biblio) {
325 my $basket = $myorder->{'basketno'};
326 if ((defined $myorder->{'datecancellationprinted'}) and ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
327 push @deletedorders_using_biblio, $myorder;
328 unless (grep(/^$basket$/, @baskets_deletedorders)){
329 push @baskets_deletedorders,$myorder->{'basketno'};
332 else {
333 push @orders_using_biblio, $myorder;
334 unless (grep(/^$basket$/, @baskets_orders)){
335 push @baskets_orders,$myorder->{'basketno'};
340 my $count_orders_using_biblio = scalar @orders_using_biblio ;
341 $template->param (countorders => $count_orders_using_biblio);
343 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
344 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
346 $biblio = Koha::Biblios->find( $biblionumber );
347 my $holds = $biblio->holds;
348 $template->param( holdcount => $holds->count );
350 output_html_with_http_headers $query, $cookie, $template->output;