adding authentification with Auth.pm and
[koha.git] / acqui.simple / addbiblio.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::Biblio;
27 use C4::Context;
28 use HTML::Template;
29 use MARC::File::USMARC;
30
31 sub find_value {
32         my ($tagfield,$insubfield,$record) = @_;
33 #       warn "$tagfield / $insubfield // ";
34         my $result;
35         my $indicator;
36         foreach my $field ($record->field($tagfield)) {
37                 my @subfields = $field->subfields();
38                 foreach my $subfield (@subfields) {
39                         if (@$subfield[0] eq $insubfield) {
40                                 $result .= @$subfield[1];
41                                 $indicator = $field->indicator(1).$field->indicator(2);
42                         }
43                 }
44         }
45         return($indicator,$result);
46 }
47
48 sub MARCfindbreeding {
49         my ($dbh,$isbn) = @_;
50         my $sth = $dbh->prepare("select file,marc from marc_breeding where isbn=?");
51         $sth->execute($isbn);
52         my ($file,$marc) = $sth->fetchrow;
53         if ($marc) {
54                 my $record = MARC::File::USMARC::decode($marc);
55                 if (ref($record) eq undef) {
56                         return -1;
57                 } else {
58                         return $record;
59                 }
60         }
61         return -1;
62
63 }
64 my $input = new CGI;
65 my $error = $input->param('error');
66 my $oldbiblionumber=$input->param('oldbiblionumber'); # if bib exists, it's a modif, not a new biblio.
67 my $isbn = $input->param('isbn');
68 my $op = $input->param('op');
69 my $dbh = C4::Context->dbh;
70 my $bibid;
71 if ($oldbiblionumber) {
72         $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
73 }else {
74         $bibid = $input->param('bibid');
75 }
76 my ($template, $loggedinuser, $cookie)
77     = get_template_and_user({template_name => "acqui.simple/addbiblio.tmpl",
78                              query => $input,
79                              type => "intranet",
80                              authnotrequired => 0,
81                              flagsrequired => {catalogue => 1},
82                              debug => 1,
83                              });
84
85 my $tagslib = &MARCgettagslib($dbh,1);
86 my $record=-1;
87 $record = MARCgetbiblio($dbh,$bibid) if ($bibid);
88 $record = MARCfindbreeding($dbh,$isbn) if ($isbn);
89 my $is_a_modif=0;
90 my ($oldbiblionumtagfield,$oldbiblionumtagsubfield);
91 if ($bibid) {
92         $is_a_modif=1;
93 ($oldbiblionumtagfield,$oldbiblionumtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"biblio.biblionumber");
94 warn "==>$oldbiblionumtagfield,$oldbiblionumtagsubfield";
95
96 }
97 #------------------------------------------------------------------------------------------------------------------------------
98 if ($op eq "addbiblio") {
99 #------------------------------------------------------------------------------------------------------------------------------
100         # rebuild
101         my @tags = $input->param('tag');
102         my @subfields = $input->param('subfield');
103         my @values = $input->param('field_value');
104         # build indicator hash.
105         my @ind_tag = $input->param('ind_tag');
106         my @indicator = $input->param('indicator');
107         my %indicators;
108         for (my $i=0;$i<=$#ind_tag;$i++) {
109                 $indicators{$ind_tag[$i]} = $indicator[$i];
110         }
111         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
112 # MARC::Record builded => now, record in DB
113         my $oldbibnum;
114         my $oldbibitemnum;
115         if ($is_a_modif) {
116                  NEWmodbiblio($dbh,$record,$bibid);
117         } else {
118                 ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbiblio($dbh,$record);
119         }
120 # now, redirect to additem page
121         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=additem.pl?bibid=$bibid\"></html>";
122         exit;
123 #------------------------------------------------------------------------------------------------------------------------------
124 } else {
125 #------------------------------------------------------------------------------------------------------------------------------
126         # fill arrays
127         my @loop_data =();
128         my $tag;
129         my $i=0;
130         my $authorised_values_sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=? order by authorised_value");
131 # loop through each tab 0 through 9
132         for (my $tabloop = 0; $tabloop<=9;$tabloop++) {
133         #       my @fields = $record->fields();
134                 my @loop_data =();
135                 foreach my $tag (sort(keys (%{$tagslib}))) {
136                         my $previous_tag = '';
137                         my @subfields_data;
138                         my $indicator;
139 # loop through each subfield
140                         foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
141                                 next if ($subfield eq 'lib'); # skip lib and tabs, which are koha internal
142                                 next if ($subfield eq 'tab');
143                                 next if ($tagslib->{$tag}->{$subfield}->{tab}  ne $tabloop);
144                                 my %subfield_data;
145                                 $subfield_data{tag}=$tag;
146                                 $subfield_data{subfield}=$subfield;
147                                 $subfield_data{marc_lib}="<DIV id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</div>";
148                                 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
149                                 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
150                                 # if breeding is not empty
151                                 if ($record ne -1) {
152                                         my ($x,$value) = find_value($tag,$subfield,$record);
153                                         $indicator = $x if $x;
154                                         if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
155                                                 my @authorised_values;
156                                                 my %authorised_lib;
157                                                 # builds list, depending on authorised value...
158                                                 #---- branch
159                                                 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
160                                                         my $sth=$dbh->prepare("select branchcode,branchname from branches");
161                                                         $sth->execute;
162                                                         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
163                                                         while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
164                                                                 push @authorised_values, $branchcode;
165                                                                 $authorised_lib{$branchcode}=$branchname;
166                                                         }
167                                                 #----- itemtypes
168                                                 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
169                                                         my $sth=$dbh->prepare("select itemtype,description from itemtypes");
170                                                         $sth->execute;
171                                                         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
172                                                         while (my ($itemtype,$description) = $sth->fetchrow_array) {
173                                                                 push @authorised_values, $itemtype;
174                                                                 $authorised_lib{$itemtype}=$description;
175                                                         }
176                                                 #---- "true" authorised value
177                                                 } else {
178                                                         $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
179                                                         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
180                                                         while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
181                                                                 push @authorised_values, $value;
182                                                                 $authorised_lib{$value}=$lib;
183                                                         }
184                                                 }
185                                                 $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
186                                                                                                                                                                         -values=> \@authorised_values,
187                                                                                                                                                                         -default=>"$value",
188                                                                                                                                                                         -labels => \%authorised_lib,
189                                                                                                                                                                         -size=>1,
190                                                                                                                                                                         -multiple=>0,
191                                                                                                                                                                         );
192                                         } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
193                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\"  size=47 maxlength=255> <a href=\"javascript:Dopop('../thesaurus_popup.pl?category=$tagslib->{$tag}->{$subfield}->{thesaurus_category}&index=$i',$i)\">...</a>"; #"
194                                         } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
195                                                 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
196                                                 require $plugin;
197                                                 my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,$tabloop);
198                                                 my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,$tabloop);
199                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\"  value=\"$value\" 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";
200                                         } else {
201                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
202                                         }
203                                 # if breeding is empty
204                                 } else {
205                                         my ($x,$value);
206                                         ($x,$value) = find_value($tag,$subfield,$record) if ($record ne -1);
207                                         if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
208                                                 my @authorised_values;
209                                                 my %authorised_lib;
210                                                 # builds list, depending on authorised value...
211                                                 #---- branch
212                                                 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
213                                                         my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchcode");
214                                                         $sth->execute;
215                                                         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
216                                                         while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
217                                                                 push @authorised_values, $branchcode;
218                                                                 $authorised_lib{$branchcode}=$branchname;
219                                                         }
220                                                 #----- itemtypes
221                                                 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
222                                                         my $sth=$dbh->prepare("select itemtype,description from itemtypes order by itemtype");
223                                                         $sth->execute;
224                                                         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
225                                                         while (my ($itemtype,$description) = $sth->fetchrow_array) {
226                                                                 push @authorised_values, $itemtype;
227                                                                 $authorised_lib{$itemtype}=$description;
228                                                         }
229                                                 #---- "true" authorised value
230                                                 } else {
231                                                         $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
232                                                         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
233                                                         while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
234                                                                 push @authorised_values, $value;
235                                                                 $authorised_lib{$value}=$lib;
236                                                         }
237                                                 }
238                                                 $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
239                                                                                                                                                                         -values=> \@authorised_values,
240                                                                                                                                                                         -default=>"$value",
241                                                                                                                                                                         -labels => \%authorised_lib,
242                                                                                                                                                                         -size=>1,
243                                                                                                                                                                         -multiple=>0,
244                                                                                                                                                                         );
245                                         } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
246                                                 $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>";
247                                         } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
248                                                 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
249                                                 require $plugin;
250                                                 my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,$tabloop);
251                                                 my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,$tabloop);
252                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\"  size=47 maxlength=255 DISABLE READONLY OnFocus=\"javascript:Focus$function_name($i)\" OnBlur=\"javascript:Blur$function_name($i)\"> <a href=\"javascript:Clic$function_name($i)\">...</a> $javascript";
253                                         } else {
254                                                 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" size=50 maxlength=255>";
255                                         }
256                                 }
257                                 push(@subfields_data, \%subfield_data);
258                                 $i++;
259                         }
260                         if ($#subfields_data>=0) {
261                                 my %tag_data;
262                                 $tag_data{tag}=$tag;
263                                 $tag_data{tag_lib} = $tagslib->{$tag}->{lib};
264                                 $tag_data{indicator} = $indicator;
265                                 $tag_data{subfield_loop} = \@subfields_data;
266                                 push (@loop_data, \%tag_data);
267                         }
268                 }
269                 $template->param($tabloop."XX" =>\@loop_data);
270         }
271         # now, build hidden datas => we store everything, even if we show only requested subfields.
272         my @loop_data =();
273         my $i=0;
274         foreach my $tag (keys %{$tagslib}) {
275                 my $previous_tag = '';
276         # loop through each subfield
277                 foreach my $subfield (keys %{$tagslib->{$tag}}) {
278                         next if ($subfield eq 'lib');
279                         next if ($subfield eq 'tab');
280                         next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "-1");
281                         my %subfield_data;
282                         $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
283                         $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
284                         $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
285                         $subfield_data{marc_value}="<input type=\"hidden\" name=\"field_value[]\">";
286                         push(@loop_data, \%subfield_data);
287                         $i++
288                 }
289         }
290         $template->param(
291                                                         oldbiblionumber => $oldbiblionumber,
292                                                         bibid => $bibid,
293                                                         oldbiblionumtagfield => $oldbiblionumtagfield,
294                                                         oldbiblionumtagsubfield => $oldbiblionumtagsubfield);
295 }
296 print $input->header(-cookie => $cookie),$template->output;