Opac files before updates
[koha.git] / authorities / detail.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 detail.pl : script to show an authority in MARC format
23
24 =head1 SYNOPSIS
25
26
27 =head1 DESCRIPTION
28
29 This script needs an authid
30
31 It shows the authority in a (nice) MARC format depending on authority MARC
32 parameters tables.
33
34 =head1 FUNCTIONS
35
36 =over 2
37
38 =cut
39
40
41 use strict;
42 use warnings;
43
44 use C4::AuthoritiesMarc;
45 use C4::Auth;
46 use C4::Context;
47 use C4::Output;
48 use CGI;
49 use MARC::Record;
50 use C4::Koha;
51 # use C4::Biblio;
52 # use C4::Catalogue;
53
54 our ($tagslib);
55
56 =item build_authorized_values_list
57
58 =cut
59
60 sub build_authorized_values_list ($$$$$$$) {
61     my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
62
63     my @authorised_values;
64     my %authorised_lib;
65
66     # builds list, depending on authorised value...
67
68     #---- branch
69     if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
70         my $sth =
71           $dbh->prepare(
72             "select branchcode,branchname from branches order by branchname");
73         $sth->execute;
74         push @authorised_values, ""
75           unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
76
77         while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
78             push @authorised_values, $branchcode;
79             $authorised_lib{$branchcode} = $branchname;
80         }
81
82         #----- itemtypes
83     }
84     elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
85         my $sth =
86           $dbh->prepare(
87             "select itemtype,description from itemtypes order by description");
88         $sth->execute;
89         push @authorised_values, ""
90           unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
91           
92         my $itemtype;
93         
94         while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
95             push @authorised_values, $itemtype;
96             $authorised_lib{$itemtype} = $description;
97         }
98         $value = $itemtype unless ($value);
99
100         #---- "true" authorised value
101     }
102     else {
103         $authorised_values_sth->execute(
104             $tagslib->{$tag}->{$subfield}->{authorised_value} );
105
106         push @authorised_values, ""
107           unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
108
109         while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
110             push @authorised_values, $value;
111             $authorised_lib{$value} = $lib;
112         }
113     }
114     return CGI::scrolling_list(
115         -name     => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
116         -values   => \@authorised_values,
117         -default  => $value,
118         -labels   => \%authorised_lib,
119         -override => 1,
120         -size     => 1,
121         -multiple => 0,
122         -tabindex => 1,
123         -id       => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
124         -class    => "input_marceditor",
125     );
126 }
127
128
129 =item create_input
130  builds the <input ...> entry for a subfield.
131 =cut
132
133 sub create_input {
134     my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
135     
136     my $index_subfield = CreateKey(); # create a specifique key for each subfield
137
138     $value =~ s/"/&quot;/g;
139
140     # if there is no value provided but a default value in parameters, get it
141     unless ($value) {
142         $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
143
144         # get today date & replace YYYY, MM, DD if provided in the default value
145         my ( $year, $month, $day ) = Today();
146         $month = sprintf( "%02d", $month );
147         $day   = sprintf( "%02d", $day );
148         $value =~ s/YYYY/$year/g;
149         $value =~ s/MM/$month/g;
150         $value =~ s/DD/$day/g;
151     }
152     my $dbh = C4::Context->dbh;
153     my %subfield_data = (
154         tag        => $tag,
155         subfield   => $subfield,
156         marc_lib   => substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 22 ),
157         marc_lib_plain => $tagslib->{$tag}->{$subfield}->{lib}, 
158         tag_mandatory  => $tagslib->{$tag}->{mandatory},
159         mandatory      => $tagslib->{$tag}->{$subfield}->{mandatory},
160         repeatable     => $tagslib->{$tag}->{$subfield}->{repeatable},
161         kohafield      => $tagslib->{$tag}->{$subfield}->{kohafield},
162         index          => $index_tag,
163         id             => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
164         value          => $value,
165     );
166     if($subfield eq '@'){
167         $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_tag."_".$index_subfield;
168     } else {
169          $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield;
170     }
171
172     
173     $subfield_data{visibility} = "display:none;"
174         if (    ($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne ''
175             or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
176         );
177     
178     # it's an authorised field
179     if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
180         $subfield_data{marc_value} =
181           build_authorized_values_list( $tag, $subfield, $value, $dbh,
182             $authorised_values_sth,$index_tag,$index_subfield );
183
184     # it's a thesaurus / authority field
185     }
186     elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
187         $subfield_data{marc_value} =
188     "<input type=\"text\"
189                         id=\"".$subfield_data{id}."\"
190                         name=\"".$subfield_data{id}."\"
191       value=\"$value\"
192       class=\"input_marceditor\"
193                         tabindex=\"1\"                     
194       DISABLE READONLY \/>
195       <span class=\"buttonDot\"
196         onclick=\"Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=".$tagslib->{$tag}->{$subfield}->{authtypecode}."&index=$subfield_data{id}','$subfield_data{id}')\">...</span>
197     ";
198     # it's a plugin field
199     }
200     elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
201
202         # opening plugin. Just check wether we are on a developper computer on a production one
203         # (the cgidir differs)
204         my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
205         unless (-r $cgidir and -d $cgidir) {
206             $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
207         }
208         my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
209         do $plugin || die "Plugin Failed: ".$plugin;
210         my $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
211         my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
212 #         my ( $function_name, $javascript,$extended_param );
213         
214         $subfield_data{marc_value} =
215     "<input tabindex=\"1\"
216                         type=\"text\"
217                         id=".$subfield_data{id}."
218       name=".$subfield_data{id}."
219       value=\"$value\"
220                         class=\"input_marceditor\"
221       onfocus=\"javascript:Focus$function_name($index_tag)\"
222       onblur=\"javascript:Blur$function_name($index_tag); \" \/>
223     <span class=\"buttonDot\"
224       onclick=\"Clic$function_name('$subfield_data{id}')\">...</a>
225     $javascript";
226         # it's an hidden field
227     }
228     elsif ( $tag eq '' ) {
229         $subfield_data{marc_value} =
230             "<input tabindex=\"1\"
231                     type=\"hidden\"
232                     id=".$subfield_data{id}."
233                     name=".$subfield_data{id}."
234                     value=\"$value\" \/>
235             ";
236     }
237     elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
238         $subfield_data{marc_value} =
239             "<input type=\"text\"
240                     id=".$subfield_data{id}."
241                     name=".$subfield_data{id}."
242                     class=\"input_marceditor\"
243                     tabindex=\"1\"
244                     value=\"$value\"
245             \/>";
246
247         # it's a standard field
248     }
249     else {
250         if (
251             length($value) > 100
252             or
253             ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
254                 and $tag < 400 && $subfield eq 'a' )
255             or (    $tag >= 500
256                 and $tag < 600
257                 && C4::Context->preference("marcflavour") eq "MARC21" )
258           )
259         {
260             $subfield_data{marc_value} =
261                 "<textarea cols=\"70\"
262                            rows=\"4\"
263                            id=".$subfield_data{id}."
264                            name=".$subfield_data{id}."
265                            class=\"input_marceditor\"
266                            tabindex=\"1\"
267                            >$value</textarea>
268                 ";
269         }
270         else {
271             $subfield_data{marc_value} =
272                 "<input type=\"text\"
273                         id=".$subfield_data{id}."
274                         name=".$subfield_data{id}."
275                         value=\"$value\"
276                         tabindex=\"1\"
277                         class=\"input_marceditor\"
278                 \/>
279                 ";
280         }
281     }
282     $subfield_data{'index_subfield'} = $index_subfield;
283     return \%subfield_data;
284 }
285
286 =item CreateKey
287
288     Create a random value to set it into the input name
289
290 =cut
291
292 sub CreateKey(){
293     return int(rand(1000000));
294 }
295
296 sub build_tabs ($$$$$) {
297     my ( $template, $record, $dbh, $encoding,$input ) = @_;
298
299     # fill arrays
300     my @loop_data = ();
301     my $tag;
302
303     my $authorised_values_sth = $dbh->prepare(
304         "select authorised_value,lib
305         from authorised_values
306         where category=? order by lib"
307     );
308     
309     # in this array, we will push all the 10 tabs
310     # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
311     my @BIG_LOOP;
312     my %seen;
313     my @tab_data; # all tags to display
314     
315     foreach my $used ( keys %$tagslib ){
316         push @tab_data,$used if not $seen{$used};
317         $seen{$used}++;
318     }
319         
320     my $max_num_tab=9;
321     # loop through each tab 0 through 9
322     for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
323         my @loop_data = (); #innerloop in the template.
324         my $i = 0;
325         foreach my $tag (sort @tab_data) {
326             $i++;
327             next if ! $tag;
328             my $index_tag = CreateKey;
329
330             # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
331             # if MARC::Record is empty => use tab as master loop.
332             if ( $record != -1 && ( $record->field($tag) || $tag eq '000' ) ) {
333                 my @fields;
334                 if ( $tag ne '000' ) {
335                     @fields = $record->field($tag);
336                 }
337                 else {
338                   push @fields, MARC::Field->new('000', $record->leader()); # if tag == 000
339                 }
340                 # loop through each field
341                 foreach my $field (@fields) {
342                     my @subfields_data;
343                     if ($field->tag()<10) {
344                         next
345                         if (
346                             $tagslib->{ $field->tag() }->{ '@' }->{tab}
347                             ne $tabloop );
348                       next if ($tagslib->{$field->tag()}->{'@'}->{hidden});
349                       my %subfield_data;
350                       $subfield_data{marc_lib}=$tagslib->{$field->tag()}->{'@'}->{lib};
351                       $subfield_data{marc_value}=$field->data();
352                       $subfield_data{marc_subfield}='@';
353                       $subfield_data{marc_tag}=$field->tag();
354                       push(@subfields_data, \%subfield_data);
355                     } else {
356                       my @subf=$field->subfields;
357                   # loop through each subfield
358                       for my $i (0..$#subf) {
359                         $subf[$i][0] = "@" unless $subf[$i][0];
360                         next
361                         if (
362                             $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab}
363                             ne $tabloop );
364                         next
365                         if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }
366                             ->{hidden} );
367                         my %subfield_data;
368                         $subfield_data{marc_lib}=$tagslib->{$field->tag()}->{$subf[$i][0]}->{lib};
369                         if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{isurl}) {
370                           $subfield_data{marc_value}="<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
371                         } else {
372                           $subfield_data{marc_value}=$subf[$i][1];
373                         }
374                               $subfield_data{short_desc} = substr(
375                                   $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib},
376                                   0, 20
377                               );
378                               $subfield_data{long_desc} =
379                                 $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
380                         $subfield_data{marc_subfield}=$subf[$i][0];
381                         $subfield_data{marc_tag}=$field->tag();
382                         push(@subfields_data, \%subfield_data);
383                       }
384                     }
385                     if ($#subfields_data>=0) {
386                       my %tag_data;
387                       $tag_data{tag}=$field->tag(). ' '  
388                                      . C4::Koha::display_marc_indicators($field) 
389                                      . ' - '
390                                      . $tagslib->{$field->tag()}->{lib};
391                       $tag_data{subfield} = \@subfields_data;
392                       push (@loop_data, \%tag_data);
393                     }
394                   }
395               }
396             }
397             if ( $#loop_data >= 0 ) {
398                 push @BIG_LOOP, {
399                     number    => $tabloop,
400                     innerloop => \@loop_data,
401                 };
402             }
403         }
404         $template->param( singletab => (scalar(@BIG_LOOP)==1), BIG_LOOP => \@BIG_LOOP );
405 }
406
407
408
409 my $query=new CGI;
410
411 my $dbh=C4::Context->dbh;
412
413 # open template
414 my ($template, $loggedinuser, $cookie)
415                 = get_template_and_user({template_name => "authorities/detail.tmpl",
416                              query => $query,
417                              type => "intranet",
418                              authnotrequired => 0,
419                              flagsrequired => {catalogue => 1},
420                              debug => 1,
421                              });
422
423 my $authid = $query->param('authid');
424
425
426
427 my $authtypecode = &GetAuthTypeCode($authid);
428 $tagslib = &GetTagsLabels(1,$authtypecode);
429
430 my $record;
431 if (C4::Context->preference("AuthDisplayHierarchy")){
432   my $trees=BuildUnimarcHierarchies($authid);
433   my @trees = split /;/,$trees ;
434   push @trees,$trees unless (@trees);
435   my @loophierarchies;
436   foreach my $tree (@trees){
437     my @tree=split /,/,$tree;
438     push @tree,$tree unless (@tree);
439     my $cnt=0;
440     my @loophierarchy;
441     foreach my $element (@tree){
442       my $elementdata = GetAuthority($element);
443       $record= $elementdata if ($authid==$element);
444       push @loophierarchy, BuildUnimarcHierarchy($elementdata,"child".$cnt, $authid);
445       $cnt++;
446     }
447     push @loophierarchies, { 'loopelement' =>\@loophierarchy};
448   }
449   $template->param(
450     'displayhierarchy' =>C4::Context->preference("AuthDisplayHierarchy"),
451     'loophierarchies' =>\@loophierarchies,
452   );
453 } else {
454   $record=GetAuthority($authid);
455 }
456 my $count = CountUsage($authid);
457
458 # find the marc field/subfield used in biblio by this authority
459 my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
460 $sth->execute($authtypecode);
461 my $biblio_fields;
462 while (my ($tagfield) = $sth->fetchrow) {
463         $biblio_fields.= $tagfield."9,";
464 }
465 chop $biblio_fields;
466
467
468 # fill arrays
469 my @loop_data =();
470 my $tag;
471 # loop through each tab 0 through 9
472 # for (my $tabloop = 0; $tabloop<=10;$tabloop++) {
473 # loop through each tag
474   build_tabs ($template, $record, $dbh,"",$query);
475
476 my $authtypes = getauthtypes;
477 my @authtypesloop;
478 foreach my $thisauthtype (sort { $authtypes->{$b} cmp $authtypes->{$a} } keys %$authtypes) {
479         my %row =(value => $thisauthtype,
480                                 selected => $thisauthtype eq $authtypecode,
481                                 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
482                         );
483         push @authtypesloop, \%row;
484 }
485
486 $template->param(authid => $authid,
487                 count => $count,
488                 biblio_fields => $biblio_fields,
489                 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
490                 authtypesloop => \@authtypesloop,
491                 );
492 output_html_with_http_headers $query, $cookie, $template->output;
493