code cleanup - remove unused find_values() copy and paste
[koha.git] / authorities / authorities.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use CGI;
23 use C4::Auth;
24 use C4::Output;
25 use C4::AuthoritiesMarc;
26 use C4::Context;
27 use C4::Koha; # XXX subfield_is_koha_internal_p
28 use Date::Calc qw(Today);
29 use MARC::File::USMARC;
30 use MARC::File::XML;
31 use C4::Biblio;
32 use vars qw( $tagslib);
33 use vars qw( $authorised_values_sth);
34 use vars qw( $is_a_modif );
35
36 my $itemtype; # created here because it can be used in build_authorized_values_list sub
37 our($authorised_values_sth,$is_a_modif,$usedTagsLib,$mandatory_z3950);
38
39 =item build_authorized_values_list
40
41 =cut
42
43 sub build_authorized_values_list ($$$$$$$) {
44     my ( $tag, $subfield, $value, $dbh, $authorised_values_sth,$index_tag,$index_subfield ) = @_;
45
46     my @authorised_values;
47     my %authorised_lib;
48
49     # builds list, depending on authorised value...
50
51     #---- branch
52     if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
53         my $sth =
54         $dbh->prepare(
55             "select branchcode,branchname from branches order by branchname");
56         $sth->execute;
57         push @authorised_values, ""
58         unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
59
60         while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
61             push @authorised_values, $branchcode;
62             $authorised_lib{$branchcode} = $branchname;
63         }
64
65         #----- itemtypes
66     }
67     elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
68         my $sth =
69         $dbh->prepare(
70             "select itemtype,description from itemtypes order by description");
71         $sth->execute;
72         push @authorised_values, ""
73         unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
74         
75         my $itemtype;
76         
77         while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
78             push @authorised_values, $itemtype;
79             $authorised_lib{$itemtype} = $description;
80         }
81         $value = $itemtype unless ($value);
82
83         #---- "true" authorised value
84     }
85     else {
86         $authorised_values_sth->execute(
87             $tagslib->{$tag}->{$subfield}->{authorised_value} );
88
89         push @authorised_values, ""
90         unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
91
92         while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
93             push @authorised_values, $value;
94             $authorised_lib{$value} = $lib;
95         }
96     }
97     return CGI::scrolling_list(
98         -name     => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
99         -values   => \@authorised_values,
100         -default  => $value,
101         -labels   => \%authorised_lib,
102         -override => 1,
103         -size     => 1,
104         -multiple => 0,
105         -tabindex => 1,
106         -id       => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
107         -class    => "input_marceditor",
108     );
109 }
110
111
112 =item create_input
113 builds the <input ...> entry for a subfield.
114 =cut
115
116 sub create_input {
117     my ( $tag, $subfield, $value, $index_tag, $tabloop, $rec, $authorised_values_sth,$cgi ) = @_;
118     
119     my $index_subfield = CreateKey(); # create a specifique key for each subfield
120
121     $value =~ s/"/&quot;/g;
122
123     # if there is no value provided but a default value in parameters, get it
124     unless ($value) {
125         $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
126
127         # get today date & replace YYYY, MM, DD if provided in the default value
128         my ( $year, $month, $day ) = Today();
129         $month = sprintf( "%02d", $month );
130         $day   = sprintf( "%02d", $day );
131         $value =~ s/YYYY/$year/g;
132         $value =~ s/MM/$month/g;
133         $value =~ s/DD/$day/g;
134     }
135     my $dbh = C4::Context->dbh;
136     my %subfield_data = (
137         tag        => $tag,
138         subfield   => $subfield,
139         marc_lib   => substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 22 ),
140         marc_lib_plain => $tagslib->{$tag}->{$subfield}->{lib}, 
141         tag_mandatory  => $tagslib->{$tag}->{mandatory},
142         mandatory      => $tagslib->{$tag}->{$subfield}->{mandatory},
143         repeatable     => $tagslib->{$tag}->{$subfield}->{repeatable},
144         kohafield      => $tagslib->{$tag}->{$subfield}->{kohafield},
145         index          => $index_tag,
146         id             => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
147         value          => $value,
148     );
149     if($subfield eq '@'){
150         $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_tag."_".$index_subfield;
151     } else {
152         $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield;
153     }
154
155     if(exists $mandatory_z3950->{$tag.$subfield}){
156         $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
157     }
158     
159     $subfield_data{visibility} = "display:none;"
160         if (    ($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne ''
161             or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
162         );
163     
164     # it's an authorised field
165     if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
166         $subfield_data{marc_value} =
167         build_authorized_values_list( $tag, $subfield, $value, $dbh,
168             $authorised_values_sth,$index_tag,$index_subfield );
169
170     # it's a thesaurus / authority field
171     }
172     elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
173         $subfield_data{marc_value} =
174     "<input type=\"text\"
175                         id=\"".$subfield_data{id}."\"
176                         name=\"".$subfield_data{id}."\"
177     value=\"$value\"
178     class=\"input_marceditor\"
179     tabindex=\"1\"                     
180     disabled=\"disabled\"
181         readonly=\"readonly\" \/>
182     <span class=\"buttonDot\"
183         onclick=\"Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=".$tagslib->{$tag}->{$subfield}->{authtypecode}."&index=$subfield_data{id}','$subfield_data{id}')\">...</span>
184     ";
185     # it's a plugin field
186     }
187     elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
188
189         # opening plugin. Just check wether we are on a developper computer on a production one
190         # (the cgidir differs)
191         my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
192         unless (-r $cgidir and -d $cgidir) {
193             $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
194         }
195         my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
196         do $plugin || die "Plugin Failed: ".$plugin;
197         my $extended_param = plugin_parameters( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
198         my ( $function_name, $javascript ) = plugin_javascript( $dbh, $rec, $tagslib, $subfield_data{id}, $tabloop );
199 #         my ( $function_name, $javascript,$extended_param );
200         
201         $subfield_data{marc_value} =
202             "<input tabindex=\"1\"
203                     type=\"text\"
204                     id=\"".$subfield_data{id}."\"
205                     size=\"67\"
206                     maxlength=\"255\" 
207                     name=\"".$subfield_data{id}."\"
208                     value=\"$value\"
209                     class=\"input_marceditor\"
210                     onfocus=\"Focus$function_name($index_tag)\"
211                     onblur=\"Blur$function_name($index_tag); \" \/>
212                     <a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
213                     $javascript";
214         # it's an hidden field
215     }
216     elsif ( $tag eq '' ) {
217         $subfield_data{marc_value} =
218             "<input tabindex=\"1\"
219                     type=\"hidden\"
220                     id=\"".$subfield_data{id}."\"
221                     name=\"".$subfield_data{id}."\"
222                     size=\"67\"
223                     maxlength=\"255\" 
224                     value=\"$value\" \/>
225             ";
226     }
227     elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
228         $subfield_data{marc_value} =
229             "<input type=\"text\"
230                     id=\"".$subfield_data{id}."\"
231                     name=\"".$subfield_data{id}."\"
232                     class=\"input_marceditor\"
233                     tabindex=\"1\"
234                     size=\"67\"
235                     maxlength=\"255\" 
236                     value=\"$value\"
237             \/>";
238
239         # it's a standard field
240     }
241     else {
242         if (
243             length($value) > 100
244             or
245             ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
246                 and $tag < 400 && $subfield eq 'a' )
247             or (    $tag >= 500
248                 and $tag < 600
249                 && C4::Context->preference("marcflavour") eq "MARC21" )
250         )
251         {
252             $subfield_data{marc_value} =
253                 "<textarea cols=\"70\"
254                         rows=\"4\"
255                         id=\"".$subfield_data{id}."\"
256                         name=\"".$subfield_data{id}."\"
257                         class=\"input_marceditor\"
258                         tabindex=\"1\"
259                         size=\"67\"
260                         maxlength=\"255\" 
261                         >$value</textarea>
262                 ";
263         }
264         else {
265             $subfield_data{marc_value} =
266                 "<input type=\"text\"
267                         id=\"".$subfield_data{id}."\"
268                         name=\"".$subfield_data{id}."\"
269                         value=\"$value\"
270                         tabindex=\"1\"
271                         size=\"67\"
272                         maxlength=\"255\" 
273                         class=\"input_marceditor\"
274                 \/>
275                 ";
276         }
277     }
278     $subfield_data{'index_subfield'} = $index_subfield;
279     return \%subfield_data;
280 }
281
282 =item CreateKey
283
284     Create a random value to set it into the input name
285
286 =cut
287
288 sub CreateKey(){
289     return int(rand(1000000));
290 }
291
292 sub build_tabs ($$$$$) {
293     my ( $template, $record, $dbh, $encoding,$input ) = @_;
294
295     # fill arrays
296     my @loop_data = ();
297     my $tag;
298
299     my $authorised_values_sth = $dbh->prepare(
300         "SELECT authorised_value,lib
301         FROM authorised_values
302         WHERE category=? ORDER BY lib"
303     );
304     
305     # in this array, we will push all the 10 tabs
306     # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
307     my @BIG_LOOP;
308     my %seen;
309     my @tab_data; # all tags to display
310     
311     foreach my $used ( keys %$tagslib ){
312         push @tab_data,$used if not $seen{$used};
313         $seen{$used}++;
314     }
315         
316     my $max_num_tab=9;
317     # loop through each tab 0 through 9
318     for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
319         my @loop_data = (); #innerloop in the template.
320         my $i = 0;
321         foreach my $tag (sort @tab_data) {
322             $i++;
323             next if ! $tag;
324             my $indicator;
325             my $index_tag = CreateKey;
326
327             # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
328             # if MARC::Record is empty => use tab as master loop.
329             if ( $record ne -1 && ( $record->field($tag) || $tag eq '000' ) ) {
330                 my @fields;
331                 if ( $tag ne '000' ) {
332                                 @fields = $record->field($tag);
333                 }
334                 else {
335                 push @fields, $record->leader(); # if tag == 000
336                 }
337                 # loop through each field
338                 foreach my $field (@fields) {
339                     
340                     my @subfields_data;
341                     if ( $tag < 10 ) {
342                         my ( $value, $subfield );
343                         if ( $tag ne '000' ) {
344                             $value    = $field->data();
345                             $subfield = "@";
346                         }
347                         else {
348                             $value    = $field;
349                             $subfield = '@';
350                         }
351                         next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
352                         push(
353                             @subfields_data,
354                             &create_input(
355                                 $tag, $subfield, $value, $index_tag, $tabloop, $record,
356                                 $authorised_values_sth,$input
357                             )
358                         );
359                     }
360                     else {
361                         my @subfields = $field->subfields();
362                         foreach my $subfieldcount ( 0 .. $#subfields ) {
363                             my $subfield = $subfields[$subfieldcount][0];
364                             my $value    = $subfields[$subfieldcount][1];
365                             next if ( length $subfield != 1 );
366                             next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
367                             push(
368                                 @subfields_data,
369                                 &create_input(
370                                     $tag, $subfield, $value, $index_tag, $tabloop,
371                                     $record, $authorised_values_sth,$input
372                                 )
373                             );
374                         }
375                     }
376
377                     # now, loop again to add parameter subfield that are not in the MARC::Record
378                     foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
379                     {
380                         next if ( length $subfield != 1 );
381                         next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
382                         next if ( $tag < 10 );
383                         next
384                         if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -4 )
385                             or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 5 )
386                         );    #check for visibility flag
387                         next if ( defined( $field->subfield($subfield) ) );
388                         push(
389                             @subfields_data,
390                             &create_input(
391                                 $tag, $subfield, '', $index_tag, $tabloop, $record,
392                                 $authorised_values_sth,$input
393                             )
394                         );
395                     }
396                     if ( $#subfields_data >= 0 ) {
397                         # build the tag entry.
398                         # note that the random() field is mandatory. Otherwise, on repeated fields, you'll 
399                         # have twice the same "name" value, and cgi->param() will return only one, making
400                         # all subfields to be merged in a single field.
401                         my %tag_data = (
402                             tag           => $tag,
403                             index         => $index_tag,
404                             tag_lib       => $tagslib->{$tag}->{lib},
405                             repeatable       => $tagslib->{$tag}->{repeatable},
406                             subfield_loop => \@subfields_data,
407                             fixedfield    => ($tag < 10)?(1):(0),
408                             random        => CreateKey,
409                         );
410                         if ($tag >= 010){ # no indicator for theses tag
411                         $tag_data{indicator} = $field->indicator(1).$field->indicator(2);
412                         }
413                         push( @loop_data, \%tag_data );
414                     }
415                 } # foreach $field end
416
417             # if breeding is empty
418             }
419             else {
420                 my @subfields_data;
421                 foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) ) {
422                     next if ( length $subfield != 1 );
423                     next if ( ( $tagslib->{$tag}->{$subfield}->{hidden} <= -5 )
424                                 or ( $tagslib->{$tag}->{$subfield}->{hidden} >= 4 ) )
425                             ;    #check for visibility flag
426                     next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
427                     push(
428                         @subfields_data,
429                         &create_input(
430                             $tag, $subfield, '', $index_tag, $tabloop, $record,
431                             $authorised_values_sth,$input
432                         )
433                     );
434                 }
435                 if ( $#subfields_data >= 0 ) {
436                     my %tag_data = (
437                         tag              => $tag,
438                         index            => $index_tag,
439                         tag_lib          => $tagslib->{$tag}->{lib},
440                         repeatable       => $tagslib->{$tag}->{repeatable},
441                         indicator        => $indicator,
442                         subfield_loop    => \@subfields_data,
443                         tagfirstsubfield => $subfields_data[0],
444                         fixedfield       => ($tag < 10)?(1):(0)
445                     );
446                     
447                     push @loop_data, \%tag_data ;
448                 }
449             }
450         }
451         if ( $#loop_data >= 0 ) {
452             push @BIG_LOOP, {
453                 number    => $tabloop,
454                 innerloop => \@loop_data,
455             };
456         }
457     }
458     $template->param( BIG_LOOP => \@BIG_LOOP );
459 }
460
461
462 sub build_hidden_data () {
463     # build hidden data =>
464     # we store everything, even if we show only requested subfields.
465
466     my @loop_data =();
467     my $i=0;
468     foreach my $tag (keys %{$tagslib}) {
469         my $previous_tag = '';
470
471         # loop through each subfield
472         foreach my $subfield (keys %{$tagslib->{$tag}}) {
473             next if ($subfield eq 'lib');
474             next if ($subfield eq 'tab');
475             next if ($subfield eq 'mandatory');
476                 next if ($subfield eq 'repeatable');
477             next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "-1");
478             my %subfield_data;
479             $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
480             $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
481             $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
482             $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
483             push(@loop_data, \%subfield_data);
484             $i++
485         }
486     }
487 }
488
489 # ======================== 
490 #          MAIN 
491 #=========================
492 my $input = new CGI;
493 my $z3950 = $input->param('z3950');
494 my $error = $input->param('error');
495 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
496 my $op = $input->param('op');
497 my $nonav = $input->param('nonav');
498 my $myindex = $input->param('index');
499 my $linkid=$input->param('linkid');
500 my $authtypecode = $input->param('authtypecode');
501
502 my $dbh = C4::Context->dbh;
503 $authtypecode = &GetAuthTypeCode($authid) if !$authtypecode;
504
505 my ($template, $loggedinuser, $cookie)
506     = get_template_and_user({template_name => "authorities/authorities.tmpl",
507                             query => $input,
508                             type => "intranet",
509                             authnotrequired => 0,
510                             flagsrequired => {editauthorities => 1},
511                             debug => 1,
512                             });
513 $template->param(nonav   => $nonav,index=>$myindex,authtypecode=>$authtypecode,);
514 $tagslib = GetTagsLabels(1,$authtypecode);
515 my $record=-1;
516 my $encoding="";
517 $record = GetAuthority($authid) if ($authid);
518 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
519 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
520 $is_a_modif=0;
521 if ($authid) {
522     $is_a_modif=1;
523     ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
524     ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
525 }
526
527 #------------------------------------------------------------------------------------------------------------------------------
528 if ($op eq "add") {
529 #------------------------------------------------------------------------------------------------------------------------------
530     # rebuild
531     my @tags = $input->param('tag');
532     my @subfields = $input->param('subfield');
533     my @values = $input->param('field_value');
534     # build indicator hash.
535     my @ind_tag = $input->param('ind_tag');
536     my @indicator = $input->param('indicator');
537     my @params = $input->param();
538     my $record = TransformHtmlToMarc(\@params,$input);
539     if  (C4::Context->preference("marcflavour") eq "UNIMARC"){
540         unless ($record->field('100')){
541         use POSIX qw(strftime);
542         my $string = strftime( "%Y%m%d", localtime(time) );
543         # set 50 to position 26 is biblios, 13 if authorities
544         my $pos=13;
545         $string = sprintf( "%-*s", 35, $string );
546         substr( $string, $pos , 2, "50" );
547         $record->append_fields(MARC::Field->new('100','','',"a"=>$string));
548         }    
549     }
550
551     my ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
552     my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
553     # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
554     if (!$duplicateauthid or $confirm_not_duplicate) {
555         if ($is_a_modif ) {     
556             ModAuthority($authid,$record,$authtypecode,1);
557         } else {
558             ($authid) = AddAuthority($record,$authid,$authtypecode);
559         }
560         print $input->redirect("detail.pl?authid=$authid");
561         exit;
562     } else {
563     # it may be a duplicate, warn the user and do nothing
564         build_tabs($template, $record, $dbh, $encoding,$input);
565         build_hidden_data;
566         $template->param(authid =>$authid,
567                         duplicateauthid     => $duplicateauthid,
568                         duplicateauthvalue  => $duplicateauthvalue,
569                         );
570     }
571 } elsif ($op eq "delete") {
572 #------------------------------------------------------------------------------------------------------------------------------
573         &AUTHdelauthority($authid);
574         if ($nonav){
575             print $input->redirect("auth_finder.pl");
576         }else{
577             print $input->redirect("authorities-home.pl?authid=0");
578         }
579                 exit;
580 } else {
581 if ($op eq "duplicate")
582         {
583                 $authid = "";
584         }
585         build_tabs ($template, $record, $dbh,$encoding,$input);
586         build_hidden_data;
587         $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
588                         oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
589                         authid                      => $authid , authtypecode=>$authtypecode,   );
590 }
591
592 $template->param(authid                       => $authid,
593                  authtypecode => $authtypecode,
594                  linkid=>$linkid,
595 );
596
597 my $authtypes = getauthtypes;
598 my @authtypesloop;
599 foreach my $thisauthtype (keys %$authtypes) {
600     my $selected = 1 if $thisauthtype eq $authtypecode;
601     my %row =(value => $thisauthtype,
602                 selected => $selected,
603                 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
604             );
605     push @authtypesloop, \%row;
606 }
607
608 $template->param(authtypesloop => \@authtypesloop,
609                 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
610                 hide_marc => C4::Context->preference('hide_marc'),
611                 );
612 output_html_with_http_headers $input, $cookie, $template->output;