Changing GetSerials2 to take more than one status in parameter.
[koha.git] / authorities / authorities.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
23 use CGI;
24 use C4::Auth;
25 use C4::Output;
26 use C4::AuthoritiesMarc;
27 use C4::Context;
28 use C4::Koha; # XXX subfield_is_koha_internal_p
29
30 use MARC::File::USMARC;
31 use MARC::File::XML;
32 use C4::Biblio;
33 use vars qw( $tagslib);
34 use vars qw( $authorised_values_sth);
35 use vars qw( $is_a_modif );
36
37 my $itemtype; # created here because it can be used in build_authorized_values_list sub
38
39 =item find_value
40
41     ($indicators, $value) = find_value($tag, $subfield, $record,$encoding);
42
43 Find the given $subfield in the given $tag in the given
44 MARC::Record $record.  If the subfield is found, returns
45 the (indicators, value) pair; otherwise, (undef, undef) is
46 returned.
47
48 =cut
49
50 sub find_value {
51         my ($tagfield,$insubfield,$record,$encoding) = @_;
52         my @result;
53         my $indicator;
54         if ($tagfield <10) {
55                 if ($record->field($tagfield)) {
56                         push @result, $record->field($tagfield)->data();
57                 } else {
58                         push @result,"";
59                 }
60         } else {
61                 foreach my $field ($record->field($tagfield)) {
62                         my @subfields = $field->subfields();
63                         foreach my $subfield (@subfields) {
64                                 if (@$subfield[0] eq $insubfield) {
65                                 push @result,@$subfield[1];
66                                                         $indicator = $field->indicator(1).$field->indicator(2);
67                                 }
68                         }
69                 }
70         }
71         return($indicator,@result);
72 }
73
74
75 =item build_authorized_values_list
76
77 =cut
78
79 sub build_authorized_values_list ($$$$$) {
80         my($tag, $subfield, $value, $dbh,$authorised_values_sth) = @_;
81
82         my @authorised_values;
83         my %authorised_lib;
84
85         # builds list, depending on authorised value...
86
87         #---- branch
88         if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
89         my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
90         $sth->execute;
91         push @authorised_values, ""
92                 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
93
94         while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
95                 push @authorised_values, $branchcode;
96                 $authorised_lib{$branchcode}=$branchname;
97         }
98
99         #----- itemtypes
100         } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
101                 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
102                 $sth->execute;
103                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
104         
105                 while (my ($itemtype,$description) = $sth->fetchrow_array) {
106                         push @authorised_values, $itemtype;
107                         $authorised_lib{$itemtype}=$description;
108                 }
109                 $value=$itemtype unless ($value);
110
111         #---- "true" authorised value
112         } else {
113                 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
114
115                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
116         
117                 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
118                         push @authorised_values, $value;
119                         $authorised_lib{$value}=$lib;
120                 }
121     }
122     return CGI::scrolling_list( -name     => 'field_value',
123                                 -values   => \@authorised_values,
124                                 -default  => $value,
125                                 -labels   => \%authorised_lib,
126                                 -override => 1,
127                                 -size     => 1,
128                                 -tabindex=>'',
129                                 -multiple => 0 );
130 }
131
132
133 =item create_input
134  builds the <input ...> entry for a subfield.
135 =cut
136 sub create_input () {
137         my ($tag,$subfield,$value,$i,$tabloop,$rec,$authorised_values_sth) = @_;
138         # must be encoded as utf-8 before it reaches the editor
139        my $dbh=C4::Context->dbh;
140         $value =~ s/"/&quot;/g;
141         my %subfield_data;
142         $subfield_data{tag}=$tag;
143         $subfield_data{subfield}=$subfield;
144         $subfield_data{marc_lib}="<span id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
145         $subfield_data{marc_lib_plain}=$tagslib->{$tag}->{$subfield}->{lib};
146         $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
147         $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
148         $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
149         $subfield_data{kohafield}=$tagslib->{$tag}->{$subfield}->{kohafield};
150         $subfield_data{index} = $i;
151         $subfield_data{visibility} = "display:none" if (substr($tagslib->{$tag}->{$subfield}->{hidden},2,1) gt "0") ; #check parity
152         # it's an authorised field
153         if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
154                 $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh,$authorised_values_sth);
155         # it's a thesaurus / authority field
156         } elsif ($tagslib->{$tag}->{$subfield}->{frameworkcode}) {
157                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=\"67\" maxlength=\"255\" DISABLE READONLY> <a href=\"javascript:Dopop('../authorities/auth_finder.pl?authtypecode=".$tagslib->{$tag}->{$subfield}->{frameworkcode}."&index=$i',$i)\">...</a>";
158         } elsif ($tagslib->{$tag}->{$subfield}->{link}) {
159                 $subfield_data{marc_value}="<input onblur=\"this.style.backgroundColor='#ffffff';\" onfocus=\"this.style.backgroundColor='#ffffff;'\" tabindex=\"1\" type=\"text\" name=\"field_value\" value=\"$value\" size=\"40\" maxlength=\"255\" DISABLE READONLY> <a  style=\"cursor: help;\" href=\"javascript:Dopop('../authorities/auth_linker.pl?index=$i',$i)\">...</a>";
160         
161                 # it's a plugin field
162         } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
163                 # opening plugin. Just check wether we are on a developper computer on a production one
164                 # (the cgidir differs)
165                 my $cgidir = C4::Context->intranetdir ."/cgi-bin/cataloguing/value_builder";
166                 unless (opendir(DIR, "$cgidir")) {
167                         $cgidir = C4::Context->intranetdir."/cataloguing/value_builder";
168                 } 
169                 my $plugin=$cgidir."/".$tagslib->{$tag}->{$subfield}->{'value_builder'}; 
170                 require $plugin;
171                 my $extended_param = plugin_parameters($dbh,$rec,$tagslib,$i,$tabloop);
172                 my ($function_name,$javascript) = plugin_javascript($dbh,$rec,$tagslib,$i,$tabloop);
173                 $subfield_data{marc_value}="<input tabindex=\"1\" type=\"text\" name=\"field_value\"  value=\"$value\" size=\"40\" maxlength=\"255\" OnFocus=\"javascript:Focus$function_name($i)\" OnBlur=\"javascript:Blur$function_name($i); \"> <a  style=\"cursor: help;\" href=\"javascript:Clic$function_name($i)\">...</a> $javascript";
174         # it's an hidden field
175         } elsif  ($tag eq '') {
176                 $subfield_data{marc_value}="<input onblur=\"this.style.backgroundColor='#ffffff';\" onfocus=\"this.style.backgroundColor='#ffffff'; \" tabindex=\"1\" type=\"hidden\" name=\"field_value\" value=\"$value\">";
177         } elsif  (substr($tagslib->{$tag}->{$subfield}->{'hidden'},2,1) gt "1") {
178
179                 $subfield_data{marc_value}="<input onblur=\"this.style.backgroundColor='#ffffff';\" onfocus=\"this.style.backgroundColor='#ffffff'; \" tabindex=\"1\" type=\"text\" name=\"field_value\" value=\"$value\" size=\"40\" maxlength=\"255\" >";
180         # it's a standard field
181         } else {
182                 if (length($value) >100) {
183                         $subfield_data{marc_value}="<textarea tabindex=\"1\" name=\"field_value\" cols=\"40\" rows=\"5\" >$value</textarea>";
184                 } else {
185                         $subfield_data{marc_value}="<input onblur=\"this.style.backgroundColor='#ffffff';\" onfocus=\"this.style.backgroundColor='#ffffff'; \" tabindex=\"1\" type=\"text\" name=\"field_value\" value=\"$value\" size=\"50\">"; #"
186                 }
187         }
188         return \%subfield_data;
189 }
190
191 sub build_tabs ($$$$) {
192     my($template, $record, $dbh,$encoding) = @_;
193     # fill arrays
194     my @loop_data =();
195     my $tag;
196     my $i=0;
197         my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
198                 from authorised_values
199                 where category=? order by lib");
200
201 # loop through each tab 0 through 9
202         for (my $tabloop = 0; $tabloop <= 9; $tabloop++) {
203                 my @loop_data = ();
204                 foreach my $tag (sort(keys (%{$tagslib}))) {
205                         my $indicator;
206         # if MARC::Record is not empty => use it as master loop, then add missing subfields that should be in the tab.
207         # if MARC::Record is empty => use tab as master loop.
208                         if ($record ne -1 && ($record->field($tag) || $tag eq '000')) {
209                                 my @fields;
210                                 if ($tag ne '000') {
211                                         @fields = $record->field($tag);
212                                 } else {
213                                         push @fields,$record->leader();
214                                 }
215                                 foreach my $field (@fields)  {
216                                         my @subfields_data;
217                                         if ($tag<10) {
218                                                 my ($value,$subfield);
219                                                 if ($tag ne '000') {
220                                                         $value=$field->data();
221                                                         $subfield="@";
222                                                 } else {
223                                                         $value = $field;
224                                                         $subfield='@';
225                                                 }
226                                                 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
227                                         #       next if ($tagslib->{$tag}->{$subfield}->{kohafield} eq 'auth_header.authid');
228                                                 push(@subfields_data, &create_input($tag,$subfield,$value,$i,$tabloop,$record,$authorised_values_sth));
229                                                 $i++;
230                                         } else {
231                                                 my @subfields=$field->subfields();
232                                                 foreach my $subfieldcount (0..$#subfields) {
233                                                         my $subfield=$subfields[$subfieldcount][0];
234                                                         my $value=$subfields[$subfieldcount][1];
235                                                         next if (length $subfield !=1);
236                                                         next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
237                                                         push(@subfields_data, &create_input($tag,$subfield,$value,$i,$tabloop,$record,$authorised_values_sth));
238                                                         $i++;
239                                                 }
240                                         }
241 # now, loop again to add parameter subfield that are not in the MARC::Record
242                                         foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
243                                                 next if (length $subfield !=1);
244                                                 next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
245                                                 next if ($tag<10);
246                                                 next if ((substr($tagslib->{$tag}->{$subfield}->{hidden},2,1) gt "1")  ); #check for visibility flag
247                                                 next if (defined($field->subfield($subfield)));
248                                                 push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
249                                                 $i++;
250                                         }
251                                         if ($#subfields_data >= 0) {
252                                                 my %tag_data;
253                                                 $tag_data{tag} = $tag;
254                                                 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
255                                                 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
256                                                 $tag_data{indicator} = $record->field($tag)->indicator(1). $record->field($tag)->indicator(2) if ($tag>=10);
257                                                 $tag_data{subfield_loop} = \@subfields_data;
258                                                 if ($tag<10) {
259                                                         $tag_data{fixedfield} = 1;
260                                                 }
261
262                                                 push (@loop_data, \%tag_data);
263                                         }
264 # If there is more than 1 field, add an empty hidden field as separator.
265                                         if ($#fields >=1 && $#loop_data >=0 && $loop_data[$#loop_data]->{'tag'} eq $tag) {
266                                                 my @subfields_data;
267                                                 my %tag_data;
268                                                 push(@subfields_data, &create_input('','','',$i,$tabloop,$record,$authorised_values_sth));
269                                                 $tag_data{tag} = '';
270                                                 $tag_data{tag_lib} = '';
271                                                 $tag_data{indicator} = '';
272                                                 $tag_data{subfield_loop} = \@subfields_data;
273                                                 if ($tag<10) {
274                                                         $tag_data{fixedfield} = 1;
275                                                 }
276                                                 push (@loop_data, \%tag_data);
277                                                 $i++;
278                                         }
279                                 }
280         
281                         } else {
282                                 my @subfields_data;
283                                 foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
284                                         next if (length $subfield !=1);
285                                         next if ((substr($tagslib->{$tag}->{$subfield}->{hidden},2,1) gt "1")  ); #check for visibility flag
286                                         next if ($tagslib->{$tag}->{$subfield}->{tab} ne $tabloop);
287                                         push(@subfields_data, &create_input($tag,$subfield,'',$i,$tabloop,$record,$authorised_values_sth));
288                                         $i++;
289                                 }
290                                 if ($#subfields_data >= 0) {
291                                         my %tag_data;
292                                         $tag_data{tag} = $tag;
293                                         $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
294                                         $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
295                                         $tag_data{indicator} = $indicator;
296                                         $tag_data{subfield_loop} = \@subfields_data;
297                                         $tag_data{tagfirstsubfield} = $tag_data{subfield_loop}[0];
298                                         if ($tag<10) {
299                                                 $tag_data{fixedfield} = 1;
300                                         }
301                                         push (@loop_data, \%tag_data);
302                                 }
303                         }
304                 }
305                 $template->param($tabloop."XX" =>\@loop_data);
306         }
307 }
308
309
310 sub build_hidden_data () {
311     # build hidden data =>
312     # we store everything, even if we show only requested subfields.
313
314     my @loop_data =();
315     my $i=0;
316     foreach my $tag (keys %{$tagslib}) {
317         my $previous_tag = '';
318
319         # loop through each subfield
320         foreach my $subfield (keys %{$tagslib->{$tag}}) {
321             next if ($subfield eq 'lib');
322             next if ($subfield eq 'tab');
323             next if ($subfield eq 'mandatory');
324                 next if ($subfield eq 'repeatable');
325             next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "-1");
326             my %subfield_data;
327             $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
328             $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
329             $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
330             $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
331             push(@loop_data, \%subfield_data);
332             $i++
333         }
334     }
335 }
336
337 # ======================== 
338 #          MAIN 
339 #=========================
340 my $input = new CGI;
341 my $z3950 = $input->param('z3950');
342 my $error = $input->param('error');
343 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
344 my $op = $input->param('op');
345 my $nonav = $input->param('nonav');
346 my $myindex = $input->param('index');
347 my $linkid=$input->param('linkid');
348 my $authtypecode = $input->param('authtypecode');
349
350 my $dbh = C4::Context->dbh;
351 $authtypecode = &GetAuthTypeCode($authid) if !$authtypecode;
352
353 my ($template, $loggedinuser, $cookie)
354     = get_template_and_user({template_name => "authorities/authorities.tmpl",
355                              query => $input,
356                              type => "intranet",
357                              authnotrequired => 0,
358                              flagsrequired => {editauthorities => 1},
359                              debug => 1,
360                              });
361 $template->param(nonav   => $nonav,index=>$myindex,authtypecode=>$authtypecode,);
362 $tagslib = GetTagsLabels(1,$authtypecode);
363 my $record=-1;
364 my $encoding="";
365 $record = GetAuthority($authid) if ($authid);
366 my ($oldauthnumtagfield,$oldauthnumtagsubfield);
367 my ($oldauthtypetagfield,$oldauthtypetagsubfield);
368 $is_a_modif=0;
369 if ($authid) {
370   $is_a_modif=1;
371   ($oldauthnumtagfield,$oldauthnumtagsubfield) = &GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
372   ($oldauthtypetagfield,$oldauthtypetagsubfield) = &GetAuthMARCFromKohaField("auth_header.authtypecode",$authtypecode);
373 }
374
375 #------------------------------------------------------------------------------------------------------------------------------
376 if ($op eq "add") {
377 #------------------------------------------------------------------------------------------------------------------------------
378         # rebuild
379         my @tags = $input->param('tag');
380         my @subfields = $input->param('subfield');
381         my @values = $input->param('field_value');
382         # build indicator hash.
383         my @ind_tag = $input->param('ind_tag');
384         my @indicator = $input->param('indicator');
385         my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag,'UNIMARCAUTH');
386 #     warn $record->as_formatted;
387         my $record=MARC::Record->new_from_xml($xml,'UTF-8',(C4::Context->preference("marcflavour") eq "UNIMARC"?"UNIMARCAUTH":C4::Context->preference("marcflavour")));
388         $record->encoding('UTF-8');
389         #warn $record->as_formatted;
390         # check for a duplicate
391         my ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
392         my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
393 # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
394         if (!$duplicateauthid or $confirm_not_duplicate) {
395 # warn "noduplicate";
396           if ($is_a_modif ) {   
397             ModAuthority($authid,$record,$authtypecode,1);
398           } else {
399             ($authid) = AddAuthority($record,$authid,$authtypecode);
400           }
401           print $input->redirect("detail.pl?authid=$authid");
402           exit;
403         } else {
404         # it may be a duplicate, warn the user and do nothing
405             build_tabs($template, $record, $dbh, $encoding);
406             build_hidden_data;
407             $template->param(authid =>$authid,
408                             duplicateauthid     => $duplicateauthid,
409                             duplicateauthvalue  => $duplicateauthvalue,
410                             );
411         }
412 #------------------------------------------------------------------------------------------------------------------------------
413 } elsif ($op eq "addfield") {
414 #------------------------------------------------------------------------------------------------------------------------------
415         my $addedfield = $input->param('addfield_field');
416         my $tagaddfield_subfield = $input->param('addfield_subfield');
417         my @tags = $input->param('tag');
418         my @subfields = $input->param('subfield');
419         my @values = $input->param('field_value');
420         # build indicator hash.
421         my @ind_tag = $input->param('ind_tag');
422         my @indicator = $input->param('indicator');
423         my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
424         my $record=MARC::Record->new_from_xml($xml,'UTF-8');
425         $record->encoding('UTF-8');
426         # adding an empty field
427         my $field = MARC::Field->new("$addedfield",'','','$tagaddfield_subfield' => "");
428         $record->append_fields($field);
429         build_tabs ($template, $record, $dbh,$encoding);
430         build_hidden_data;
431         $template->param(
432                 authid => $authid,);
433
434 } elsif ($op eq "delete") {
435 #------------------------------------------------------------------------------------------------------------------------------
436         &AUTHdelauthority($authid);
437         if ($nonav){
438         print $input->redirect("auth_finder.pl");
439         }else{
440         print $input->redirect("authorities-home.pl?authid=0");
441         }
442                 exit;
443 } else {
444 if ($op eq "duplicate")
445         {
446                 $authid = "";
447         }
448         build_tabs ($template, $record, $dbh,$encoding);
449         build_hidden_data;
450         $template->param(oldauthtypetagfield=>$oldauthtypetagfield, oldauthtypetagsubfield=>$oldauthtypetagsubfield,
451                 oldauthnumtagfield=>$oldauthnumtagfield, oldauthnumtagsubfield=>$oldauthnumtagsubfield,
452                 authid                      => $authid , authtypecode=>$authtypecode,   );
453 }
454
455 $template->param(
456         authid                       => $authid,
457         authtypecode => $authtypecode,
458         linkid=>$linkid,
459         );
460
461 my $authtypes = getauthtypes;
462 my @authtypesloop;
463 foreach my $thisauthtype (keys %$authtypes) {
464         my $selected = 1 if $thisauthtype eq $authtypecode;
465         my %row =(value => $thisauthtype,
466                                 selected => $selected,
467                                 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
468                         );
469         push @authtypesloop, \%row;
470 }
471
472 $template->param(authtypesloop => \@authtypesloop,
473                                 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
474                                 hide_marc => C4::Context->preference('hide_marc'),
475                                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
476                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
477                 IntranetNav => C4::Context->preference("IntranetNav"),
478                                 );
479 output_html_with_http_headers $input, $cookie, $template->output;