Bug 2889 - templates should use [% IF ( loop.odd ) %] - Administration
[koha.git] / catalogue / MARCdetail.pl
blob5eb50e5b5d185b421c4060fdeb675dd3d84aa2d5
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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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
49 use C4::Auth;
50 use C4::Context;
51 use C4::Output;
52 use CGI;
53 use C4::Koha;
54 use MARC::Record;
55 use C4::Biblio;
56 use C4::Items;
57 use C4::Acquisition;
58 use C4::Members; # to use GetMember
59 use C4::Serials; #uses getsubscriptionsfrombiblionumber GetSubscriptionsFromBiblionumber
60 use C4::Search; # enabled_staff_search_views
63 my $query = new CGI;
64 my $dbh = C4::Context->dbh;
65 my $biblionumber = $query->param('biblionumber');
66 my $frameworkcode = $query->param('frameworkcode');
67 $frameworkcode = GetFrameworkCode( $biblionumber ) unless ($frameworkcode);
68 my $popup =
69 $query->param('popup')
70 ; # if set to 1, then don't insert links, it's just to show the biblio
71 my $subscriptionid = $query->param('subscriptionid');
73 # open template
74 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
76 template_name => "catalogue/MARCdetail.tt",
77 query => $query,
78 type => "intranet",
79 authnotrequired => 0,
80 flagsrequired => { catalogue => 1 },
81 debug => 1,
85 my $record = GetMarcBiblio($biblionumber, 1);
86 $template->param( ocoins => GetCOinSBiblio($record) );
88 if ( not defined $record ) {
89 # biblionumber invalid -> report and exit
90 $template->param( unknownbiblionumber => 1,
91 biblionumber => $biblionumber
93 output_html_with_http_headers $query, $cookie, $template->output;
94 exit;
97 my $tagslib = &GetMarcStructure(1,$frameworkcode);
98 my $biblio = GetBiblioData($biblionumber);
100 if($query->cookie("holdfor")){
101 my $holdfor_patron = GetMember('borrowernumber' => $query->cookie("holdfor"));
102 $template->param(
103 holdfor => $query->cookie("holdfor"),
104 holdfor_surname => $holdfor_patron->{'surname'},
105 holdfor_firstname => $holdfor_patron->{'firstname'},
106 holdfor_cardnumber => $holdfor_patron->{'cardnumber'},
110 #count of item linked
111 my $itemcount = GetItemsCount($biblionumber);
112 $template->param( count => $itemcount,
113 bibliotitle => $biblio->{title}, );
115 # Getting the list of all frameworks
116 # get framework list
117 my $frameworks = getframeworks;
118 my @frameworkcodeloop;
119 foreach my $thisframeworkcode ( keys %$frameworks ) {
120 my %row = (
121 value => $thisframeworkcode,
122 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
124 if ($frameworkcode eq $thisframeworkcode){
125 $row{'selected'}= 1;
127 push @frameworkcodeloop, \%row;
129 $template->param( frameworkcodeloop => \@frameworkcodeloop, );
130 # fill arrays
131 my @loop_data = ();
132 my $tag;
134 # loop through each tab 0 through 9
135 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
137 # loop through each tag
138 my @fields = $record->fields();
139 my @loop_data = ();
140 my @subfields_data;
142 # deal with leader
143 unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop )
144 { # or ($tagslib->{'000'}->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/ )) {
145 my %subfield_data;
146 $subfield_data{marc_lib} = $tagslib->{'000'}->{'@'}->{lib};
147 $subfield_data{marc_value} = $record->leader();
148 $subfield_data{marc_subfield} = '@';
149 $subfield_data{marc_tag} = '000';
150 push( @subfields_data, \%subfield_data );
151 my %tag_data;
152 $tag_data{tag} = '000';
153 $tag_data{tag_desc} = $tagslib->{'000'}->{lib};
154 my @tmp = @subfields_data;
155 $tag_data{subfield} = \@tmp;
156 push( @loop_data, \%tag_data );
157 undef @subfields_data;
159 @fields = $record->fields();
160 for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
162 # if tag <10, there's no subfield, use the "@" trick
163 if ( $fields[$x_i]->tag() < 10 ) {
164 next
165 if (
166 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
167 next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
168 my %subfield_data;
169 $subfield_data{marc_lib} =
170 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
171 $subfield_data{marc_value} = $fields[$x_i]->data();
172 $subfield_data{marc_subfield} = '@';
173 $subfield_data{marc_tag} = $fields[$x_i]->tag();
174 push( @subfields_data, \%subfield_data );
176 else {
177 my @subf = $fields[$x_i]->subfields;
179 # loop through each subfield
180 for my $i ( 0 .. $#subf ) {
181 $subf[$i][0] = "@" unless defined $subf[$i][0];
182 next
183 if (
184 $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{tab}
185 ne $tabloop );
186 next
187 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
188 ->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
189 my %subfield_data;
190 $subfield_data{short_desc} = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
191 $subfield_data{long_desc} =
192 $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
193 $subfield_data{link} =
194 $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{link};
196 # warn "tag : ".$tagslib->{$fields[$x_i]->tag()}." subfield :".$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}."lien koha? : "$subfield_data{link};
197 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
198 ->{isurl} )
200 $subfield_data{marc_value} = $subf[$i][1];
201 $subfield_data{is_url} = 1;
203 elsif ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
204 ->{kohafield} eq "biblioitems.isbn" )
207 # 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]);
208 $subfield_data{marc_value} = $subf[$i][1];
210 else {
211 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
212 ->{authtypecode} )
214 $subfield_data{authority} = $fields[$x_i]->subfield(9);
216 $subfield_data{marc_value} =
217 GetAuthorisedValueDesc( $fields[$x_i]->tag(),
218 $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
221 $subfield_data{marc_subfield} = $subf[$i][0];
222 $subfield_data{marc_tag} = $fields[$x_i]->tag();
223 push( @subfields_data, \%subfield_data );
226 if ( $#subfields_data == 0 ) {
227 $subfields_data[0]->{marc_lib} = '';
228 # $subfields_data[0]->{marc_subfield} = '';
230 if ( $#subfields_data >= 0) {
231 my %tag_data;
232 if ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() && (C4::Context->preference('LabelMARCView') eq 'economical')) {
233 $tag_data{tag} = "";
235 else {
236 if ( C4::Context->preference('hide_marc') ) {
237 $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
239 else {
240 $tag_data{tag} = $fields[$x_i]->tag();
241 $tag_data{tag_ind} = C4::Koha::display_marc_indicators($fields[$x_i]);
242 $tag_data{tag_desc} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
245 my @tmp = @subfields_data;
246 $tag_data{subfield} = \@tmp;
247 push( @loop_data, \%tag_data );
248 undef @subfields_data;
251 $template->param( "tab" . $tabloop . "XX" => \@loop_data );
254 # now, build item tab !
255 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
256 # loop through each tag
257 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
258 # then construct template.
259 my @fields = $record->fields();
260 my %witness
261 ; #---- stores the list of subfields used at least once, with the "meaning" of the code
262 my @big_array;
263 my $norequests = 1;
264 foreach my $field (@fields) {
265 next if ( $field->tag() < 10 );
266 my @subf = $field->subfields;
267 my %this_row;
269 # loop through each subfield
270 for my $i ( 0 .. $#subf ) {
271 next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab} ne 10 );
272 next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
273 $witness{ $subf[$i][0] } =
274 $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
275 $this_row{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(),
276 $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
277 $norequests = 0 if $subf[$i][1] ==0 and $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield} eq 'items.notforloan';
279 if (%this_row) {
280 push( @big_array, \%this_row );
284 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
285 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
287 #fill big_row with missing datas
288 foreach my $subfield_code ( keys(%witness) ) {
289 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
290 $big_array[$i]{$subfield_code} = "&nbsp;"
291 unless ( $big_array[$i]{$subfield_code} );
295 # now, construct template !
296 my @item_value_loop;
297 my @header_value_loop;
298 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
299 my $items_data;
300 foreach my $subfield_code ( keys(%witness) ) {
301 $items_data .= "<td>" . $big_array[$i]{$subfield_code} . "</td>";
303 my %row_data;
304 $row_data{item_value} = $items_data;
305 push( @item_value_loop, \%row_data );
307 foreach my $subfield_code ( keys(%witness) ) {
308 my %header_value;
309 $header_value{header_value} = $witness{$subfield_code};
310 push( @header_value_loop, \%header_value );
313 my $subscriptionscount = CountSubscriptionFromBiblionumber($biblionumber);
315 if ($subscriptionscount) {
316 my $subscriptions = GetSubscriptionsFromBiblionumber($biblionumber);
317 my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
318 $template->param(
319 subscriptiontitle => $subscriptiontitle,
320 subscriptionsnumber => $subscriptionscount,
324 $template->param (
325 norequests => $norequests,
326 item_loop => \@item_value_loop,
327 item_header_loop => \@header_value_loop,
328 biblionumber => $biblionumber,
329 popup => $popup,
330 hide_marc => C4::Context->preference('hide_marc'),
331 marcview => 1,
332 z3950_search_params => C4::Search::z3950_search_args($biblio),
333 C4::Search::enabled_staff_search_views,
334 searchid => $query->param('searchid'),
337 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
338 my @deletedorders_using_biblio;
339 my @orders_using_biblio;
340 my @baskets_orders;
341 my @baskets_deletedorders;
343 foreach my $myorder (@allorders_using_biblio) {
344 my $basket = $myorder->{'basketno'};
345 if ((defined $myorder->{'datecancellationprinted'}) and ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
346 push @deletedorders_using_biblio, $myorder;
347 unless (grep(/^$basket$/, @baskets_deletedorders)){
348 push @baskets_deletedorders,$myorder->{'basketno'};
351 else {
352 push @orders_using_biblio, $myorder;
353 unless (grep(/^$basket$/, @baskets_orders)){
354 push @baskets_orders,$myorder->{'basketno'};
359 my $count_orders_using_biblio = scalar @orders_using_biblio ;
360 $template->param (countorders => $count_orders_using_biblio);
362 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
363 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
365 my $holds = C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
366 my $holdcount = scalar( @$holds );
367 $template->param( holdcount => scalar ( @$holds ) );
369 output_html_with_http_headers $query, $cookie, $template->output;