Bug 15801: Koha::BiblioFrameworks - Remove C4::Koha::getframeworks
[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 strict;
47 #use warnings; FIXME - Bug 2505
48 use CGI qw ( -utf8 );
49 use HTML::Entities;
50
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::Members; # to use GetMember
60 use C4::Serials;    #uses getsubscriptionsfrombiblionumber GetSubscriptionsFromBiblionumber
61 use C4::Search;         # enabled_staff_search_views
62 use Koha::BiblioFrameworks;
63
64 use List::MoreUtils qw( uniq );
65
66 my $query        = new CGI;
67 my $dbh          = C4::Context->dbh;
68 my $biblionumber = $query->param('biblionumber');
69 $biblionumber = HTML::Entities::encode($biblionumber);
70 my $frameworkcode = $query->param('frameworkcode');
71 $frameworkcode = GetFrameworkCode( $biblionumber ) unless ($frameworkcode);
72 my $popup        =
73   $query->param('popup')
74   ;    # if set to 1, then don't insert links, it's just to show the biblio
75 my $subscriptionid = $query->param('subscriptionid');
76
77 # open template
78 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
79     {
80         template_name   => "catalogue/MARCdetail.tt",
81         query           => $query,
82         type            => "intranet",
83         authnotrequired => 0,
84         flagsrequired   => { catalogue => 1 },
85         debug           => 1,
86     }
87 );
88
89 my $record = GetMarcBiblio($biblionumber, 1);
90 $template->param( ocoins => GetCOinSBiblio($record) );
91
92 if ( not defined $record ) {
93     # biblionumber invalid -> report and exit
94     $template->param( unknownbiblionumber => 1,
95                 biblionumber => $biblionumber
96     );
97     output_html_with_http_headers $query, $cookie, $template->output;
98     exit;
99 }
100
101 my $tagslib = &GetMarcStructure(1,$frameworkcode);
102 my $biblio = GetBiblioData($biblionumber);
103
104 if($query->cookie("holdfor")){ 
105     my $holdfor_patron = GetMember('borrowernumber' => $query->cookie("holdfor"));
106     $template->param(
107         holdfor => $query->cookie("holdfor"),
108         holdfor_surname => $holdfor_patron->{'surname'},
109         holdfor_firstname => $holdfor_patron->{'firstname'},
110         holdfor_cardnumber => $holdfor_patron->{'cardnumber'},
111     );
112 }
113
114 #count of item linked
115 my $itemcount = GetItemsCount($biblionumber);
116 $template->param( count => $itemcount,
117                                         bibliotitle => $biblio->{title}, );
118
119 my $frameworks = Koha::BiblioFrameworks->search( {}, { order_by => ['frameworktext'] } );
120 $template->param(
121     frameworks    => $frameworks,
122     frameworkcode => $frameworkcode,
123 );
124 # fill arrays
125 my @loop_data = ();
126
127 # loop through each tab 0 through 9
128 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
129
130     # loop through each tag
131     my @fields    = $record->fields();
132     my @loop_data = ();
133     my @subfields_data;
134
135     # deal with leader
136     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop )
137     {    #  or ($tagslib->{'000'}->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/ )) {
138         my %subfield_data;
139         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
140         $subfield_data{marc_value}    = $record->leader();
141         $subfield_data{marc_subfield} = '@';
142         $subfield_data{marc_tag}      = '000';
143         push( @subfields_data, \%subfield_data );
144         my %tag_data;
145         $tag_data{tag} = '000';
146         $tag_data{tag_desc} = $tagslib->{'000'}->{lib};
147         my @tmp = @subfields_data;
148         $tag_data{subfield} = \@tmp;
149         push( @loop_data, \%tag_data );
150         undef @subfields_data;
151     }
152     @fields = $record->fields();
153     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
154
155         # if tag <10, there's no subfield, use the "@" trick
156         if ( $fields[$x_i]->tag() < 10 ) {
157             next
158               if (
159                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
160             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
161             my %subfield_data;
162             $subfield_data{marc_lib} =
163               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
164             $subfield_data{marc_value}    = $fields[$x_i]->data();
165             $subfield_data{marc_subfield} = '@';
166             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
167             push( @subfields_data, \%subfield_data );
168         }
169         else {
170             my @subf = $fields[$x_i]->subfields;
171
172             # loop through each subfield
173             for my $i ( 0 .. $#subf ) {
174                 $subf[$i][0] = "@" unless defined $subf[$i][0];
175                 next
176                   if (
177                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{tab}
178                     ne $tabloop );
179                 next
180                   if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
181                     ->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
182                 my %subfield_data;
183                 $subfield_data{short_desc} = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
184                 $subfield_data{long_desc} =
185                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
186                 $subfield_data{link} =
187                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{link};
188
189 #                 warn "tag : ".$tagslib->{$fields[$x_i]->tag()}." subfield :".$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}."lien koha? : "$subfield_data{link};
190                 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
191                     ->{isurl} )
192                 {
193                     $subfield_data{marc_value} = $subf[$i][1];
194                                         $subfield_data{is_url} = 1;
195                 }
196                 elsif ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
197                     ->{kohafield} eq "biblioitems.isbn" )
198                 {
199
200 #                    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]);
201                     $subfield_data{marc_value} = $subf[$i][1];
202                 }
203                 else {
204                     if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
205                         ->{authtypecode} )
206                     {
207                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
208                     }
209                     $subfield_data{marc_value} =
210                       GetAuthorisedValueDesc( $fields[$x_i]->tag(),
211                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
212
213                 }
214                 $subfield_data{marc_subfield} = $subf[$i][0];
215                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
216                 push( @subfields_data, \%subfield_data );
217             }
218         }
219         if ( $#subfields_data == 0 ) {
220             $subfields_data[0]->{marc_lib}      = '';
221 #            $subfields_data[0]->{marc_subfield} = '';
222         }
223         if ( $#subfields_data >= 0) {
224             my %tag_data;
225             if ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() && (C4::Context->preference('LabelMARCView') eq 'economical')) {
226                 $tag_data{tag} = "";
227             }
228             else {
229                 if ( C4::Context->preference('hide_marc') ) {
230                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
231                 }
232                 else {
233                     $tag_data{tag} = $fields[$x_i]->tag();
234             $tag_data{tag_ind} = C4::Koha::display_marc_indicators($fields[$x_i]);
235             $tag_data{tag_desc} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
236                 }
237             }
238             my @tmp = @subfields_data;
239             $tag_data{subfield} = \@tmp;
240             push( @loop_data, \%tag_data );
241             undef @subfields_data;
242         }
243     }
244     $template->param( "tab" . $tabloop . "XX" => \@loop_data );
245 }
246
247 # now, build item tab !
248 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
249 # loop through each tag
250 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
251 # then construct template.
252 my @fields = $record->fields();
253 my %witness
254   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
255 my @item_subfield_codes;
256 my @item_loop;
257 my $norequests = 1;
258 foreach my $field (@fields) {
259     next if ( $field->tag() < 10 );
260     my @subf = $field->subfields;
261     my $item;
262
263     # loop through each subfield
264     for my $i ( 0 .. $#subf ) {
265         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab} ne 10 );
266         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
267         push @item_subfield_codes, $subf[$i][0];
268         $witness{ $subf[$i][0] } =
269         $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
270         $item->{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(),
271                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
272         $norequests = 0 if $subf[$i][1] ==0 and $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield} eq 'items.notforloan';
273     }
274     push @item_loop, $item if $item;
275 }
276
277 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
278 @item_loop = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @item_loop;
279
280 @item_subfield_codes = uniq @item_subfield_codes;
281 # fill item info
282 my @item_header_loop;
283 for my $subfield_code ( @item_subfield_codes ) {
284     push @item_header_loop, $witness{$subfield_code};
285     for my $item_data ( @item_loop ) {
286         $item_data->{$subfield_code} ||= "&nbsp;"
287     }
288 }
289
290 my $subscriptionscount = CountSubscriptionFromBiblionumber($biblionumber);
291
292 if ($subscriptionscount) {
293     my $subscriptions = GetSubscriptionsFromBiblionumber($biblionumber);
294     my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
295     $template->param(
296         subscriptiontitle   => $subscriptiontitle,
297         subscriptionsnumber => $subscriptionscount,
298     );
299 }
300
301 $template->param (
302     norequests              => $norequests,
303     item_loop               => \@item_loop,
304     item_header_loop        => \@item_header_loop,
305     item_subfield_codes     => \@item_subfield_codes,
306     biblionumber            => $biblionumber,
307     popup                   => $popup,
308     hide_marc               => C4::Context->preference('hide_marc'),
309         marcview => 1,
310         z3950_search_params             => C4::Search::z3950_search_args($biblio),
311         C4::Search::enabled_staff_search_views,
312     searchid            => scalar $query->param('searchid'),
313 );
314
315 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
316 my @deletedorders_using_biblio;
317 my @orders_using_biblio;
318 my @baskets_orders;
319 my @baskets_deletedorders;
320
321 foreach my $myorder (@allorders_using_biblio) {
322     my $basket = $myorder->{'basketno'};
323     if ((defined $myorder->{'datecancellationprinted'}) and  ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
324         push @deletedorders_using_biblio, $myorder;
325         unless (grep(/^$basket$/, @baskets_deletedorders)){
326             push @baskets_deletedorders,$myorder->{'basketno'};
327         }
328     }
329     else {
330         push @orders_using_biblio, $myorder;
331         unless (grep(/^$basket$/, @baskets_orders)){
332             push @baskets_orders,$myorder->{'basketno'};
333             }
334     }
335 }
336
337 my $count_orders_using_biblio = scalar @orders_using_biblio ;
338 $template->param (countorders => $count_orders_using_biblio);
339
340 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
341 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
342
343 my $holds = C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
344 my $holdcount = scalar( @$holds );
345 $template->param( holdcount => scalar ( @$holds ) );
346
347 output_html_with_http_headers $query, $cookie, $template->output;