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