MT 3044 : Fix CSV Export when there are blank chars in tag names
[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
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
55 =item build_authorized_values_list
56
57 =cut
58
59 sub build_authorized_values_list ($$$$$$$) {
60     my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
61
62     my @authorised_values;
63     my %authorised_lib;
64
65     # builds list, depending on authorised value...
66
67     #---- branch
68     if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
69         my $sth =
70           $dbh->prepare(
71             "select branchcode,branchname from branches order by branchname");
72         $sth->execute;
73         push @authorised_values, ""
74           unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
75
76         while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
77             push @authorised_values, $branchcode;
78             $authorised_lib{$branchcode} = $branchname;
79         }
80
81         #----- itemtypes
82     }
83     elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
84         my $sth =
85           $dbh->prepare(
86             "select itemtype,description from itemtypes order by description");
87         $sth->execute;
88         push @authorised_values, ""
89           unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
90           
91         my $itemtype;
92         
93         while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
94             push @authorised_values, $itemtype;
95             $authorised_lib{$itemtype} = $description;
96         }
97         $value = $itemtype unless ($value);
98
99         #---- "true" authorised value
100     }
101     else {
102         $authorised_values_sth->execute(
103             $tagslib->{$tag}->{$subfield}->{authorised_value} );
104
105         push @authorised_values, ""
106           unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
107
108         while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
109             push @authorised_values, $value;
110             $authorised_lib{$value} = $lib;
111         }
112     }
113     return CGI::scrolling_list(
114         -name     => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
115         -values   => \@authorised_values,
116         -default  => $value,
117         -labels   => \%authorised_lib,
118         -override => 1,
119         -size     => 1,
120         -multiple => 0,
121         -tabindex => 1,
122         -id       => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
123         -class    => "input_marceditor",
124     );
125 }
126
127
128 =item create_input
129  builds the <input ...> entry for a subfield.
130 =cut
131
132 sub create_input {
133     my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
134     
135     my $index_subfield = CreateKey(); # create a specifique key for each subfield
136
137     $value =~ s/"/&quot;/g;
138
139     # if there is no value provided but a default value in parameters, get it
140     unless ($value) {
141         $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
142
143         # get today date & replace YYYY, MM, DD if provided in the default value
144         my ( $year, $month, $day ) = Today();
145         $month = sprintf( "%02d", $month );
146         $day   = sprintf( "%02d", $day );
147         $value =~ s/YYYY/$year/g;
148         $value =~ s/MM/$month/g;
149         $value =~ s/DD/$day/g;
150     }
151     my $dbh = C4::Context->dbh;
152     my %subfield_data = (
153         tag        => $tag,
154         subfield   => $subfield,
155         marc_lib   => substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 22 ),
156         marc_lib_plain => $tagslib->{$tag}->{$subfield}->{lib}, 
157         tag_mandatory  => $tagslib->{$tag}->{mandatory},
158         mandatory      => $tagslib->{$tag}->{$subfield}->{mandatory},
159         repeatable     => $tagslib->{$tag}->{$subfield}->{repeatable},
160         kohafield      => $tagslib->{$tag}->{$subfield}->{kohafield},
161         index          => $index_tag,
162         id             => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
163         value          => $value,
164     );
165     if($subfield eq '@'){
166         $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_tag."_".$index_subfield;
167     } else {
168          $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield;
169     }
170
171     
172     $subfield_data{visibility} = "display:none;"
173         if (    ($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne ''
174             or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
175         );
176     
177     # it's an authorised field
178     if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
179         $subfield_data{marc_value} =
180           build_authorized_values_list( $tag, $subfield, $value, $dbh,
181             $authorised_values_sth,$index_tag,$index_subfield );
182
183     # it's a thesaurus / authority field
184     }
185     elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
186         $subfield_data{marc_value} =
187     "<input type=\"text\"
188                         id=\"".$subfield_data{id}."\"
189                         name=\"".$subfield_data{id}."\"
190       value=\"$value\"
191       class=\"input_marceditor\"
192                         tabindex=\"1\"                     
193       DISABLE READONLY \/>
194       <span class=\"buttonDot\"
195         onclick=\"Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=".$tagslib->{$tag}->{$subfield}->{authtypecode}."&index=$subfield_data{id}','$subfield_data{id}')\">...</span>
196     ";
197     # it's a plugin field
198     }
199     elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
200
201         # opening plugin. Just check wether we are on a developper computer on a production one
202         # (the cgidir differs)
203         my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
204         unless (-r $cgidir and -d $cgidir) {
205             $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
206         }
207         my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
208         do $plugin || die "Plugin Failed: ".$plugin;
209         my $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
210         my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
211 #         my ( $function_name, $javascript,$extended_param );
212         
213         $subfield_data{marc_value} =
214     "<input tabindex=\"1\"
215                         type=\"text\"
216                         id=".$subfield_data{id}."
217       name=".$subfield_data{id}."
218       value=\"$value\"
219                         class=\"input_marceditor\"
220       onfocus=\"javascript:Focus$function_name($index_tag)\"
221       onblur=\"javascript:Blur$function_name($index_tag); \" \/>
222     <span class=\"buttonDot\"
223       onclick=\"Clic$function_name('$subfield_data{id}')\">...</a>
224     $javascript";
225         # it's an hidden field
226     }
227     elsif ( $tag eq '' ) {
228         $subfield_data{marc_value} =
229             "<input tabindex=\"1\"
230                     type=\"hidden\"
231                     id=".$subfield_data{id}."
232                     name=".$subfield_data{id}."
233                     value=\"$value\" \/>
234             ";
235     }
236     elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
237         $subfield_data{marc_value} =
238             "<input type=\"text\"
239                     id=".$subfield_data{id}."
240                     name=".$subfield_data{id}."
241                     class=\"input_marceditor\"
242                     tabindex=\"1\"
243                     value=\"$value\"
244             \/>";
245
246         # it's a standard field
247     }
248     else {
249         if (
250             length($value) > 100
251             or
252             ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
253                 and $tag < 400 && $subfield eq 'a' )
254             or (    $tag >= 500
255                 and $tag < 600
256                 && C4::Context->preference("marcflavour") eq "MARC21" )
257           )
258         {
259             $subfield_data{marc_value} =
260                 "<textarea cols=\"70\"
261                            rows=\"4\"
262                            id=".$subfield_data{id}."
263                            name=".$subfield_data{id}."
264                            class=\"input_marceditor\"
265                            tabindex=\"1\"
266                            >$value</textarea>
267                 ";
268         }
269         else {
270             $subfield_data{marc_value} =
271                 "<input type=\"text\"
272                         id=".$subfield_data{id}."
273                         name=".$subfield_data{id}."
274                         value=\"$value\"
275                         tabindex=\"1\"
276                         class=\"input_marceditor\"
277                 \/>
278                 ";
279         }
280     }
281     $subfield_data{'index_subfield'} = $index_subfield;
282     return \%subfield_data;
283 }
284
285 =item CreateKey
286
287     Create a random value to set it into the input name
288
289 =cut
290
291 sub CreateKey(){
292     return int(rand(1000000));
293 }
294
295 sub build_tabs ($$$$$) {
296     my ( $template, $record, $dbh, $encoding,$input ) = @_;
297
298     # fill arrays
299     my @loop_data = ();
300     my $tag;
301
302     my $authorised_values_sth = $dbh->prepare(
303         "select authorised_value,lib
304         from authorised_values
305         where category=? order by lib"
306     );
307     
308     # in this array, we will push all the 10 tabs
309     # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
310     my @BIG_LOOP;
311     my %seen;
312     my @tab_data; # all tags to display
313     
314     foreach my $used ( keys %$tagslib ){
315         push @tab_data,$used if not $seen{$used};
316         $seen{$used}++;
317     }
318         
319     my $max_num_tab=9;
320     # loop through each tab 0 through 9
321     for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
322         my @loop_data = (); #innerloop in the template.
323         my $i = 0;
324         foreach my $tag (sort @tab_data) {
325             $i++;
326             next if ! $tag;
327             my $indicator;
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 ne -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 $cell;
443       my $elementdata = GetAuthority($element);
444       $record= $elementdata if ($authid==$element);
445       push @loophierarchy, BuildUnimarcHierarchy($elementdata,"child".$cnt, $authid);
446       $cnt++;
447     }
448     push @loophierarchies, { 'loopelement' =>\@loophierarchy};
449   }
450   $template->param(
451     'displayhierarchy' =>C4::Context->preference("AuthDisplayHierarchy"),
452     'loophierarchies' =>\@loophierarchies,
453   );
454 } else {
455   $record=GetAuthority($authid);
456 }
457 my $count = CountUsage($authid);
458
459 # find the marc field/subfield used in biblio by this authority
460 my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
461 $sth->execute($authtypecode);
462 my $biblio_fields;
463 while (my ($tagfield) = $sth->fetchrow) {
464         $biblio_fields.= $tagfield."9,";
465 }
466 chop $biblio_fields;
467
468
469 # fill arrays
470 my @loop_data =();
471 my $tag;
472 # loop through each tab 0 through 9
473 # for (my $tabloop = 0; $tabloop<=10;$tabloop++) {
474 # loop through each tag
475   build_tabs ($template, $record, $dbh,"",$query);
476
477 my $authtypes = getauthtypes;
478 my @authtypesloop;
479 foreach my $thisauthtype (sort { $authtypes->{$b} cmp $authtypes->{$a} } keys %$authtypes) {
480         my $selected = 1 if $thisauthtype eq $authtypecode;
481         my %row =(value => $thisauthtype,
482                                 selected => $selected,
483                                 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
484                         );
485         push @authtypesloop, \%row;
486 }
487
488 $template->param(authid => $authid,
489                 count => $count,
490                 biblio_fields => $biblio_fields,
491                 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
492                 authtypesloop => \@authtypesloop,
493                 );
494 output_html_with_http_headers $query, $cookie, $template->output;
495