fixing permissions on scripts
[koha.git] / catalogue / MARCdetail.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 =head1 NAME
21
22 MARCdetail.pl : script to show a biblio in MARC format
23
24 =head1 SYNOPSIS
25
26
27 =head1 DESCRIPTION
28
29 This script needs a biblionumber as parameter
30
31 It shows the biblio in a (nice) MARC format depending on MARC
32 parameters tables.
33
34 The template is in <templates_dir>/catalogue/MARCdetail.tmpl.
35 this template must be divided into 11 "tabs".
36
37 The first 10 tabs present the biblio, the 11th one presents
38 the items attached to the biblio
39
40 =head1 FUNCTIONS
41
42 =over 2
43
44 =cut
45
46 use strict;
47 require Exporter;
48 use C4::Auth;
49 use C4::Context;
50 use C4::Output;
51 use CGI;
52 use C4::Koha;
53 use MARC::Record;
54 use C4::Biblio;
55 use C4::Acquisition;
56 use C4::Serials;    #uses getsubscriptionsfrombiblionumber GetSubscriptionsFromBiblionumber
57
58 #---- Internal function ---
59 sub get_authorised_value_desc ($$$$$$) {
60     my ( $tagslib, $tag, $subfield, $value, $framework, $dbh ) = @_;
61
62     #---- branch
63     #if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
64
65         #       return GetBranchDetail($value)->{branchname};
66     #}
67
68     #---- itemtypes
69     #if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) {
70
71         #          my $itemtypedef = getitemtypeinfo($itemtype);
72         #      return $itemtypedef->{description};
73     #}
74
75     #---- "true" authorized value
76     my $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'};
77
78     if ( $category ne "" ) {
79         my $sth =
80           $dbh->prepare(
81 "select lib from authorised_values where category = ? and authorised_value = ?"
82           );
83         $sth->execute( $category, $value );
84         my $data = $sth->fetchrow_hashref;
85         return $data->{'lib'};
86     }
87     else {
88         return $value;    # if nothing is found return the original value
89     }
90 }
91
92 #---------
93
94 my $query        = new CGI;
95 my $dbh          = C4::Context->dbh;
96 my $biblionumber = $query->param('biblionumber');
97 my $frameworkcode = $query->param('frameworkcode');
98 $frameworkcode = GetFrameworkCode( $biblionumber ) unless ($frameworkcode);
99 my $popup        =
100   $query->param('popup')
101   ;    # if set to 1, then don't insert links, it's just to show the biblio
102 my $subscriptionid = $query->param('subscriptionid');
103
104 my $tagslib = &GetMarcStructure(1,$frameworkcode);
105
106 my $record = GetMarcBiblio($biblionumber);
107
108 # open template
109 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
110     {
111         template_name   => "catalogue/MARCdetail.tmpl",
112         query           => $query,
113         type            => "intranet",
114         authnotrequired => 0,
115         flagsrequired   => { catalogue => 1 },
116         debug           => 1,
117     }
118 );
119
120 #count of item linked
121 my $itemcount = GetItemsCount($biblionumber);
122 $template->param( count => $itemcount);
123
124 #Getting the list of all frameworks
125 my $queryfwk =
126   $dbh->prepare("select frameworktext, frameworkcode from biblio_framework");
127 $queryfwk->execute;
128 my %select_fwk;
129 my @select_fwk;
130 my $curfwk;
131 push @select_fwk, "Default";
132 $select_fwk{"Default"} = "Default";
133
134 while ( my ( $description, $fwk ) = $queryfwk->fetchrow ) {
135     push @select_fwk, $fwk;
136     $select_fwk{$fwk} = $description;
137 }
138 $curfwk=$frameworkcode;
139 my $framework=CGI::scrolling_list( -name     => 'Frameworks',
140             -id => 'Frameworks',
141             -default => $curfwk,
142             -OnChange => 'Changefwk(this);',
143             -values   => \@select_fwk,
144             -labels   => \%select_fwk,
145             -size     => 1,
146             -multiple => 0 );
147 $template->param(framework => $framework);
148 # fill arrays
149 my @loop_data = ();
150 my $tag;
151
152 # loop through each tab 0 through 9
153 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
154
155     # loop through each tag
156     my @fields    = $record->fields();
157     my @loop_data = ();
158     my @subfields_data;
159
160     # deal with leader
161     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop )
162     {    #  or ($tagslib->{'000'}->{'@'}->{hidden}==(-7|-4|-3|-2|2|3|5|8))) {
163         my %subfield_data;
164         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
165         $subfield_data{marc_value}    = $record->leader();
166         $subfield_data{marc_subfield} = '@';
167         $subfield_data{marc_tag}      = '000';
168         push( @subfields_data, \%subfield_data );
169         my %tag_data;
170         $tag_data{tag} = '000 -' . $tagslib->{'000'}->{lib};
171         my @tmp = @subfields_data;
172         $tag_data{subfield} = \@tmp;
173         push( @loop_data, \%tag_data );
174         undef @subfields_data;
175     }
176     @fields = $record->fields();
177     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
178
179         # if tag <10, there's no subfield, use the "@" trick
180         if ( $fields[$x_i]->tag() < 10 ) {
181             next
182               if (
183                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
184             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} );
185             my %subfield_data;
186             $subfield_data{marc_lib} =
187               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
188             $subfield_data{marc_value}    = $fields[$x_i]->data();
189             $subfield_data{marc_subfield} = '@';
190             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
191             push( @subfields_data, \%subfield_data );
192         }
193         else {
194             my @subf = $fields[$x_i]->subfields;
195
196             # loop through each subfield
197             for my $i ( 0 .. $#subf ) {
198                 $subf[$i][0] = "@" unless $subf[$i][0];
199                 next
200                   if (
201                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{tab}
202                     ne $tabloop );
203                 next
204                   if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
205                     ->{hidden} );
206                 my %subfield_data;
207                 $subfield_data{short_desc} = substr(
208                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib},
209                     0, 20
210                 );
211                 $subfield_data{long_desc} =
212                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
213                 $subfield_data{link} =
214                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{link};
215
216 #                 warn "tag : ".$tagslib->{$fields[$x_i]->tag()}." subfield :".$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}."lien koha? : "$subfield_data{link};
217                 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
218                     ->{isurl} )
219                 {
220                     $subfield_data{marc_value} =
221                       "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
222                 }
223                 elsif ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
224                     ->{kohafield} eq "biblioitems.isbn" )
225                 {
226
227 #                    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]);
228                     $subfield_data{marc_value} = DisplayISBN( $subf[$i][1] );
229                 }
230                 else {
231                     if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
232                         ->{authtypecode} )
233                     {
234                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
235                     }
236                     $subfield_data{marc_value} =
237                       get_authorised_value_desc( $tagslib, $fields[$x_i]->tag(),
238                         $subf[$i][0], $subf[$i][1], '', $dbh );
239                 }
240                 $subfield_data{marc_subfield} = $subf[$i][0];
241                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
242                 push( @subfields_data, \%subfield_data );
243             }
244         }
245         if ( $#subfields_data == 0 ) {
246             $subfields_data[0]->{marc_lib}      = '';
247             $subfields_data[0]->{marc_subfield} = '';
248         }
249         if ( $#subfields_data >= 0 ) {
250             my %tag_data;
251             if ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() ) {
252                 $tag_data{tag} = "";
253             }
254             else {
255                 if ( C4::Context->preference('hide_marc') ) {
256                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
257                 }
258                 else {
259                     $tag_data{tag} =
260                         $fields[$x_i]->tag() . ' -'
261                       . $tagslib->{ $fields[$x_i]->tag() }->{lib};
262                 }
263             }
264             my @tmp = @subfields_data;
265             $tag_data{subfield} = \@tmp;
266             push( @loop_data, \%tag_data );
267             undef @subfields_data;
268         }
269     }
270     $template->param( $tabloop . "XX" => \@loop_data );
271 }
272
273 # now, build item tab !
274 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
275 # loop through each tag
276 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
277 # then construct template.
278 my @fields = $record->fields();
279 my %witness
280   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
281 my @big_array;
282 foreach my $field (@fields) {
283     next if ( $field->tag() < 10 );
284     my @subf = $field->subfields;
285     my %this_row;
286
287     # loop through each subfield
288     for my $i ( 0 .. $#subf ) {
289         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab} ne 10 );
290         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} );
291         $witness{ $subf[$i][0] } =
292           $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
293         $this_row{ $subf[$i][0] } = $subf[$i][1];
294     }
295     if (%this_row) {
296         push( @big_array, \%this_row );
297     }
298 }
299 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
300 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
301
302 #fill big_row with missing datas
303 foreach my $subfield_code ( keys(%witness) ) {
304     for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
305         $big_array[$i]{$subfield_code} = "&nbsp;"
306           unless ( $big_array[$i]{$subfield_code} );
307     }
308 }
309
310 # now, construct template !
311 my @item_value_loop;
312 my @header_value_loop;
313 for ( my $i = 0 ; $i <= $#big_array ; $i++ ) {
314     my $items_data;
315     foreach my $subfield_code ( keys(%witness) ) {
316         $items_data .= "<td>" . $big_array[$i]{$subfield_code} . "</td>";
317     }
318     my %row_data;
319     $row_data{item_value} = $items_data;
320     push( @item_value_loop, \%row_data );
321 }
322 foreach my $subfield_code ( keys(%witness) ) {
323     my %header_value;
324     $header_value{header_value} = $witness{$subfield_code};
325     push( @header_value_loop, \%header_value );
326 }
327
328 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
329
330 if ($subscriptionsnumber) {
331     my $subscriptions = GetSubscriptionsFromBiblionumber($biblionumber);
332     my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
333     $template->param(
334         subscriptiontitle   => $subscriptiontitle,
335         subscriptionsnumber => $subscriptionsnumber,
336     );
337 }
338
339 $template->param (
340     item_loop               => \@item_value_loop,
341     item_header_loop        => \@header_value_loop,
342     biblionumber            => $biblionumber,
343     popup                   => $popup,
344     hide_marc               => C4::Context->preference('hide_marc'),
345     intranetcolorstylesheet =>
346       C4::Context->preference("intranetcolorstylesheet"),
347     intranetstylesheet => C4::Context->preference("intranetstylesheet"),
348     IntranetNav        => C4::Context->preference("IntranetNav"),
349
350 );
351
352 output_html_with_http_headers $query, $cookie, $template->output;