Bug 19436: Add SRU support for authorities
[koha.git] / catalogue / MARCdetail.pl
blob8940471456c52fa59a54ed52e206b00070b85349
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;
65 use List::MoreUtils qw( uniq );
67 my $query = new CGI;
68 my $dbh = C4::Context->dbh;
69 my $biblionumber = $query->param('biblionumber');
70 $biblionumber = HTML::Entities::encode($biblionumber);
71 my $frameworkcode = $query->param('frameworkcode');
72 $frameworkcode = GetFrameworkCode( $biblionumber ) unless ($frameworkcode);
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 });
93 $template->param( ocoins => GetCOinSBiblio($record) );
95 if ( not defined $record ) {
96 # biblionumber invalid -> report and exit
97 $template->param( unknownbiblionumber => 1,
98 biblionumber => $biblionumber
100 output_html_with_http_headers $query, $cookie, $template->output;
101 exit;
104 my $biblio_object = Koha::Biblios->find( $biblionumber ); # FIXME Should replace $biblio
105 my $tagslib = &GetMarcStructure(1,$frameworkcode);
106 my $biblio = GetBiblioData($biblionumber);
108 if($query->cookie("holdfor")){
109 my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") );
110 $template->param(
111 holdfor => $query->cookie("holdfor"),
112 holdfor_surname => $holdfor_patron->surname,
113 holdfor_firstname => $holdfor_patron->firstname,
114 holdfor_cardnumber => $holdfor_patron->cardnumber,
118 #count of item linked
119 my $itemcount = $biblio_object->items->count;
120 $template->param( count => $itemcount,
121 bibliotitle => $biblio->{title}, );
123 my $frameworks = Koha::BiblioFrameworks->search( {}, { order_by => ['frameworktext'] } );
124 $template->param(
125 frameworks => $frameworks,
126 frameworkcode => $frameworkcode,
128 # fill arrays
129 my @loop_data = ();
131 # loop through each tab 0 through 9
132 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
134 # loop through each tag
135 my @fields = $record->fields();
136 my @loop_data = ();
137 my @subfields_data;
139 # deal with leader
140 unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop )
141 { # or ($tagslib->{'000'}->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/ )) {
142 my %subfield_data;
143 $subfield_data{marc_lib} = $tagslib->{'000'}->{'@'}->{lib};
144 $subfield_data{marc_value} = $record->leader();
145 $subfield_data{marc_subfield} = '@';
146 $subfield_data{marc_tag} = '000';
147 push( @subfields_data, \%subfield_data );
148 my %tag_data;
149 $tag_data{tag} = '000';
150 $tag_data{tag_desc} = $tagslib->{'000'}->{lib};
151 my @tmp = @subfields_data;
152 $tag_data{subfield} = \@tmp;
153 push( @loop_data, \%tag_data );
154 undef @subfields_data;
156 @fields = $record->fields();
157 for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
159 # if tag <10, there's no subfield, use the "@" trick
160 if ( $fields[$x_i]->tag() < 10 ) {
161 next
162 if (
163 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
164 next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
165 my %subfield_data;
166 $subfield_data{marc_lib} =
167 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
168 $subfield_data{marc_value} = $fields[$x_i]->data();
169 $subfield_data{marc_subfield} = '@';
170 $subfield_data{marc_tag} = $fields[$x_i]->tag();
171 push( @subfields_data, \%subfield_data );
173 else {
174 my @subf = $fields[$x_i]->subfields;
176 # loop through each subfield
177 for my $i ( 0 .. $#subf ) {
178 $subf[$i][0] = "@" unless defined $subf[$i][0];
179 next
180 if (
181 $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{tab}
182 ne $tabloop );
183 next
184 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
185 ->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
186 my %subfield_data;
187 $subfield_data{short_desc} = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
188 $subfield_data{long_desc} =
189 $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
190 $subfield_data{link} =
191 $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{link};
193 # warn "tag : ".$tagslib->{$fields[$x_i]->tag()}." subfield :".$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}."lien koha? : "$subfield_data{link};
194 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
195 ->{isurl} )
197 $subfield_data{marc_value} = $subf[$i][1];
198 $subfield_data{is_url} = 1;
200 elsif ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
201 ->{kohafield} eq "biblioitems.isbn" )
204 # 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]);
205 $subfield_data{marc_value} = $subf[$i][1];
207 else {
208 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
209 ->{authtypecode} )
211 $subfield_data{authority} = $fields[$x_i]->subfield(9);
213 $subfield_data{marc_value} =
214 GetAuthorisedValueDesc( $fields[$x_i]->tag(),
215 $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
218 $subfield_data{marc_subfield} = $subf[$i][0];
219 $subfield_data{marc_tag} = $fields[$x_i]->tag();
220 push( @subfields_data, \%subfield_data );
223 if ( $#subfields_data == 0 ) {
224 $subfields_data[0]->{marc_lib} = '';
225 # $subfields_data[0]->{marc_subfield} = '';
227 if ( $#subfields_data >= 0) {
228 my %tag_data;
229 if ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() && (C4::Context->preference('LabelMARCView') eq 'economical')) {
230 $tag_data{tag} = "";
232 else {
233 if ( C4::Context->preference('hide_marc') ) {
234 $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
236 else {
237 $tag_data{tag} = $fields[$x_i]->tag();
238 $tag_data{tag_ind} = C4::Koha::display_marc_indicators($fields[$x_i]);
239 $tag_data{tag_desc} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
242 my @tmp = @subfields_data;
243 $tag_data{subfield} = \@tmp;
244 push( @loop_data, \%tag_data );
245 undef @subfields_data;
248 $template->param( "tab" . $tabloop . "XX" => \@loop_data );
251 # now, build item tab !
252 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
253 # loop through each tag
254 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
255 # then construct template.
256 my @fields = $record->fields();
257 my %witness
258 ; #---- stores the list of subfields used at least once, with the "meaning" of the code
259 my @item_subfield_codes;
260 my @item_loop;
261 my $norequests = 1;
262 foreach my $field (@fields) {
263 next if ( $field->tag() < 10 );
264 my @subf = $field->subfields;
265 my $item;
267 # loop through each subfield
268 for my $i ( 0 .. $#subf ) {
269 next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab} ne 10 );
270 next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
271 push @item_subfield_codes, $subf[$i][0];
272 $witness{ $subf[$i][0] } =
273 $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
274 $item->{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(),
275 $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
276 $norequests = 0 if $subf[$i][1] ==0 and $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield} eq 'items.notforloan';
278 push @item_loop, $item if $item;
281 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
282 @item_loop = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @item_loop;
284 @item_subfield_codes = uniq @item_subfield_codes;
285 # fill item info
286 my @item_header_loop;
287 for my $subfield_code ( @item_subfield_codes ) {
288 push @item_header_loop, $witness{$subfield_code};
289 for my $item_data ( @item_loop ) {
290 $item_data->{$subfield_code} ||= "&nbsp;"
294 my $subscriptionscount = CountSubscriptionFromBiblionumber($biblionumber);
296 if ($subscriptionscount) {
297 my $subscriptions = GetSubscriptionsFromBiblionumber($biblionumber);
298 my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
299 $template->param(
300 subscriptiontitle => $subscriptiontitle,
301 subscriptionsnumber => $subscriptionscount,
305 $template->param (
306 norequests => $norequests,
307 item_loop => \@item_loop,
308 item_header_loop => \@item_header_loop,
309 item_subfield_codes => \@item_subfield_codes,
310 biblionumber => $biblionumber,
311 popup => $popup,
312 hide_marc => C4::Context->preference('hide_marc'),
313 marcview => 1,
314 z3950_search_params => C4::Search::z3950_search_args($biblio),
315 C4::Search::enabled_staff_search_views,
316 searchid => scalar $query->param('searchid'),
319 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
320 my @deletedorders_using_biblio;
321 my @orders_using_biblio;
322 my @baskets_orders;
323 my @baskets_deletedorders;
325 foreach my $myorder (@allorders_using_biblio) {
326 my $basket = $myorder->{'basketno'};
327 if ((defined $myorder->{'datecancellationprinted'}) and ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
328 push @deletedorders_using_biblio, $myorder;
329 unless (grep(/^$basket$/, @baskets_deletedorders)){
330 push @baskets_deletedorders,$myorder->{'basketno'};
333 else {
334 push @orders_using_biblio, $myorder;
335 unless (grep(/^$basket$/, @baskets_orders)){
336 push @baskets_orders,$myorder->{'basketno'};
341 my $count_orders_using_biblio = scalar @orders_using_biblio ;
342 $template->param (countorders => $count_orders_using_biblio);
344 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
345 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
347 $biblio = Koha::Biblios->find( $biblionumber );
348 my $holds = $biblio->holds;
349 $template->param( holdcount => $holds->count );
351 output_html_with_http_headers $query, $cookie, $template->output;