big commit, still breaking things...
[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::Interface::CGI::Output;
27 use C4::AuthoritiesMarc;
28 use C4::Context;
29 use C4::Koha; # XXX subfield_is_koha_internal_p
30 use HTML::Template;
31 use MARC::File::USMARC;
32
33 use vars qw( $tagslib);
34 use vars qw( $authorised_values_sth);
35 use vars qw( $is_a_modif );
36
37 =item find_value
38
39     ($indicators, $value) = find_value($tag, $subfield, $record,$encoding);
40
41 Find the given $subfield in the given $tag in the given
42 MARC::Record $record.  If the subfield is found, returns
43 the (indicators, value) pair; otherwise, (undef, undef) is
44 returned.
45
46 =cut
47
48 sub find_value {
49         my ($tagfield,$insubfield,$record,$encoding) = @_;
50         my @result;
51         my $indicator;
52         if ($tagfield <10) {
53                 if ($record->field($tagfield)) {
54                         push @result, $record->field($tagfield)->data();
55                 } else {
56                         push @result,"";
57                 }
58         } else {
59                 foreach my $field ($record->field($tagfield)) {
60                         my @subfields = $field->subfields();
61                         foreach my $subfield (@subfields) {
62                                 if (@$subfield[0] eq $insubfield) {
63                                         push @result,char_decode(@$subfield[1],$encoding);
64                                         $indicator = $field->indicator(1).$field->indicator(2);
65                                 }
66                         }
67                 }
68         }
69         return($indicator,@result);
70 }
71
72
73 =item build_authorized_values_list
74
75 =cut
76
77 sub build_authorized_values_list ($$$$$) {
78         my($tag, $subfield, $value, $dbh,$authorised_values_sth) = @_;
79
80         my @authorised_values;
81         my %authorised_lib;
82
83         # builds list, depending on authorised value...
84
85         #---- branch
86         if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
87         my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
88         $sth->execute;
89         push @authorised_values, ""
90                 unless ($tagslib->{$tag}->{$subfield}->{mandatory});
91
92         while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
93                 push @authorised_values, $branchcode;
94                 $authorised_lib{$branchcode}=$branchname;
95         }
96
97         #----- itemtypes
98         } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
99                 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
100                 $sth->execute;
101                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
102         
103                 while (my ($itemtype,$description) = $sth->fetchrow_array) {
104                         push @authorised_values, $itemtype;
105                         $authorised_lib{$itemtype}=$description;
106                 }
107 #               $value=$itemtype unless ($value);
108
109         #---- "true" authorised value
110         } else {
111                 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
112
113                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
114         
115                 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
116                         push @authorised_values, $value;
117                         $authorised_lib{$value}=$lib;
118                 }
119     }
120     return CGI::scrolling_list( -name     => 'field_value',
121                                 -values   => \@authorised_values,
122                                 -default  => $value,
123                                 -labels   => \%authorised_lib,
124                                 -size     => 1,
125                                 -multiple => 0 );
126 }
127
128 =item create_input
129  builds the <input ...> entry for a subfield.
130 =cut
131 sub create_input () {
132         my ($tag,$subfield,$value,$i,$rec,$authorised_values_sth) = @_;
133         $value =~ s/"/&quot;/g;
134         my $dbh = C4::Context->dbh;
135         my %subfield_data;
136         $subfield_data{tag}=$tag;
137         $subfield_data{subfield}=$subfield;
138         $subfield_data{marc_lib}="<span id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
139         $subfield_data{tag_mandatory}=$tagslib->{$tag}->{mandatory};
140         $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
141         $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
142         $subfield_data{index} = $i;
143         if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
144                 $subfield_data{marc_value}= build_authorized_values_list($tag, $subfield, $value, $dbh,$authorised_values_sth);
145         } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
146                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\"  size=47 maxlength=255 DISABLE READONLY> <a href=\"javascript:Dopop('../thesaurus_popup.pl?category=$tagslib->{$tag}->{$subfield}->{thesaurus_category}&index=$i',$i)\">...</a>";
147         } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
148                 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
149                 require $plugin;
150                 my $extended_param = plugin_parameters($dbh,$rec,$tagslib,$i,0);
151                 my ($function_name,$javascript) = plugin_javascript($dbh,$rec,$tagslib,$i,0);
152                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\"  value=\"$value\" DISABLE READONLY size=47 maxlength=255 OnFocus=\"javascript:Focus$function_name($i)\" OnBlur=\"javascript:Blur$function_name($i)\"> <a href=\"javascript:Clic$function_name($i)\">...</a> $javascript";
153         } elsif  ($tag eq '') {
154                 $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value\" size=50 maxlength=255>"; #"
155         } else {
156                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>"; #"
157         }
158         return \%subfield_data;
159 }
160
161 sub build_tabs ($$$$) {
162     my($template, $record, $dbh,$encoding) = @_;
163     # fill arrays
164     my @loop_data =();
165     my $tag;
166     my $i=0;
167         my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
168                 from authorised_values
169                 where category=? order by lib");
170
171         my @loop_data = ();
172         foreach my $tag (sort(keys (%{$tagslib}))) {
173                 my $indicator;
174 #               warn "TAG : $tag => ".$tagslib->{$tag}->{lib}."//";
175 # if MARC::Record is not empty => use it as master loop, then add missing subfields that should be in the tab.
176 # if MARC::Record is empty => use tab as master loop.
177                 if ($record ne -1 && $record->field($tag)) {
178                         my @fields = $record->field($tag);
179                         foreach my $field (@fields)  {
180                                 my @subfields_data;
181                                 if ($tag<10) {
182                                         my $value=$field->data();
183                                         my $subfield="@";
184                                         next if ($tagslib->{$tag}->{$subfield}->{tab} eq -1);
185                                         push(@subfields_data, &create_input($tag,$subfield,char_decode($value,$encoding),$i,$record,$authorised_values_sth));
186                                         $i++;
187                                 } else {
188                                         my @subfields=$field->subfields();
189                                         foreach my $subfieldcount (0..$#subfields) {
190                                                 my $subfield=$subfields[$subfieldcount][0];
191                                                 my $value=$subfields[$subfieldcount][1];
192                                                 next if (length $subfield !=1);
193                                                 next if ($tagslib->{$tag}->{$subfield}->{tab} eq -1);
194                                                 push(@subfields_data, &create_input($tag,$subfield,char_decode($value,$encoding),$i,$record,$authorised_values_sth));
195                                                 $i++;
196                                         }
197                                 }
198 # now, loop again to add parameter subfield that are not in the MARC::Record
199                                 foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
200                                         next if (length $subfield !=1);
201                                         next if ($tagslib->{$tag}->{$subfield}->{tab} eq -1);
202                                         next if ($tag<10);
203                                         next if (defined($record->field($tag)->subfield($subfield)));
204                                         push(@subfields_data, &create_input($tag,$subfield,'',$i,$record,$authorised_values_sth));
205                                         $i++;
206                                 }
207                                 if ($#subfields_data >= 0) {
208                                         my %tag_data;
209                                         $tag_data{tag} = $tag;
210                                         $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
211                                         $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
212                                         $tag_data{indicator} = $record->field($tag)->indicator(1). $record->field($tag)->indicator(2) if ($tag>=10);
213                                         $tag_data{subfield_loop} = \@subfields_data;
214                                         push (@loop_data, \%tag_data);
215                                 }
216 # If there is more than 1 field, add an empty hidden field as separator.
217                                 if ($#fields >=1) {
218                                         my @subfields_data;
219                                         my %tag_data;
220                                         push(@subfields_data, &create_input('','','',$i,$record,$authorised_values_sth));
221                                         $tag_data{tag} = '';
222                                         $tag_data{tag_lib} = '';
223                                         $tag_data{indicator} = '';
224                                         $tag_data{subfield_loop} = \@subfields_data;
225                                         push (@loop_data, \%tag_data);
226                                         $i++;
227                                 }
228                         }
229 # if breeding is empty
230                 } else {
231                         my @subfields_data;
232                         foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
233                                 next if (length $subfield !=1);
234                                 next if ($tagslib->{$tag}->{$subfield}->{tab} eq -1);
235                                 push(@subfields_data, &create_input($tag,$subfield,'',$i,$record,$authorised_values_sth));
236                                 $i++;
237                         }
238                         if ($#subfields_data >= 0) {
239                                 my %tag_data;
240                                 $tag_data{tag} = $tag;
241                                 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
242                                 $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
243                                 $tag_data{indicator} = $indicator;
244                                 $tag_data{subfield_loop} = \@subfields_data;
245                                 push (@loop_data, \%tag_data);
246                         }
247                 }
248         }
249         $template->param("0XX" =>\@loop_data);
250 }
251
252
253 sub build_hidden_data () {
254     # build hidden data =>
255     # we store everything, even if we show only requested subfields.
256
257     my @loop_data =();
258     my $i=0;
259     foreach my $tag (keys %{$tagslib}) {
260         my $previous_tag = '';
261
262         # loop through each subfield
263         foreach my $subfield (keys %{$tagslib->{$tag}}) {
264             next if ($subfield eq 'lib');
265             next if ($subfield eq 'tab');
266             next if ($subfield eq 'mandatory');
267                 next if ($subfield eq 'repeatable');
268             next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "-1");
269             my %subfield_data;
270             $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
271             $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
272             $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
273             $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
274             push(@loop_data, \%subfield_data);
275             $i++
276         }
277     }
278 }
279
280
281 # ======================== 
282 #          MAIN 
283 #=========================
284 my $input = new CGI;
285 my $error = $input->param('error');
286 my $authid=$input->param('authid'); # if authid exists, it's a modif, not a new authority.
287 my $z3950 = $input->param('z3950');
288 my $op = $input->param('op');
289 # warn "OP : $op";
290 my $authtypecode = $input->param('authtypecode');
291 my $dbh = C4::Context->dbh;
292 $authtypecode = &AUTHfind_authtypecode($dbh,$authid) if $authid;
293 # warn "authtypecode : $authtypecode && authid = $authid";
294 my ($template, $loggedinuser, $cookie)
295     = get_template_and_user({template_name => "authorities/authorities.tmpl",
296                              query => $input,
297                              type => "intranet",
298                              authnotrequired => 0,
299                              flagsrequired => {editcatalogue => 1},
300                              debug => 1,
301                              });
302
303 $tagslib = AUTHgettagslib($dbh,1,$authtypecode);
304 my $record=-1;
305 my $encoding="";
306 $record = AUTHgetauthority($dbh,$authid) if ($authid);
307
308 $is_a_modif=0;
309 # my ($oldbiblionumtagfield,$oldbiblionumtagsubfield);
310 # my ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield,$bibitem,$oldbiblioitemnumber);
311 if ($authid) {
312         $is_a_modif=1;
313         # if it's a modif, retrieve old biblio and bibitem numbers for the future modification of old-DB.
314 #       ($oldbiblionumtagfield,$oldbiblionumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblio.biblionumber");
315 #       ($oldbiblioitemnumtagfield,$oldbiblioitemnumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblioitems.biblioitemnumber");
316 #       # search biblioitems value
317 #       my $sth=$dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
318 #       $sth->execute($oldbiblionumber);
319 #       ($oldbiblioitemnumber) = $sth->fetchrow;
320 }
321 #------------------------------------------------------------------------------------------------------------------------------
322 if ($op eq "add") {
323 #------------------------------------------------------------------------------------------------------------------------------
324         # rebuild
325         my @tags = $input->param('tag');
326         my @subfields = $input->param('subfield');
327         my @values = $input->param('field_value');
328         # build indicator hash.
329         my @ind_tag = $input->param('ind_tag');
330         my @indicator = $input->param('indicator');
331         my %indicators;
332         for (my $i=0;$i<=$#ind_tag;$i++) {
333                 $indicators{$ind_tag[$i]} = $indicator[$i];
334         }
335         my $record = AUTHhtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
336 # MARC::Record built => now, record in DB
337         # check for a duplicate
338         my ($duplicateauthid,$duplicateauthvalue) = FindDuplicate($record,$authtypecode) if ($op eq "add") && (!$is_a_modif);
339         my $confirm_not_duplicate = $input->param('confirm_not_duplicate');
340         # it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
341         if (!$duplicateauthid or $confirm_not_duplicate) {
342                 if ($is_a_modif) {
343                         AUTHmodauthority($dbh,$authid,$record,$authtypecode);
344                 } else {
345                         ($authid) = AUTHaddauthority($dbh,$record,$authid,$authtypecode);
346                 }
347         # now, redirect to detail page
348                 print $input->redirect("detail.pl?authid=$authid");
349                 exit;
350         } else {
351         # it may be a duplicate, warn the user and do nothing
352                 build_tabs ($template, $record, $dbh,$encoding);
353                 build_hidden_data;
354                 $template->param(
355                         duplicateauthid                         => $duplicateauthid,
356                         duplicateauthvalue                              => $duplicateauthvalue,
357                          );
358         }
359 #------------------------------------------------------------------------------------------------------------------------------
360 } elsif ($op eq "addfield") {
361 #------------------------------------------------------------------------------------------------------------------------------
362         my $addedfield = $input->param('addfield_field');
363         my @tags = $input->param('tag');
364         my @subfields = $input->param('subfield');
365         my @values = $input->param('field_value');
366         # build indicator hash.
367         my @ind_tag = $input->param('ind_tag');
368         my @indicator = $input->param('indicator');
369         my %indicators;
370         for (my $i=0;$i<=$#ind_tag;$i++) {
371                 $indicators{$ind_tag[$i]} = $indicator[$i];
372         }
373         my $record = AUTHhtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
374         my $field = MARC::Field->new("$addedfield",'','','a'=> "");
375         $record->append_fields($field);
376
377         build_tabs ($template, $record, $dbh,$encoding);
378         build_hidden_data;
379         $template->param(
380                 authid                       => $authid,);
381 #               oldbiblionumtagfield        => $oldbiblionumtagfield,
382 #               oldbiblionumtagsubfield     => $oldbiblionumtagsubfield,
383 #               oldbiblioitemnumtagfield    => $oldbiblioitemnumtagfield,
384 #               oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
385 #               oldbiblioitemnumber         => $oldbiblioitemnumber );
386 } elsif ($op eq "delete") {
387 #------------------------------------------------------------------------------------------------------------------------------
388         &AUTHdelauthority($dbh,$authid);
389 } else {
390         build_tabs ($template, $record, $dbh,$encoding);
391         build_hidden_data;
392 }
393
394 unless ($op) {
395 #       warn "BUILDING";
396         build_tabs ($template, $record, $dbh,$encoding);
397         build_hidden_data;
398 }
399 $template->param(
400         authid                       => $authid,
401 #       oldbiblionumtagfield        => $oldbiblionumtagfield,
402 #       oldbiblionumtagsubfield     => $oldbiblionumtagsubfield,
403 #       oldbiblioitemnumtagfield    => $oldbiblioitemnumtagfield,
404 # #     oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
405 #       oldbiblioitemnumber         => $oldbiblioitemnumber,
406         authtypecode => $authtypecode,
407         );
408
409 my $authtypes = getauthtypes;
410 my @authtypesloop;
411 foreach my $thisauthtype (keys %$authtypes) {
412         my $selected = 1 if $thisauthtype eq $authtypecode;
413         my %row =(value => $thisauthtype,
414                                 selected => $selected,
415                                 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
416                         );
417         push @authtypesloop, \%row;
418 }
419
420 $template->param(authtypesloop => \@authtypesloop,
421                                 authtypetext => $authtypes->{$authtypecode}{'authtypetext'},
422                                 hide_marc => C4::Context->preference('hide_marc'),
423                                 );
424 output_html_with_http_headers $input, $cookie, $template->output;