Those fixes solves the "internal server error" with MARC::Record 1.12.
[koha.git] / acqui.simple / additem.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 CGI;
23 use strict;
24 use C4::Auth;
25 use C4::Output;
26 use C4::Interface::CGI::Output;
27 use C4::Biblio;
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 sub find_value {
34         my ($tagfield,$insubfield,$record) = @_;
35         my $result;
36         my $indicator;
37         foreach my $field ($record->field($tagfield)) {
38                 my @subfields = $field->subfields();
39                 foreach my $subfield (@subfields) {
40                         if (@$subfield[0] eq $insubfield) {
41                                 $result .= @$subfield[1];
42                                 $indicator = $field->indicator(1).$field->indicator(2);
43                         }
44                 }
45         }
46         return($indicator,$result);
47 }
48 my $input = new CGI;
49 my $dbh = C4::Context->dbh;
50 my $error = $input->param('error');
51 my $bibid = $input->param('bibid');
52 my $oldbiblionumber = &MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
53
54 my $op = $input->param('op');
55 my $itemnum = $input->param('itemnum');
56
57 my $tagslib = &MARCgettagslib($dbh,1);
58 my $record = MARCgetbiblio($dbh,$bibid);
59 my $itemrecord;
60 my $nextop="additem";
61 #------------------------------------------------------------------------------------------------------------------------------
62 if ($op eq "additem") {
63 #------------------------------------------------------------------------------------------------------------------------------
64         # rebuild
65         my @tags = $input->param('tag');
66         my @subfields = $input->param('subfield');
67         my @values = $input->param('field_value');
68         # build indicator hash.
69         my @ind_tag = $input->param('ind_tag');
70         my @indicator = $input->param('indicator');
71         my %indicators;
72         for (my $i=0;$i<=$#ind_tag;$i++) {
73                 $indicators{$ind_tag[$i]} = $indicator[$i];
74         }
75         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
76 # MARC::Record builded => now, record in DB
77         my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = NEWnewitem($dbh,$record,$bibid);
78         $nextop = "additem";
79 #------------------------------------------------------------------------------------------------------------------------------
80 } elsif ($op eq "edititem") {
81 #------------------------------------------------------------------------------------------------------------------------------
82 # retrieve item if exist => then, it's a modif
83         $itemrecord = MARCgetitem($dbh,$bibid,$itemnum);
84         $nextop="saveitem";
85 #------------------------------------------------------------------------------------------------------------------------------
86 } elsif ($op eq "saveitem") {
87 #------------------------------------------------------------------------------------------------------------------------------
88         # rebuild
89         my @tags = $input->param('tag');
90         my @subfields = $input->param('subfield');
91         my @values = $input->param('field_value');
92         # build indicator hash.
93         my @ind_tag = $input->param('ind_tag');
94         my @indicator = $input->param('indicator');
95 #       my $itemnum = $input->param('itemnum');
96         my %indicators;
97         for (my $i=0;$i<=$#ind_tag;$i++) {
98                 $indicators{$ind_tag[$i]} = $indicator[$i];
99         }
100         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
101 # MARC::Record builded => now, record in DB
102         my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = NEWmoditem($dbh,$record,$bibid,$itemnum,0);
103         $itemnum="";
104         $nextop="additem";
105 }
106
107 #
108 #------------------------------------------------------------------------------------------------------------------------------
109 # build screen with existing items. and "new" one
110 #------------------------------------------------------------------------------------------------------------------------------
111
112 my %indicators;
113 $indicators{995}='  ';
114 # now, build existiing item list
115 my $temp = MARCgetbiblio($dbh,$bibid);
116 my @fields = $temp->fields();
117 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
118 my @big_array;
119 #---- finds where items.itemnumber is stored
120 my ($itemtagfield,$itemtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.itemnumber");
121 my @itemnums; # array to store itemnums
122 foreach my $field (@fields) {
123         next if ($field->tag()<10);
124         my @subf=$field->subfields;
125         my %this_row;
126 # loop through each subfield
127         for my $i (0..$#subf) {
128                 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  ne 10 && ($field->tag() ne $itemtagfield && $subf[$i][0] ne $itemtagsubfield));
129                 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  eq 10);
130                 $this_row{$subf[$i][0]} =$subf[$i][1] if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  eq 10);
131                 push @itemnums,$this_row{$subf[$i][0]} =$subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
132         }
133         if (%this_row) {
134                 push(@big_array, \%this_row);
135         }
136 }
137 #fill big_row with missing datas
138 foreach my $subfield_code  (keys(%witness)) {
139         for (my $i=0;$i<=$#big_array;$i++) {
140                 $big_array[$i]{$subfield_code}="&nbsp;" unless ($big_array[$i]{$subfield_code});
141         }
142 }
143 # now, construct template !
144 my @item_value_loop;
145 my @header_value_loop;
146 for (my $i=0;$i<=$#big_array; $i++) {
147         my $items_data;
148         foreach my $subfield_code (sort keys(%witness)) {
149                 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
150         }
151         my %row_data;
152         $row_data{item_value} = $items_data;
153         $row_data{itemnum} = $itemnums[$i];
154         push(@item_value_loop,\%row_data);
155 }
156 foreach my $subfield_code (sort keys(%witness)) {
157         my %header_value;
158         $header_value{header_value} = $witness{$subfield_code};
159         push(@header_value_loop, \%header_value);
160 }
161
162 # next item form
163 my @loop_data =();
164 my $i=0;
165 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=? order by authorised_value");
166
167 foreach my $tag (sort keys %{$tagslib}) {
168         my $previous_tag = '';
169 # loop through each subfield
170         foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
171                 next if subfield_is_koha_internal_p($subfield);
172                 next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "10");
173                 my %subfield_data;
174                 $subfield_data{tag}=$tag;
175                 $subfield_data{subfield}=$subfield;
176 #               $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
177                 $subfield_data{marc_lib}="<DIV id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</div>";
178                 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
179                 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
180                 my ($x,$value);
181                 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
182                 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
183                         my @authorised_values;
184                         my %authorised_lib;
185                         # builds list, depending on authorised value...
186                         #---- branch
187                         if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
188                                 my $sth=$dbh->prepare("select branchcode,branchname from branches");
189                                 $sth->execute;
190                                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
191                                 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
192                                         push @authorised_values, $branchcode;
193                                         $authorised_lib{$branchcode}=$branchname;
194                                 }
195                         #----- itemtypes
196                         } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
197                                 my $sth=$dbh->prepare("select itemtype,description from itemtypes");
198                                 $sth->execute;
199                                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
200                                 while (my ($itemtype,$description) = $sth->fetchrow_array) {
201                                         push @authorised_values, $itemtype;
202                                         $authorised_lib{$itemtype}=$description;
203                                 }
204                         #---- "true" authorised value
205                         } else {
206                                 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
207                                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
208                                 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
209                                         push @authorised_values, $value;
210                                         $authorised_lib{$value}=$lib;
211                                 }
212                         }
213                         $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
214                                                                                                                                                 -values=> \@authorised_values,
215                                                                                                                                                 -default=>"$value",
216                                                                                                                                                 -labels => \%authorised_lib,
217                                                                                                                                                 -size=>1,
218                                                                                                                                                 -multiple=>0,
219                                                                                                                                                 );
220                 } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
221                         $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>";
222                 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
223                         my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
224                         require $plugin;
225                         my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,0);
226                         my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,0);
227                         $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";
228                 } else {
229                         $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
230                 }
231 #               $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
232                 push(@loop_data, \%subfield_data);
233                 $i++
234         }
235 }
236 my ($template, $loggedinuser, $cookie)
237     = get_template_and_user({template_name => "acqui.simple/additem.tmpl",
238                              query => $input,
239                              type => "intranet",
240                              authnotrequired => 0,
241                              flagsrequired => {parameters => 1},
242                              debug => 1,
243                              });
244
245 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
246 $template->param(item_loop => \@item_value_loop,
247                                                 item_header_loop => \@header_value_loop,
248                                                 bibid => $bibid,
249                                                 biblionumber =>$oldbiblionumber,
250                                                 item => \@loop_data,
251                                                 itemnum => $itemnum,
252                                                 itemtagfield => $itemtagfield,
253                                                 itemtagsubfield =>$itemtagsubfield,
254                                                 op => $nextop,
255                                                 opisadd => ($nextop eq "saveitem")?0:1);
256 output_html_with_http_headers $input, $cookie, $template->output;