fixes in authorities. Now, it should work well...
[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{kohafield}=$tagslib->{$tag}->{$subfield}->{kohafield};
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 warn "=>".$record->as_formatted;
164     # fill arrays
165     my @loop_data =();
166     my $tag;
167     my $i=0;
168         my $authorised_values_sth = $dbh->prepare("select authorised_value,lib
169                 from authorised_values
170                 where category=? order by lib");
171
172         my @loop_data = ();
173         foreach my $tag (sort(keys (%{$tagslib}))) {
174                 my $indicator;
175 #               warn "TAG : $tag => ".$tagslib->{$tag}->{lib}."//";
176 # if MARC::Record is not empty => use it as master loop, then add missing subfields that should be in the tab.
177 # if MARC::Record is empty => use tab as master loop.
178                 if ($record ne -1 && $record->field($tag)) {
179                         my @fields = $record->field($tag);
180                         foreach my $field (@fields)  {
181                                 my @subfields_data;
182                                 if ($tag<10) {
183                                         my $value=$field->data();
184                                         my $subfield="@";
185                                         next if ($tagslib->{$tag}->{$subfield}->{tab} eq -1);
186                                         push(@subfields_data, &create_input($tag,$subfield,char_decode($value,$encoding),$i,$record,$authorised_values_sth));
187                                         $i++;
188                                 } else {
189                                         my @subfields=$field->subfields();
190                                         foreach my $subfieldcount (0..$#subfields) {
191                                                 my $subfield=$subfields[$subfieldcount][0];
192                                                 my $value=$subfields[$subfieldcount][1];
193                                                 next if (length $subfield !=1);
194                                                 next if ($tagslib->{$tag}->{$subfield}->{tab} eq -1);
195                                                 push(@subfields_data, &create_input($tag,$subfield,char_decode($value,$encoding),$i,$record,$authorised_values_sth));
196                                                 $i++;
197                                         }
198                                 }
199 # now, loop again to add parameter subfield that are not in the MARC::Record
200                                 foreach my $subfield (sort( keys %{$tagslib->{$tag}})) {
201                                         next if (length $subfield !=1);
202                                         next if ($tagslib->{$tag}->{$subfield}->{tab} eq -1);
203                                         next if ($tag<10);
204                                         next if (defined($record->field($tag)->subfield($subfield)));
205                                         push(@subfields_data, &create_input($tag,$subfield,'',$i,$record,$authorised_values_sth));
206                                         $i++;
207                                 }
208                                 if ($#subfields_data >= 0) {
209                                         my %tag_data;
210                                         $tag_data{tag} = $tag;
211                                         $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
212                                         $tag_data{repeatable} = $tagslib->{$tag}->{repeatable};
213                                         $tag_data{indicator} = $record->field($tag)->indicator(1). $record->field($tag)->indicator(2) if ($tag>=10);
214                                         $tag_data{subfield_loop} = \@subfields_data;
215                                         push (@loop_data, \%tag_data);
216                                 }
217 # If there is more than 1 field, add an empty hidden field as separator.
218                                 if ($#fields >=1) {
219                                         my @subfields_data;
220                                         my %tag_data;
221                                         push(@subfields_data, &create_input('','','',$i,$record,$authorised_values_sth));
222                                         $tag_data{tag} = '';
223                                         $tag_data{tag_lib} = '';
224                                         $tag_data{indicator} = '';
225                                         $tag_data{subfield_loop} = \@subfields_data;
226                                         push (@loop_data, \%tag_data);
227                                         $i++;
228                                 }
229                         }
230 # if breeding is empty
231                 } else {
232                         my @subfields_data;
233                         foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
234                                 next if (length $subfield !=1);
235                                 next if ($tagslib->{$tag}->{$subfield}->{tab} eq -1);
236                                 push(@subfields_data, &create_input($tag,$subfield,'',$i,$record,$authorised_values_sth));
237                                 $i++;
238                         }
239                         if ($#subfields_data >= 0) {
240                                 my %tag_data;
241                                 $tag_data{tag} = $tag;
242                                 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
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         warn "IN ADD : ".$record->as_formatted();
338         if ($is_a_modif) {
339                  AUTHmodauthority($dbh,$authid,$record,$authtypecode);
340         } else {
341                 ($authid) = AUTHaddauthority($dbh,$record,$authid,$authtypecode);
342         }
343         print $input->redirect("authorities-home.pl");
344 #------------------------------------------------------------------------------------------------------------------------------
345 } elsif ($op eq "addfield") {
346 #------------------------------------------------------------------------------------------------------------------------------
347         my $addedfield = $input->param('addfield_field');
348         my @tags = $input->param('tag');
349         my @subfields = $input->param('subfield');
350         my @values = $input->param('field_value');
351         # build indicator hash.
352         my @ind_tag = $input->param('ind_tag');
353         my @indicator = $input->param('indicator');
354         splice(@tags,$addedfield,0,$tags[$addedfield]);
355         splice(@subfields,$addedfield,0,$subfields[$addedfield]);
356         splice(@values,$addedfield,0,$values[$addedfield]);
357         splice(@ind_tag,$addedfield,0,$ind_tag[$addedfield]);
358         my %indicators;
359         for (my $i=0;$i<=$#ind_tag;$i++) {
360                 $indicators{$ind_tag[$i]} = $indicator[$i];
361         }
362 # search the part of the array to duplicate.
363         my $start=0;
364         my $end=0;
365         my $started;
366         for (my $i=0;$i<=$#tags;$i++) {
367                 $start=$i if ($start eq 0 && $tags[$i] == $addedfield);
368                 $end=$i if ($start>0 && $tags[$i] eq $addedfield);
369                 last if ($start>0 && $tags[$i] ne $addedfield);
370         }
371 # add an empty line in all arrays. This forces a new field in MARC::Record.
372         splice(@tags,$end+1,0,'');
373         splice(@subfields,$end+1,0,'');
374         splice(@values,$end+1,0,'');
375         splice(@ind_tag,$end+1,0,'');
376         splice(@indicator,$end+1,0,'');
377 # then duplicate the field.
378         splice(@tags,$end+2,0,@tags[$start..$end]);
379         splice(@subfields,$end+2,0,@subfields[$start..$end]);
380         splice(@values,$end+2,0,@values[$start..$end]);
381         splice(@ind_tag,$end+2,0,@ind_tag[$start..$end]);
382         splice(@indicator,$end+2,0,@indicator[$start..$end]);
383
384         my %indicators;
385         for (my $i=0;$i<=$#ind_tag;$i++) {
386                 $indicators{$ind_tag[$i]} = $indicator[$i];
387         }
388         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
389         build_tabs ($template, $record, $dbh,$encoding);
390         build_hidden_data;
391         $template->param(
392                 authid                       => $authid,);
393 #               oldbiblionumtagfield        => $oldbiblionumtagfield,
394 #               oldbiblionumtagsubfield     => $oldbiblionumtagsubfield,
395 #               oldbiblioitemnumtagfield    => $oldbiblioitemnumtagfield,
396 #               oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
397 #               oldbiblioitemnumber         => $oldbiblioitemnumber );
398 } elsif ($op eq "delete") {
399 #------------------------------------------------------------------------------------------------------------------------------
400         &AUTHdelauthority($dbh,$authid);
401 }
402
403 build_tabs ($template, $record, $dbh,$encoding);
404 build_hidden_data;
405 $template->param(
406         authid                       => $authid,
407 #       oldbiblionumtagfield        => $oldbiblionumtagfield,
408 #       oldbiblionumtagsubfield     => $oldbiblionumtagsubfield,
409 #       oldbiblioitemnumtagfield    => $oldbiblioitemnumtagfield,
410 # #     oldbiblioitemnumtagsubfield => $oldbiblioitemnumtagsubfield,
411 #       oldbiblioitemnumber         => $oldbiblioitemnumber,
412         authtypecode => $authtypecode,
413         );
414
415         my $authtypes = getauthtypes;
416 my @authtypesloop;
417 foreach my $thisauthtype (keys %$authtypes) {
418         my $selected = 1 if $thisauthtype eq $authtypecode;
419         my %row =(value => $thisauthtype,
420                                 selected => $selected,
421                                 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
422                         );
423         push @authtypesloop, \%row;
424 }
425
426 $template->param(authtypesloop => \@authtypesloop);
427 output_html_with_http_headers $input, $cookie, $template->output;