Bug 3215 [REBASED] It would be nice if "Add to Cart" changed to "Remove from Cart"
[koha.git] / opac / opac-MARCdetail.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Parts 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 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.
12 #
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.
16 #
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.
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.tmpl.
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 =cut
43
44 use strict;
45 use warnings;
46
47 use C4::Auth;
48 use C4::Context;
49 use C4::Output;
50 use CGI;
51 use MARC::Record;
52 use C4::Biblio;
53 use C4::Acquisition;
54 use C4::Koha;
55
56 my $query = new CGI;
57
58 my $dbh = C4::Context->dbh;
59
60 my $biblionumber = $query->param('biblionumber');
61 my $itemtype     = &GetFrameworkCode($biblionumber);
62 my $tagslib      = &GetMarcStructure( 0, $itemtype );
63 my $biblio = GetBiblioData($biblionumber);
64 my $record = GetMarcBiblio($biblionumber, 1);
65 if ( ! $record ) {
66     print $query->redirect("/cgi-bin/koha/errors/404.pl");
67     exit;
68 }
69 # open template
70 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
71     {
72         template_name   => "opac-MARCdetail.tmpl",
73         query           => $query,
74         type            => "opac",
75         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
76         debug           => 1,
77     }
78 );
79
80 $template->param(
81     bibliotitle => $biblio->{title},
82 );
83
84 # get biblionumbers stored in the cart
85 my @cart_list;
86
87 if($query->cookie("bib_list")){
88     my $cart_list = $query->cookie("bib_list");
89     @cart_list = split(/\//, $cart_list);
90     if ( grep {$_ eq $biblionumber} @cart_list) {
91         $template->param( incart => 1 );
92     }
93 }
94
95 $template->param( 'AllowOnShelfHolds' => C4::Context->preference('AllowOnShelfHolds') );
96 $template->param( 'ItemsIssued' => CountItemsIssued( $biblionumber ) );
97
98 # adding the $RequestOnOpac param
99 my $RequestOnOpac;
100 if (C4::Context->preference("RequestOnOpac")) {
101         $RequestOnOpac = 1;
102 }
103
104 # fill arrays
105 my @loop_data = ();
106 my $tag;
107
108 # loop through each tab 0 through 9
109 for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
110
111     # loop through each tag
112     my @loop_data = ();
113     my @subfields_data;
114
115     # deal with leader
116     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop
117         or $tagslib->{'000'}->{'@'}->{hidden} > 0 )
118     {
119         my %subfield_data;
120         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
121         $subfield_data{marc_value}    = $record->leader();
122         $subfield_data{marc_subfield} = '@';
123         $subfield_data{marc_tag}      = '000';
124         push( @subfields_data, \%subfield_data );
125         my %tag_data;
126         $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
127         my @tmp = @subfields_data;
128         $tag_data{subfield} = \@tmp;
129         push( @loop_data, \%tag_data );
130         undef @subfields_data;
131     }
132     my @fields = $record->fields();
133     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
134
135         # if tag <10, there's no subfield, use the "@" trick
136         if ( $fields[$x_i]->tag() < 10 ) {
137             next
138               if (
139                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
140             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} > 0 );
141             my %subfield_data;
142             $subfield_data{marc_lib} =
143               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
144             $subfield_data{marc_value}    = $fields[$x_i]->data();
145             $subfield_data{marc_subfield} = '@';
146             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
147             push( @subfields_data, \%subfield_data );
148         }
149         else {
150             my @subf = $fields[$x_i]->subfields;
151             my $previous = '';
152             # loop through each subfield
153             for my $i ( 0 .. $#subf ) {
154                 $subf[$i][0] = "@" unless defined($subf[$i][0]);
155                 my $sf_def = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] };
156                 next if ( ($sf_def->{tab}||0) != $tabloop );
157                 next if ( ($sf_def->{hidden}||0) > 0 );
158                 my %subfield_data;
159                 $subfield_data{marc_lib} = ($sf_def->{lib} eq $previous) ?  '--' : $sf_def->{lib};
160                 $previous = $sf_def->{lib};
161                 $subfield_data{link} = $sf_def->{link};
162                 $subf[$i][1] =~ s/\n/<br\/>/g;
163                 if ( $sf_def->{isurl} ) {
164                     $subfield_data{marc_value} = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
165                 }
166                 elsif ( defined($sf_def->{kohafield}) && $sf_def->{kohafield} eq "biblioitems.isbn" ) {
167                     $subfield_data{marc_value} = $subf[$i][1];
168                 }
169                 else {
170                     if ( $sf_def->{authtypecode} ) {
171                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
172                     }
173                     $subfield_data{marc_value} = GetAuthorisedValueDesc( $fields[$x_i]->tag(),
174                         $subf[$i][0], $subf[$i][1], '', $tagslib, '', 'opac' );
175                 }
176                 $subfield_data{marc_subfield} = $subf[$i][0];
177                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
178                 push( @subfields_data, \%subfield_data );
179             }
180         }
181         if ( $#subfields_data >= 0 ) {
182             my %tag_data;
183             if (   ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() )
184                 && ( C4::Context->preference('LabelMARCView') eq 'economical' )
185               )
186             {
187                 $tag_data{tag} = "";
188             }
189             else {
190                 if ( C4::Context->preference('hide_marc') ) {
191                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
192                 }
193                 else {
194                     $tag_data{tag} =
195                         $fields[$x_i]->tag() 
196                       . ' '
197                       . C4::Koha::display_marc_indicators($fields[$x_i])
198                       . ' - '
199                       . $tagslib->{ $fields[$x_i]->tag() }->{lib};
200                 }
201             }
202             my @tmp = @subfields_data;
203             $tag_data{subfield} = \@tmp;
204             push( @loop_data, \%tag_data );
205             undef @subfields_data;
206         }
207     }
208     $template->param( "tab" . $tabloop . "XX" => \@loop_data );
209 }
210
211
212 # now, build item tab !
213 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
214 # loop through each tag
215 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
216 # then construct template.
217 my @fields = $record->fields();
218 my %witness
219   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
220 my @big_array;
221 foreach my $field (@fields) {
222     next if ( $field->tag() < 10 );
223     my @subf = $field->subfields;
224     my %this_row;
225
226     # loop through each subfield
227     for my $i ( 0 .. $#subf ) {
228         my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
229         next if ( ($sf_def->{tab}||0) != 10 );
230         next if ( ($sf_def->{hidden}||0) > 0 );
231         $witness{ $subf[$i][0] } = $sf_def->{lib};
232
233         if ( $sf_def->{isurl} ) {
234             $this_row{ $subf[$i][0] } = "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
235         }
236         elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
237             $this_row{ $subf[$i][0] } = $subf[$i][1];
238         }
239         else {
240             $this_row{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(), $subf[$i][0],
241                 $subf[$i][1], '', $tagslib, '', 'opac' );
242         }
243     }
244     if (%this_row) {
245         push( @big_array, \%this_row );
246     }
247 }
248 my ( $holdingbrtagf, $holdingbrtagsubf ) =
249   &GetMarcFromKohaField( "items.holdingbranch", $itemtype );
250 @big_array =
251   sort { ($a->{$holdingbrtagsubf}||'') cmp ($b->{$holdingbrtagsubf}||'') } @big_array;
252
253 #fill big_row with missing datas
254 foreach my $subfield_code ( keys(%witness) ) {
255     for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
256         $big_array[$i]{$subfield_code} = "&nbsp;"
257           unless ( $big_array[$i]{$subfield_code} );
258     }
259 }
260
261 # now, construct template !
262 my @item_value_loop;
263 my @header_value_loop;
264 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
265     my $items_data;
266     foreach my $subfield_code ( keys(%witness) ) {
267         $items_data .= "<td>" . $big_array[$i]{$subfield_code} . "</td>";
268     }
269     my %row_data;
270     $row_data{item_value} = $items_data;
271     push( @item_value_loop, \%row_data );
272 }
273
274 foreach my $subfield_code ( keys(%witness) ) {
275     my %header_value;
276     $header_value{header_value} = $witness{$subfield_code};
277     push( @header_value_loop, \%header_value );
278 }
279
280 if(C4::Context->preference("ISBD")) {
281         $template->param(ISBD => 1);
282 }
283
284 #Export options
285 my $OpacExportOptions=C4::Context->preference("OpacExportOptions");
286 my @export_options = split(/\|/,$OpacExportOptions);
287 $template->{VARS}->{'export_options'} = \@export_options;
288
289 #Search for title in links
290 my $marcflavour  = C4::Context->preference("marcflavour");
291 my $dat = TransformMarcToKoha( $dbh, $record );
292 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
293 my $marccontrolnumber   = GetMarcControlnumber   ($record, $marcflavour);
294
295 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
296     $dat->{author} ? $search_for_title =~ s/{AUTHOR}/$dat->{author}/g : $search_for_title =~ s/{AUTHOR}//g;
297     $dat->{title} =~ s/\/+$//; # remove trailing slash
298     $dat->{title} =~ s/\s+$//; # remove trailing space
299     $dat->{title} ? $search_for_title =~ s/{TITLE}/$dat->{title}/g : $search_for_title =~ s/{TITLE}//g;
300     $isbn ? $search_for_title =~ s/{ISBN}/$isbn/g : $search_for_title =~ s/{ISBN}//g;
301     $marccontrolnumber ? $search_for_title =~ s/{CONTROLNUMBER}/$marccontrolnumber/g : $search_for_title =~ s/{CONTROLNUMBER}//g;
302     $search_for_title =~ s/{BIBLIONUMBER}/$biblionumber/g;
303  $template->param('OPACSearchForTitleIn' => $search_for_title);
304 }
305
306 $template->param(
307     item_loop        => \@item_value_loop,
308     item_header_loop => \@header_value_loop,
309     biblionumber     => $biblionumber,
310 );
311
312 output_html_with_http_headers $query, $cookie, $template->output;